MikaFil commited on
Commit
a63bcf2
·
verified ·
1 Parent(s): 26257e0

Update viewer.js

Browse files
Files changed (1) hide show
  1. viewer.js +102 -93
viewer.js CHANGED
@@ -1,6 +1,7 @@
1
  // viewer.js
2
- // Modern PlayCanvas GSplat/SOGS Viewer
3
- let pc;
 
4
  export let app = null;
5
  let cameraEntity = null;
6
  let modelEntity = null;
@@ -10,56 +11,38 @@ let resizeObserver = null;
10
  let chosenCameraX, chosenCameraY, chosenCameraZ;
11
  let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
12
  let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
 
13
 
14
- // Main SOGS meta.json URL
15
- let sogsUrl;
16
-
17
- // == MAIN ENTRY POINT ==
18
  export async function initializeViewer(config, instanceId) {
19
  if (viewerInitialized) return;
20
 
21
- // --- PlayCanvas: dynamically import from esm.run ---
22
- if (!pc) {
23
- try {
24
- pc = await import("https://esm.run/playcanvas");
25
- window.pc = pc; // for debugging
26
- } catch (e) {
27
- alert('Failed to load PlayCanvas engine from esm.run: ' + e);
28
- return;
29
- }
30
- }
31
-
32
- // -- Device type detection --
33
  const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
34
  const isMobile = isIOS || /Android/i.test(navigator.userAgent);
35
 
36
- // -- Get config values --
37
- sogsUrl = config.sogs_json_url;
38
- if (!sogsUrl) {
39
- alert('Missing SOGS (meta.json) URL in config.sogs_json_url.');
40
- return;
41
- }
42
-
43
- minZoom = parseFloat(config.minZoom || "1");
44
- maxZoom = parseFloat(config.maxZoom || "20");
45
- minAngle = parseFloat(config.minAngle || "-45");
46
- maxAngle = parseFloat(config.maxAngle || "90");
47
- minAzimuth = (config.minAzimuth !== undefined) ? parseFloat(config.minAzimuth) : -360;
48
- maxAzimuth = (config.maxAzimuth !== undefined) ? parseFloat(config.maxAzimuth) : 360;
49
  minPivotY = parseFloat(config.minPivotY || "0");
50
- minY = (config.minY !== undefined) ? parseFloat(config.minY) : 0;
51
-
52
- modelX = (config.modelX !== undefined) ? parseFloat(config.modelX) : 0;
53
- modelY = (config.modelY !== undefined) ? parseFloat(config.modelY) : 0;
54
- modelZ = (config.modelZ !== undefined) ? parseFloat(config.modelZ) : 0;
55
- modelScale = (config.modelScale !== undefined) ? parseFloat(config.modelScale) : 1;
56
- modelRotationX = (config.modelRotationX !== undefined) ? parseFloat(config.modelRotationX) : 0;
57
- modelRotationY = (config.modelRotationY !== undefined) ? parseFloat(config.modelRotationY) : 0;
58
- modelRotationZ = (config.modelRotationZ !== undefined) ? parseFloat(config.modelRotationZ) : 0;
59
-
60
- const cameraX = (config.cameraX !== undefined) ? parseFloat(config.cameraX) : 0;
61
- const cameraY = (config.cameraY !== undefined) ? parseFloat(config.cameraY) : 2;
62
- const cameraZ = (config.cameraZ !== undefined) ? parseFloat(config.cameraZ) : 5;
63
  const cameraXPhone = (config.cameraXPhone !== undefined) ? parseFloat(config.cameraXPhone) : cameraX;
64
  const cameraYPhone = (config.cameraYPhone !== undefined) ? parseFloat(config.cameraYPhone) : cameraY;
65
  const cameraZPhone = (config.cameraZPhone !== undefined) ? parseFloat(config.cameraZPhone) : (cameraZ * 1.5);
@@ -68,16 +51,14 @@ export async function initializeViewer(config, instanceId) {
68
  chosenCameraY = isMobile ? cameraYPhone : cameraY;
69
  chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
70
 
71
- // -- DOM Elements --
72
- const canvasId = 'canvas-' + instanceId;
73
- const progressDialog = document.getElementById('progress-dialog-' + instanceId);
74
- const viewerContainer = document.getElementById('viewer-container-' + instanceId);
75
 
76
- // Remove old canvas if any
77
  let oldCanvas = document.getElementById(canvasId);
78
  if (oldCanvas) oldCanvas.remove();
79
 
80
- // Create and add canvas
81
  const canvas = document.createElement('canvas');
82
  canvas.id = canvasId;
83
  canvas.className = 'ply-canvas';
@@ -85,6 +66,10 @@ export async function initializeViewer(config, instanceId) {
85
  canvas.style.height = "100%";
86
  canvas.setAttribute('tabindex', '0');
87
  viewerContainer.insertBefore(canvas, progressDialog);
 
 
 
 
88
  canvas.style.touchAction = "none";
89
  canvas.style.webkitTouchCallout = "none";
90
  canvas.addEventListener('gesturestart', e => e.preventDefault());
@@ -92,48 +77,51 @@ export async function initializeViewer(config, instanceId) {
92
  canvas.addEventListener('gestureend', e => e.preventDefault());
93
  canvas.addEventListener('dblclick', e => e.preventDefault());
94
  canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
95
- canvas.addEventListener('wheel', (e) => { e.preventDefault(); }, { passive: false });
 
 
 
 
96
 
97
  progressDialog.style.display = 'block';
98
 
99
- // --- PlayCanvas Graphics Device ---
100
- let device;
101
- try {
102
- device = await pc.createGraphicsDevice(canvas, {
103
- deviceTypes: ["webgl2"],
104
- antialias: false,
105
- glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
106
- twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js"
107
- });
108
- device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
109
- } catch (e) {
110
- alert('Failed to create PlayCanvas GraphicsDevice: ' + e);
111
- return;
112
  }
113
 
114
- // --- PlayCanvas AppBase (modern) ---
115
- let appOpts = new pc.AppOptions();
116
- appOpts.graphicsDevice = device;
117
- appOpts.mouse = new pc.Mouse(canvas);
118
- appOpts.touch = new pc.TouchDevice(canvas);
119
- appOpts.componentSystems = [
 
 
 
 
 
 
 
 
120
  pc.RenderComponentSystem,
121
  pc.CameraComponentSystem,
122
  pc.LightComponentSystem,
123
  pc.ScriptComponentSystem,
124
- pc.GSplatComponentSystem
 
 
 
125
  ];
126
- appOpts.resourceHandlers = [
127
  pc.TextureHandler,
128
  pc.ContainerHandler,
129
  pc.ScriptHandler,
 
130
  pc.GSplatHandler
131
  ];
132
 
133
- app = new pc.AppBase(canvas);
134
- app.init(appOpts);
135
-
136
- // Fill/resize
137
  app.setCanvasFillMode(pc.FILLMODE_NONE);
138
  app.setCanvasResolution(pc.RESOLUTION_AUTO);
139
 
@@ -147,11 +135,11 @@ export async function initializeViewer(config, instanceId) {
147
  window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
148
  app.on('destroy', () => resizeObserver.disconnect());
149
 
150
- // --- Assets: GSplat model + orbit camera script ---
151
  const assets = {
152
- model: new pc.Asset('gsplat', 'gsplat', { url: sogsUrl }),
153
- orbit: new pc.Asset('orbit', 'script', { url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" })
154
- // Optionally add others if needed
155
  };
156
  for (const key in assets) app.assets.add(assets[key]);
157
 
@@ -160,22 +148,42 @@ export async function initializeViewer(config, instanceId) {
160
  app.start();
161
  progressDialog.style.display = 'none';
162
 
163
- // --- Add GSplat Model ---
164
  modelEntity = new pc.Entity('model');
165
- modelEntity.addComponent('gsplat', { asset: assets.model });
166
  modelEntity.setLocalPosition(modelX, modelY, modelZ);
167
  modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
168
  modelEntity.setLocalScale(modelScale, modelScale, modelScale);
169
  app.root.addChild(modelEntity);
170
 
171
- // --- Camera ---
 
 
 
 
 
 
172
  cameraEntity = new pc.Entity('camera');
173
- cameraEntity.addComponent('camera', {
174
- clearColor: new pc.Color(1, 1, 1, 1)
175
- });
 
 
 
 
 
 
 
 
 
 
 
 
176
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
177
  cameraEntity.lookAt(modelEntity.getPosition());
178
  cameraEntity.addComponent('script');
 
 
179
  cameraEntity.script.create('orbitCamera', {
180
  attributes: {
181
  focusEntity: modelEntity,
@@ -195,12 +203,13 @@ export async function initializeViewer(config, instanceId) {
195
  app.root.addChild(cameraEntity);
196
 
197
  app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
 
198
  app.once('update', () => resetViewerCamera());
199
 
200
- // --- Tooltips (optional) ---
201
  try {
202
  if (config.tooltips_url) {
203
- import('./tooltips.js').then(tooltipsModule => {
204
  tooltipsModule.initializeTooltips({
205
  app,
206
  cameraEntity,
@@ -217,7 +226,7 @@ export async function initializeViewer(config, instanceId) {
217
  });
218
  }
219
 
220
- // --- Reset camera helper ---
221
  export function resetViewerCamera() {
222
  try {
223
  if (!cameraEntity || !modelEntity || !app) return;
@@ -239,7 +248,7 @@ export function resetViewerCamera() {
239
 
240
  orbitCam.pivotPoint = modelPos.clone();
241
  orbitCam._targetDistance = dist;
242
- orbitCam._distance = dist;
243
 
244
  const rot = tempEnt.getRotation();
245
  const fwd = new pc.Vec3();
@@ -252,10 +261,10 @@ export function resetViewerCamera() {
252
  rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
253
  const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
254
 
255
- orbitCam._targetYaw = yaw;
256
- orbitCam._yaw = yaw;
257
  orbitCam._targetPitch = pitch;
258
- orbitCam._pitch = pitch;
259
  if (orbitCam._updatePosition) orbitCam._updatePosition();
260
 
261
  tempEnt.destroy();
 
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, canvasBg;
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
+ // Parse config fields
23
+ sogsUrl = config.sogs_json_url;
24
+ glbUrl = config.glb_url;
25
+ canvasBg = config.canvas_background || "#ffffff";
26
+ minZoom = parseFloat(config.minZoom || "1");
27
+ maxZoom = parseFloat(config.maxZoom || "20");
28
+ minAngle = parseFloat(config.minAngle || "-45");
29
+ maxAngle = parseFloat(config.maxAngle || "90");
30
+ minAzimuth= (config.minAzimuth !== undefined) ? parseFloat(config.minAzimuth) : -360;
31
+ maxAzimuth= (config.maxAzimuth !== undefined) ? parseFloat(config.maxAzimuth) : 360;
 
 
 
32
  minPivotY = parseFloat(config.minPivotY || "0");
33
+ minY = (config.minY !== undefined) ? parseFloat(config.minY) : 0;
34
+
35
+ modelX = (config.modelX !== undefined) ? parseFloat(config.modelX) : 0;
36
+ modelY = (config.modelY !== undefined) ? parseFloat(config.modelY) : 0;
37
+ modelZ = (config.modelZ !== undefined) ? parseFloat(config.modelZ) : 0;
38
+ modelScale = (config.modelScale !== undefined) ? parseFloat(config.modelScale) : 1;
39
+ modelRotationX = (config.modelRotationX !== undefined) ? parseFloat(config.modelRotationX) : 0;
40
+ modelRotationY = (config.modelRotationY !== undefined) ? parseFloat(config.modelRotationY) : 0;
41
+ modelRotationZ = (config.modelRotationZ !== undefined) ? parseFloat(config.modelRotationZ) : 0;
42
+
43
+ const cameraX = (config.cameraX !== undefined) ? parseFloat(config.cameraX) : 0;
44
+ const cameraY = (config.cameraY !== undefined) ? parseFloat(config.cameraY) : 2;
45
+ const cameraZ = (config.cameraZ !== undefined) ? parseFloat(config.cameraZ) : 5;
46
  const cameraXPhone = (config.cameraXPhone !== undefined) ? parseFloat(config.cameraXPhone) : cameraX;
47
  const cameraYPhone = (config.cameraYPhone !== undefined) ? parseFloat(config.cameraYPhone) : cameraY;
48
  const cameraZPhone = (config.cameraZPhone !== undefined) ? parseFloat(config.cameraZPhone) : (cameraZ * 1.5);
 
51
  chosenCameraY = isMobile ? cameraYPhone : cameraY;
52
  chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
53
 
54
+ const canvasId = 'canvas-' + instanceId;
55
+ const progressDialog = document.getElementById('progress-dialog-' + instanceId);
56
+ const progressIndicator= document.getElementById('progress-indicator-' + instanceId);
57
+ const viewerContainer = document.getElementById('viewer-container-' + instanceId);
58
 
 
59
  let oldCanvas = document.getElementById(canvasId);
60
  if (oldCanvas) oldCanvas.remove();
61
 
 
62
  const canvas = document.createElement('canvas');
63
  canvas.id = canvasId;
64
  canvas.className = 'ply-canvas';
 
66
  canvas.style.height = "100%";
67
  canvas.setAttribute('tabindex', '0');
68
  viewerContainer.insertBefore(canvas, progressDialog);
69
+
70
+ // Apply background color from config if present
71
+ canvas.style.background = canvasBg;
72
+
73
  canvas.style.touchAction = "none";
74
  canvas.style.webkitTouchCallout = "none";
75
  canvas.addEventListener('gesturestart', e => e.preventDefault());
 
77
  canvas.addEventListener('gestureend', e => e.preventDefault());
78
  canvas.addEventListener('dblclick', e => e.preventDefault());
79
  canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
80
+
81
+ // --- Mouse wheel suppression
82
+ canvas.addEventListener('wheel', (e) => {
83
+ e.preventDefault(); // Only block page scroll if mouse is over viewer
84
+ }, { passive: false });
85
 
86
  progressDialog.style.display = 'block';
87
 
88
+ if (!pc) {
89
+ pc = await import("https://cdn.jsdelivr.net/npm/playcanvas@latest/+esm"); // always use latest for SOGS/GSplat support
90
+ window.pc = pc;
 
 
 
 
 
 
 
 
 
 
91
  }
92
 
93
+ // Create app and graphics device
94
+ const device = await pc.createGraphicsDevice(canvas, {
95
+ deviceTypes: ["webgl2"],
96
+ glslangUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/glslang/glslang.js",
97
+ twgslUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/twgsl/twgsl.js",
98
+ antialias: false
99
+ });
100
+ device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
101
+
102
+ const opts = new pc.AppOptions();
103
+ opts.graphicsDevice = device;
104
+ opts.mouse = new pc.Mouse(canvas);
105
+ opts.touch = new pc.TouchDevice(canvas);
106
+ opts.componentSystems = [
107
  pc.RenderComponentSystem,
108
  pc.CameraComponentSystem,
109
  pc.LightComponentSystem,
110
  pc.ScriptComponentSystem,
111
+ // "GSplat" component supports SOGS and GSplat
112
+ pc.GSplatComponentSystem,
113
+ pc.CollisionComponentSystem,
114
+ pc.RigidbodyComponentSystem
115
  ];
116
+ opts.resourceHandlers = [
117
  pc.TextureHandler,
118
  pc.ContainerHandler,
119
  pc.ScriptHandler,
120
+ // GSplatHandler handles GSplat and SOGS
121
  pc.GSplatHandler
122
  ];
123
 
124
+ app = new pc.Application(canvas, opts);
 
 
 
125
  app.setCanvasFillMode(pc.FILLMODE_NONE);
126
  app.setCanvasResolution(pc.RESOLUTION_AUTO);
127
 
 
135
  window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
136
  app.on('destroy', () => resizeObserver.disconnect());
137
 
138
+ // Prepare asset loading (SOGS as 'gsplat')
139
  const assets = {
140
+ sogs: new pc.Asset('gsplat', 'gsplat', { url: sogsUrl }),
141
+ orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" }),
142
+ glb: new pc.Asset('glb', 'container', { url: glbUrl }),
143
  };
144
  for (const key in assets) app.assets.add(assets[key]);
145
 
 
148
  app.start();
149
  progressDialog.style.display = 'none';
150
 
151
+ // Add SOGS model
152
  modelEntity = new pc.Entity('model');
153
+ modelEntity.addComponent('gsplat', { asset: assets.sogs });
154
  modelEntity.setLocalPosition(modelX, modelY, modelZ);
155
  modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
156
  modelEntity.setLocalScale(modelScale, modelScale, modelScale);
157
  app.root.addChild(modelEntity);
158
 
159
+ // Add the GLB entity if provided
160
+ if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
161
+ const glbEntity = assets.glb.resource.instantiateRenderEntity();
162
+ app.root.addChild(glbEntity);
163
+ }
164
+
165
+ // CAMERA
166
  cameraEntity = new pc.Entity('camera');
167
+ // Support background color
168
+ let bg = [1, 1, 1, 1];
169
+ if (canvasBg && /^#?[0-9a-f]{6,8}$/i.test(canvasBg.replace("#", ""))) {
170
+ // convert hex color to [r,g,b,a]
171
+ let hex = canvasBg.replace("#", "");
172
+ if (hex.length === 6) hex += "FF";
173
+ const num = parseInt(hex, 16);
174
+ bg = [
175
+ ((num >> 24) & 0xFF) / 255,
176
+ ((num >> 16) & 0xFF) / 255,
177
+ ((num >> 8) & 0xFF) / 255,
178
+ (num & 0xFF) / 255
179
+ ];
180
+ }
181
+ cameraEntity.addComponent('camera', { clearColor: new pc.Color(bg[0], bg[1], bg[2], bg[3]) });
182
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
183
  cameraEntity.lookAt(modelEntity.getPosition());
184
  cameraEntity.addComponent('script');
185
+
186
+ // Pass all attributes to Orbit Camera script
187
  cameraEntity.script.create('orbitCamera', {
188
  attributes: {
189
  focusEntity: modelEntity,
 
203
  app.root.addChild(cameraEntity);
204
 
205
  app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
206
+
207
  app.once('update', () => resetViewerCamera());
208
 
209
+ // Tooltips support (optional)
210
  try {
211
  if (config.tooltips_url) {
212
+ import('https://mikafil-viewer-sgos.static.hf.space/tooltips.js').then(tooltipsModule => {
213
  tooltipsModule.initializeTooltips({
214
  app,
215
  cameraEntity,
 
226
  });
227
  }
228
 
229
+ // Resets the viewer camera (as in your PLY example)
230
  export function resetViewerCamera() {
231
  try {
232
  if (!cameraEntity || !modelEntity || !app) return;
 
248
 
249
  orbitCam.pivotPoint = modelPos.clone();
250
  orbitCam._targetDistance = dist;
251
+ orbitCam._distance = dist;
252
 
253
  const rot = tempEnt.getRotation();
254
  const fwd = new pc.Vec3();
 
261
  rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
262
  const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
263
 
264
+ orbitCam._targetYaw = yaw;
265
+ orbitCam._yaw = yaw;
266
  orbitCam._targetPitch = pitch;
267
+ orbitCam._pitch = pitch;
268
  if (orbitCam._updatePosition) orbitCam._updatePosition();
269
 
270
  tempEnt.destroy();