MikaFil commited on
Commit
cb1ca43
·
verified ·
1 Parent(s): b880cac

Update viewer.js

Browse files
Files changed (1) hide show
  1. viewer.js +105 -10
viewer.js CHANGED
@@ -178,15 +178,85 @@ export async function initializeViewer(config, instanceId) {
178
  app.scene.layers.remove(depthLayer);
179
  app.scene.layers.insertOpaque(depthLayer, 2);
180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  // Instantiate GLB entities
182
  modelEntity = assets.model.resource.instantiateRenderEntity();
183
  tubeEntity = assets.tube.resource.instantiateRenderEntity();
184
  filtreEntity = assets.filtre.resource.instantiateRenderEntity();
185
-
186
  app.root.addChild(modelEntity);
187
  app.root.addChild(tubeEntity);
188
  app.root.addChild(filtreEntity);
189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  // ----- Materials Setup -----
191
  // Transparent material (main model)
192
  matTransparent = new pc.StandardMaterial();
@@ -274,9 +344,11 @@ export async function initializeViewer(config, instanceId) {
274
  filtreEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
275
 
276
  // ----- Camera + Orbit Controls -----
 
277
  cameraEntity = new pc.Entity('camera');
278
  cameraEntity.addComponent('camera', {
279
  clearColor: new pc.Color(0.8, 0.8, 0.8, 1),
 
280
  toneMapping: pc.TONEMAP_NEUTRAL
281
  });
282
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
@@ -303,7 +375,6 @@ export async function initializeViewer(config, instanceId) {
303
  app.root.addChild(cameraEntity);
304
 
305
  // Remove Skybox layer from camera
306
- const skyboxLayer = app.scene.layers.getLayerByName("Skybox");
307
  if (skyboxLayer) {
308
  const camLayers = cameraEntity.camera.layers.slice();
309
  const idx = camLayers.indexOf(skyboxLayer.id);
@@ -312,25 +383,49 @@ export async function initializeViewer(config, instanceId) {
312
  cameraEntity.camera.layers = camLayers;
313
  }
314
  }
315
- /*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
  // ----- Camera-attached background plane -----
317
  const bgPlane = new pc.Entity("Plane");
318
- bgPlane.addComponent("model", { type: "plane" });
 
 
 
 
319
  bgPlane.setLocalPosition(0, 0, -10);
320
  bgPlane.setLocalScale(11, 1, 5.5);
321
  bgPlane.setLocalEulerAngles(90, 0, 0);
322
  // Simple material for the banner
323
  const mat = new pc.StandardMaterial();
324
- mat.diffuse = new pc.Color(1, 1, 1);
325
- mat.diffuseMap = bgTex;
326
  mat.emissive = new pc.Color(1, 1, 1);
327
- mat.emissiveMap = bgTex;
328
- mat.emissiveIntensity = 1;
329
  mat.useLighting = false;
330
  mat.update();
331
- bgPlane.model.material = mat;
 
 
 
 
 
 
 
332
  cameraEntity.addChild(bgPlane);
333
- */
 
 
334
  // ----- Lighting -----
335
  const light = new pc.Entity("mainLight");
336
  light.addComponent('light', {
 
178
  app.scene.layers.remove(depthLayer);
179
  app.scene.layers.insertOpaque(depthLayer, 2);
180
 
181
+ //create texture and render target foro rendering into, including depth buffer
182
+ const texture = new pc.Texture(app.graphicsDevice, {
183
+ width: 512,
184
+ height: 256,
185
+ format: pc.PIXELFORMAT_R8_G8_B8_A8,
186
+ mipmaps: true,
187
+ addressU: pc.ADDRESS_CLAMP_TO_EDGE,
188
+ addressV: pc.ADDRESS_CLAMP_TO_EDGE,
189
+ });
190
+ const renderTarget = new pc.RenderTarget({
191
+ name :'RT',
192
+ colorBuffer: texture,
193
+ depth: true,
194
+ flipY: !app.graphicsDevice.isWebGPU,
195
+ samples: 2
196
+ });
197
+
198
+ const excludeLayer = new pc.Layer({name : 'Excluded'});
199
+ app.scene.layers.insert(excludeLayer, 1);
200
+
201
+ const tubeLayer = new pc.Layer({name : 'TubeLayer'});
202
+ app.scene.layers.insert(tubeLayer, 1);
203
+
204
+ //get existing layers
205
+ const worldLayer = app.scene.layers.getLayerByName('World');
206
+ const skyboxLayer = app.scene.layers.getLayerByName('Skybox');
207
+ const uiLayer = app.scene.layers.getLayerByName('UI');
208
+
209
  // Instantiate GLB entities
210
  modelEntity = assets.model.resource.instantiateRenderEntity();
211
  tubeEntity = assets.tube.resource.instantiateRenderEntity();
212
  filtreEntity = assets.filtre.resource.instantiateRenderEntity();
213
+
214
  app.root.addChild(modelEntity);
215
  app.root.addChild(tubeEntity);
216
  app.root.addChild(filtreEntity);
217
 
218
+
219
+
220
+
221
+
222
+
223
+
224
+ //precise dans quel layer les tubes sont visibles
225
+ traverse(tubeEntity, node => {
226
+ if(node.render && node.render.meshInstances){
227
+ for(let mi of node.render.meshInstances){
228
+ mi.layer = tubeLayer.id;
229
+ }
230
+ }
231
+ });
232
+
233
+ //precise dans quel layer les filtres sont visibles
234
+ traverse(filtreEntity, node => {
235
+ if(node.render && node.render.meshInstances){
236
+ for(let mi of node.render.meshInstances){
237
+ mi.layer = tubeLayer.id;
238
+ }
239
+ }
240
+ });
241
+
242
+
243
+ //test avec modele separe du reste
244
+ const modelLayer = new pc.Layer({name : 'ModelLayer'});
245
+ app.scene.layers.insert(tubeLayer, 1);
246
+ traverse(modelEntity, node => {
247
+ if(node.render && node.render.meshInstances){
248
+ for(let mi of node.render.meshInstances){
249
+ mi.layer = modelLayer.id;
250
+ }
251
+ }
252
+ });
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
  // ----- Materials Setup -----
261
  // Transparent material (main model)
262
  matTransparent = new pc.StandardMaterial();
 
344
  filtreEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
345
 
346
  // ----- Camera + Orbit Controls -----
347
+ //test : si je mets pas le skyboxLayer la, j'ai pas besoin de l'enlever apres
348
  cameraEntity = new pc.Entity('camera');
349
  cameraEntity.addComponent('camera', {
350
  clearColor: new pc.Color(0.8, 0.8, 0.8, 1),
351
+ layers:[modelLayer.id, skyboxLayer.id, uiLayer.id],
352
  toneMapping: pc.TONEMAP_NEUTRAL
353
  });
354
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
 
375
  app.root.addChild(cameraEntity);
376
 
377
  // Remove Skybox layer from camera
 
378
  if (skyboxLayer) {
379
  const camLayers = cameraEntity.camera.layers.slice();
380
  const idx = camLayers.indexOf(skyboxLayer.id);
 
383
  cameraEntity.camera.layers = camLayers;
384
  }
385
  }
386
+
387
+ const textureCamera = new pc.Entity('TextureCamera');
388
+ textureCamera.addComponent('camera', {
389
+ layers: [tubeLayer.id],
390
+ toneMapping: pc.TONEMAP_NEUTRAL,
391
+
392
+ // set the priority of textureCamera to lower number than the priority of the main camera (which is at default 0)
393
+ // to make it rendered first each frame
394
+ priority: -1,
395
+
396
+ // this camera renders into texture target
397
+ renderTarget: renderTarget
398
+ });
399
+ cameraEntity.addChild(textureCamera);
400
+
401
  // ----- Camera-attached background plane -----
402
  const bgPlane = new pc.Entity("Plane");
403
+ bgPlane.addComponent("model", {
404
+ type: "plane",
405
+ layers: excludeLayer
406
+ });
407
+
408
  bgPlane.setLocalPosition(0, 0, -10);
409
  bgPlane.setLocalScale(11, 1, 5.5);
410
  bgPlane.setLocalEulerAngles(90, 0, 0);
411
  // Simple material for the banner
412
  const mat = new pc.StandardMaterial();
 
 
413
  mat.emissive = new pc.Color(1, 1, 1);
414
+ mat.emissiveMap = texture;
 
415
  mat.useLighting = false;
416
  mat.update();
417
+
418
+ traverse(bgPlane, node => {
419
+ if (node.render && node.render.meshInstances) {
420
+ for (let mi of node.render.meshInstances) {
421
+ mi.material = mat;
422
+ }
423
+ }
424
+ });
425
  cameraEntity.addChild(bgPlane);
426
+
427
+
428
+
429
  // ----- Lighting -----
430
  const light = new pc.Entity("mainLight");
431
  light.addComponent('light', {