Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
// viewer.js
|
| 2 |
// ==============================
|
| 3 |
|
| 4 |
-
let pc;
|
| 5 |
export let app = null;
|
| 6 |
let cameraEntity = null;
|
| 7 |
let modelEntity = null;
|
|
@@ -13,6 +13,60 @@ let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, min
|
|
| 13 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 14 |
let sogsUrl, glbUrl;
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
export async function initializeViewer(config, instanceId) {
|
| 17 |
if (viewerInitialized) return;
|
| 18 |
|
|
@@ -72,11 +126,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 72 |
canvas.addEventListener('gestureend', e => e.preventDefault());
|
| 73 |
canvas.addEventListener('dblclick', e => e.preventDefault());
|
| 74 |
canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
|
| 75 |
-
|
| 76 |
-
// --- The following line attaches mouse wheel suppression to canvas only ---
|
| 77 |
-
canvas.addEventListener('wheel', (e) => {
|
| 78 |
-
e.preventDefault(); // Only block page scroll if mouse is over viewer
|
| 79 |
-
}, { passive: false });
|
| 80 |
|
| 81 |
progressDialog.style.display = 'block';
|
| 82 |
|
|
@@ -96,7 +146,6 @@ export async function initializeViewer(config, instanceId) {
|
|
| 96 |
|
| 97 |
const opts = new pc.AppOptions();
|
| 98 |
opts.graphicsDevice = device;
|
| 99 |
-
// Attach input only to canvas
|
| 100 |
opts.mouse = new pc.Mouse(canvas);
|
| 101 |
opts.touch = new pc.TouchDevice(canvas);
|
| 102 |
opts.componentSystems = [
|
|
@@ -136,20 +185,35 @@ export async function initializeViewer(config, instanceId) {
|
|
| 136 |
glb: new pc.Asset('glb', 'container', { url: glbUrl }),
|
| 137 |
};
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
| 141 |
if (asset && asset.url) {
|
| 142 |
-
console.log(`[DEBUG] Asset "${
|
| 143 |
}
|
| 144 |
});
|
|
|
|
| 145 |
for (const key in assets) app.assets.add(assets[key]);
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 148 |
loader.load(() => {
|
|
|
|
| 149 |
app.start();
|
| 150 |
progressDialog.style.display = 'none';
|
| 151 |
|
| 152 |
-
// Add PLY/GSplat model
|
| 153 |
modelEntity = new pc.Entity('model');
|
| 154 |
modelEntity.addComponent('gsplat', { asset: assets.sogs });
|
| 155 |
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
|
@@ -157,20 +221,16 @@ export async function initializeViewer(config, instanceId) {
|
|
| 157 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 158 |
app.root.addChild(modelEntity);
|
| 159 |
|
| 160 |
-
// --- Add the GLB entity ---
|
| 161 |
if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
|
| 162 |
const glbEntity = assets.glb.resource.instantiateRenderEntity();
|
| 163 |
app.root.addChild(glbEntity);
|
| 164 |
}
|
| 165 |
|
| 166 |
cameraEntity = new pc.Entity('camera');
|
| 167 |
-
|
| 168 |
-
cameraEntity.addComponent('camera', { clearColor: new pc.Color(1, 1, 1, 1) }); // White RGBA
|
| 169 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 170 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 171 |
cameraEntity.addComponent('script');
|
| 172 |
-
|
| 173 |
-
// === MAIN FIX: Pass ALL relevant attributes including minY ===
|
| 174 |
cameraEntity.script.create('orbitCamera', {
|
| 175 |
attributes: {
|
| 176 |
focusEntity: modelEntity,
|
|
@@ -190,7 +250,6 @@ export async function initializeViewer(config, instanceId) {
|
|
| 190 |
app.root.addChild(cameraEntity);
|
| 191 |
|
| 192 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 193 |
-
|
| 194 |
app.once('update', () => resetViewerCamera());
|
| 195 |
|
| 196 |
// Tooltips supported if tooltips_url set
|
|
@@ -238,7 +297,7 @@ export function resetViewerCamera() {
|
|
| 238 |
|
| 239 |
orbitCam.pivotPoint = modelPos.clone();
|
| 240 |
orbitCam._targetDistance = dist;
|
| 241 |
-
orbitCam._distance
|
| 242 |
|
| 243 |
const rot = tempEnt.getRotation();
|
| 244 |
const fwd = new pc.Vec3();
|
|
@@ -251,14 +310,14 @@ export function resetViewerCamera() {
|
|
| 251 |
rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
|
| 252 |
const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
|
| 253 |
|
| 254 |
-
orbitCam._targetYaw
|
| 255 |
-
orbitCam._yaw
|
| 256 |
orbitCam._targetPitch = pitch;
|
| 257 |
-
orbitCam._pitch
|
| 258 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 259 |
|
| 260 |
tempEnt.destroy();
|
| 261 |
} catch (e) {
|
| 262 |
-
|
| 263 |
}
|
| 264 |
}
|
|
|
|
| 1 |
// viewer.js
|
| 2 |
// ==============================
|
| 3 |
|
| 4 |
+
let pc;
|
| 5 |
export let app = null;
|
| 6 |
let cameraEntity = null;
|
| 7 |
let modelEntity = null;
|
|
|
|
| 13 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 14 |
let sogsUrl, glbUrl;
|
| 15 |
|
| 16 |
+
// --- Log user agent and page location ---
|
| 17 |
+
console.log("%c[VIEWER DEBUG] User Agent:", "color: #0074D9;", navigator.userAgent);
|
| 18 |
+
console.log("%c[VIEWER DEBUG] Page:", "color: #0074D9;", location.href);
|
| 19 |
+
|
| 20 |
+
// --- Monkeypatch Image to log src and crossOrigin ---
|
| 21 |
+
(function() {
|
| 22 |
+
const OriginalImage = window.Image;
|
| 23 |
+
window.Image = function(...args) {
|
| 24 |
+
const img = new OriginalImage(...args);
|
| 25 |
+
const origSetAttribute = img.setAttribute;
|
| 26 |
+
img.setAttribute = function(name, value) {
|
| 27 |
+
if (name === "crossOrigin") {
|
| 28 |
+
console.log("%c[VIEWER DEBUG] setAttribute crossOrigin:", "color: orange;", value, img.src);
|
| 29 |
+
}
|
| 30 |
+
return origSetAttribute.apply(this, arguments);
|
| 31 |
+
};
|
| 32 |
+
Object.defineProperty(img, "src", {
|
| 33 |
+
set(value) {
|
| 34 |
+
console.log("%c[VIEWER DEBUG] Setting img.src:", "color: orange;", value, "crossOrigin:", img.crossOrigin);
|
| 35 |
+
origSetAttribute.call(img, "src", value);
|
| 36 |
+
}
|
| 37 |
+
});
|
| 38 |
+
img.onload = function() {
|
| 39 |
+
console.log("%c[VIEWER DEBUG] Image loaded:", "color: orange;", img.src, "crossOrigin:", img.crossOrigin);
|
| 40 |
+
};
|
| 41 |
+
img.onerror = function(e) {
|
| 42 |
+
console.error("%c[VIEWER DEBUG] Image load error:", "color: orange;", img.src, e);
|
| 43 |
+
};
|
| 44 |
+
return img;
|
| 45 |
+
};
|
| 46 |
+
})();
|
| 47 |
+
|
| 48 |
+
// --- Monkeypatch WebGL texImage2D for detailed logging ---
|
| 49 |
+
(function() {
|
| 50 |
+
const orig = WebGLRenderingContext.prototype.texImage2D;
|
| 51 |
+
WebGLRenderingContext.prototype.texImage2D = function() {
|
| 52 |
+
try {
|
| 53 |
+
console.log("%c[VIEWER DEBUG] texImage2D called with:", "color: #FF4136;", arguments);
|
| 54 |
+
return orig.apply(this, arguments);
|
| 55 |
+
} catch (e) {
|
| 56 |
+
console.error("%c[VIEWER DEBUG] texImage2D ERROR:", "color: #FF4136;", e, arguments);
|
| 57 |
+
throw e;
|
| 58 |
+
}
|
| 59 |
+
};
|
| 60 |
+
})();
|
| 61 |
+
|
| 62 |
+
// --- Global error logging ---
|
| 63 |
+
window.addEventListener("error", function(e) {
|
| 64 |
+
if (e.message && /insecure|taint|cross-origin/i.test(e.message)) {
|
| 65 |
+
alert("[VIEWER] WebGL/CORS error: " + e.message);
|
| 66 |
+
console.error("%c[VIEWER DEBUG] Global error:", "color: #FF4136;", e);
|
| 67 |
+
}
|
| 68 |
+
});
|
| 69 |
+
|
| 70 |
export async function initializeViewer(config, instanceId) {
|
| 71 |
if (viewerInitialized) return;
|
| 72 |
|
|
|
|
| 126 |
canvas.addEventListener('gestureend', e => e.preventDefault());
|
| 127 |
canvas.addEventListener('dblclick', e => e.preventDefault());
|
| 128 |
canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
|
| 129 |
+
canvas.addEventListener('wheel', (e) => { e.preventDefault(); }, { passive: false });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
progressDialog.style.display = 'block';
|
| 132 |
|
|
|
|
| 146 |
|
| 147 |
const opts = new pc.AppOptions();
|
| 148 |
opts.graphicsDevice = device;
|
|
|
|
| 149 |
opts.mouse = new pc.Mouse(canvas);
|
| 150 |
opts.touch = new pc.TouchDevice(canvas);
|
| 151 |
opts.componentSystems = [
|
|
|
|
| 185 |
glb: new pc.Asset('glb', 'container', { url: glbUrl }),
|
| 186 |
};
|
| 187 |
|
| 188 |
+
// --- LOG: asset list and all URLs
|
| 189 |
+
console.log("%c[VIEWER DEBUG] About to load assets:", "color: #39CCCC;", JSON.stringify(
|
| 190 |
+
Object.fromEntries(Object.entries(assets).map(([k, v]) => [k, v && v.url]))
|
| 191 |
+
, null, 2));
|
| 192 |
+
Object.entries(assets).forEach(([name, asset]) => {
|
| 193 |
if (asset && asset.url) {
|
| 194 |
+
console.log(`[VIEWER DEBUG] Asset "${name}" loading from: ${asset.url}`);
|
| 195 |
}
|
| 196 |
});
|
| 197 |
+
|
| 198 |
for (const key in assets) app.assets.add(assets[key]);
|
| 199 |
|
| 200 |
+
// --- LOG: asset load or error events
|
| 201 |
+
Object.entries(assets).forEach(([name, asset]) => {
|
| 202 |
+
if (!asset) return;
|
| 203 |
+
asset.once('load', function(resource) {
|
| 204 |
+
console.log(`%c[VIEWER DEBUG] Asset loaded: "${name}"`, "color: #2ECC40;", asset.url, resource);
|
| 205 |
+
});
|
| 206 |
+
asset.once('error', function(err) {
|
| 207 |
+
console.error(`%c[VIEWER DEBUG] Asset failed to load: "${name}"`, "color: #FF4136;", asset.url, err);
|
| 208 |
+
});
|
| 209 |
+
});
|
| 210 |
+
|
| 211 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 212 |
loader.load(() => {
|
| 213 |
+
console.log("%c[VIEWER DEBUG] All assets loaded, starting app", "color: #2ECC40;");
|
| 214 |
app.start();
|
| 215 |
progressDialog.style.display = 'none';
|
| 216 |
|
|
|
|
| 217 |
modelEntity = new pc.Entity('model');
|
| 218 |
modelEntity.addComponent('gsplat', { asset: assets.sogs });
|
| 219 |
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
|
|
|
| 221 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 222 |
app.root.addChild(modelEntity);
|
| 223 |
|
|
|
|
| 224 |
if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
|
| 225 |
const glbEntity = assets.glb.resource.instantiateRenderEntity();
|
| 226 |
app.root.addChild(glbEntity);
|
| 227 |
}
|
| 228 |
|
| 229 |
cameraEntity = new pc.Entity('camera');
|
| 230 |
+
cameraEntity.addComponent('camera', { clearColor: new pc.Color(1, 1, 1, 1) });
|
|
|
|
| 231 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 232 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 233 |
cameraEntity.addComponent('script');
|
|
|
|
|
|
|
| 234 |
cameraEntity.script.create('orbitCamera', {
|
| 235 |
attributes: {
|
| 236 |
focusEntity: modelEntity,
|
|
|
|
| 250 |
app.root.addChild(cameraEntity);
|
| 251 |
|
| 252 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
|
|
|
| 253 |
app.once('update', () => resetViewerCamera());
|
| 254 |
|
| 255 |
// Tooltips supported if tooltips_url set
|
|
|
|
| 297 |
|
| 298 |
orbitCam.pivotPoint = modelPos.clone();
|
| 299 |
orbitCam._targetDistance = dist;
|
| 300 |
+
orbitCam._distance = dist;
|
| 301 |
|
| 302 |
const rot = tempEnt.getRotation();
|
| 303 |
const fwd = new pc.Vec3();
|
|
|
|
| 310 |
rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
|
| 311 |
const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
|
| 312 |
|
| 313 |
+
orbitCam._targetYaw = yaw;
|
| 314 |
+
orbitCam._yaw = yaw;
|
| 315 |
orbitCam._targetPitch = pitch;
|
| 316 |
+
orbitCam._pitch = pitch;
|
| 317 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 318 |
|
| 319 |
tempEnt.destroy();
|
| 320 |
} catch (e) {
|
| 321 |
+
console.error("[VIEWER DEBUG] resetViewerCamera error", e);
|
| 322 |
}
|
| 323 |
}
|