MikaFil commited on
Commit
830bfd9
·
verified ·
1 Parent(s): 5b6be20

Update viewer.js

Browse files
Files changed (1) hide show
  1. viewer.js +339 -293
viewer.js CHANGED
@@ -10,319 +10,365 @@ let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, min
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
- resizeObserver.observe(viewerContainer);
156
-
157
- window.addEventListener('resize', () => {
158
- try {
159
- app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
160
- } catch (e) { }
161
  });
162
- app.on('destroy', () => resizeObserver.disconnect());
163
 
164
- // Prepare asset loading (SOGS as 'gsplat')
165
- const assets = {
166
- sogs: new pc.Asset('gsplat', 'gsplat', { url: sogsUrl }),
167
- orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" }),
168
- glb: new pc.Asset('glb', 'container', { url: glbUrl }),
169
- };
170
- for (const key in assets) app.assets.add(assets[key]);
171
 
172
- // Individual asset error listeners
173
- assets.sogs.on('error', (err) => { });
174
- assets.orbit.on('error', (err) => { });
175
- if (assets.glb) assets.glb.on('error', (err) => { });
176
 
177
- assets.sogs.ready(() => { });
178
- assets.orbit.ready(() => { });
179
- if (assets.glb) assets.glb.ready(() => { });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
- const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
 
 
 
 
 
 
 
 
 
 
 
 
182
 
183
- loader.on('error', (err) => { });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
- loader.load(() => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
- try {
188
- app.start();
189
- } catch (e) {
190
- throw e;
191
- }
192
-
193
- if (progressDialog) progressDialog.style.display = 'none';
194
-
195
- // Add SOGS model
196
- try {
197
- modelEntity = new pc.Entity('model');
198
- modelEntity.addComponent('gsplat', { asset: assets.sogs });
199
- modelEntity.setLocalPosition(modelX, modelY, modelZ);
200
- modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
201
- modelEntity.setLocalScale(modelScale, modelScale, modelScale);
202
- app.root.addChild(modelEntity);
203
- } catch (e) {
204
  throw e;
205
- }
 
206
 
207
- // Add the GLB entity if provided
208
- try {
209
- if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
210
- const glbEntity = assets.glb.resource.instantiateRenderEntity();
211
- app.root.addChild(glbEntity);
212
- }
213
- } catch (e) { }
214
-
215
- // CAMERA
216
- try {
217
- cameraEntity = new pc.Entity('camera');
218
- // Support background color
219
- let bg = [1, 1, 1, 1];
220
- if (canvasBg && /^#?[0-9a-f]{6,8}$/i.test(canvasBg.replace("#", ""))) {
221
- bg = hexToRgbaArray(canvasBg);
222
  }
223
- cameraEntity.addComponent('camera', { clearColor: new pc.Color(bg[0], bg[1], bg[2], bg[3]) });
224
- cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
225
- cameraEntity.lookAt(modelEntity.getPosition());
226
- cameraEntity.addComponent('script');
227
-
228
- // Pass all attributes to Orbit Camera script
229
- cameraEntity.script.create('orbitCamera', {
230
- attributes: {
231
- focusEntity: modelEntity,
232
- inertiaFactor: 0.2,
233
- distanceMax: maxZoom,
234
- distanceMin: minZoom,
235
- pitchAngleMax: maxAngle,
236
- pitchAngleMin: minAngle,
237
- yawAngleMax: maxAzimuth,
238
- yawAngleMin: minAzimuth,
239
- minY: minY,
240
- frameOnStart: false
241
- }
242
- });
243
- cameraEntity.script.create('orbitCameraInputMouse');
244
- cameraEntity.script.create('orbitCameraInputTouch');
245
- app.root.addChild(cameraEntity);
246
-
247
- } catch (e) { }
248
-
249
- try {
250
- app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
251
- } catch (e) { }
252
-
253
- try {
254
- app.once('update', () => resetViewerCamera());
255
- } catch (e) { }
256
-
257
- // Tooltips support (optional)
258
- try {
259
- if (config.tooltips_url) {
260
- import('https://mikafil-viewer-sgos.static.hf.space/tooltips.js').then(tooltipsModule => {
261
- tooltipsModule.initializeTooltips({
262
- app,
263
- cameraEntity,
264
- modelEntity,
265
- tooltipsUrl: config.tooltips_url,
266
- defaultVisible: !!config.showTooltipsDefault,
267
- moveDuration: config.tooltipMoveDuration || 0.6
268
- });
269
- }).catch(e => { });
270
  }
271
- } catch (e) { }
272
 
