MikaFil commited on
Commit
bee6833
·
verified ·
1 Parent(s): 8c64c02

Update viewer.js

Browse files
Files changed (1) hide show
  1. viewer.js +57 -13
viewer.js CHANGED
@@ -1,7 +1,6 @@
1
  // viewer.js
2
  // ==============================
3
- // viewer.js
4
- // ==============================
5
 
6
  let pc; // will hold the PlayCanvas module once imported
7
  export let app = null;
@@ -15,6 +14,15 @@ let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, min
15
  let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
16
  let plyUrl, glbUrl;
17
 
 
 
 
 
 
 
 
 
 
18
  /**
19
  * initializeViewer(config, instanceId)
20
  */
@@ -23,6 +31,15 @@ export async function initializeViewer(config, instanceId) {
23
 
24
  const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
25
  const isMobile = isIOS || /Android/i.test(navigator.userAgent);
 
 
 
 
 
 
 
 
 
26
 
27
  // 1. Read config
28
  plyUrl = config.ply_url;
@@ -95,14 +112,43 @@ export async function initializeViewer(config, instanceId) {
95
 
96
  try {
97
  // 6. Setup device & app
98
- const device = await pc.createGraphicsDevice(canvas, {
99
- deviceTypes: ["webgl2"],
100
- glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
101
- twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
102
- antialias: false
103
- });
104
- device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  const opts = new pc.AppOptions();
107
  opts.graphicsDevice = device;
108
  opts.mouse = new pc.Mouse(canvas);
@@ -126,8 +172,6 @@ export async function initializeViewer(config, instanceId) {
126
  app = new pc.Application(canvas, opts);
127
  app.setCanvasFillMode(pc.FILLMODE_NONE);
128
  app.setCanvasResolution(pc.RESOLUTION_AUTO);
129
- //app.scene.exposure = 0.5;
130
- //app.scene.toneMapping = pc.TONEMAP_ACES;
131
 
132
  // Attach ResizeObserver to keep canvas in sync with container size
133
  resizeObserver = new ResizeObserver(entries => {
@@ -219,7 +263,6 @@ export async function initializeViewer(config, instanceId) {
219
  clearColor: config.canvas_background
220
  ? parseInt(config.canvas_background.substr(1, 2), 16) / 255
221
  : 0,
222
- //toneMapping: pc.TONEMAP_ACES
223
  });
224
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
225
  cameraEntity.lookAt(modelEntity.getPosition());
@@ -295,6 +338,7 @@ export async function initializeViewer(config, instanceId) {
295
  }
296
  }
297
 
 
298
  export function resetViewerCamera() {
299
  try {
300
  if (!cameraEntity || !modelEntity || !app) return;
@@ -357,4 +401,4 @@ export function cleanupViewer() {
357
  resizeObserver.disconnect();
358
  resizeObserver = null;
359
  }
360
- }
 
1
  // viewer.js
2
  // ==============================
3
+ // WhatsApp iOS: Fallback to webgl1 if webgl2 fails
 
4
 
5
  let pc; // will hold the PlayCanvas module once imported
6
  export let app = null;
 
14
  let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
15
  let plyUrl, glbUrl;
16
 
17
+ function isWhatsappIOS() {
18
+ return /iphone|ipod|ipad/i.test(navigator.userAgent) &&
19
+ /WhatsApp/i.test(navigator.userAgent);
20
+ }
21
+ function isInstagramIOS() {
22
+ return /iphone|ipod|ipad/i.test(navigator.userAgent) &&
23
+ /Instagram/i.test(navigator.userAgent);
24
+ }
25
+
26
  /**
27
  * initializeViewer(config, instanceId)
28
  */
 
31
 
32
  const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
33
  const isMobile = isIOS || /Android/i.test(navigator.userAgent);
34
+ const isWA = isWhatsappIOS();
35
+ const isIG = isInstagramIOS();
36
+
37
+ if (isWA) {
38
+ alert("WhatsApp iOS detected: trying WebGL fallback.");
39
+ }
40
+ if (isIG) {
41
+ // Optional: alert("Instagram iOS detected.");
42
+ }
43
 
44
  // 1. Read config
45
  plyUrl = config.ply_url;
 
112
 
113
  try {
114
  // 6. Setup device & app
115
+ let device = null;
116
+ let contextSuccess = false;
117
+ let contextType = null;
 
 
 
 
118
 
119
+ // Try webgl2, then webgl1
120
+ try {
121
+ device = await pc.createGraphicsDevice(canvas, {
122
+ deviceTypes: ["webgl2"],
123
+ glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
124
+ twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
125
+ antialias: false
126
+ });
127
+ device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
128
+ contextType = "webgl2";
129
+ contextSuccess = true;
130
+ alert("WebGL2 context creation successful.");
131
+ } catch (err) {
132
+ // Try webgl1 if webgl2 fails
133
+ try {
134
+ device = await pc.createGraphicsDevice(canvas, {
135
+ deviceTypes: ["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
+ contextType = "webgl1";
142
+ contextSuccess = true;
143
+ alert("WebGL1 context creation successful (WebGL2 failed).");
144
+ } catch (err2) {
145
+ alert("WebGL context creation FAILED.\nWhatsApp iOS browser does not support WebGL. Try opening this link in Safari or another browser.");
146
+ progressDialog.innerHTML = `<p style="color: red">WebGL is not supported in this browser. Please try opening in Safari or Chrome.</p>`;
147
+ throw err2;
148
+ }
149
+ }
150
+
151
+ // ... continue as before ...
152
  const opts = new pc.AppOptions();
153
  opts.graphicsDevice = device;
154
  opts.mouse = new pc.Mouse(canvas);
 
172
  app = new pc.Application(canvas, opts);
173
  app.setCanvasFillMode(pc.FILLMODE_NONE);
174
  app.setCanvasResolution(pc.RESOLUTION_AUTO);
 
 
175
 
176
  // Attach ResizeObserver to keep canvas in sync with container size
177
  resizeObserver = new ResizeObserver(entries => {
 
263
  clearColor: config.canvas_background
264
  ? parseInt(config.canvas_background.substr(1, 2), 16) / 255
265
  : 0,
 
266
  });
267
  cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
268
  cameraEntity.lookAt(modelEntity.getPosition());
 
338
  }
339
  }
340
 
341
+ // (resetViewerCamera and cleanupViewer unchanged)
342
  export function resetViewerCamera() {
343
  try {
344
  if (!cameraEntity || !modelEntity || !app) return;
 
401
  resizeObserver.disconnect();
402
  resizeObserver = null;
403
  }
404
+ }