MikaFil commited on
Commit
3cbcb8a
·
verified ·
1 Parent(s): 89ea27c

Update viewer.js

Browse files
Files changed (1) hide show
  1. viewer.js +184 -234
viewer.js CHANGED
@@ -1,4 +1,7 @@
1
- let pc;
 
 
 
2
  export let app = null;
3
  let cameraEntity = null;
4
  let modelEntity = null;
@@ -8,237 +11,185 @@ let resizeObserver = null;
8
  let chosenCameraX, chosenCameraY, chosenCameraZ;
9
  let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
10
  let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
11
- let sogsUrl, glbUrl, canvasBg;
12
-
13
- function hexToRgbaArray(hex) {
14
- try {
15
- hex = hex.replace("#", "");
16
- if (hex.length === 6) hex += "FF";
17
- if (hex.length !== 8) return [1, 1, 1, 1];
18
- const num = parseInt(hex, 16);
19
- return [
20
- ((num >> 24) & 0xFF) / 255,
21
- ((num >> 16) & 0xFF) / 255,
22
- ((num >> 8) & 0xFF) / 255,
23
- (num & 0xFF) / 255
24
- ];
25
- } catch (e) {
26
- return [1, 1, 1, 1];
27
- }
28
- }
29
 
30
  export async function initializeViewer(config, instanceId) {
31
-
32
- const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
33
- const isMobile = isIOS || /Android/i.test(navigator.userAgent);
34
 
35
- try {
36
- // Parse config
37
- sogsUrl = config.sogs_json_url;
38
- glbUrl = config.glb_url;
39
- canvasBg = config.canvas_background || "#ffffff";
40
- minZoom = parseFloat(config.minZoom || "1");
41
- maxZoom = parseFloat(config.maxZoom || "20");
42
- minAngle = parseFloat(config.minAngle || "-45");
43
- maxAngle = parseFloat(config.maxAngle || "90");
44
- minAzimuth= (config.minAzimuth !== undefined) ? parseFloat(config.minAzimuth) : -360;
45
- maxAzimuth= (config.maxAzimuth !== undefined) ? parseFloat(config.maxAzimuth) : 360;
46
- minPivotY = parseFloat(config.minPivotY || "0");
47
- minY = (config.minY !== undefined) ? parseFloat(config.minY) : 0;
48
-
49
- modelX = (config.modelX !== undefined) ? parseFloat(config.modelX) : 0;
50
- modelY = (config.modelY !== undefined) ? parseFloat(config.modelY) : 0;
51
- modelZ = (config.modelZ !== undefined) ? parseFloat(config.modelZ) : 0;
52
- modelScale = (config.modelScale !== undefined) ? parseFloat(config.modelScale) : 1;
53
- modelRotationX = (config.modelRotationX !== undefined) ? parseFloat(config.modelRotationX) : 0;
54
- modelRotationY = (config.modelRotationY !== undefined) ? parseFloat(config.modelRotationY) : 0;
55
- modelRotationZ = (config.modelRotationZ !== undefined) ? parseFloat(config.modelRotationZ) : 0;
56
-
57
- const cameraX = (config.cameraX !== undefined) ? parseFloat(config.cameraX) : 0;
58
- const cameraY = (config.cameraY !== undefined) ? parseFloat(config.cameraY) : 2;
59
- const cameraZ = (config.cameraZ !== undefined) ? parseFloat(config.cameraZ) : 5;
60
- const cameraXPhone = (config.cameraXPhone !== undefined) ? parseFloat(config.cameraXPhone) : cameraX;
61
- const cameraYPhone = (config.cameraYPhone !== undefined) ? parseFloat(config.cameraYPhone) : cameraY;
62
- const cameraZPhone = (config.cameraZPhone !== undefined) ? parseFloat(config.cameraZPhone) : (cameraZ * 1.5);
63
-
64
- chosenCameraX = isMobile ? cameraXPhone : cameraX;
65
- chosenCameraY = isMobile ? cameraYPhone : cameraY;
66
- chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
67
-
68
- const canvasId = 'canvas-' + instanceId;
69
- const progressDialog = document.getElementById('progress-dialog-' + instanceId);
70
- const progressIndicator= document.getElementById('progress-indicator-' + instanceId);
71
- const viewerContainer = document.getElementById('viewer-container-' + instanceId);
72
-
73
- let oldCanvas = document.getElementById(canvasId);
74
- if (oldCanvas) oldCanvas.remove();
75
-
76
- const canvas = document.createElement('canvas');
77
- canvas.id = canvasId;
78
- canvas.className = 'ply-canvas';
79
- canvas.style.width = "100%";
80
- canvas.style.height = "100%";
81
- canvas.setAttribute('tabindex', '0');
82
- viewerContainer.insertBefore(canvas, progressDialog);
83
-
84
- // Apply background color from config if present
85
- canvas.style.background = canvasBg;
86
-
87
- canvas.style.touchAction = "none";
88
- canvas.style.webkitTouchCallout = "none";
89
- canvas.addEventListener('gesturestart', e => e.preventDefault());
90
- canvas.addEventListener('gesturechange', e => e.preventDefault());
91
- canvas.addEventListener('gestureend', e => e.preventDefault());
92
- canvas.addEventListener('dblclick', e => e.preventDefault());
93
- canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
94
-
95
- // --- Mouse wheel suppression
96
- canvas.addEventListener('wheel', (e) => {
97
- e.preventDefault(); // Only block page scroll if mouse is over viewer
98
- }, { passive: false });
99
-
100
- if (progressDialog) progressDialog.style.display = 'block';
101
-
102
-
103
- if (!pc) {
104
- pc = await import("https://cdn.jsdelivr.net/npm/playcanvas@latest/+esm");
105
- window.pc = pc;
106
- }
107
 
108
- // Create app and graphics device
109
- let device;
110
- try {
111
- device = await pc.createGraphicsDevice(canvas, {
112
- deviceTypes: ["webgl2", "webgl1"],
113
- glslangUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/glslang/glslang.js",
114
- twgslUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/twgsl/twgsl.js",
115
- antialias: false
116
- });
117
- device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
118
- } catch (e) {
119
- throw e;
120
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
- const opts = new pc.AppOptions();
123
- opts.graphicsDevice = device;
124
- opts.mouse = new pc.Mouse(canvas);
125
- opts.touch = new pc.TouchDevice(canvas);
126
- opts.componentSystems = [
127
- pc.RenderComponentSystem,
128
- pc.CameraComponentSystem,
129
- pc.LightComponentSystem,
130
- pc.ScriptComponentSystem,
131
- pc.GSplatComponentSystem,
132
- pc.CollisionComponentSystem,
133
- pc.RigidbodyComponentSystem
134
- ];
135
- opts.resourceHandlers = [
136
- pc.TextureHandler,
137
- pc.ContainerHandler,
138
- pc.ScriptHandler,
139
- pc.GSplatHandler
140
- ];
141
-
142
- app = new pc.Application(canvas, opts);
143
- app.setCanvasFillMode(pc.FILLMODE_NONE);
144
- app.setCanvasResolution(pc.RESOLUTION_AUTO);
145
-
146
-
147
- // Resizing
148
- resizeObserver = new ResizeObserver(entries => {
149
- entries.forEach(entry => {
150
- try {
151
- app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
152
- } catch (e) {
153
-
154
- }
155
- });
 
 
 
156
  });
157
- resizeObserver.observe(viewerContainer);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
- window.addEventListener('resize', () => {
160
- try {
161
- app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
162
- } catch (e) {
163
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  }
165
  });
166
- app.on('destroy', () => resizeObserver.disconnect());
167
-
168
- // Prepare asset loading (SOGS as 'gsplat')
169
- const assets = {
170
- sogs: new pc.Asset('gsplat', 'gsplat', { url: sogsUrl }),
171
- orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" }),
172
- glb: new pc.Asset('glb', 'container', { url: glbUrl }),
173
- };
174
- for (const key in assets) app.assets.add(assets[key]);
175
-
176
-
177
- const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
178
 
179
- loader.load(() => {
180
 
181
- app.start();
182
 
183
-
184
- if (progressDialog) progressDialog.style.display = 'none';
185
-
186
- // Add SOGS model
187
- modelEntity = new pc.Entity('model');
188
- modelEntity.addComponent('gsplat', { asset: assets.sogs });
189
- modelEntity.setLocalPosition(modelX, modelY, modelZ);
190
- modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
191
- modelEntity.setLocalScale(modelScale, modelScale, modelScale);
192
- app.root.addChild(modelEntity);
193
-
194
-
195
- // Add the GLB entity if provided
196
- if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
197
- const glbEntity = assets.glb.resource.instantiateRenderEntity();
198
- app.root.addChild(glbEntity);
199
- }
200
-
201
-
202
- // CAMERA
203
- cameraEntity = new pc.Entity('camera');
204
- // Support background color
205
- let bg = [1, 1, 1, 1];
206
- if (canvasBg && /^#?[0-9a-f]{6,8}$/i.test(canvasBg.replace("#", ""))) {
207
- bg = hexToRgbaArray(canvasBg);
208
- }
209
- cameraEntity.addComponent('camera', { clearColor: new pc.Color(bg[0], bg[1], bg[2], bg[3]) });
210
- cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
211
- cameraEntity.lookAt(modelEntity.getPosition());
212
- cameraEntity.addComponent('script');
213
-
214
- // Pass all attributes to Orbit Camera script
215
- cameraEntity.script.create('orbitCamera', {
216
- attributes: {
217
- focusEntity: modelEntity,
218
- inertiaFactor: 0.2,
219
- distanceMax: maxZoom,
220
- distanceMin: minZoom,
221
- pitchAngleMax: maxAngle,
222
- pitchAngleMin: minAngle,
223
- yawAngleMax: maxAzimuth,
224
- yawAngleMin: minAzimuth,
225
- minY: minY,
226
- frameOnStart: false
227
- }
228
- });
229
- cameraEntity.script.create('orbitCameraInputMouse');
230
- cameraEntity.script.create('orbitCameraInputTouch');
231
- app.root.addChild(cameraEntity);
232
-
233
- app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
234
-
235
- app.once('update', () => resetViewerCamera());
236
-
237
-
238
-
239
- // Tooltips support (optional)
240
  if (config.tooltips_url) {
241
- import('https://mikafil-viewer-sgos.static.hf.space/tooltips.js').then(tooltipsModule => {
242
  tooltipsModule.initializeTooltips({
243
  app,
244
  cameraEntity,
@@ -247,27 +198,23 @@ export async function initializeViewer(config, instanceId) {
247
  defaultVisible: !!config.showTooltipsDefault,
248
  moveDuration: config.tooltipMoveDuration || 0.6
249
  });
250
- alert("Tooltips loaded.");
251
- }).catch(e => { alert("Tooltips module error: " + e); });
 
252
  }
 
 
 
253
 
254
-
255
- viewerInitialized = true;
256
- alert("Viewer initialized success!");
257
- });
258
  }
259
 
260
- // Resets the viewer camera (as in your PLY example)
261
  export function resetViewerCamera() {
262
- if (!cameraEntity || !modelEntity || !app) {
263
- alert("resetViewerCamera: cameraEntity/modelEntity/app not ready");
264
- return;
265
- }
266
  const orbitCam = cameraEntity.script.orbitCamera;
267
- if (!orbitCam) {
268
- alert("resetViewerCamera: orbitCam missing");
269
- return;
270
- }
271
 
272
  const modelPos = modelEntity.getPosition();
273
  const tempEnt = new pc.Entity();
@@ -304,4 +251,7 @@ export function resetViewerCamera() {
304
  if (orbitCam._updatePosition) orbitCam._updatePosition();
305
 
306
  tempEnt.destroy();
 
 
 
307
  }
 
1
+ // viewer.js
2
+ // ==============================
3
+
4
+ let pc;
5
  export let app = null;
6
  let cameraEntity = null;
7
  let modelEntity = null;
 
11
  let chosenCameraX, chosenCameraY, chosenCameraZ;
12
  let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
13
  let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
14
+ let sogsUrl, glbUrl;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  export async function initializeViewer(config, instanceId) {
17
+ if (viewerInitialized) return;
 
 
18
 
19
+ const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
20
+ const isMobile = isIOS || /Android/i.test(navigator.userAgent);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ sogsUrl = config.sogs_json_url;
23
+ glbUrl = config.glb_url;
24
+ minZoom = parseFloat(config.minZoom || "1");
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;
50
+ chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
51
+
52
+ const canvasId = 'canvas-' + instanceId;
53
+ const progressDialog = document.getElementById('progress-dialog-' + instanceId);
54
+ const progressIndicator = document.getElementById('progress-indicator-' + instanceId);
55
+ const viewerContainer = document.getElementById('viewer-container-' + instanceId);
56
+
57
+ let oldCanvas = document.getElementById(canvasId);
58
+ if (oldCanvas) oldCanvas.remove();
59
+
60
+ const canvas = document.createElement('canvas');
61
+ canvas.id = canvasId;
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
+ // --- The following line attaches mouse wheel suppression to canvas only ---
77
+ canvas.addEventListener('wheel', (e) => {
78
+ e.preventDefault(); // Only block page scroll if mouse is over viewer
79
+ }, { passive: false });
80
+
81
+ progressDialog.style.display = 'block';
82
+
83
+ if (!pc) {
84
+ pc = await import("https://esm.run/playcanvas");
85
+ window.pc = pc;
86
+ }
87
 
88
+ // Create app first
89
+ const device = await pc.createGraphicsDevice(canvas, {
90
+ deviceTypes: ["webgl2"],
91
+ glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
92
+ twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
93
+ antialias: false
94
+ });
95
+ device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
96
+
97
+ const opts = new pc.AppOptions();
98
+ opts.graphicsDevice = device;
99
+ // Attach input only to canvas
100
+ opts.mouse = new pc.Mouse(canvas);
101
+ opts.touch = new pc.TouchDevice(canvas);
102
+ opts.componentSystems = [
103
+ pc.RenderComponentSystem,
104
+ pc.CameraComponentSystem,
105
+ pc.LightComponentSystem,
106
+ pc.ScriptComponentSystem,
107
+ pc.GSplatComponentSystem,
108
+ pc.CollisionComponentSystem,
109
+ pc.RigidbodyComponentSystem
110
+ ];
111
+ opts.resourceHandlers = [
112
+ pc.TextureHandler,
113
+ pc.ContainerHandler,
114
+ pc.ScriptHandler,
115
+ pc.GSplatHandler
116
+ ];
117
+
118
+ app = new pc.Application(canvas, opts);
119
+ app.setCanvasFillMode(pc.FILLMODE_NONE);
120
+ app.setCanvasResolution(pc.RESOLUTION_AUTO);
121
+
122
+ resizeObserver = new ResizeObserver(entries => {
123
+ entries.forEach(entry => {
124
+ app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
125
  });
126
+ });
127
+ resizeObserver.observe(viewerContainer);
128
+
129
+ window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
130
+ app.on('destroy', () => resizeObserver.disconnect());
131
+
132
+ // Assets after app exists
133
+ const assets = {
134
+ sogs: new pc.Asset('gsplat', 'gsplat', { url: sogsUrl }),
135
+ orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
136
+ glb: new pc.Asset('glb', 'container', { url: glbUrl }),
137
+ };
138
+ for (const key in assets) app.assets.add(assets[key]);
139
+
140
+ const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
141
+ loader.load(() => {
142
+ app.start();
143
+ progressDialog.style.display = 'none';
144
+
145
+ // Add PLY/GSplat model
146
+ modelEntity = new pc.Entity('model');
147
+ modelEntity.addComponent('gsplat', { asset: assets.sogs });
148
+ modelEntity.setLocalPosition(modelX, modelY, modelZ);
149
+ modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
150
+ modelEntity.setLocalScale(modelScale, modelScale, modelScale);
151
+ app.root.addChild(modelEntity);
152
+
153
+ // --- Add the GLB entity ---
154
+ if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
155
+ const glbEntity = assets.glb.resource.instantiateRenderEntity();
156
+ app.root.addChild(glbEntity);
157
+ }
158
 
159
+ cameraEntity = new pc.Entity('camera');
160
+ // PURE WHITE BACKGROUND
161
+ cameraEntity.addComponent('camera', { clearColor: new pc.Color(1, 1, 1, 1) }); // White RGBA
162
+ cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
163
+ cameraEntity.lookAt(modelEntity.getPosition());
164
+ cameraEntity.addComponent('script');
165
+
166
+ // === MAIN FIX: Pass ALL relevant attributes including minY ===
167
+ cameraEntity.script.create('orbitCamera', {
168
+ attributes: {
169
+ focusEntity: modelEntity,
170
+ inertiaFactor: 0.2,
171
+ distanceMax: maxZoom,
172
+ distanceMin: minZoom,
173
+ pitchAngleMax: maxAngle,
174
+ pitchAngleMin: minAngle,
175
+ yawAngleMax: maxAzimuth,
176
+ yawAngleMin: minAzimuth,
177
+ minY: minY,
178
+ frameOnStart: false
179
  }
180
  });
181
+ cameraEntity.script.create('orbitCameraInputMouse');
182
+ cameraEntity.script.create('orbitCameraInputTouch');
183
+ app.root.addChild(cameraEntity);
 
 
 
 
 
 
 
 
 
184
 
185
+ app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
186
 
187
+ app.once('update', () => resetViewerCamera());
188
 
189
+ // Tooltips supported if tooltips_url set
190
+ try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  if (config.tooltips_url) {
192
+ import('./tooltips.js').then(tooltipsModule => {
193
  tooltipsModule.initializeTooltips({
194
  app,
195
  cameraEntity,
 
198
  defaultVisible: !!config.showTooltipsDefault,
199
  moveDuration: config.tooltipMoveDuration || 0.6
200
  });
201
+ }).catch(e => {
202
+ // Tooltips optional: fail silently if missing
203
+ });
204
  }
205
+ } catch (e) {
206
+ // Tooltips optional, fail silently
207
+ }
208
 
209
+ viewerInitialized = true;
210
+ });
 
 
211
  }
212
 
 
213
  export function resetViewerCamera() {
214
+ try {
215
+ if (!cameraEntity || !modelEntity || !app) return;
 
 
216
  const orbitCam = cameraEntity.script.orbitCamera;
217
+ if (!orbitCam) return;
 
 
 
218
 
219
  const modelPos = modelEntity.getPosition();
220
  const tempEnt = new pc.Entity();
 
251
  if (orbitCam._updatePosition) orbitCam._updatePosition();
252
 
253
  tempEnt.destroy();
254
+ } catch (e) {
255
+ // Silent fail
256
+ }
257
  }