273
- viewerInitialized = true;
274
- });
 
 
275
 
276
- } catch (e) {
277
- throw e;
278
- }
279
- }
280
 
281
- // Resets the viewer camera (as in your PLY example)
282
- export function resetViewerCamera() {
283
- try {
284
- if (!cameraEntity || !modelEntity || !app) {
285
- return;
286
- }
287
- const orbitCam = cameraEntity.script.orbitCamera;
288
- if (!orbitCam) {
289
- return;
290
- }
291
-
292
- const modelPos = modelEntity.getPosition();
293
- const tempEnt = new pc.Entity();
294
- tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
295
- tempEnt.lookAt(modelPos);
296
-
297
- const dist = new pc.Vec3().sub2(
298
- new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ),
299
- modelPos
300
- ).length();
301
-
302
- cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
303
- cameraEntity.lookAt(modelPos);
304
-
305
- orbitCam.pivotPoint = modelPos.clone();
306
- orbitCam._targetDistance = dist;
307
- orbitCam._distance = dist;
308
-
309
- const rot = tempEnt.getRotation();
310
- const fwd = new pc.Vec3();
311
- rot.transformVector(pc.Vec3.FORWARD, fwd);
312
-
313
- const yaw = Math.atan2(-fwd.x, -fwd.z) * pc.math.RAD_TO_DEG;
314
- const yawQuat = new pc.Quat().setFromEulerAngles(0, -yaw, 0);
315
- const rotNoYaw = new pc.Quat().mul2(yawQuat, rot);
316
- const fNoYaw = new pc.Vec3();
317
- rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
318
- const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
319
-
320
- orbitCam._targetYaw = yaw;
321
- orbitCam._yaw = yaw;
322
- orbitCam._targetPitch = pitch;
323
- orbitCam._pitch = pitch;
324
- if (orbitCam._updatePosition) orbitCam._updatePosition();
325
-
326
- tempEnt.destroy();
327
- } catch (e) { }
328
  }
 
10
  let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
11
  let sogsUrl, glbUrl, canvasBg;
12
 
13
+ // -------------------- CORS PATCHER --------------------
14
+ async function patchSogsJsonForCors(config) {
15
+ const originalMetaUrl = config.sogs_json_url;
16
+ const baseDir = originalMetaUrl.substring(0, originalMetaUrl.lastIndexOf('/') + 1);
17
+ // Use your own proxy for production; this public one is for dev/demo only
18
+ const corsPrefix = "https://corsproxy.io/?";
19
+ const resp = await fetch(originalMetaUrl);
20
+ if (!resp.ok) throw new Error("Failed to fetch meta.json");
21
+ const meta = await resp.json();
22
+ for (const key of Object.keys(meta)) {
23
+ if (meta[key].files) {
24
+ meta[key].files = meta[key].files.map(filename => {
25
+ let url = filename;
26
+ if (!filename.startsWith("http")) url = baseDir + filename;
27
+ return corsPrefix + url;
28
+ });
29
+ }
30
+ }
31
+ const metaBlob = new Blob([JSON.stringify(meta)], { type: "application/json" });
32
+ // Save blob URL for use in asset loader
33
+ config.sogs_json_url = URL.createObjectURL(metaBlob);
34
  }
35
 
36
+ // ------------------------------------------------------
37
 
38
+ function hexToRgbaArray(hex) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  try {
40
+ hex = hex.replace("#", "");
41
+ if (hex.length === 6) hex += "FF";
42
+ if (hex.length !== 8) return [1, 1, 1, 1];
43
+ const num = parseInt(hex, 16);
44
+ return [
45
+ ((num >> 24) & 0xFF) / 255,
46
+ ((num >> 16) & 0xFF) / 255,
47
+ ((num >> 8) & 0xFF) / 255,
48
+ (num & 0xFF) / 255
49
+ ];
50
  } catch (e) {
51
+ return [1, 1, 1, 1];
52
  }
53
+ }
54
 
55
+ // This is not used for SOGS loading, but kept for completeness/legacy usage
56
+ async function loadImageAsTexture(url, app) {
57
+ return new Promise((resolve, reject) => {
58
+ const img = new window.Image();
59
+ img.crossOrigin = "anonymous";
60
+ img.onload = function () {
61
+ const tex = new pc.Texture(app.graphicsDevice, {
62
+ width: img.width,
63
+ height: img.height,
64
+ format: pc.PIXELFORMAT_R8_G8_B8_A8,
65
+ });
66
+ tex.setSource(img);
67
+ resolve(tex);
68
+ };
69
+ img.onerror = reject;
70
+ img.src = url;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  });
72
+ }
73
 
