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

Commit 1067bf4

Browse files
committed
Skybox simple interpolation + PBR + IBL
Skybox the simpliest interpolation but works good PBR Material with IBL based on generated skybox
1 parent 1062b4b commit 1067bf4

20 files changed

+1557
-101
lines changed

app.js

+86-32
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import GL from '@luma.gl/constants';
2-
import {AnimationLoop, Model, Geometry, CubeGeometry, setParameters, log, TextureCube} from '@luma.gl/core';
2+
import {AnimationLoop, Model, Geometry, CubeGeometry, setParameters, log, Texture2D, TextureCube, loadImage} from '@luma.gl/core';
33
import {Matrix4, Vector3, Vector4} from 'math.gl';
44
import {parse} from '@loaders.gl/core';
55
// eslint-disable-next-line import/no-unresolved
66
import {DracoLoader} from '@loaders.gl/draco';
77
import {addEvents, GLTFScenegraphLoader, createGLTFObjects, GLTFEnvironment} from '@luma.gl/addons';
8-
import { Program } from '@luma.gl/webgl';
8+
import { Program, FragmentShader } from '@luma.gl/webgl';
99
import { VERTEX_SHADER, FRAGMENT_SHADER, V_SHADER, F_SHADER, VS_PBR_SHADER, PS_PBR_SHADER } from './shaders';
10-
import { SpaceSkybox } from './spaceSkybox';
10+
import { SpaceSkybox, generateSimpleCubemap } from './spaceSkybox';
1111

