1
1
import GL from '@luma.gl/constants' ;
2
2
import { AnimationLoop , Model , Geometry , CubeGeometry , setParameters , log ,
3
3
Texture2D , TextureCube , loadImage , Framebuffer , Renderbuffer , clear } from '@luma.gl/core' ;
4
- import { Matrix4 , Vector3 , Vector4 , Quaternion } from 'math.gl' ;
4
+ import { Matrix4 , Vector3 , Vector4 , Quaternion , Vector2 } from 'math.gl' ;
5
5
import { parse } from '@loaders.gl/core' ;
6
6
// eslint-disable-next-line import/no-unresolved
7
7
import { DracoLoader } from '@loaders.gl/draco' ;
8
8
import { addEvents , GLTFScenegraphLoader , createGLTFObjects , GLTFEnvironment } from '@luma.gl/addons' ;
9
9
import { Program , FragmentShader , VertexShader } from '@luma.gl/webgl' ;
10
10
import { VERTEX_SHADER , FRAGMENT_SHADER , SHADOWMAP_VERTEX , SHADOWMAP_FRAGMENT ,
11
- PBR_VS_WITH_SHADOWMAP , PBR_FS_WITH_SHADOWMAP } from './shaders' ;
11
+ PBR_VS_WITH_SHADOWMAP , PBR_FS_WITH_SHADOWMAP , PBR_FS } from './shaders' ;
12
12
import { SpaceSkybox , generateSimpleCubemap } from './spaceSkybox' ;
13
13
import * as KeyCode from 'keycode-js' ;
14
14
import * as Stats from 'stats.js' ;
@@ -42,7 +42,8 @@ const CONTROLS = {
42
42
const SETTINGS = {
43
43
SKYBOX_RES : 1024 ,
44
44
SHADOWMAP_RES : 1024 ,
45
- SHIP_DELTA : new Vector3 ( [ 0 , 0 , - 15 ] )
45
+ SHIP_DELTA : new Vector3 ( [ 0 , 0 , - 15 ] ) ,
46
+ MAX_SHIP_VEL : 0.5
46
47
}
47
48
48
49
Matrix4 . prototype . removeTranslate = function ( ) {
@@ -246,6 +247,8 @@ export default class AppAnimationLoop extends AnimationLoop {
246
247
onInitialize ( { canvas, gl} ) {
247
248
this . camera = new Camera ( new Vector3 ( ) ) ;
248
249
this . ship = new Camera ( new Vector3 ( ) ) ;
250
+ this . ship . vel = new Vector3 ( ) ;
251
+ this . ship . acc = 0.0 ;
249
252
this . loadOptions = {
250
253
pbrDebug : true ,
251
254
imageBasedLightingEnvironment : null ,
@@ -305,6 +308,7 @@ export default class AppAnimationLoop extends AnimationLoop {
305
308
this . loadOptions . imageBasedLightingEnvironment = this . environment ;
306
309
this . shadowProgram = new Program ( gl , { vs : SHADOWMAP_VERTEX , fs :SHADOWMAP_FRAGMENT } ) ;
307
310
this . pbrShadowProgram = new Program ( gl , { vs : PBR_VS_WITH_SHADOWMAP , fs :PBR_FS_WITH_SHADOWMAP } ) ;
311
+ this . pbrProgram = new Program ( gl , { vs : PBR_VS_WITH_SHADOWMAP , fs :PBR_FS } ) ;
308
312
this . loadOptions . pbrShadowProgram = this . pbrShadowProgram ;
309
313
loadGLTF ( "/resources/45-e/scene.gltf" , this . gl , this . loadOptions ) . then ( result =>
310
314
Object . assign ( this , result )
@@ -336,7 +340,7 @@ export default class AppAnimationLoop extends AnimationLoop {
336
340
[ GL . DEPTH_ATTACHMENT ] : this . shadowTxt2D
337
341
}
338
342
} ) ;
339
- this . fbShadow . gl . drawBuffers ( [ GL . DEPTH_ATTACHMENT ] ) ;
343
+ this . fbShadow . gl . drawBuffers ( [ GL . BACK ] ) ;
340
344
/*this.fbShadow.attach({
341
345
[GL.DEPTH_ATTACHMENT]: this.shadowTxt2D
342
346
}, {resizeAttachments: false});*/
@@ -365,6 +369,9 @@ export default class AppAnimationLoop extends AnimationLoop {
365
369
this . lastTick = tick ;
366
370
367
371
updateCamera ( this . camera , this . ship , deltaTick ) ;
372
+ updateShip ( this . ship , deltaTick ) ;
373
+ let crDelta = ( this . ship . vel . dot ( this . ship . vel ) ) * 100 ;
374
+ skybox . criticalDelta = Math . max ( 10 , crDelta ) ;
368
375
369
376
const projection = new Matrix4 ( ) . perspective ( { aspect} ) ;
370
377
let view_pos = this . camera . pos ;
@@ -474,6 +481,7 @@ export default class AppAnimationLoop extends AnimationLoop {
474
481
} ) . draw ( ) ;
475
482
476
483
let success = true ;
484
+ //console.log(this.ship.acc);
477
485
if ( this . scenes !== undefined )
478
486
this . scenes [ 0 ] . traverse ( ( model , { worldMatrix} ) => {
479
487
// In glTF, meshes and primitives do no have their own matrix.
@@ -482,7 +490,7 @@ export default class AppAnimationLoop extends AnimationLoop {
482
490
color : [ 255 * .2 , 255 * .5 , 255 * .8 ] ,
483
491
position : this . engineLight ,
484
492
attenuation : [ 0 , 0 , 0.01 ] ,
485
- intensity : 1.0
493
+ intensity : this . ship . acc
486
494
} ) ;
487
495
if ( triggers . cameraLight ) pointLights . push ( {
488
496
color : [ 255 , 0 , 0 ] ,
@@ -502,14 +510,17 @@ export default class AppAnimationLoop extends AnimationLoop {
502
510
const u_MVPMatrix = new Matrix4 ( projection ) . multiplyRight ( view ) . multiplyRight ( this . ship . viewMatrix ) . multiplyRight ( worldMatrix ) ;
503
511
let old_program = model . model . program ;
504
512
if ( model . id === "Cube.021_0-primitive-0" ) model . model . program = this . pbrShadowProgram ;
505
- this . pbrShadowProgram . setUniforms ( old_program . uniforms ) ;
513
+ else model . model . program = this . pbrProgram ;
514
+ model . model . program . setUniforms ( old_program . uniforms ) ;
506
515
model . setUniforms ( {
507
516
u_Camera : view_pos ,
508
517
u_MVPMatrix, u_MSVSPMatirx,
509
518
u_ShadowMap : this . shadowTxt2D ,
510
519
u_ModelMatrix : this . ship . viewMatrix . clone ( ) . multiplyRight ( worldMatrix ) ,
511
520
u_NormalMatrix : new Matrix4 ( this . ship . viewMatrix ) . multiplyRight ( worldMatrix ) ,
512
521
u_SpecularEnvSampler : skybox . rttCubemap ,
522
+ u_SpecularEnvSampler2 : skybox . rttNewCubemap ,
523
+ u_SkyInterpolation : skybox . delta ,
513
524
u_ScaleIBLAmbient : [ 1 , 5 ]
514
525
} ) . draw ( {
515
526
drawMode : model . model . getDrawMode ( ) ,
@@ -521,7 +532,7 @@ export default class AppAnimationLoop extends AnimationLoop {
521
532
model . model . program = old_program ;
522
533
} ) ;
523
534
524
- skybox . update ( gl , view_pos ) ;
535
+ skybox . update ( gl , view_pos . clone ( ) . negate ( ) ) ;
525
536
gl . viewport ( 0 , 0 , canvas . width , canvas . height ) ;
526
537
skybox . setUniforms ( {
527
538
uProjection : projection ,
@@ -588,17 +599,28 @@ function addPointerHandler(canvas, camera) {
588
599
let pointerPos = function ( x , y ) {
589
600
const dx = x - currentX ;
590
601
const dy = y - currentY ;
591
- camera . updateCamera ( new Vector3 ( ) , new Vector3 ( dy , dx ) . scale ( 0.001 ) ) ;
602
+ camera . updateCamera ( new Vector3 ( ) , new Vector3 ( dx , dy ) . scale ( 0.001 ) ) ;
592
603
currentX = x ;
593
604
currentY = y ;
594
605
}
595
606
}
596
607
608
+ function updateShip ( ship , k ) {
609
+ ship . vel . add ( ship . front . clone ( ) . scale ( ship . acc * 0.005 ) ) ;
610
+ let velC = Math . sqrt ( ship . vel . dot ( ship . vel ) ) ; //TODO: Can be improved with fast invsqrt
611
+ ship . vel . scale ( Math . min ( 1.0 , SETTINGS . MAX_SHIP_VEL / velC ) ) ;
612
+ if ( Math . abs ( ship . acc ) > 0.0000001 )
613
+ ship . acc -= 0.01 * k * Math . sign ( ship . acc ) ;
614
+ else ship . acc = 0 ;
615
+ ship . updateCamera ( ship . vel . clone ( ) . scale ( k ) , new Vector3 ( ) , false ) ;
616
+ }
617
+
597
618
function updateCamera ( camera , ship , tick ) {
598
619
let dpos = new Vector3 ( 0 ) ;
599
620
let kdpos = new Vector3 ( 0 ) ;
600
621
let roll = 0 ;
601
622
let k = tick * .1 ;
623
+ const RT_K = .15 ;
602
624
if ( currentlyPressedKeys [ CONTROLS . MOVE_LEFT ] ) {
603
625
dpos . subtract ( camera . right ) ;
604
626
kdpos . subtract ( [ 1 , 0 , 0 ] ) ;
@@ -621,23 +643,27 @@ function updateCamera(camera, ship, tick){
621
643
kdpos . subtract ( [ 0 , 1 , 0 ] ) ;
622
644
}
623
645
if ( currentlyPressedKeys [ CONTROLS . ROLL_LEFT ] ) {
624
- roll -= .25 ;
646
+ roll -= 1 ;
625
647
} else if ( currentlyPressedKeys [ CONTROLS . ROLL_RIGHT ] ) {
626
- roll += .25 ;
648
+ roll += 1 ;
627
649
}
628
650
if ( currentlyPressedKeys [ CONTROLS . RESET_CAMERA ] ) {
629
651
camera . applyQuaternion ( new Quaternion ( ) . slerp ( { target : new Quaternion ( ) . rotationTo ( camera . up , [ 0 , 1 , 0 ] ) , ratio : Math . min ( 1.0 , k ) } ) ) ;
630
652
camera . updateVectors ( ) ;
653
+ ship . vel . scale ( Math . min ( 0.95 / k , 0.95 ) ) ;
631
654
}
632
655
633
656
if ( currentlyPressedKeys [ CONTROLS . MOVE_FAST ] ) k *= 3 ;
634
657
635
658
if ( ! triggers . freeCamera ) {
636
659
k *= .5 ;
637
- ship . updateCamera ( ship . front . clone ( ) . scale ( - kdpos [ 2 ] * k ) , new Vector3 ( - kdpos [ 0 ] * k * .25 , kdpos [ 1 ] * k * .25 , roll * k ) , false ) ;
660
+ ship . updateCamera ( new Vector3 ( ) , new Vector3 ( - kdpos [ 0 ] , kdpos [ 1 ] , roll ) . scale ( RT_K * k ) , false ) ;
661
+
662
+ ship . acc = Math . max ( - 1 , Math . min ( 1 , ship . acc + k * - kdpos [ 2 ] ) ) ;
663
+
638
664
}
639
665
if ( triggers . freeCamera )
640
- camera . updateCamera ( dpos . negate ( ) . scale ( k ) , new Vector3 ( 0 , 0 , roll * k ) ) ;
666
+ camera . updateCamera ( dpos . negate ( ) . scale ( k ) , new Vector3 ( 0 , 0 , roll * k * RT_K ) ) ;
641
667
}
642
668
643
669
0 commit comments