74
+ export async function initializeViewer(config, instanceId) {
75
+ // Patch SOGS meta.json for CORS (only needed for HuggingFace or remote dataset)
76
+ if (config.sogs_json_url && config.sogs_json_url.includes("huggingface.co")) {
77
+ await patchSogsJsonForCors(config);
78
+ }
 
 
79
 
80
+ const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
81
+ const isMobile = isIOS || /Android/i.test(navigator.userAgent);
 
 
82
 
83
+ try {
84
+ // Parse config
85
+ sogsUrl = config.sogs_json_url;
86
+ glbUrl = config.glb_url;
87
+ canvasBg = config.canvas_background || "#ffffff";
88
+ minZoom = parseFloat(config.minZoom || "1");
89
+ maxZoom = parseFloat(config.maxZoom || "20");
90
+ minAngle = parseFloat(config.minAngle || "-45");
91
+ maxAngle = parseFloat(config.maxAngle || "90");
92
+ minAzimuth = (config.minAzimuth !== undefined) ? parseFloat(config.minAzimuth) : -360;
93
+ maxAzimuth = (config.maxAzimuth !== undefined) ? parseFloat(config.maxAzimuth) : 360;
94
+ minPivotY = parseFloat(config.minPivotY || "0");
95
+ minY = (config.minY !== undefined) ? parseFloat(config.minY) : 0;
96
+
97
+ modelX = (config.modelX !== undefined) ? parseFloat(config.modelX) : 0;
98
+ modelY = (config.modelY !== undefined) ? parseFloat(config.modelY) : 0;
99
+ modelZ = (config.modelZ !== undefined) ? parseFloat(config.modelZ) : 0;
100
+ modelScale = (config.modelScale !== undefined) ? parseFloat(config.modelScale) : 1;
101
+ modelRotationX = (config.modelRotationX !== undefined) ? parseFloat(config.modelRotationX) : 0;
102
+ modelRotationY = (config.modelRotationY !== undefined) ? parseFloat(config.modelRotationY) : 0;
103
+ modelRotationZ = (config.modelRotationZ !== undefined) ? parseFloat(config.modelRotationZ) : 0;
104
+
105
+ const cameraX = (config.cameraX !== undefined) ? parseFloat(config.cameraX) : 0;
106
+ const cameraY = (config.cameraY !== undefined) ? parseFloat(config.cameraY) : 2;
107
+ const cameraZ = (config.cameraZ !== undefined) ? parseFloat(config.cameraZ) : 5;
108
+ const cameraXPhone = (config.cameraXPhone !== undefined) ? parseFloat(config.cameraXPhone) : cameraX;
109
+ const cameraYPhone = (config.cameraYPhone !== undefined) ? parseFloat(config.cameraYPhone) : cameraY;
110
+ const cameraZPhone = (config.cameraZPhone !== undefined) ? parseFloat(config.cameraZPhone) : (cameraZ * 1.5);
111
+
112
+ chosenCameraX = isMobile ? cameraXPhone : cameraX;
113
+ chosenCameraY = isMobile ? cameraYPhone : cameraY;
114
+ chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
115
+
116
+ const canvasId = 'canvas-' + instanceId;
117
+ const progressDialog = document.getElementById('progress-dialog-' + instanceId);
118
+ const progressIndicator = document.getElementById('progress-indicator-' + instanceId);
119
+ const viewerContainer = document.getElementById('viewer-container-' + instanceId);
120
+
121
+ let oldCanvas = document.getElementById(canvasId);
122
+ if (oldCanvas) oldCanvas.remove();
123
+
124
+ const canvas = document.createElement('canvas');
125
+ canvas.id = canvasId;
126
+ canvas.className = 'ply-canvas';
127
+ canvas.style.width = "100%";
128
+ canvas.style.height = "100%";
129
+ canvas.setAttribute('tabindex', '0');
130
+ viewerContainer.insertBefore(canvas, progressDialog);
131
+
132
+ // Apply background color from config if present
133
+ canvas.style.background = canvasBg;
134
+
135
+ canvas.style.touchAction = "none";
136
+ canvas.style.webkitTouchCallout = "none";
137
+ canvas.addEventListener('gesturestart', e => e.preventDefault());
138
+ canvas.addEventListener('gesturechange', e => e.preventDefault());
139
+ canvas.addEventListener('gestureend', e => e.preventDefault());
140
+ canvas.addEventListener('dblclick', e => e.preventDefault());
141
+ canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
142
+
143
+ // --- Mouse wheel suppression
144
+ canvas.addEventListener('wheel', (e) => {
145
+ e.preventDefault(); // Only block page scroll if mouse is over viewer
146
+ }, { passive: false });
147
+
148
+ if (progressDialog) progressDialog.style.display = 'block';
149
+
150
+ if (!pc) {
151
+ pc = await import("https://cdn.jsdelivr.net/npm/playcanvas@latest/+esm");
152
+ window.pc = pc;
153
+ }
154
 
155
+ // Create app and graphics device
156
+ let device;
157
+ try {
158
+ device = await pc.createGraphicsDevice(canvas, {
159
+ deviceTypes: ["webgl2", "webgl1"],
160
+ glslangUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/glslang/glslang.js",
161
+ twgslUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/twgsl/twgsl.js",
162
+ antialias: false
163
+ });
164
+ device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
165
+ } catch (e) {
166
+ throw e;
167
+ }
168
 
169
+ const opts = new pc.AppOptions();
170
+ opts.graphicsDevice = device;
171
+ opts.mouse = new pc.Mouse(canvas);
172
+ opts.touch = new pc.TouchDevice(canvas);
173
+ opts.componentSystems = [
174
+ pc.RenderComponentSystem,
175
+ pc.CameraComponentSystem,
176
+ pc.LightComponentSystem,
177
+ pc.ScriptComponentSystem,
178
+ pc.GSplatComponentSystem,
179
+ pc.CollisionComponentSystem,
180
+ pc.RigidbodyComponentSystem
181
+ ];
182
+ opts.resourceHandlers = [
183
+ pc.TextureHandler,
184
+ pc.ContainerHandler,
185
+ pc.ScriptHandler,
186
+ pc.GSplatHandler
187
+ ];
188
+
189
+ app = new pc.Application(canvas, opts);
190
+ app.setCanvasFillMode(pc.FILLMODE_NONE);
191
+ app.setCanvasResolution(pc.RESOLUTION_AUTO);
192
+
193
+ // Resizing
194
+ resizeObserver = new ResizeObserver(entries => {
195
+ entries.forEach(entry => {
196
+ try {
197
+ app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
198
+ } catch (e) { }
199
+ });
200
+ });
201
+ resizeObserver.observe(viewerContainer);
202
 
203
+ window.addEventListener('resize', () => {
204
+ try {
205
+ app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
206
+ } catch (e) { }
207
+ });
208
+ app.on('destroy', () => resizeObserver.disconnect());
209
+
210
+ // Prepare asset loading (SOGS as 'gsplat')
211
+ const assets = {
212
+ sogs: new pc.Asset('gsplat', 'gsplat', { url: sogsUrl }),
213
+ orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" }),
214
+ glb: new pc.Asset('glb', 'container', { url: glbUrl }),
215
+ };
216
+ for (const key in assets) app.assets.add(assets[key]);
217
+
218
+ // Individual asset error listeners
219
+ assets.sogs.on('error', (err) => { });
220
+ assets.orbit.on('error', (err) => { });
221
+ if (assets.glb) assets.glb.on('error', (err) => { });
222
+
223
+ assets.sogs.ready(() => { });
224
+ assets.orbit.ready(() => { });
225
+ if (assets.glb) assets.glb.ready(() => { });
226
+
227
+ const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
228
+
229
+ loader.on('error', (err) => { });
230
+
231
+ loader.load(() => {
232
+
233
+ try {
234
+ app.start();
235
+ } catch (e) {
236
+ throw e;
237
+ }
238
+
239
+ if (progressDialog) progressDialog.style.display = 'none';
240
+
241
+ // Add SOGS model
242
+ try {
243
+ modelEntity = new pc.Entity('model');
244
+ modelEntity.addComponent('gsplat', { asset: assets.sogs });
245
+ modelEntity.setLocalPosition(modelX, modelY, modelZ);
246
+ modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
247
+ modelEntity.setLocalScale(modelScale, modelScale, modelScale);
248
+ app.root.addChild(modelEntity);
249
+ } catch (e) {
250
+ throw e;
251
+ }
252
+
253
+ // Add the GLB entity if provided
254
+ try {
255
+ if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
256
+ const glbEntity = assets.glb.resource.instantiateRenderEntity();
257
+ app.root.addChild(glbEntity);
258
+ }
259
+ } catch (e) { }
260
+
261
+ // CAMERA
262
+ try {
263
+ cameraEntity = new pc.Entity('camera');
264
+ // Support background color
265
+ let bg = [1, 1, 1, 1];
266
+ if (canvasBg && /^#?[0-9a-f]{6,8}$/i.test(canvasBg.replace("#", ""))) {
267
+ bg = hexToRgbaArray(canvasBg);
268
+ }
269
+ cameraEntity.addComponent('camera', { clearColor: new pc.Color(bg[0], bg[1], bg[2], bg[3]) });
270
+ cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
271
+ cameraEntity.lookAt(modelEntity.getPosition());
272
+ cameraEntity.addComponent('script');
273
+
274
+ // Pass all attributes to Orbit Camera script
275
+ cameraEntity.script.create('orbitCamera', {
276
+ attributes: {
277
+ focusEntity: modelEntity,
278
+ inertiaFactor: 0.2,
279
+ distanceMax: maxZoom,
280
+ distanceMin: minZoom,
281
+ pitchAngleMax: maxAngle,
282
+ pitchAngleMin: minAngle,
283
+ yawAngleMax: maxAzimuth,
284
+ yawAngleMin: minAzimuth,
285
+ minY: minY,
286
+ frameOnStart: false
287
+ }
288
+ });
289
+ cameraEntity.script.create('orbitCameraInputMouse');
290
+ cameraEntity.script.create('orbitCameraInputTouch');
291
+ app.root.addChild(cameraEntity);
292
+
293
+ } catch (e) { }
294
+
295
+ try {
296
+ app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
297
+ } catch (e) { }
298
+
299
+ try {
300
+ app.once('update', () => resetViewerCamera());
301
+ } catch (e) { }
302
+
303
+ // Tooltips support (optional)
304
+ try {
305
+ if (config.tooltips_url) {
306
+ import('https://mikafil-viewer-sgos.static.hf.space/tooltips.js').then(tooltipsModule => {
307
+ tooltipsModule.initializeTooltips({
308
+ app,
309
+ cameraEntity,
310
+ modelEntity,
311
+ tooltipsUrl: config.tooltips_url,
312
+ defaultVisible: !!config.showTooltipsDefault,
313
+ moveDuration: config.tooltipMoveDuration || 0.6
314
+ });
315
+ }).catch(e => { });
316
+ }
317
+ } catch (e) { }
318
+
319
+ viewerInitialized = true;
320
+ });
321
 
322
+ } catch (e) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  throw e;
324
+ }
325
+ }
326
 
