Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -23,18 +23,11 @@ function hexToRgbaArray(hex) {
|
|
| 23 |
(num & 0xFF) / 255
|
| 24 |
];
|
| 25 |
} catch (e) {
|
| 26 |
-
alert("hexToRgbaArray error: " + e);
|
| 27 |
return [1, 1, 1, 1];
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
export async function initializeViewer(config, instanceId) {
|
| 32 |
-
alert("initializeViewer: start");
|
| 33 |
-
|
| 34 |
-
if (viewerInitialized) {
|
| 35 |
-
alert("Already initialized!");
|
| 36 |
-
return;
|
| 37 |
-
}
|
| 38 |
|
| 39 |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
| 40 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
|
@@ -106,12 +99,10 @@ export async function initializeViewer(config, instanceId) {
|
|
| 106 |
|
| 107 |
if (progressDialog) progressDialog.style.display = 'block';
|
| 108 |
|
| 109 |
-
alert("Canvas created. Loading PlayCanvas...");
|
| 110 |
|
| 111 |
if (!pc) {
|
| 112 |
pc = await import("https://cdn.jsdelivr.net/npm/playcanvas@latest/+esm");
|
| 113 |
window.pc = pc;
|
| 114 |
-
alert("PlayCanvas imported.");
|
| 115 |
}
|
| 116 |
|
| 117 |
// Create app and graphics device
|
|
@@ -124,9 +115,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 124 |
antialias: false
|
| 125 |
});
|
| 126 |
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 127 |
-
alert("Graphics device created.");
|
| 128 |
} catch (e) {
|
| 129 |
-
alert("Graphics device error: " + e);
|
| 130 |
throw e;
|
| 131 |
}
|
| 132 |
|
|
@@ -154,14 +143,13 @@ export async function initializeViewer(config, instanceId) {
|
|
| 154 |
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 155 |
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 156 |
|
| 157 |
-
alert("App created.");
|
| 158 |
|
| 159 |
// Resizing
|
| 160 |
resizeObserver = new ResizeObserver(entries => {
|
| 161 |
entries.forEach(entry => {
|
| 162 |
try {
|
| 163 |
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
| 164 |
-
} catch (e) {
|
| 165 |
});
|
| 166 |
});
|
| 167 |
resizeObserver.observe(viewerContainer);
|
|
@@ -169,7 +157,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 169 |
window.addEventListener('resize', () => {
|
| 170 |
try {
|
| 171 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 172 |
-
} catch (e) {
|
| 173 |
});
|
| 174 |
app.on('destroy', () => resizeObserver.disconnect());
|
| 175 |
|
|
@@ -182,25 +170,23 @@ export async function initializeViewer(config, instanceId) {
|
|
| 182 |
for (const key in assets) app.assets.add(assets[key]);
|
| 183 |
|
| 184 |
// Individual asset error listeners
|
| 185 |
-
assets.sogs.on('error', (err) => {
|
| 186 |
-
assets.orbit.on('error', (err) => {
|
| 187 |
-
if (assets.glb) assets.glb.on('error', (err) => {
|
| 188 |
|
| 189 |
-
assets.sogs.ready(() => {
|
| 190 |
-
assets.orbit.ready(() => {
|
| 191 |
-
if (assets.glb) assets.glb.ready(() => {
|
| 192 |
|
| 193 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 194 |
|
| 195 |
-
loader.on('error', (err) => {
|
| 196 |
|
| 197 |
loader.load(() => {
|
| 198 |
-
alert("All assets loaded. Starting app...");
|
| 199 |
|
| 200 |
try {
|
| 201 |
app.start();
|
| 202 |
} catch (e) {
|
| 203 |
-
alert("app.start() error: " + e);
|
| 204 |
throw e;
|
| 205 |
}
|
| 206 |
|
|
@@ -214,9 +200,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 214 |
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 215 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 216 |
app.root.addChild(modelEntity);
|
| 217 |
-
alert("Model entity added!");
|
| 218 |
} catch (e) {
|
| 219 |
-
alert("Model entity error: " + e);
|
| 220 |
throw e;
|
| 221 |
}
|
| 222 |
|
|
@@ -225,11 +209,8 @@ export async function initializeViewer(config, instanceId) {
|
|
| 225 |
if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
|
| 226 |
const glbEntity = assets.glb.resource.instantiateRenderEntity();
|
| 227 |
app.root.addChild(glbEntity);
|
| 228 |
-
alert("GLB entity added.");
|
| 229 |
}
|
| 230 |
-
} catch (e) {
|
| 231 |
-
alert("GLB entity error: " + e);
|
| 232 |
-
}
|
| 233 |
|
| 234 |
// CAMERA
|
| 235 |
try {
|
|
@@ -263,24 +244,15 @@ export async function initializeViewer(config, instanceId) {
|
|
| 263 |
cameraEntity.script.create('orbitCameraInputTouch');
|
| 264 |
app.root.addChild(cameraEntity);
|
| 265 |
|
| 266 |
-
|
| 267 |
-
} catch (e) {
|
| 268 |
-
alert("Camera error: " + e);
|
| 269 |
-
}
|
| 270 |
|
| 271 |
try {
|
| 272 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 273 |
-
|
| 274 |
-
} catch (e) {
|
| 275 |
-
alert("resizeCanvas error: " + e);
|
| 276 |
-
}
|
| 277 |
|
| 278 |
try {
|
| 279 |
app.once('update', () => resetViewerCamera());
|
| 280 |
-
|
| 281 |
-
} catch (e) {
|
| 282 |
-
alert("resetViewerCamera schedule error: " + e);
|
| 283 |
-
}
|
| 284 |
|
| 285 |
// Tooltips support (optional)
|
| 286 |
try {
|
|
@@ -294,35 +266,26 @@ export async function initializeViewer(config, instanceId) {
|
|
| 294 |
defaultVisible: !!config.showTooltipsDefault,
|
| 295 |
moveDuration: config.tooltipMoveDuration || 0.6
|
| 296 |
});
|
| 297 |
-
|
| 298 |
-
}).catch(e => { alert("Tooltips module error: " + e); });
|
| 299 |
}
|
| 300 |
-
} catch (e) {
|
| 301 |
-
alert("Tooltips try-catch error: " + e);
|
| 302 |
-
}
|
| 303 |
|
| 304 |
viewerInitialized = true;
|
| 305 |
-
alert("Viewer initialized success!");
|
| 306 |
});
|
| 307 |
|
| 308 |
-
alert("Asset loader started.");
|
| 309 |
} catch (e) {
|
| 310 |
-
alert("Main try-catch error: " + e);
|
| 311 |
throw e;
|
| 312 |
}
|
| 313 |
}
|
| 314 |
|
| 315 |
// Resets the viewer camera (as in your PLY example)
|
| 316 |
export function resetViewerCamera() {
|
| 317 |
-
alert("resetViewerCamera called.");
|
| 318 |
try {
|
| 319 |
if (!cameraEntity || !modelEntity || !app) {
|
| 320 |
-
alert("resetViewerCamera: cameraEntity/modelEntity/app not ready");
|
| 321 |
return;
|
| 322 |
}
|
| 323 |
const orbitCam = cameraEntity.script.orbitCamera;
|
| 324 |
if (!orbitCam) {
|
| 325 |
-
alert("resetViewerCamera: orbitCam missing");
|
| 326 |
return;
|
| 327 |
}
|
| 328 |
|
|
@@ -361,8 +324,5 @@ export function resetViewerCamera() {
|
|
| 361 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 362 |
|
| 363 |
tempEnt.destroy();
|
| 364 |
-
|
| 365 |
-
} catch (e) {
|
| 366 |
-
alert("resetViewerCamera: error: " + e);
|
| 367 |
-
}
|
| 368 |
}
|
|
|
|
| 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);
|
|
|
|
| 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
|
|
|
|
| 115 |
antialias: false
|
| 116 |
});
|
| 117 |
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
|
|
|
| 118 |
} catch (e) {
|
|
|
|
| 119 |
throw e;
|
| 120 |
}
|
| 121 |
|
|
|
|
| 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);
|
|
|
|
| 157 |
window.addEventListener('resize', () => {
|
| 158 |
try {
|
| 159 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 160 |
+
} catch (e) { }
|
| 161 |
});
|
| 162 |
app.on('destroy', () => resizeObserver.disconnect());
|
| 163 |
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
|
|
|
| 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 {
|
|
|
|
| 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 {
|
|
|
|
| 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 |
|
|
|
|
| 324 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 325 |
|
| 326 |
tempEnt.destroy();
|
| 327 |
+
} catch (e) { }
|
|
|
|
|
|
|
|
|
|
| 328 |
}
|