Skip to content
This repository was archived by the owner on Aug 29, 2020. It is now read-only.

Commit 50e2276

Browse files
committed
Ship flight + skybox interpolation
1 parent 61be951 commit 50e2276

File tree

3 files changed

+518
-15
lines changed

3 files changed

+518
-15
lines changed

app.js

+38-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import GL from '@luma.gl/constants';
22
import {AnimationLoop, Model, Geometry, CubeGeometry, setParameters, log,
33
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';
55
import {parse} from '@loaders.gl/core';
66
// eslint-disable-next-line import/no-unresolved
77
import {DracoLoader} from '@loaders.gl/draco';
88
import {addEvents, GLTFScenegraphLoader, createGLTFObjects, GLTFEnvironment} from '@luma.gl/addons';
99
import { Program, FragmentShader, VertexShader } from '@luma.gl/webgl';
1010
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';
1212
import { SpaceSkybox, generateSimpleCubemap } from './spaceSkybox';
1313
import * as KeyCode from 'keycode-js';
1414
import * as Stats from 'stats.js';
@@ -42,7 +42,8 @@ const CONTROLS = {
4242
const SETTINGS = {
4343
SKYBOX_RES: 1024,
4444
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
4647
}
4748

4849
Matrix4.prototype.removeTranslate = function(){
@@ -246,6 +247,8 @@ export default class AppAnimationLoop extends AnimationLoop {
246247
onInitialize({canvas, gl}) {
247248
this.camera = new Camera(new Vector3());
248249
this.ship = new Camera(new Vector3());
250+
this.ship.vel = new Vector3();
251+
this.ship.acc = 0.0;
249252
this.loadOptions = {
250253
pbrDebug: true,
251254
imageBasedLightingEnvironment: null,
@@ -305,6 +308,7 @@ export default class AppAnimationLoop extends AnimationLoop {
305308
this.loadOptions.imageBasedLightingEnvironment = this.environment;
306309
this.shadowProgram = new Program(gl, {vs: SHADOWMAP_VERTEX, fs:SHADOWMAP_FRAGMENT});
307310
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});
308312
this.loadOptions.pbrShadowProgram = this.pbrShadowProgram;
309313
loadGLTF("/resources/45-e/scene.gltf", this.gl, this.loadOptions).then(result =>
310314
Object.assign(this, result)
@@ -336,7 +340,7 @@ export default class AppAnimationLoop extends AnimationLoop {
336340
[GL.DEPTH_ATTACHMENT]: this.shadowTxt2D
337341
}
338342
});
339-
this.fbShadow.gl.drawBuffers([GL.DEPTH_ATTACHMENT]);
343+
this.fbShadow.gl.drawBuffers([GL.BACK]);
340344
/*this.fbShadow.attach({
341345
[GL.DEPTH_ATTACHMENT]: this.shadowTxt2D
342346
}, {resizeAttachments: false});*/
@@ -365,6 +369,9 @@ export default class AppAnimationLoop extends AnimationLoop {
365369
this.lastTick = tick;
366370

367371
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);
368375

369376
const projection = new Matrix4().perspective({aspect});
370377
let view_pos = this.camera.pos;
@@ -474,6 +481,7 @@ export default class AppAnimationLoop extends AnimationLoop {
474481
}).draw();
475482

476483
let success = true;
484+
//console.log(this.ship.acc);
477485
if (this.scenes !== undefined)
478486
this.scenes[0].traverse((model, {worldMatrix}) => {
479487
// In glTF, meshes and primitives do no have their own matrix.
@@ -482,7 +490,7 @@ export default class AppAnimationLoop extends AnimationLoop {
482490
color: [255*.2, 255*.5, 255*.8],
483491
position: this.engineLight,
484492
attenuation: [0, 0, 0.01],
485-
intensity: 1.0
493+
intensity: this.ship.acc
486494
});
487495
if(triggers.cameraLight) pointLights.push({
488496
color: [255, 0, 0],
@@ -502,14 +510,17 @@ export default class AppAnimationLoop extends AnimationLoop {
502510
const u_MVPMatrix = new Matrix4(projection).multiplyRight(view).multiplyRight(this.ship.viewMatrix).multiplyRight(worldMatrix);
503511
let old_program = model.model.program;
504512
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);
506515
model.setUniforms({
507516
u_Camera: view_pos,
508517
u_MVPMatrix, u_MSVSPMatirx,
509518
u_ShadowMap: this.shadowTxt2D,
510519
u_ModelMatrix: this.ship.viewMatrix.clone().multiplyRight(worldMatrix),
511520
u_NormalMatrix: new Matrix4(this.ship.viewMatrix).multiplyRight(worldMatrix),
512521
u_SpecularEnvSampler: skybox.rttCubemap,
522+
u_SpecularEnvSampler2: skybox.rttNewCubemap,
523+
u_SkyInterpolation: skybox.delta,
513524
u_ScaleIBLAmbient: [1, 5]
514525
}).draw({
515526
drawMode: model.model.getDrawMode(),
@@ -521,7 +532,7 @@ export default class AppAnimationLoop extends AnimationLoop {
521532
model.model.program = old_program;
522533
});
523534

524-
skybox.update(gl, view_pos);
535+
skybox.update(gl, view_pos.clone().negate());
525536
gl.viewport(0, 0, canvas.width, canvas.height);
526537
skybox.setUniforms({
527538
uProjection: projection,
@@ -588,17 +599,28 @@ function addPointerHandler(canvas, camera) {
588599
let pointerPos = function(x, y){
589600
const dx = x - currentX;
590601
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));
592603
currentX = x;
593604
currentY = y;
594605
}
595606
}
596607

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+
597618
function updateCamera(camera, ship, tick){
598619
let dpos = new Vector3(0);
599620
let kdpos = new Vector3(0);
600621
let roll = 0;
601622
let k = tick*.1;
623+
const RT_K = .15;
602624
if (currentlyPressedKeys[CONTROLS.MOVE_LEFT]) {
603625
dpos.subtract(camera.right);
604626
kdpos.subtract([1,0,0]);
@@ -621,23 +643,27 @@ function updateCamera(camera, ship, tick){
621643
kdpos.subtract([0,1,0]);
622644
}
623645
if (currentlyPressedKeys[CONTROLS.ROLL_LEFT]) {
624-
roll -= .25;
646+
roll -= 1;
625647
}else if (currentlyPressedKeys[CONTROLS.ROLL_RIGHT]) {
626-
roll += .25;
648+
roll += 1;
627649
}
628650
if (currentlyPressedKeys[CONTROLS.RESET_CAMERA]){
629651
camera.applyQuaternion(new Quaternion().slerp({target: new Quaternion().rotationTo(camera.up, [0,1,0]), ratio: Math.min(1.0, k)}));
630652
camera.updateVectors();
653+
ship.vel.scale(Math.min(0.95/k,0.95));
631654
}
632655

633656
if (currentlyPressedKeys[CONTROLS.MOVE_FAST]) k *= 3;
634657

635658
if(!triggers.freeCamera){
636659
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+
638664
}
639665
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));
641667
}
642668

643669

0 commit comments

Comments
 (0)