MikaFil commited on
Commit
3746bff
·
verified ·
1 Parent(s): bdb09c3

Update viewer.js

Browse files
Files changed (1) hide show
  1. viewer.js +13 -17
viewer.js CHANGED
@@ -1,7 +1,5 @@
1
  // viewer.js
2
  // ==============================
3
- // viewer.js
4
- // ==============================
5
 
6
  let pc; // will hold the PlayCanvas module once imported
7
  export let app = null;
@@ -126,8 +124,6 @@ export async function initializeViewer(config, instanceId) {
126
  app = new pc.Application(canvas, opts);
127
  app.setCanvasFillMode(pc.FILLMODE_NONE);
128
  app.setCanvasResolution(pc.RESOLUTION_AUTO);
129
- //app.scene.exposure = 0.5;
130
- //app.scene.toneMapping = pc.TONEMAP_ACES;
131
 
132
  // Attach ResizeObserver to keep canvas in sync with container size
133
  resizeObserver = new ResizeObserver(entries => {
@@ -143,6 +139,7 @@ export async function initializeViewer(config, instanceId) {
143
  window.addEventListener('resize', () => {
144
  if (app) app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
145
  });
 
146
  app.on('destroy', () => {
147
  window.removeEventListener('resize', resizeCanvas);
148
  });
@@ -153,7 +150,7 @@ export async function initializeViewer(config, instanceId) {
153
  orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
154
  galerie: new pc.Asset('galerie', 'container', { url: glbUrl }),
155
  hdr: new pc.Asset('hdr', 'texture', {
156
- url: https://huggingface.co/datasets/bilca/ply_files/resolve/main/galeries/blanc.png
157
  }, { type: pc.TEXTURETYPE_RGBP, mipmaps: false })
158
  };
159
 
@@ -161,8 +158,7 @@ export async function initializeViewer(config, instanceId) {
161
  let lastProg = 0;
162
  assets.model.on('load', () => progressDialog.style.display = 'none');
163
  assets.model.on('error', err => {
164
- console.error("Error loading PLY file:", err);
165
- progressDialog.innerHTML = <p style="color: red">Error loading model: ${err}</p>;
166
  });
167
 
168
  const progCheck = setInterval(() => {
@@ -210,16 +206,17 @@ export async function initializeViewer(config, instanceId) {
210
  app.root.addChild(dirLight);
211
 
212
  // Gallery GLB
213
- const galleryEntity = assets.galerie.resource.instantiateRenderEntity();
214
- app.root.addChild(galleryEntity);
 
 
215
 
216
  // Camera setup
217
  cameraEntity = new pc.Entity('camera');
218
  cameraEntity.addComponent('camera', {
219
  clearColor: config.canvas_background
220
  ? parseInt(config.canvas_background.substr(1, 2), 16) / 255
221
- : 0,
222
- //toneMapping: pc.TONEMAP_ACES
223
  });
224
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
225
  cameraEntity.lookAt(modelEntity.getPosition());
@@ -282,7 +279,7 @@ export async function initializeViewer(config, instanceId) {
282
  moveDuration: config.tooltipMoveDuration || 0.6
283
  });
284
  } catch (e) {
285
- console.error("Error loading tooltips.js:", e);
286
  }
287
 
288
  progressDialog.style.display = 'none';
@@ -290,8 +287,7 @@ export async function initializeViewer(config, instanceId) {
290
  });
291
 
292
  } catch (error) {
293
- console.error("Error initializing PlayCanvas viewer:", error);
294
- progressDialog.innerHTML = <p style="color: red">Error loading viewer: ${error.message}</p>;
295
  }
296
  }
297
 
@@ -333,11 +329,11 @@ export function resetViewerCamera() {
333
  orbitCam._yaw = yaw;
334
  orbitCam._targetPitch = pitch;
335
  orbitCam._pitch = pitch;
336
- orbitCam._updatePosition && orbitCam._updatePosition();
337
 
338
  tempEnt.destroy();
339
  } catch (e) {
340
- console.error("Error resetting camera:", e);
341
  }
342
  }
343
 
@@ -357,4 +353,4 @@ export function cleanupViewer() {
357
  resizeObserver.disconnect();
358
  resizeObserver = null;
359
  }
360
- }
 
1
  // viewer.js
2
  // ==============================
 
 
3
 
4
  let pc; // will hold the PlayCanvas module once imported
5
  export let app = null;
 
124
  app = new pc.Application(canvas, opts);
125
  app.setCanvasFillMode(pc.FILLMODE_NONE);
126
  app.setCanvasResolution(pc.RESOLUTION_AUTO);
 
 
127
 
128
  // Attach ResizeObserver to keep canvas in sync with container size
129
  resizeObserver = new ResizeObserver(entries => {
 
139
  window.addEventListener('resize', () => {
140
  if (app) app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
141
  });
142
+
143
  app.on('destroy', () => {
144
  window.removeEventListener('resize', resizeCanvas);
145
  });
 
150
  orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
151
  galerie: new pc.Asset('galerie', 'container', { url: glbUrl }),
152
  hdr: new pc.Asset('hdr', 'texture', {
153
+ url: "https://huggingface.co/datasets/bilca/ply_files/resolve/main/galeries/blanc.png"
154
  }, { type: pc.TEXTURETYPE_RGBP, mipmaps: false })
155
  };
156
 
 
158
  let lastProg = 0;
159
  assets.model.on('load', () => progressDialog.style.display = 'none');
160
  assets.model.on('error', err => {
161
+ progressDialog.innerHTML = `<p style="color: red">Error loading model: ${err}</p>`;
 
162
  });
163
 
164
  const progCheck = setInterval(() => {
 
206
  app.root.addChild(dirLight);
207
 
208
  // Gallery GLB
209
+ if (assets.galerie && assets.galerie.resource && assets.galerie.resource.instantiateRenderEntity) {
210
+ const galleryEntity = assets.galerie.resource.instantiateRenderEntity();
211
+ app.root.addChild(galleryEntity);
212
+ }
213
 
214
  // Camera setup
215
  cameraEntity = new pc.Entity('camera');
216
  cameraEntity.addComponent('camera', {
217
  clearColor: config.canvas_background
218
  ? parseInt(config.canvas_background.substr(1, 2), 16) / 255
219
+ : 0
 
220
  });
221
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
222
  cameraEntity.lookAt(modelEntity.getPosition());
 
279
  moveDuration: config.tooltipMoveDuration || 0.6
280
  });
281
  } catch (e) {
282
+ // Tooltips are optional
283
  }
284
 
285
  progressDialog.style.display = 'none';
 
287
  });
288
 
289
  } catch (error) {
290
+ progressDialog.innerHTML = `<p style="color: red">Error loading viewer: ${error && error.message ? error.message : error}</p>`;
 
291
  }
292
  }
293
 
 
329
  orbitCam._yaw = yaw;
330
  orbitCam._targetPitch = pitch;
331
  orbitCam._pitch = pitch;
332
+ if (orbitCam._updatePosition) orbitCam._updatePosition();
333
 
334
  tempEnt.destroy();
335
  } catch (e) {
336
+ // Silent fail: resetting camera should never crash app
337
  }
338
  }
339
 
 
353
  resizeObserver.disconnect();
354
  resizeObserver = null;
355
  }
356
+ }