1212
const INFO_HTML = `
1313
<p>
@@ -107,7 +107,7 @@ class ColoredCubeGeometry extends CubeGeometry {
107107
}
108108

109109
class Camera{
110-
constructor(pos, ang, wfront=new Vector3(0,0,1), wup=new Vector3(0,1,0)){
110+
constructor(pos, ang, wfront=new Vector3(0,0,-1), wup=new Vector3(0,1,0)){
111111
this.pos = pos;
112112
this.ang = ang;
113113
this.front = new Vector3();
@@ -123,7 +123,7 @@ class Camera{
123123
this.front = new Vector3(this.viewMatrix.transformDirection(this.wfront));
124124
this.up = new Vector3(this.viewMatrix.transformDirection(this.wup));
125125
this.right = this.front.clone().cross(this.up).normalize();
126-
this.viewMatrix.transpose().translate(this.pos);
126+
this.viewMatrix.transpose().translate(this.pos.clone().negate());
127127
}
128128
updateCamera(dpos=new Vector3(0), dang=new Vector3(0)){
129129
this.pos.add(dpos);
@@ -140,7 +140,12 @@ async function loadGLTF(url, gl, options) {
140140
DracoLoader
141141
}, url);
142142

143-
scenes[0].traverse((node, {worldMatrix}) => log.info(4, 'Using model: ', node)());
143+
scenes[0].traverse((node, {worldMatrix}) => {
144+
log.info(4, 'Using model: ', node);
145+
//node.model.props.defines["USE_TEX_LOD"] = 0;
146+
//node.model.program = node.model._createProgram(node.props);
147+
//node.model.program.fs = new FragmentShader(gl, node.model.program.fs.source.replace("USE_TEX_LOD 1", "USE_TEX_LOD 0"));
148+
});
144149
return {scenes, animator, gltf};
145150
}
146151

@@ -154,14 +159,15 @@ export default class AppAnimationLoop extends AnimationLoop {
154159
...opts,
155160
glOptions: {
156161
// alpha causes issues with some glTF demos
157-
webgl1: true,
162+
webgl1: false,
158163
webgl2: true,
159164
alpha: false
160165
}
161166
});
162167
}
163168
onInitialize({canvas, gl}) {
164169
this.camera = new Camera(new Vector3(), new Vector3());
170+
this.ship = new Camera(new Vector3(), new Vector3());
165171
this.loadOptions = {
166172
pbrDebug: true,
167173
imageBasedLightingEnvironment: null,
@@ -176,11 +182,51 @@ export default class AppAnimationLoop extends AnimationLoop {
176182
depthFunc: GL.LEQUAL
177183
});
178184

179-
const SKYBOX_RES = 512;
185+
const SKYBOX_RES = 1024;
186+
let skybox = new SpaceSkybox(gl, {}, SKYBOX_RES);
180187

181-
this.main_program = new Program(gl, {id:"main_pr", vs: VS_PBR_SHADER, fs: PS_PBR_SHADER, varyings: ['gl_Position']});
188+
//make environment
189+
this.BrdfTexture = new Texture2D(this.gl, {
190+
id: 'brdfLUT',
191+
parameters: {
192+
[GL.TEXTURE_WRAP_S]: GL.CLAMP_TO_EDGE,
193+
[GL.TEXTURE_WRAP_T]: GL.CLAMP_TO_EDGE,
194+
[GL.TEXTURE_MIN_FILTER]: GL.LINEAR,
195+
[GL.TEXTURE_MAG_FILTER]: GL.LINEAR
196+
},
197+
pixelStore: {
198+
[this.gl.UNPACK_FLIP_Y_WEBGL]: false
199+
},
200+
// Texture2D accepts a promise that returns an image as data (Async Textures)
201+
data: loadImage('/resources/brdfLUT.png')
202+
});
203+
this.DiffuseEnvSampler = generateSimpleCubemap(gl, 16, [127,127,255]);
204+
this.SpecularEnvSampler = skybox.rttCubemap;
205+
let environment = {
206+
getDiffuseEnvSampler: x=>this.DiffuseEnvSampler,
207+
getSpecularEnvSampler: x=>this.SpecularEnvSampler,
208+
getBrdfTexture: x=>this.BrdfTexture
209+
}
210+
this.loadOptions.imageBasedLightingEnvironment = environment;
182211

183-
loadGLTF("/resources/space_ranger_sr1/scene.gltf", this.gl, this.loadOptions).then(result =>
212+
const CUBE_FACE_TO_DIRECTION = {
213+
[GL.TEXTURE_CUBE_MAP_POSITIVE_X]: 'right',
214+
[GL.TEXTURE_CUBE_MAP_NEGATIVE_X]: 'left',
215+
[GL.TEXTURE_CUBE_MAP_POSITIVE_Y]: 'top',
216+
[GL.TEXTURE_CUBE_MAP_NEGATIVE_Y]: 'bottom',
217+
[GL.TEXTURE_CUBE_MAP_POSITIVE_Z]: 'front',
218+
[GL.TEXTURE_CUBE_MAP_NEGATIVE_Z]: 'back'
219+
};
220+
const SITE_LINK = 'https://raw.githubusercontent.com/uber-common/deck.gl-data/master/luma.gl/examples/gltf/';
221+
this.environment = new GLTFEnvironment(gl, {
222+
brdfLutUrl: `${SITE_LINK}/brdfLUT.png`,
223+
getTexUrl: (type, dir, mipLevel) =>
224+
`${SITE_LINK}/papermill/${type}/${type}_${CUBE_FACE_TO_DIRECTION[dir]}_${mipLevel}.jpg`
225+
});
226+
//this.environment._SpecularEnvSampler = skybox.rttCubemap;
227+
//this.environment._DiffuseEnvSampler = this.DiffuseEnvSampler;
228+
this.loadOptions.imageBasedLightingEnvironment = this.environment;
229+
loadGLTF("/resources/45-e/scene.gltf", this.gl, this.loadOptions).then(result =>
184230
Object.assign(this, result)
185231
);
186232
this.test = true;
@@ -195,16 +241,14 @@ export default class AppAnimationLoop extends AnimationLoop {
195241
fs: FRAGMENT_SHADER,
196242
geometry: new ColoredCubeGeometry()
197243
}),
198-
skybox: new SpaceSkybox(gl, {}, SKYBOX_RES)
244+
skybox: skybox
199245
};
200246
}
201247
onRender({gl, tick, aspect, pyramid, cube, skybox, canvas}) {
202248
gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
203249

204250
updateCamera(this.camera);
205251

206-
gl.viewport(0, 0, canvas.width, canvas.height);
207-
208252
const projection = new Matrix4().perspective({aspect});
209253
const view = this.camera.viewMatrix;
210254

@@ -232,18 +276,26 @@ export default class AppAnimationLoop extends AnimationLoop {
232276
let success = true;
233277
if (this.scenes !== undefined)
234278
this.scenes[0].traverse((model, {worldMatrix}) => {
279+
//if(model.id !== "mesh--primitive-0") return;
235280
// In glTF, meshes and primitives do no have their own matrix.
281+
let pointLights = [];
282+
if(triggers.cameraLight) pointLights.push({
283+
color: [255, 0, 0],
284+
position: this.camera.pos,
285+
attenuation: [0, 0, 0.01],
286+
intensity: 1.0
287+
});
288+
pointLights.push({
289+
color: [255*.2, 255*.5, 255*.8],
290+
position: this.ship.pos.clone().subtract([0,-1.28,-6.7]),
291+
attenuation: [0, 0, 0.01],
292+
intensity: 1.0
293+
});
294+
236295
model.updateModuleSettings({lightSources: {
237-
pointLights: [
238-
{
239-
color: [255, 0, 0],
240-
position: this.camera.pos.clone().negate(),
241-
attenuation: [0, 0, 0.01],
242-
intensity: 1.0
243-
}
244-
],
296+
pointLights: pointLights,
245297
ambientLight: {
246-
color: [255, 255, 255],
298+
color: [255*.2, 255*.5, 255*.8],
247299
intensity: 1.0
248300
}
249301
}});
@@ -255,16 +307,13 @@ export default class AppAnimationLoop extends AnimationLoop {
255307
u_MVMatrix: worldMatrix.clone().multiplyRight(view),
256308
u_ModelMatrix: worldMatrix,
257309
u_NormalMatrix: new Matrix4(worldMatrix).invert().transpose(),
258-
}).draw({
259-
drawMode: model.model.getDrawMode(),
260-
vertexCount: model.model.getVertexCount(),
261-
vertexArray: model.model.vertexArray,
262-
isIndexed: true,
263-
indexType: model.model.indexType,
264-
});
310+
u_SpecularEnvSampler: skybox.rttCubemap,
311+
u_ScaleIBLAmbient: [1, 5]
312+
}).draw();
265313
});
266314

267315
skybox.update(gl, this.camera.pos);
316+
gl.viewport(0, 0, canvas.width, canvas.height);
268317
skybox.setUniforms({
269318
uProjection: projection,
270319
uView: view
@@ -274,6 +323,7 @@ export default class AppAnimationLoop extends AnimationLoop {
274323
return success;
275324
}
276325
}
326+
const triggers = {};
277327
const currentlyPressedKeys = {};
278328
function addKeyboardHandler(canvas) {
279329
addEvents(canvas, {
@@ -282,6 +332,7 @@ function addKeyboardHandler(canvas) {
282332
},
283333
onKeyUp(e) {
284334
currentlyPressedKeys[e.code] = false;
335+
if(e.code === 76) triggers.cameraLight = !triggers.cameraLight;
285336
}
286337
});
287338
}
@@ -302,7 +353,10 @@ function addPointerHandler(canvas, camera) {
302353
}
303354
}, false);
304355
canvas.addEventListener('click', function(e){
305-
if(canvas !== document.pointerLockElement) canvas.requestPointerLock();
356+
if(canvas !== document.pointerLockElement){
357+
canvas.requestPointerLock();
358+
canvas.requestFullscreen();
359+
}
306360
else document.exitPointerLock();
307361
}, false);
308362
addEvents(canvas, {
@@ -339,9 +393,9 @@ function updateCamera(camera){
339393
}
340394
if (currentlyPressedKeys[81]) {
341395
// Q
342-
dpos.add(camera.up);
343-
}else if (currentlyPressedKeys[69]) {
344396
dpos.subtract(camera.up);
397+
}else if (currentlyPressedKeys[69]) {
398+
dpos.add(camera.up);
345399
}
346400

347401
dpos.scale(.1);

resources/45-e.zip

6.05 MB
Binary file not shown.

resources/45-e/scene.bin

3.01 MB
Binary file not shown.

0 commit comments

Comments
 (0)