MikaFil commited on
Commit
7dc0daa
·
verified ·
1 Parent(s): 30a46e8

Update viewer.js

Browse files
Files changed (1) hide show
  1. viewer.js +39 -79
viewer.js CHANGED
@@ -1,7 +1,7 @@
1
  // viewer.js
2
  // ==============================
3
 
4
- let pc;
5
  export let app = null;
6
  let cameraEntity = null;
7
  let modelEntity = null;
@@ -25,25 +25,25 @@ export async function initializeViewer(config, instanceId) {
25
  maxZoom = parseFloat(config.maxZoom || "20");
26
  minAngle = parseFloat(config.minAngle || "-45");
27
  maxAngle = parseFloat(config.maxAngle || "90");
28
- minAzimuth = parseFloat(config.minAzimuth || "-360");
29
- maxAzimuth = parseFloat(config.maxAzimuth || "360");
30
  minPivotY = parseFloat(config.minPivotY || "0");
31
- minY = parseFloat(config.minY || "0");
32
-
33
- modelX = parseFloat(config.modelX || "0");
34
- modelY = parseFloat(config.modelY || "0");
35
- modelZ = parseFloat(config.modelZ || "0");
36
- modelScale = parseFloat(config.modelScale || "1");
37
- modelRotationX = parseFloat(config.modelRotationX || "0");
38
- modelRotationY = parseFloat(config.modelRotationY || "0");
39
- modelRotationZ = parseFloat(config.modelRotationZ || "0");
40
-
41
- const cameraX = parseFloat(config.cameraX || "0");
42
- const cameraY = parseFloat(config.cameraY || "2");
43
- const cameraZ = parseFloat(config.cameraZ || "5");
44
- const cameraXPhone = parseFloat(config.cameraXPhone || cameraX);
45
- const cameraYPhone = parseFloat(config.cameraYPhone || cameraY);
46
- const cameraZPhone = parseFloat(config.cameraZPhone || (cameraZ * 1.5));
47
 
48
  chosenCameraX = isMobile ? cameraXPhone : cameraX;
49
  chosenCameraY = isMobile ? cameraYPhone : cameraY;
@@ -62,10 +62,19 @@ export async function initializeViewer(config, instanceId) {
62
  canvas.className = 'ply-canvas';
63
  canvas.style.width = "100%";
64
  canvas.style.height = "100%";
65
- canvas.style.touchAction = "none";
66
  canvas.setAttribute('tabindex', '0');
67
  viewerContainer.insertBefore(canvas, progressDialog);
68
 
 
 
 
 
 
 
 
 
 
 
69
  if (!pc) {
70
  pc = await import("https://esm.run/playcanvas");
71
  window.pc = pc;
@@ -113,27 +122,17 @@ export async function initializeViewer(config, instanceId) {
113
  window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
114
  app.on('destroy', () => resizeObserver.disconnect());
115
 
116
- if (isIOS) {
117
- const originalRequest = pc.Http.request;
118
- pc.Http.request = function(options) {
119
- if (options && options.url.endsWith('.ply')) options.responseType = 'arraybuffer';
120
- return originalRequest.call(this, options);
121
- };
122
- }
123
-
124
  const assets = {
125
  model: new pc.Asset('gsplat', 'gsplat', { url: plyUrl }),
126
  orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
127
  galerie: new pc.Asset('galerie', 'container', { url: glbUrl }),
128
- hdr: new pc.Asset('hdr', 'texture', {
129
- url: "https://huggingface.co/datasets/bilca/ply_files/resolve/main/galeries/blanc.png"
130
- }, { type: pc.TEXTURETYPE_RGBP, mipmaps: false })
131
  };
132
 
133
  const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
134
- loader.load(async () => {
135
  app.start();
136
- app.scene.envAtlas = assets.hdr.resource;
137
 
138
  modelEntity = new pc.Entity('model');
139
  modelEntity.addComponent('gsplat', { asset: assets.model });
@@ -142,67 +141,28 @@ export async function initializeViewer(config, instanceId) {
142
  modelEntity.setLocalScale(modelScale, modelScale, modelScale);
143
  app.root.addChild(modelEntity);
144
 
145
- if (assets.galerie && assets.galerie.resource) {
146
- const galleryEntity = assets.galerie.resource.instantiateRenderEntity();
147
- app.root.addChild(galleryEntity);
148
- }
149
-
150
  cameraEntity = new pc.Entity('camera');
151
  cameraEntity.addComponent('camera', { clearColor: new pc.Color(0.2, 0.2, 0.2, 1) });
152
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
153
  cameraEntity.lookAt(modelEntity.getPosition());
154
-
155
  cameraEntity.addComponent('script');
156
  cameraEntity.script.create('orbitCamera', {
157
  attributes: {
158
- inertiaFactor: 0.2,
159
- focusEntity: modelEntity,
160
- distanceMax: maxZoom,
161
- distanceMin: minZoom,
162
- pitchAngleMax: maxAngle,
163
- pitchAngleMin: minAngle,
164
- yawAngleMax: maxAzimuth,
165
- yawAngleMin: minAzimuth,
166
- minPivotY: minPivotY,
167
- frameOnStart: false
168
  }
169
  });
170
- cameraEntity.script.create('orbitCameraInputMouse', { attributes: { orbitSensitivity: isMobile ? 0.6 : 0.3 } });
171
- cameraEntity.script.create('orbitCameraInputTouch', { attributes: { orbitSensitivity: 0.6 } });
172
  app.root.addChild(cameraEntity);
173
 
174
- app.once('update', () => resetViewerCamera());
175
- app.on('update', () => {
176
- const pos = cameraEntity.getPosition();
177
- if (pos.y < minY) cameraEntity.setPosition(pos.x, minY, pos.z);
178
- });
179
-
180
- try {
181
- const tooltipsModule = await import('./tooltips.js');
182
- tooltipsModule.initializeTooltips({
183
- app, cameraEntity, modelEntity,
184
- tooltipsUrl: config.tooltips_url,
185
- defaultVisible: !!config.showTooltipsDefault,
186
- moveDuration: config.tooltipMoveDuration || 0.6
187
- });
188
- } catch {}
189
-
190
- progressDialog.style.display = 'none';
191
  viewerInitialized = true;
192
  });
193
  }
194
 
195
  export function resetViewerCamera() {
196
  if (!cameraEntity || !modelEntity || !app) return;
197
- cameraEntity.script.orbitCamera.reset();
198
- }
199
-
200
- export function cleanupViewer() {
201
- if (app) app.destroy();
202
- app = null;
203
- cameraEntity = null;
204
- modelEntity = null;
205
- viewerInitialized = false;
206
- if (resizeObserver) resizeObserver.disconnect();
207
- resizeObserver = null;
208
  }
 
1
  // viewer.js
2
  // ==============================
3
 
4
+ let pc;
5
  export let app = null;
6
  let cameraEntity = null;
7
  let modelEntity = null;
 
25
  maxZoom = parseFloat(config.maxZoom || "20");
26
  minAngle = parseFloat(config.minAngle || "-45");
27
  maxAngle = parseFloat(config.maxAngle || "90");
28
+ minAzimuth = (config.minAzimuth !== undefined) ? parseFloat(config.minAzimuth) : -360;
29
+ maxAzimuth = (config.maxAzimuth !== undefined) ? parseFloat(config.maxAzimuth) : 360;
30
  minPivotY = parseFloat(config.minPivotY || "0");
31
+ minY = (config.minY !== undefined) ? parseFloat(config.minY) : 0;
32
+
33
+ modelX = (config.modelX !== undefined) ? parseFloat(config.modelX) : 0;
34
+ modelY = (config.modelY !== undefined) ? parseFloat(config.modelY) : 0;
35
+ modelZ = (config.modelZ !== undefined) ? parseFloat(config.modelZ) : 0;
36
+ modelScale = (config.modelScale !== undefined) ? parseFloat(config.modelScale) : 1;
37
+ modelRotationX = (config.modelRotationX !== undefined) ? parseFloat(config.modelRotationX) : 0;
38
+ modelRotationY = (config.modelRotationY !== undefined) ? parseFloat(config.modelRotationY) : 0;
39
+ modelRotationZ = (config.modelRotationZ !== undefined) ? parseFloat(config.modelRotationZ) : 0;
40
+
41
+ const cameraX = (config.cameraX !== undefined) ? parseFloat(config.cameraX) : 0;
42
+ const cameraY = (config.cameraY !== undefined) ? parseFloat(config.cameraY) : 2;
43
+ const cameraZ = (config.cameraZ !== undefined) ? parseFloat(config.cameraZ) : 5;
44
+ const cameraXPhone = (config.cameraXPhone !== undefined) ? parseFloat(config.cameraXPhone) : cameraX;
45
+ const cameraYPhone = (config.cameraYPhone !== undefined) ? parseFloat(config.cameraYPhone) : cameraY;
46
+ const cameraZPhone = (config.cameraZPhone !== undefined) ? parseFloat(config.cameraZPhone) : (cameraZ * 1.5);
47
 
48
  chosenCameraX = isMobile ? cameraXPhone : cameraX;
49
  chosenCameraY = isMobile ? cameraYPhone : cameraY;
 
62
  canvas.className = 'ply-canvas';
63
  canvas.style.width = "100%";
64
  canvas.style.height = "100%";
 
65
  canvas.setAttribute('tabindex', '0');
66
  viewerContainer.insertBefore(canvas, progressDialog);
67
 
68
+ canvas.style.touchAction = "none";
69
+ canvas.style.webkitTouchCallout = "none";
70
+ canvas.addEventListener('gesturestart', e => e.preventDefault());
71
+ canvas.addEventListener('gesturechange', e => e.preventDefault());
72
+ canvas.addEventListener('gestureend', e => e.preventDefault());
73
+ canvas.addEventListener('dblclick', e => e.preventDefault());
74
+ canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
75
+
76
+ progressDialog.style.display = 'block';
77
+
78
  if (!pc) {
79
  pc = await import("https://esm.run/playcanvas");
80
  window.pc = pc;
 
122
  window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
123
  app.on('destroy', () => resizeObserver.disconnect());
124
 
 
 
 
 
 
 
 
 
125
  const assets = {
126
  model: new pc.Asset('gsplat', 'gsplat', { url: plyUrl }),
127
  orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
128
  galerie: new pc.Asset('galerie', 'container', { url: glbUrl }),
129
+ hdr: new pc.Asset('hdr', 'texture', { url: "https://huggingface.co/datasets/bilca/ply_files/resolve/main/galeries/blanc.png" }, { type: pc.TEXTURETYPE_RGBP, mipmaps: false })
 
 
130
  };
131
 
132
  const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
133
+ loader.load(() => {
134
  app.start();
135
+ progressDialog.style.display = 'none';
136
 
137
  modelEntity = new pc.Entity('model');
138
  modelEntity.addComponent('gsplat', { asset: assets.model });
 
141
  modelEntity.setLocalScale(modelScale, modelScale, modelScale);
142
  app.root.addChild(modelEntity);
143
 
 
 
 
 
 
144
  cameraEntity = new pc.Entity('camera');
145
  cameraEntity.addComponent('camera', { clearColor: new pc.Color(0.2, 0.2, 0.2, 1) });
146
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
147
  cameraEntity.lookAt(modelEntity.getPosition());
 
148
  cameraEntity.addComponent('script');
149
  cameraEntity.script.create('orbitCamera', {
150
  attributes: {
151
+ focusEntity: modelEntity, inertiaFactor: 0.2, distanceMax: maxZoom, distanceMin: minZoom,
152
+ pitchAngleMax: maxAngle, pitchAngleMin: minAngle, frameOnStart: false
 
 
 
 
 
 
 
 
153
  }
154
  });
155
+ cameraEntity.script.create('orbitCameraInputMouse');
156
+ cameraEntity.script.create('orbitCameraInputTouch');
157
  app.root.addChild(cameraEntity);
158
 
159
+ app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  viewerInitialized = true;
161
  });
162
  }
163
 
164
  export function resetViewerCamera() {
165
  if (!cameraEntity || !modelEntity || !app) return;
166
+ const orbitCam = cameraEntity.script.orbitCamera;
167
+ if (orbitCam) orbitCam.reset();
 
 
 
 
 
 
 
 
 
168
  }