MikaFil commited on
Commit
385e9d2
·
verified ·
1 Parent(s): e0e7fb0

Update viewer_2.js

Browse files
Files changed (1) hide show
  1. viewer_2.js +43 -35
viewer_2.js CHANGED
@@ -1,4 +1,4 @@
1
- /*(function(){
2
  let logDiv = document.createElement('div');
3
  logDiv.style.position = 'fixed';
4
  logDiv.style.bottom = '0';
@@ -35,9 +35,9 @@
35
  window.onerror = function(msg, src, lineno, colno, error){
36
  printLog('[ERR] ' + msg + ' at ' + src + ':' + lineno, '#f55');
37
  };
38
- })();*/
39
 
40
- // viewer.js
41
  // ==============================
42
 
43
  let pc;
@@ -65,6 +65,9 @@ function traverse(entity, callback) {
65
  export async function initializeViewer(config, instanceId) {
66
  if (viewerInitialized) return;
67
 
 
 
 
68
  glbUrl = config.glb_url;
69
  minZoom = 0.2;
70
  maxZoom = 2;
@@ -89,7 +92,6 @@ export async function initializeViewer(config, instanceId) {
89
 
90
  const canvasId = 'canvas-' + instanceId;
91
  const progressDialog = document.getElementById('progress-dialog-' + instanceId);
92
- const progressIndicator = document.getElementById('progress-indicator-' + instanceId);
93
  const viewerContainer = document.getElementById('viewer-container-' + instanceId);
94
 
95
  let oldCanvas = document.getElementById(canvasId);
@@ -121,20 +123,26 @@ export async function initializeViewer(config, instanceId) {
121
  if (!pc) {
122
  pc = await import("https://esm.run/playcanvas");
123
  window.pc = pc;
 
124
  }
125
 
126
- // Create app first
 
 
 
 
127
  const device = await pc.createGraphicsDevice(canvas, {
128
- deviceTypes: ["webgl2", "webgl1"],
129
  glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
130
  twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
131
  antialias: false
132
  });
133
  device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
 
134
 
 
135
  const opts = new pc.AppOptions();
136
  opts.graphicsDevice = device;
137
- // Attach input only to canvas
138
  opts.mouse = new pc.Mouse(canvas);
139
  opts.touch = new pc.TouchDevice(canvas);
140
  opts.componentSystems = [
@@ -153,12 +161,15 @@ export async function initializeViewer(config, instanceId) {
153
  pc.GSplatHandler
154
  ];
155
 
156
-
157
- ///////// APP comme dans exemple ///////
158
- ////////////////////////////////////////
159
- app = new pc.Application(canvas, opts);
160
- //app = new pc.AppBase(canvas);
161
- //app.init(opts);
 
 
 
162
 
163
  app.setCanvasFillMode(pc.FILLMODE_NONE);
164
  app.setCanvasResolution(pc.RESOLUTION_AUTO);
@@ -178,9 +189,9 @@ export async function initializeViewer(config, instanceId) {
178
  orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-glb.static.hf.space/orbit-camera.js" }),
179
  glb: new pc.Asset('glb', 'container', { url: glbUrl }),
180
  hdr: new pc.Asset(
181
- 'hdr-env-atlas', 'texture',
182
- {url: 'https://playcanvas.vercel.app/static/assets/cubemaps/helipad-env-atlas.png'},
183
- { type: pc.TEXTURETYPE_RGBP, mipmaps: false }
184
  ),
185
  ao_map: new pc.Asset('color', 'texture', { url: 'https://huggingface.co/datasets/MikaFil/3D_models/resolve/main/retop_2/emit_map_2k.png' }),
186
  op_map: new pc.Asset('color', 'texture', { url: 'https://huggingface.co/datasets/MikaFil/3D_models/resolve/main/retop_2/op_map_2k.png' }),
@@ -189,12 +200,14 @@ export async function initializeViewer(config, instanceId) {
189
 
190
  for (const key in assets) app.assets.add(assets[key]);
191
 
192
-
193
  const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
194
  loader.load(() => {
195
  app.start();
196
  progressDialog.style.display = 'none';
 
197
 
 
198
  app.scene.envAtlas = assets.hdr.resource;
199
  app.scene.skyboxRotation = new pc.Quat().setFromEulerAngles(0, -90, 0);
200
  app.scene.skyboxIntensity = 4;
@@ -205,15 +218,12 @@ export async function initializeViewer(config, instanceId) {
205
  app.scene.layers.remove(depthLayer);
206
  app.scene.layers.insertOpaque(depthLayer, 2);
207
 
208
-
209
  // --- Add the GLB entity ---
210
  glbEntity = assets.glb.resource.instantiateRenderEntity();
211
  app.root.addChild(glbEntity);
212
 
213
  // 2. Materials for main model & tube
214
  const ao = assets.ao_map.resource;
215
- //const op = assets.op_map.resource;
216
- //const thickness = assets.thickness_map.resource;
217
 
218
  // mat transparent
219
  matTransparent = new pc.StandardMaterial();
@@ -225,11 +235,11 @@ export async function initializeViewer(config, instanceId) {
225
  matTransparent.useMetalness = true;
226
  matTransparent.useDynamicRefraction = true;
227
  matTransparent.depthWrite = true;
228
- matTransparent.refraction= 0.8;
229
  matTransparent.refractionIndex = 1;
230
  matTransparent.thickness = 0.02;
231
- //matTransparent.thicknessMap = thickness;
232
- //matTransparent.opacityMap = op;
233
  //matTransparent.opacityMapChannel = "r";
234
  matTransparent.opacity = 0.97;
235
  matTransparent.emissive = new pc.Color(1, 1, 1);
@@ -245,21 +255,20 @@ export async function initializeViewer(config, instanceId) {
245
  }
246
  });
247
 
248
-
249
  glbEntity.setPosition(modelX, modelY, modelZ);
250
  glbEntity.setLocalScale(modelScale, modelScale, modelScale);
251
  glbEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
252
 
253
  cameraEntity = new pc.Entity('camera');
254
  // PURE WHITE BACKGROUND
255
- cameraEntity.addComponent('camera'); // White RGBA
256
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
257
  cameraEntity.lookAt(glbEntity.getPosition());
258
  cameraEntity.addComponent('script');
259
 
260
  cameraEntity.camera.requestSceneColorMap(true);
261
 
262
- // === MAIN FIX: Pass ALL relevant attributes including minY ===
263
  cameraEntity.script.create('orbitCamera', {
264
  attributes: {
265
  focusEntity: glbEntity,
@@ -303,25 +312,24 @@ export async function initializeViewer(config, instanceId) {
303
  }
304
 
305
  viewerInitialized = true;
 
306
  });
307
 
308
  }
309
 
310
  export function resetViewerCamera() {
311
- console.log(glbEntity.getPosition());
312
  try {
313
  if (!cameraEntity || !glbEntity || !app) return;
314
  const orbitCam = cameraEntity.script.orbitCamera;
315
  if (!orbitCam) return;
316
 
317
  const modelPos = glbEntity.getPosition();
318
- console.log(modelPos);
319
  const tempEnt = new pc.Entity();
320
  tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
321
  tempEnt.lookAt(modelPos);
322
 
323
  const dist = new pc.Vec3().sub2(
324
- new pc.Vec3(chosenCamearaX, chosenCameraY, chosenCameraZ),
325
  modelPos
326
  ).length();
327
 
@@ -330,7 +338,7 @@ console.log(glbEntity.getPosition());
330
 
331
  orbitCam.pivotPoint = modelPos.clone();
332
  orbitCam._targetDistance = dist;
333
- orbitCam._distance = dist;
334
 
335
  const rot = tempEnt.getRotation();
336
  const fwd = new pc.Vec3();
@@ -343,15 +351,15 @@ console.log(glbEntity.getPosition());
343
  rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
344
  const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
345
 
346
- orbitCam._targetYaw = yaw;
347
- orbitCam._yaw = yaw;
348
  orbitCam._targetPitch = pitch;
349
- orbitCam._pitch = pitch;
350
  if (orbitCam._updatePosition) orbitCam._updatePosition();
351
 
352
  tempEnt.destroy();
 
353
  } catch (e) {
354
- // Silent fail
355
  }
356
  }
357
-
 
1
+ (function(){
2
  let logDiv = document.createElement('div');
3
  logDiv.style.position = 'fixed';
4
  logDiv.style.bottom = '0';
 
35
  window.onerror = function(msg, src, lineno, colno, error){
36
  printLog('[ERR] ' + msg + ' at ' + src + ':' + lineno, '#f55');
37
  };
38
+ })();
39
 
40
+ // viewer_2.js
41
  // ==============================
42
 
43
  let pc;
 
65
  export async function initializeViewer(config, instanceId) {
66
  if (viewerInitialized) return;
67
 
68
+ console.log("Initializing viewer for instance:", instanceId);
69
+ console.log(config);
70
+
71
  glbUrl = config.glb_url;
72
  minZoom = 0.2;
73
  maxZoom = 2;
 
92
 
93
  const canvasId = 'canvas-' + instanceId;
94
  const progressDialog = document.getElementById('progress-dialog-' + instanceId);
 
95
  const viewerContainer = document.getElementById('viewer-container-' + instanceId);
96
 
97
  let oldCanvas = document.getElementById(canvasId);
 
123
  if (!pc) {
124
  pc = await import("https://esm.run/playcanvas");
125
  window.pc = pc;
126
+ console.log("PlayCanvas loaded:", pc.version || "(no version field)");
127
  }
128
 
129
+ // Device selection for iOS reliability
130
+ const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
131
+ console.log("iOS detected:", isIOS);
132
+
133
+ // Create PlayCanvas graphics device (iOS: webgl1 only!)
134
  const device = await pc.createGraphicsDevice(canvas, {
135
+ deviceTypes: isIOS ? ["webgl1"] : ["webgl2", "webgl1"],
136
  glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
137
  twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
138
  antialias: false
139
  });
140
  device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
141
+ console.log("Graphics device created:", device);
142
 
143
+ // AppOptions setup
144
  const opts = new pc.AppOptions();
145
  opts.graphicsDevice = device;
 
146
  opts.mouse = new pc.Mouse(canvas);
147
  opts.touch = new pc.TouchDevice(canvas);
148
  opts.componentSystems = [
 
161
  pc.GSplatHandler
162
  ];
163
 
164
+ // Use Application (NOT AppBase)
165
+ try {
166
+ app = new pc.Application(canvas, opts);
167
+ console.log("App initialized:", app);
168
+ } catch (e) {
169
+ console.error("Failed to initialize pc.Application:", e);
170
+ progressDialog.style.display = 'none';
171
+ return;
172
+ }
173
 
174
  app.setCanvasFillMode(pc.FILLMODE_NONE);
175
  app.setCanvasResolution(pc.RESOLUTION_AUTO);
 
189
  orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-glb.static.hf.space/orbit-camera.js" }),
190
  glb: new pc.Asset('glb', 'container', { url: glbUrl }),
191
  hdr: new pc.Asset(
192
+ 'hdr-env-atlas', 'texture',
193
+ { url: 'https://playcanvas.vercel.app/static/assets/cubemaps/helipad-env-atlas.png' },
194
+ { type: pc.TEXTURETYPE_RGBP, mipmaps: false }
195
  ),
196
  ao_map: new pc.Asset('color', 'texture', { url: 'https://huggingface.co/datasets/MikaFil/3D_models/resolve/main/retop_2/emit_map_2k.png' }),
197
  op_map: new pc.Asset('color', 'texture', { url: 'https://huggingface.co/datasets/MikaFil/3D_models/resolve/main/retop_2/op_map_2k.png' }),
 
200
 
201
  for (const key in assets) app.assets.add(assets[key]);
202
 
203
+ // Load assets before starting!
204
  const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
205
  loader.load(() => {
206
  app.start();
207
  progressDialog.style.display = 'none';
208
+ console.log("App started, assets loaded.");
209
 
210
+ // --- Scene setup ---
211
  app.scene.envAtlas = assets.hdr.resource;
212
  app.scene.skyboxRotation = new pc.Quat().setFromEulerAngles(0, -90, 0);
213
  app.scene.skyboxIntensity = 4;
 
218
  app.scene.layers.remove(depthLayer);
219
  app.scene.layers.insertOpaque(depthLayer, 2);
220
 
 
221
  // --- Add the GLB entity ---
222
  glbEntity = assets.glb.resource.instantiateRenderEntity();
223
  app.root.addChild(glbEntity);
224
 
225
  // 2. Materials for main model & tube
226
  const ao = assets.ao_map.resource;
 
 
227
 
228
  // mat transparent
229
  matTransparent = new pc.StandardMaterial();
 
235
  matTransparent.useMetalness = true;
236
  matTransparent.useDynamicRefraction = true;
237
  matTransparent.depthWrite = true;
238
+ matTransparent.refraction = 0.8;
239
  matTransparent.refractionIndex = 1;
240
  matTransparent.thickness = 0.02;
241
+ //matTransparent.thicknessMap = assets.thickness_map.resource;
242
+ //matTransparent.opacityMap = assets.op_map.resource;
243
  //matTransparent.opacityMapChannel = "r";
244
  matTransparent.opacity = 0.97;
245
  matTransparent.emissive = new pc.Color(1, 1, 1);
 
255
  }
256
  });
257
 
 
258
  glbEntity.setPosition(modelX, modelY, modelZ);
259
  glbEntity.setLocalScale(modelScale, modelScale, modelScale);
260
  glbEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
261
 
262
  cameraEntity = new pc.Entity('camera');
263
  // PURE WHITE BACKGROUND
264
+ cameraEntity.addComponent('camera');
265
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
266
  cameraEntity.lookAt(glbEntity.getPosition());
267
  cameraEntity.addComponent('script');
268
 
269
  cameraEntity.camera.requestSceneColorMap(true);
270
 
271
+ // === Pass ALL relevant attributes including minY ===
272
  cameraEntity.script.create('orbitCamera', {
273
  attributes: {
274
  focusEntity: glbEntity,
 
312
  }
313
 
314
  viewerInitialized = true;
315
+ console.log("Viewer initialized and ready.");
316
  });
317
 
318
  }
319
 
320
  export function resetViewerCamera() {
 
321
  try {
322
  if (!cameraEntity || !glbEntity || !app) return;
323
  const orbitCam = cameraEntity.script.orbitCamera;
324
  if (!orbitCam) return;
325
 
326
  const modelPos = glbEntity.getPosition();
 
327
  const tempEnt = new pc.Entity();
328
  tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
329
  tempEnt.lookAt(modelPos);
330
 
331
  const dist = new pc.Vec3().sub2(
332
+ new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ),
333
  modelPos
334
  ).length();
335
 
 
338
 
339
  orbitCam.pivotPoint = modelPos.clone();
340
  orbitCam._targetDistance = dist;
341
+ orbitCam._distance = dist;
342
 
343
  const rot = tempEnt.getRotation();
344
  const fwd = new pc.Vec3();
 
351
  rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
352
  const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
353
 
354
+ orbitCam._targetYaw = yaw;
355
+ orbitCam._yaw = yaw;
356
  orbitCam._targetPitch = pitch;
357
+ orbitCam._pitch = pitch;
358
  if (orbitCam._updatePosition) orbitCam._updatePosition();
359
 
360
  tempEnt.destroy();
361
+ console.log("Camera reset");
362
  } catch (e) {
363
+ console.error("Error in resetViewerCamera:", e);
364
  }
365
  }