Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -10,7 +10,6 @@ let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, min
|
|
| 10 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 11 |
let sogsUrl, glbUrl, canvasBg;
|
| 12 |
|
| 13 |
-
// ---- Utility: hex color to RGBA ----
|
| 14 |
function hexToRgbaArray(hex) {
|
| 15 |
try {
|
| 16 |
hex = hex.replace("#", "");
|
|
@@ -24,23 +23,24 @@ function hexToRgbaArray(hex) {
|
|
| 24 |
(num & 0xFF) / 255
|
| 25 |
];
|
| 26 |
} catch (e) {
|
| 27 |
-
|
| 28 |
return [1, 1, 1, 1];
|
| 29 |
}
|
| 30 |
}
|
| 31 |
|
| 32 |
export async function initializeViewer(config, instanceId) {
|
|
|
|
|
|
|
| 33 |
if (viewerInitialized) {
|
| 34 |
-
|
| 35 |
return;
|
| 36 |
}
|
| 37 |
|
| 38 |
-
// ---- Platform Detection ----
|
| 39 |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
| 40 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 41 |
|
| 42 |
try {
|
| 43 |
-
//
|
| 44 |
sogsUrl = config.sogs_json_url;
|
| 45 |
glbUrl = config.glb_url;
|
| 46 |
canvasBg = config.canvas_background || "#ffffff";
|
|
@@ -77,59 +77,63 @@ export async function initializeViewer(config, instanceId) {
|
|
| 77 |
const progressIndicator= document.getElementById('progress-indicator-' + instanceId);
|
| 78 |
const viewerContainer = document.getElementById('viewer-container-' + instanceId);
|
| 79 |
|
| 80 |
-
// Remove previous canvas if present
|
| 81 |
let oldCanvas = document.getElementById(canvasId);
|
| 82 |
if (oldCanvas) oldCanvas.remove();
|
| 83 |
|
| 84 |
-
// ---- Create and Insert Canvas ----
|
| 85 |
const canvas = document.createElement('canvas');
|
| 86 |
canvas.id = canvasId;
|
| 87 |
canvas.className = 'ply-canvas';
|
| 88 |
-
|
| 89 |
-
canvas.
|
| 90 |
canvas.setAttribute('tabindex', '0');
|
|
|
|
|
|
|
|
|
|
| 91 |
canvas.style.background = canvasBg;
|
|
|
|
| 92 |
canvas.style.touchAction = "none";
|
| 93 |
canvas.style.webkitTouchCallout = "none";
|
| 94 |
-
|
| 95 |
-
canvas.addEventListener('
|
| 96 |
-
canvas.addEventListener('
|
| 97 |
-
canvas.addEventListener('
|
| 98 |
-
canvas.addEventListener('dblclick', e => e.preventDefault(), { passive: false });
|
| 99 |
canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
if (progressDialog) progressDialog.style.display = 'block';
|
| 105 |
|
| 106 |
-
|
|
|
|
| 107 |
if (!pc) {
|
| 108 |
pc = await import("https://cdn.jsdelivr.net/npm/playcanvas@latest/+esm");
|
| 109 |
window.pc = pc;
|
|
|
|
| 110 |
}
|
| 111 |
|
| 112 |
-
//
|
| 113 |
let device;
|
| 114 |
try {
|
| 115 |
-
// Robust: force pixel ratio to 1 on mobile to avoid Safari VRAM crash!
|
| 116 |
device = await pc.createGraphicsDevice(canvas, {
|
| 117 |
-
deviceTypes: ["webgl2", "webgl1"],
|
| 118 |
glslangUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/glslang/glslang.js",
|
| 119 |
twgslUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/twgsl/twgsl.js",
|
| 120 |
-
antialias: false
|
| 121 |
});
|
| 122 |
-
device.maxPixelRatio =
|
|
|
|
| 123 |
} catch (e) {
|
| 124 |
-
|
| 125 |
throw e;
|
| 126 |
}
|
| 127 |
|
| 128 |
-
// --- APP OPTIONS: input target fix ---
|
| 129 |
const opts = new pc.AppOptions();
|
| 130 |
opts.graphicsDevice = device;
|
| 131 |
-
opts.mouse = new pc.Mouse(
|
| 132 |
-
opts.touch = new pc.TouchDevice(
|
| 133 |
opts.componentSystems = [
|
| 134 |
pc.RenderComponentSystem,
|
| 135 |
pc.CameraComponentSystem,
|
|
@@ -146,82 +150,63 @@ export async function initializeViewer(config, instanceId) {
|
|
| 146 |
pc.GSplatHandler
|
| 147 |
];
|
| 148 |
|
| 149 |
-
// The PlayCanvas Vercel demo uses AppBase, but we can keep using Application
|
| 150 |
app = new pc.Application(canvas, opts);
|
| 151 |
-
|
| 152 |
-
// ---- iOS/Fill Mode handling ----
|
| 153 |
-
if (isIOS) {
|
| 154 |
-
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); // always fill window on iOS
|
| 155 |
-
} else {
|
| 156 |
-
app.setCanvasFillMode(pc.FILLMODE_NONE); // your original behavior for desktop/Android
|
| 157 |
-
}
|
| 158 |
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 159 |
|
| 160 |
-
|
| 161 |
-
// iOS: Always fill window. Desktop: use div resizeObserver.
|
| 162 |
-
if (isIOS) {
|
| 163 |
-
// Remove manual style sizes on the canvas
|
| 164 |
-
canvas.style.width = "";
|
| 165 |
-
canvas.style.height = "";
|
| 166 |
-
// Fill window
|
| 167 |
-
const resizeCanvasToWindow = () => {
|
| 168 |
-
app.resizeCanvas(window.innerWidth, window.innerHeight);
|
| 169 |
-
};
|
| 170 |
-
window.addEventListener('resize', resizeCanvasToWindow);
|
| 171 |
-
app.on('destroy', () => {
|
| 172 |
-
window.removeEventListener('resize', resizeCanvasToWindow);
|
| 173 |
-
});
|
| 174 |
-
resizeCanvasToWindow();
|
| 175 |
-
} else {
|
| 176 |
-
// Desktop: use ResizeObserver on the container div
|
| 177 |
-
resizeObserver = new ResizeObserver(entries => {
|
| 178 |
-
entries.forEach(entry => {
|
| 179 |
-
try {
|
| 180 |
-
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
| 181 |
-
} catch (e) { console.error("resizeCanvas error: " + e); }
|
| 182 |
-
});
|
| 183 |
-
});
|
| 184 |
-
resizeObserver.observe(viewerContainer);
|
| 185 |
|
| 186 |
-
|
|
|
|
|
|
|
| 187 |
try {
|
| 188 |
-
app.resizeCanvas(
|
| 189 |
-
} catch (e) {
|
| 190 |
});
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
//
|
| 195 |
const assets = {
|
| 196 |
sogs: new pc.Asset('gsplat', 'gsplat', { url: sogsUrl }),
|
| 197 |
orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" }),
|
| 198 |
-
glb:
|
| 199 |
};
|
| 200 |
-
for (const key in assets)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
-
assets.sogs.
|
| 203 |
-
assets.orbit.
|
| 204 |
-
if (assets.glb) assets.glb.
|
| 205 |
|
| 206 |
-
|
| 207 |
-
const loader = new pc.AssetListLoader(
|
| 208 |
-
Object.values(assets).filter(Boolean),
|
| 209 |
-
app.assets
|
| 210 |
-
);
|
| 211 |
|
| 212 |
-
loader.on('error', (err) => {
|
| 213 |
|
| 214 |
loader.load(() => {
|
|
|
|
|
|
|
| 215 |
try {
|
| 216 |
app.start();
|
| 217 |
} catch (e) {
|
| 218 |
-
|
| 219 |
throw e;
|
| 220 |
}
|
| 221 |
|
| 222 |
if (progressDialog) progressDialog.style.display = 'none';
|
| 223 |
|
| 224 |
-
//
|
| 225 |
try {
|
| 226 |
modelEntity = new pc.Entity('model');
|
| 227 |
modelEntity.addComponent('gsplat', { asset: assets.sogs });
|
|
@@ -229,22 +214,24 @@ export async function initializeViewer(config, instanceId) {
|
|
| 229 |
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 230 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 231 |
app.root.addChild(modelEntity);
|
|
|
|
| 232 |
} catch (e) {
|
| 233 |
-
|
| 234 |
throw e;
|
| 235 |
}
|
| 236 |
|
| 237 |
-
//
|
| 238 |
try {
|
| 239 |
if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
|
| 240 |
const glbEntity = assets.glb.resource.instantiateRenderEntity();
|
| 241 |
app.root.addChild(glbEntity);
|
|
|
|
| 242 |
}
|
| 243 |
} catch (e) {
|
| 244 |
-
|
| 245 |
}
|
| 246 |
|
| 247 |
-
//
|
| 248 |
try {
|
| 249 |
cameraEntity = new pc.Entity('camera');
|
| 250 |
// Support background color
|
|
@@ -257,7 +244,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 257 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 258 |
cameraEntity.addComponent('script');
|
| 259 |
|
| 260 |
-
// Orbit
|
| 261 |
cameraEntity.script.create('orbitCamera', {
|
| 262 |
attributes: {
|
| 263 |
focusEntity: modelEntity,
|
|
@@ -275,23 +262,27 @@ export async function initializeViewer(config, instanceId) {
|
|
| 275 |
cameraEntity.script.create('orbitCameraInputMouse');
|
| 276 |
cameraEntity.script.create('orbitCameraInputTouch');
|
| 277 |
app.root.addChild(cameraEntity);
|
|
|
|
|
|
|
| 278 |
} catch (e) {
|
| 279 |
-
|
| 280 |
}
|
| 281 |
|
| 282 |
try {
|
| 283 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
|
|
|
| 284 |
} catch (e) {
|
| 285 |
-
|
| 286 |
}
|
| 287 |
|
| 288 |
try {
|
| 289 |
app.once('update', () => resetViewerCamera());
|
|
|
|
| 290 |
} catch (e) {
|
| 291 |
-
|
| 292 |
}
|
| 293 |
|
| 294 |
-
//
|
| 295 |
try {
|
| 296 |
if (config.tooltips_url) {
|
| 297 |
import('https://mikafil-viewer-sgos.static.hf.space/tooltips.js').then(tooltipsModule => {
|
|
@@ -303,33 +294,35 @@ export async function initializeViewer(config, instanceId) {
|
|
| 303 |
defaultVisible: !!config.showTooltipsDefault,
|
| 304 |
moveDuration: config.tooltipMoveDuration || 0.6
|
| 305 |
});
|
| 306 |
-
|
|
|
|
| 307 |
}
|
| 308 |
} catch (e) {
|
| 309 |
-
|
| 310 |
}
|
| 311 |
|
| 312 |
viewerInitialized = true;
|
| 313 |
-
|
| 314 |
});
|
| 315 |
|
| 316 |
-
|
| 317 |
} catch (e) {
|
| 318 |
-
|
| 319 |
throw e;
|
| 320 |
}
|
| 321 |
}
|
| 322 |
|
| 323 |
// Resets the viewer camera (as in your PLY example)
|
| 324 |
export function resetViewerCamera() {
|
|
|
|
| 325 |
try {
|
| 326 |
if (!cameraEntity || !modelEntity || !app) {
|
| 327 |
-
|
| 328 |
return;
|
| 329 |
}
|
| 330 |
const orbitCam = cameraEntity.script.orbitCamera;
|
| 331 |
if (!orbitCam) {
|
| 332 |
-
|
| 333 |
return;
|
| 334 |
}
|
| 335 |
|
|
@@ -368,7 +361,8 @@ export function resetViewerCamera() {
|
|
| 368 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 369 |
|
| 370 |
tempEnt.destroy();
|
|
|
|
| 371 |
} catch (e) {
|
| 372 |
-
|
| 373 |
}
|
| 374 |
}
|
|
|
|
| 10 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 11 |
let sogsUrl, glbUrl, canvasBg;
|
| 12 |
|
|
|
|
| 13 |
function hexToRgbaArray(hex) {
|
| 14 |
try {
|
| 15 |
hex = hex.replace("#", "");
|
|
|
|
| 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);
|
| 41 |
|
| 42 |
try {
|
| 43 |
+
// Parse config
|
| 44 |
sogsUrl = config.sogs_json_url;
|
| 45 |
glbUrl = config.glb_url;
|
| 46 |
canvasBg = config.canvas_background || "#ffffff";
|
|
|
|
| 77 |
const progressIndicator= document.getElementById('progress-indicator-' + instanceId);
|
| 78 |
const viewerContainer = document.getElementById('viewer-container-' + instanceId);
|
| 79 |
|
|
|
|
| 80 |
let oldCanvas = document.getElementById(canvasId);
|
| 81 |
if (oldCanvas) oldCanvas.remove();
|
| 82 |
|
|
|
|
| 83 |
const canvas = document.createElement('canvas');
|
| 84 |
canvas.id = canvasId;
|
| 85 |
canvas.className = 'ply-canvas';
|
| 86 |
+
canvas.style.width = "100%";
|
| 87 |
+
canvas.style.height = "100%";
|
| 88 |
canvas.setAttribute('tabindex', '0');
|
| 89 |
+
viewerContainer.insertBefore(canvas, progressDialog);
|
| 90 |
+
|
| 91 |
+
// Apply background color from config if present
|
| 92 |
canvas.style.background = canvasBg;
|
| 93 |
+
|
| 94 |
canvas.style.touchAction = "none";
|
| 95 |
canvas.style.webkitTouchCallout = "none";
|
| 96 |
+
canvas.addEventListener('gesturestart', e => e.preventDefault());
|
| 97 |
+
canvas.addEventListener('gesturechange', e => e.preventDefault());
|
| 98 |
+
canvas.addEventListener('gestureend', e => e.preventDefault());
|
| 99 |
+
canvas.addEventListener('dblclick', e => e.preventDefault());
|
|
|
|
| 100 |
canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
|
| 101 |
+
|
| 102 |
+
// --- Mouse wheel suppression
|
| 103 |
+
canvas.addEventListener('wheel', (e) => {
|
| 104 |
+
e.preventDefault(); // Only block page scroll if mouse is over viewer
|
| 105 |
+
}, { passive: false });
|
| 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
|
| 118 |
let device;
|
| 119 |
try {
|
|
|
|
| 120 |
device = await pc.createGraphicsDevice(canvas, {
|
| 121 |
+
deviceTypes: ["webgl2", "webgl1"],
|
| 122 |
glslangUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/glslang/glslang.js",
|
| 123 |
twgslUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/twgsl/twgsl.js",
|
| 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 |
|
|
|
|
| 133 |
const opts = new pc.AppOptions();
|
| 134 |
opts.graphicsDevice = device;
|
| 135 |
+
opts.mouse = new pc.Mouse(canvas);
|
| 136 |
+
opts.touch = new pc.TouchDevice(canvas);
|
| 137 |
opts.componentSystems = [
|
| 138 |
pc.RenderComponentSystem,
|
| 139 |
pc.CameraComponentSystem,
|
|
|
|
| 150 |
pc.GSplatHandler
|
| 151 |
];
|
| 152 |
|
|
|
|
| 153 |
app = new pc.Application(canvas, opts);
|
| 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) { alert("resizeCanvas error: " + e); }
|
| 165 |
});
|
| 166 |
+
});
|
| 167 |
+
resizeObserver.observe(viewerContainer);
|
| 168 |
+
|
| 169 |
+
window.addEventListener('resize', () => {
|
| 170 |
+
try {
|
| 171 |
+
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 172 |
+
} catch (e) { alert("resizeCanvas error: " + e); }
|
| 173 |
+
});
|
| 174 |
+
app.on('destroy', () => resizeObserver.disconnect());
|
| 175 |
|
| 176 |
+
// Prepare asset loading (SOGS as 'gsplat')
|
| 177 |
const assets = {
|
| 178 |
sogs: new pc.Asset('gsplat', 'gsplat', { url: sogsUrl }),
|
| 179 |
orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" }),
|
| 180 |
+
glb: new pc.Asset('glb', 'container', { url: glbUrl }),
|
| 181 |
};
|
| 182 |
+
for (const key in assets) app.assets.add(assets[key]);
|
| 183 |
+
|
| 184 |
+
// Individual asset error listeners
|
| 185 |
+
assets.sogs.on('error', (err) => { alert("SOGS asset error: " + err); });
|
| 186 |
+
assets.orbit.on('error', (err) => { alert("Orbit-camera.js asset error: " + err); });
|
| 187 |
+
if (assets.glb) assets.glb.on('error', (err) => { alert("GLB asset error: " + err); });
|
| 188 |
|
| 189 |
+
assets.sogs.ready(() => { alert("SOGS asset ready."); });
|
| 190 |
+
assets.orbit.ready(() => { alert("orbit-camera.js asset ready."); });
|
| 191 |
+
if (assets.glb) assets.glb.ready(() => { alert("GLB asset ready."); });
|
| 192 |
|
| 193 |
+
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
+
loader.on('error', (err) => { alert("Asset loader 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 |
|
| 207 |
if (progressDialog) progressDialog.style.display = 'none';
|
| 208 |
|
| 209 |
+
// Add SOGS model
|
| 210 |
try {
|
| 211 |
modelEntity = new pc.Entity('model');
|
| 212 |
modelEntity.addComponent('gsplat', { asset: assets.sogs });
|
|
|
|
| 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 |
|
| 223 |
+
// Add the GLB entity if provided
|
| 224 |
try {
|
| 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 {
|
| 236 |
cameraEntity = new pc.Entity('camera');
|
| 237 |
// Support background color
|
|
|
|
| 244 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 245 |
cameraEntity.addComponent('script');
|
| 246 |
|
| 247 |
+
// Pass all attributes to Orbit Camera script
|
| 248 |
cameraEntity.script.create('orbitCamera', {
|
| 249 |
attributes: {
|
| 250 |
focusEntity: modelEntity,
|
|
|
|
| 262 |
cameraEntity.script.create('orbitCameraInputMouse');
|
| 263 |
cameraEntity.script.create('orbitCameraInputTouch');
|
| 264 |
app.root.addChild(cameraEntity);
|
| 265 |
+
|
| 266 |
+
alert("Camera added.");
|
| 267 |
} catch (e) {
|
| 268 |
+
alert("Camera error: " + e);
|
| 269 |
}
|
| 270 |
|
| 271 |
try {
|
| 272 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 273 |
+
alert("resizeCanvas called.");
|
| 274 |
} catch (e) {
|
| 275 |
+
alert("resizeCanvas error: " + e);
|
| 276 |
}
|
| 277 |
|
| 278 |
try {
|
| 279 |
app.once('update', () => resetViewerCamera());
|
| 280 |
+
alert("resetViewerCamera scheduled.");
|
| 281 |
} catch (e) {
|
| 282 |
+
alert("resetViewerCamera schedule error: " + e);
|
| 283 |
}
|
| 284 |
|
| 285 |
+
// Tooltips support (optional)
|
| 286 |
try {
|
| 287 |
if (config.tooltips_url) {
|
| 288 |
import('https://mikafil-viewer-sgos.static.hf.space/tooltips.js').then(tooltipsModule => {
|
|
|
|
| 294 |
defaultVisible: !!config.showTooltipsDefault,
|
| 295 |
moveDuration: config.tooltipMoveDuration || 0.6
|
| 296 |
});
|
| 297 |
+
alert("Tooltips loaded.");
|
| 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 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 362 |
|
| 363 |
tempEnt.destroy();
|
| 364 |
+
alert("resetViewerCamera: success");
|
| 365 |
} catch (e) {
|
| 366 |
+
alert("resetViewerCamera: error: " + e);
|
| 367 |
}
|
| 368 |
}
|