1
1
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' ;
3
3
import { Matrix4 , Vector3 , Vector4 } from 'math.gl' ;
4
4
import { parse } from '@loaders.gl/core' ;
5
5
// eslint-disable-next-line import/no-unresolved
6
6
import { DracoLoader } from '@loaders.gl/draco' ;
7
7
import { addEvents , GLTFScenegraphLoader , createGLTFObjects , GLTFEnvironment } from '@luma.gl/addons' ;
8
- import { Program } from '@luma.gl/webgl' ;
8
+ import { Program , FragmentShader } from '@luma.gl/webgl' ;
9
9
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' ;
11
11
12
12
const INFO_HTML = `
13
13
<p>
@@ -107,7 +107,7 @@ class ColoredCubeGeometry extends CubeGeometry {
107
107
}
108
108
109
109
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 ) ) {
111
111
this . pos = pos ;
112
112
this . ang = ang ;
113
113
this . front = new Vector3 ( ) ;
@@ -123,7 +123,7 @@ class Camera{
123
123
this . front = new Vector3 ( this . viewMatrix . transformDirection ( this . wfront ) ) ;
124
124
this . up = new Vector3 ( this . viewMatrix . transformDirection ( this . wup ) ) ;
125
125
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 ( ) ) ;
127
127
}
128
128
updateCamera ( dpos = new Vector3 ( 0 ) , dang = new Vector3 ( 0 ) ) {
129
129
this . pos . add ( dpos ) ;
@@ -140,7 +140,12 @@ async function loadGLTF(url, gl, options) {
140
140
DracoLoader
141
141
} , url ) ;
142
142
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
+ } ) ;
144
149
return { scenes, animator, gltf} ;
145
150
}
146
151
@@ -154,14 +159,15 @@ export default class AppAnimationLoop extends AnimationLoop {
154
159
...opts ,
155
160
glOptions : {
156
161
// alpha causes issues with some glTF demos
157
- webgl1 : true ,
162
+ webgl1 : false ,
158
163
webgl2 : true ,
159
164
alpha : false
160
165
}
161
166
} ) ;
162
167
}
163
168
onInitialize ( { canvas, gl} ) {
164
169
this . camera = new Camera ( new Vector3 ( ) , new Vector3 ( ) ) ;
170
+ this . ship = new Camera ( new Vector3 ( ) , new Vector3 ( ) ) ;
165
171
this . loadOptions = {
166
172
pbrDebug : true ,
167
173
imageBasedLightingEnvironment : null ,
@@ -176,11 +182,51 @@ export default class AppAnimationLoop extends AnimationLoop {
176
182
depthFunc : GL . LEQUAL
177
183
} ) ;
178
184
179
- const SKYBOX_RES = 512 ;
185
+ const SKYBOX_RES = 1024 ;
186
+ let skybox = new SpaceSkybox ( gl , { } , SKYBOX_RES ) ;
180
187
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 ;
182
211
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 =>
184
230
Object . assign ( this , result )
185
231
) ;
186
232
this . test = true ;
@@ -195,16 +241,14 @@ export default class AppAnimationLoop extends AnimationLoop {
195
241
fs : FRAGMENT_SHADER ,
196
242
geometry : new ColoredCubeGeometry ( )
197
243
} ) ,
198
- skybox : new SpaceSkybox ( gl , { } , SKYBOX_RES )
244
+ skybox : skybox
199
245
} ;
200
246
}
201
247
onRender ( { gl, tick, aspect, pyramid, cube, skybox, canvas} ) {
202
248
gl . clear ( GL . COLOR_BUFFER_BIT | GL . DEPTH_BUFFER_BIT ) ;
203
249
204
250
updateCamera ( this . camera ) ;
205
251
206
- gl . viewport ( 0 , 0 , canvas . width , canvas . height ) ;
207
-
208
252
const projection = new Matrix4 ( ) . perspective ( { aspect} ) ;
209
253
const view = this . camera . viewMatrix ;
210
254
@@ -232,18 +276,26 @@ export default class AppAnimationLoop extends AnimationLoop {
232
276
let success = true ;
233
277
if ( this . scenes !== undefined )
234
278
this . scenes [ 0 ] . traverse ( ( model , { worldMatrix} ) => {
279
+ //if(model.id !== "mesh--primitive-0") return;
235
280
// 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
+
236
295
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 ,
245
297
ambientLight : {
246
- color : [ 255 , 255 , 255 ] ,
298
+ color : [ 255 * .2 , 255 * .5 , 255 * .8 ] ,
247
299
intensity : 1.0
248
300
}
249
301
} } ) ;
@@ -255,16 +307,13 @@ export default class AppAnimationLoop extends AnimationLoop {
255
307
u_MVMatrix : worldMatrix . clone ( ) . multiplyRight ( view ) ,
256
308
u_ModelMatrix : worldMatrix ,
257
309
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 ( ) ;
265
313
} ) ;
266
314
267
315
skybox . update ( gl , this . camera . pos ) ;
316
+ gl . viewport ( 0 , 0 , canvas . width , canvas . height ) ;
268
317
skybox . setUniforms ( {
269
318
uProjection : projection ,
270
319
uView : view
@@ -274,6 +323,7 @@ export default class AppAnimationLoop extends AnimationLoop {
274
323
return success ;
275
324
}
276
325
}
326
+ const triggers = { } ;
277
327
const currentlyPressedKeys = { } ;
278
328
function addKeyboardHandler ( canvas ) {
279
329
addEvents ( canvas , {
@@ -282,6 +332,7 @@ function addKeyboardHandler(canvas) {
282
332
} ,
283
333
onKeyUp ( e ) {
284
334
currentlyPressedKeys [ e . code ] = false ;
335
+ if ( e . code === 76 ) triggers . cameraLight = ! triggers . cameraLight ;
285
336
}
286
337
} ) ;
287
338
}
@@ -302,7 +353,10 @@ function addPointerHandler(canvas, camera) {
302
353
}
303
354
} , false ) ;
304
355
canvas . addEventListener ( 'click' , function ( e ) {
305
- if ( canvas !== document . pointerLockElement ) canvas . requestPointerLock ( ) ;
356
+ if ( canvas !== document . pointerLockElement ) {
357
+ canvas . requestPointerLock ( ) ;
358
+ canvas . requestFullscreen ( ) ;
359
+ }
306
360
else document . exitPointerLock ( ) ;
307
361
} , false ) ;
308
362
addEvents ( canvas , {
@@ -339,9 +393,9 @@ function updateCamera(camera){
339
393
}
340
394
if ( currentlyPressedKeys [ 81 ] ) {
341
395
// Q
342
- dpos . add ( camera . up ) ;
343
- } else if ( currentlyPressedKeys [ 69 ] ) {
344
396
dpos . subtract ( camera . up ) ;
397
+ } else if ( currentlyPressedKeys [ 69 ] ) {
398
+ dpos . add ( camera . up ) ;
345
399
}
346
400
347
401
dpos . scale ( .1 ) ;
0 commit comments