Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
// ==============================
|
| 2 |
// viewer.js
|
| 3 |
// ==============================
|
|
@@ -20,6 +21,8 @@ let plyUrl, glbUrl;
|
|
| 20 |
export async function initializeViewer(config, instanceId) {
|
| 21 |
if (viewerInitialized) return;
|
| 22 |
|
|
|
|
|
|
|
| 23 |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
| 24 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 25 |
|
|
@@ -102,17 +105,17 @@ export async function initializeViewer(config, instanceId) {
|
|
| 102 |
antialias: false
|
| 103 |
};
|
| 104 |
try {
|
| 105 |
-
|
| 106 |
...baseOpts,
|
| 107 |
deviceTypes: preferWebgl2 ? ["webgl2"] : ["webgl1"]
|
| 108 |
});
|
|
|
|
|
|
|
| 109 |
} catch (err) {
|
| 110 |
if (preferWebgl2) {
|
| 111 |
console.warn("[viewer] WebGL2 unavailable, retrying with WebGL1 …", err);
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
deviceTypes: ["webgl1"]
|
| 115 |
-
});
|
| 116 |
}
|
| 117 |
throw err;
|
| 118 |
}
|
|
@@ -121,6 +124,8 @@ export async function initializeViewer(config, instanceId) {
|
|
| 121 |
const device = await getGraphicsDevice(true); // first try WebGL2
|
| 122 |
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 123 |
|
|
|
|
|
|
|
| 124 |
const opts = new pc.AppOptions();
|
| 125 |
opts.graphicsDevice = device;
|
| 126 |
opts.mouse = new pc.Mouse(canvas);
|
|
@@ -169,16 +174,21 @@ export async function initializeViewer(config, instanceId) {
|
|
| 169 |
orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
|
| 170 |
galerie: new pc.Asset('galerie', 'container', { url: glbUrl }),
|
| 171 |
hdr: new pc.Asset('hdr', 'texture', {
|
| 172 |
-
url: https://huggingface.co/datasets/bilca/ply_files/resolve/main/galeries/blanc.png
|
| 173 |
}, { type: pc.TEXTURETYPE_RGBP, mipmaps: false })
|
| 174 |
};
|
| 175 |
|
| 176 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 177 |
let lastProg = 0;
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
assets.model.on('error', err => {
|
|
|
|
| 180 |
console.error("Error loading PLY file:", err);
|
| 181 |
-
progressDialog.innerHTML =
|
| 182 |
});
|
| 183 |
|
| 184 |
const progCheck = setInterval(() => {
|
|
@@ -192,7 +202,9 @@ export async function initializeViewer(config, instanceId) {
|
|
| 192 |
}
|
| 193 |
}, 100);
|
| 194 |
|
|
|
|
| 195 |
loader.load(async () => {
|
|
|
|
| 196 |
app.start();
|
| 197 |
app.scene.envAtlas = assets.hdr.resource;
|
| 198 |
|
|
@@ -269,6 +281,8 @@ export async function initializeViewer(config, instanceId) {
|
|
| 269 |
});
|
| 270 |
app.root.addChild(cameraEntity);
|
| 271 |
|
|
|
|
|
|
|
| 272 |
// 11. Misc per-frame guard & resize --------------------------------------
|
| 273 |
app.once('update', () => resetViewerCamera());
|
| 274 |
app.on('update', dt => {
|
|
@@ -299,11 +313,13 @@ export async function initializeViewer(config, instanceId) {
|
|
| 299 |
|
| 300 |
progressDialog.style.display = 'none';
|
| 301 |
viewerInitialized = true;
|
|
|
|
| 302 |
});
|
| 303 |
|
| 304 |
} catch (error) {
|
|
|
|
| 305 |
console.error("Error initializing PlayCanvas viewer:", error);
|
| 306 |
-
progressDialog.innerHTML =
|
| 307 |
}
|
| 308 |
}
|
| 309 |
|
|
@@ -371,4 +387,4 @@ export function cleanupViewer() {
|
|
| 371 |
resizeObserver.disconnect();
|
| 372 |
resizeObserver = null;
|
| 373 |
}
|
| 374 |
-
}
|
|
|
|
| 1 |
+
// viewer.js
|
| 2 |
// ==============================
|
| 3 |
// viewer.js
|
| 4 |
// ==============================
|
|
|
|
| 21 |
export async function initializeViewer(config, instanceId) {
|
| 22 |
if (viewerInitialized) return;
|
| 23 |
|
| 24 |
+
alert("Viewer init starting...");
|
| 25 |
+
|
| 26 |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
| 27 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 28 |
|
|
|
|
| 105 |
antialias: false
|
| 106 |
};
|
| 107 |
try {
|
| 108 |
+
const dev = await pc.createGraphicsDevice(canvas, {
|
| 109 |
...baseOpts,
|
| 110 |
deviceTypes: preferWebgl2 ? ["webgl2"] : ["webgl1"]
|
| 111 |
});
|
| 112 |
+
alert(preferWebgl2 ? "WebGL2 context OK" : "WebGL1 context OK");
|
| 113 |
+
return dev;
|
| 114 |
} catch (err) {
|
| 115 |
if (preferWebgl2) {
|
| 116 |
console.warn("[viewer] WebGL2 unavailable, retrying with WebGL1 …", err);
|
| 117 |
+
alert("WebGL2 unavailable on this device; falling back to WebGL1");
|
| 118 |
+
return getGraphicsDevice(false);
|
|
|
|
|
|
|
| 119 |
}
|
| 120 |
throw err;
|
| 121 |
}
|
|
|
|
| 124 |
const device = await getGraphicsDevice(true); // first try WebGL2
|
| 125 |
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 126 |
|
| 127 |
+
alert("Graphics device initialized; creating App…");
|
| 128 |
+
|
| 129 |
const opts = new pc.AppOptions();
|
| 130 |
opts.graphicsDevice = device;
|
| 131 |
opts.mouse = new pc.Mouse(canvas);
|
|
|
|
| 174 |
orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
|
| 175 |
galerie: new pc.Asset('galerie', 'container', { url: glbUrl }),
|
| 176 |
hdr: new pc.Asset('hdr', 'texture', {
|
| 177 |
+
url: `https://huggingface.co/datasets/bilca/ply_files/resolve/main/galeries/blanc.png`
|
| 178 |
}, { type: pc.TEXTURETYPE_RGBP, mipmaps: false })
|
| 179 |
};
|
| 180 |
|
| 181 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 182 |
let lastProg = 0;
|
| 183 |
+
|
| 184 |
+
assets.model.on('load', () => {
|
| 185 |
+
alert("PLY model asset loaded successfully");
|
| 186 |
+
progressDialog.style.display = 'none';
|
| 187 |
+
});
|
| 188 |
assets.model.on('error', err => {
|
| 189 |
+
alert("Error loading PLY file: " + err);
|
| 190 |
console.error("Error loading PLY file:", err);
|
| 191 |
+
progressDialog.innerHTML = `<p style="color: red">Error loading model: ${err}</p>`;
|
| 192 |
});
|
| 193 |
|
| 194 |
const progCheck = setInterval(() => {
|
|
|
|
| 202 |
}
|
| 203 |
}, 100);
|
| 204 |
|
| 205 |
+
alert("Starting asset loader…");
|
| 206 |
loader.load(async () => {
|
| 207 |
+
alert("All assets loaded; starting app…");
|
| 208 |
app.start();
|
| 209 |
app.scene.envAtlas = assets.hdr.resource;
|
| 210 |
|
|
|
|
| 281 |
});
|
| 282 |
app.root.addChild(cameraEntity);
|
| 283 |
|
| 284 |
+
alert("Camera and controls set up; rendering should now appear.");
|
| 285 |
+
|
| 286 |
// 11. Misc per-frame guard & resize --------------------------------------
|
| 287 |
app.once('update', () => resetViewerCamera());
|
| 288 |
app.on('update', dt => {
|
|
|
|
| 313 |
|
| 314 |
progressDialog.style.display = 'none';
|
| 315 |
viewerInitialized = true;
|
| 316 |
+
alert("Viewer initialization complete.");
|
| 317 |
});
|
| 318 |
|
| 319 |
} catch (error) {
|
| 320 |
+
alert("Error initializing PlayCanvas viewer: " + error);
|
| 321 |
console.error("Error initializing PlayCanvas viewer:", error);
|
| 322 |
+
progressDialog.innerHTML = `<p style="color: red">Error loading viewer: ${error.message}</p>`;
|
| 323 |
}
|
| 324 |
}
|
| 325 |
|
|
|
|
| 387 |
resizeObserver.disconnect();
|
| 388 |
resizeObserver = null;
|
| 389 |
}
|
| 390 |
+
}
|