327
+ // Resets the viewer camera (as in your PLY example)
328
+ export function resetViewerCamera() {
329
+ try {
330
+ if (!cameraEntity || !modelEntity || !app) {
331
+ return;
 
 
 
 
 
 
 
 
 
 
332
  }
333
+ const orbitCam = cameraEntity.script.orbitCamera;
334
+ if (!orbitCam) {
335
+ return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336
  }
 
337
 
338
+ const modelPos = modelEntity.getPosition();
339
+ const tempEnt = new pc.Entity();
340
+ tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
341
+ tempEnt.lookAt(modelPos);
342
 
343
+ const dist = new pc.Vec3().sub2(
344
+ new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ),
345
+ modelPos
346
+ ).length();
347
 
348
+ cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
349
+ cameraEntity.lookAt(modelPos);
350
+
351
+ orbitCam.pivotPoint = modelPos.clone();
352
+ orbitCam._targetDistance = dist;
353
+ orbitCam._distance = dist;
354
+
355
+ const rot = tempEnt.getRotation();
356
+ const fwd = new pc.Vec3();
357
+ rot.transformVector(pc.Vec3.FORWARD, fwd);
358
+
359
+ const yaw = Math.atan2(-fwd.x, -fwd.z) * pc.math.RAD_TO_DEG;
360
+ const yawQuat = new pc.Quat().setFromEulerAngles(0, -yaw, 0);
361
+ const rotNoYaw = new pc.Quat().mul2(yawQuat, rot);
362
+ const fNoYaw = new pc.Vec3();
363
+ rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
364
+ const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
365
+
366
+ orbitCam._targetYaw = yaw;
367
+ orbitCam._yaw = yaw;
368
+ orbitCam._targetPitch = pitch;
369
+ orbitCam._pitch = pitch;
370
+ if (orbitCam._updatePosition) orbitCam._updatePosition();
371
+
372
+ tempEnt.destroy();
373
+ } catch (e) { }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
  }