Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
// viewer.js
|
| 2 |
-
|
| 3 |
let pc; // will hold the PlayCanvas module once imported
|
| 4 |
export let app = null;
|
| 5 |
let cameraEntity = null;
|
|
@@ -12,20 +10,15 @@ let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, min
|
|
| 12 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 13 |
let plyUrl, glbUrl;
|
| 14 |
|
| 15 |
-
// iOS & Safari detection
|
| 16 |
-
function isIOS() {
|
| 17 |
-
return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
| 18 |
-
}
|
| 19 |
-
function isSafari() {
|
| 20 |
-
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
/**
|
| 24 |
* initializeViewer(config, instanceId)
|
| 25 |
*/
|
| 26 |
export async function initializeViewer(config, instanceId) {
|
| 27 |
if (viewerInitialized) return;
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
// 1. Read config
|
| 30 |
plyUrl = config.ply_url;
|
| 31 |
glbUrl = config.glb_url;
|
|
@@ -53,10 +46,9 @@ export async function initializeViewer(config, instanceId) {
|
|
| 53 |
const cameraYPhone = (config.cameraYPhone !== undefined) ? parseFloat(config.cameraYPhone) : cameraY;
|
| 54 |
const cameraZPhone = (config.cameraZPhone !== undefined) ? parseFloat(config.cameraZPhone) : (cameraZ * 1.5);
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
chosenCameraZ = mobile ? cameraZPhone : cameraZ;
|
| 60 |
|
| 61 |
// 2. Grab DOM
|
| 62 |
const canvasId = 'canvas-' + instanceId;
|
|
@@ -94,21 +86,16 @@ export async function initializeViewer(config, instanceId) {
|
|
| 94 |
if (!pc) {
|
| 95 |
pc = await import("https://esm.run/playcanvas");
|
| 96 |
window.pc = pc;
|
| 97 |
-
alert('[viewer.js] PlayCanvas module loaded.');
|
| 98 |
}
|
| 99 |
|
| 100 |
try {
|
| 101 |
// 6. Setup device & app
|
| 102 |
-
alert('[viewer.js] Creating graphics device...');
|
| 103 |
const device = await pc.createGraphicsDevice(canvas, {
|
| 104 |
deviceTypes: ["webgl2"],
|
| 105 |
glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
|
| 106 |
twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
|
| 107 |
-
antialias: false
|
| 108 |
-
preserveDrawingBuffer: isIOS() || isSafari() ? true : false,
|
| 109 |
-
alpha: false
|
| 110 |
});
|
| 111 |
-
alert('[viewer.js] Graphics device created!');
|
| 112 |
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 113 |
|
| 114 |
const opts = new pc.AppOptions();
|
|
@@ -134,8 +121,8 @@ export async function initializeViewer(config, instanceId) {
|
|
| 134 |
app = new pc.Application(canvas, opts);
|
| 135 |
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 136 |
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 137 |
-
|
| 138 |
-
|
| 139 |
|
| 140 |
// Attach ResizeObserver to keep canvas in sync with container size
|
| 141 |
resizeObserver = new ResizeObserver(entries => {
|
|
@@ -167,12 +154,9 @@ export async function initializeViewer(config, instanceId) {
|
|
| 167 |
|
| 168 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 169 |
let lastProg = 0;
|
| 170 |
-
assets.model.on('load', () =>
|
| 171 |
-
progressDialog.style.display = 'none';
|
| 172 |
-
alert('[viewer.js] PLY/GSplat asset loaded!');
|
| 173 |
-
});
|
| 174 |
assets.model.on('error', err => {
|
| 175 |
-
|
| 176 |
progressDialog.innerHTML = <p style="color: red">Error loading model: ${err}</p>;
|
| 177 |
});
|
| 178 |
|
|
@@ -188,7 +172,6 @@ export async function initializeViewer(config, instanceId) {
|
|
| 188 |
}, 100);
|
| 189 |
|
| 190 |
loader.load(async () => {
|
| 191 |
-
alert('[viewer.js] All assets loaded; starting app.');
|
| 192 |
app.start();
|
| 193 |
app.scene.envAtlas = assets.hdr.resource;
|
| 194 |
|
|
@@ -199,7 +182,6 @@ export async function initializeViewer(config, instanceId) {
|
|
| 199 |
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 200 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 201 |
app.root.addChild(modelEntity);
|
| 202 |
-
alert('[viewer.js] Model entity added to scene!');
|
| 203 |
|
| 204 |
// Light
|
| 205 |
const dirLight = new pc.Entity('Cascaded Light');
|
|
@@ -223,11 +205,8 @@ export async function initializeViewer(config, instanceId) {
|
|
| 223 |
app.root.addChild(dirLight);
|
| 224 |
|
| 225 |
// Gallery GLB
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
app.root.addChild(galleryEntity);
|
| 229 |
-
alert('[viewer.js] GLB entity added to scene!');
|
| 230 |
-
}
|
| 231 |
|
| 232 |
// Camera setup
|
| 233 |
cameraEntity = new pc.Entity('camera');
|
|
@@ -235,6 +214,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 235 |
clearColor: config.canvas_background
|
| 236 |
? parseInt(config.canvas_background.substr(1, 2), 16) / 255
|
| 237 |
: 0,
|
|
|
|
| 238 |
});
|
| 239 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 240 |
cameraEntity.lookAt(modelEntity.getPosition());
|
|
@@ -256,8 +236,8 @@ export async function initializeViewer(config, instanceId) {
|
|
| 256 |
});
|
| 257 |
cameraEntity.script.create('orbitCameraInputMouse', {
|
| 258 |
attributes: {
|
| 259 |
-
orbitSensitivity:
|
| 260 |
-
distanceSensitivity:
|
| 261 |
}
|
| 262 |
});
|
| 263 |
if (cameraEntity.script.orbitCameraInputMouse) {
|
|
@@ -270,13 +250,9 @@ export async function initializeViewer(config, instanceId) {
|
|
| 270 |
}
|
| 271 |
});
|
| 272 |
app.root.addChild(cameraEntity);
|
| 273 |
-
alert('[viewer.js] Camera entity added to scene!');
|
| 274 |
|
| 275 |
// Reset & constrain updates
|
| 276 |
-
app.once('update', () =>
|
| 277 |
-
alert('[viewer.js] First app update! Resetting camera.');
|
| 278 |
-
resetViewerCamera();
|
| 279 |
-
});
|
| 280 |
app.on('update', dt => {
|
| 281 |
if (cameraEntity) {
|
| 282 |
const pos = cameraEntity.getPosition();
|
|
@@ -284,11 +260,6 @@ export async function initializeViewer(config, instanceId) {
|
|
| 284 |
cameraEntity.setPosition(pos.x, minY, pos.z);
|
| 285 |
}
|
| 286 |
}
|
| 287 |
-
// Diagnostics: Only show once
|
| 288 |
-
if (!window._pcFirstUpdate) {
|
| 289 |
-
window._pcFirstUpdate = true;
|
| 290 |
-
alert('[viewer.js] Entered update/render loop!');
|
| 291 |
-
}
|
| 292 |
});
|
| 293 |
|
| 294 |
// Final resize
|
|
@@ -311,11 +282,9 @@ export async function initializeViewer(config, instanceId) {
|
|
| 311 |
|
| 312 |
progressDialog.style.display = 'none';
|
| 313 |
viewerInitialized = true;
|
| 314 |
-
alert('[viewer.js] Viewer fully initialized and running!');
|
| 315 |
});
|
| 316 |
|
| 317 |
} catch (error) {
|
| 318 |
-
alert("[viewer.js] Error initializing PlayCanvas viewer: " + error.message);
|
| 319 |
console.error("Error initializing PlayCanvas viewer:", error);
|
| 320 |
progressDialog.innerHTML = <p style="color: red">Error loading viewer: ${error.message}</p>;
|
| 321 |
}
|
|
|
|
|
|
|
|
|
|
| 1 |
let pc; // will hold the PlayCanvas module once imported
|
| 2 |
export let app = null;
|
| 3 |
let cameraEntity = null;
|
|
|
|
| 10 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 11 |
let plyUrl, glbUrl;
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
/**
|
| 14 |
* initializeViewer(config, instanceId)
|
| 15 |
*/
|
| 16 |
export async function initializeViewer(config, instanceId) {
|
| 17 |
if (viewerInitialized) return;
|
| 18 |
|
| 19 |
+
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
| 20 |
+
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 21 |
+
|
| 22 |
// 1. Read config
|
| 23 |
plyUrl = config.ply_url;
|
| 24 |
glbUrl = config.glb_url;
|
|
|
|
| 46 |
const cameraYPhone = (config.cameraYPhone !== undefined) ? parseFloat(config.cameraYPhone) : cameraY;
|
| 47 |
const cameraZPhone = (config.cameraZPhone !== undefined) ? parseFloat(config.cameraZPhone) : (cameraZ * 1.5);
|
| 48 |
|
| 49 |
+
chosenCameraX = isMobile ? cameraXPhone : cameraX;
|
| 50 |
+
chosenCameraY = isMobile ? cameraYPhone : cameraY;
|
| 51 |
+
chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
|
|
|
|
| 52 |
|
| 53 |
// 2. Grab DOM
|
| 54 |
const canvasId = 'canvas-' + instanceId;
|
|
|
|
| 86 |
if (!pc) {
|
| 87 |
pc = await import("https://esm.run/playcanvas");
|
| 88 |
window.pc = pc;
|
|
|
|
| 89 |
}
|
| 90 |
|
| 91 |
try {
|
| 92 |
// 6. Setup device & app
|
|
|
|
| 93 |
const device = await pc.createGraphicsDevice(canvas, {
|
| 94 |
deviceTypes: ["webgl2"],
|
| 95 |
glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
|
| 96 |
twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
|
| 97 |
+
antialias: false
|
|
|
|
|
|
|
| 98 |
});
|
|
|
|
| 99 |
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 100 |
|
| 101 |
const opts = new pc.AppOptions();
|
|
|
|
| 121 |
app = new pc.Application(canvas, opts);
|
| 122 |
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 123 |
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 124 |
+
//app.scene.exposure = 0.5;
|
| 125 |
+
//app.scene.toneMapping = pc.TONEMAP_ACES;
|
| 126 |
|
| 127 |
// Attach ResizeObserver to keep canvas in sync with container size
|
| 128 |
resizeObserver = new ResizeObserver(entries => {
|
|
|
|
| 154 |
|
| 155 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 156 |
let lastProg = 0;
|
| 157 |
+
assets.model.on('load', () => progressDialog.style.display = 'none');
|
|
|
|
|
|
|
|
|
|
| 158 |
assets.model.on('error', err => {
|
| 159 |
+
console.error("Error loading PLY file:", err);
|
| 160 |
progressDialog.innerHTML = <p style="color: red">Error loading model: ${err}</p>;
|
| 161 |
});
|
| 162 |
|
|
|
|
| 172 |
}, 100);
|
| 173 |
|
| 174 |
loader.load(async () => {
|
|
|
|
| 175 |
app.start();
|
| 176 |
app.scene.envAtlas = assets.hdr.resource;
|
| 177 |
|
|
|
|
| 182 |
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 183 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 184 |
app.root.addChild(modelEntity);
|
|
|
|
| 185 |
|
| 186 |
// Light
|
| 187 |
const dirLight = new pc.Entity('Cascaded Light');
|
|
|
|
| 205 |
app.root.addChild(dirLight);
|
| 206 |
|
| 207 |
// Gallery GLB
|
| 208 |
+
const galleryEntity = assets.galerie.resource.instantiateRenderEntity();
|
| 209 |
+
app.root.addChild(galleryEntity);
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
// Camera setup
|
| 212 |
cameraEntity = new pc.Entity('camera');
|
|
|
|
| 214 |
clearColor: config.canvas_background
|
| 215 |
? parseInt(config.canvas_background.substr(1, 2), 16) / 255
|
| 216 |
: 0,
|
| 217 |
+
//toneMapping: pc.TONEMAP_ACES
|
| 218 |
});
|
| 219 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 220 |
cameraEntity.lookAt(modelEntity.getPosition());
|
|
|
|
| 236 |
});
|
| 237 |
cameraEntity.script.create('orbitCameraInputMouse', {
|
| 238 |
attributes: {
|
| 239 |
+
orbitSensitivity: isMobile ? 0.6 : 0.3,
|
| 240 |
+
distanceSensitivity: isMobile ? 0.5 : 0.4
|
| 241 |
}
|
| 242 |
});
|
| 243 |
if (cameraEntity.script.orbitCameraInputMouse) {
|
|
|
|
| 250 |
}
|
| 251 |
});
|
| 252 |
app.root.addChild(cameraEntity);
|
|
|
|
| 253 |
|
| 254 |
// Reset & constrain updates
|
| 255 |
+
app.once('update', () => resetViewerCamera());
|
|
|
|
|
|
|
|
|
|
| 256 |
app.on('update', dt => {
|
| 257 |
if (cameraEntity) {
|
| 258 |
const pos = cameraEntity.getPosition();
|
|
|
|
| 260 |
cameraEntity.setPosition(pos.x, minY, pos.z);
|
| 261 |
}
|
| 262 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
});
|
| 264 |
|
| 265 |
// Final resize
|
|
|
|
| 282 |
|
| 283 |
progressDialog.style.display = 'none';
|
| 284 |
viewerInitialized = true;
|
|
|
|
| 285 |
});
|
| 286 |
|
| 287 |
} catch (error) {
|
|
|
|
| 288 |
console.error("Error initializing PlayCanvas viewer:", error);
|
| 289 |
progressDialog.innerHTML = <p style="color: red">Error loading viewer: ${error.message}</p>;
|
| 290 |
}
|