Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -2,27 +2,27 @@
|
|
| 2 |
// ==============================
|
| 3 |
|
| 4 |
async function loadImageAsTexture(url, app) {
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
}
|
| 21 |
|
| 22 |
// --- Patch all window.Image to force crossOrigin="anonymous" ---
|
| 23 |
-
(function() {
|
| 24 |
const OriginalImage = window.Image;
|
| 25 |
-
window.Image = function(...args) {
|
| 26 |
const img = new OriginalImage(...args);
|
| 27 |
img.crossOrigin = "anonymous";
|
| 28 |
return img;
|
|
@@ -36,10 +36,10 @@ function hexToRgbaArray(hex) {
|
|
| 36 |
if (hex.length !== 8) return [1, 1, 1, 1];
|
| 37 |
const num = parseInt(hex, 16);
|
| 38 |
return [
|
| 39 |
-
((num >> 24) &
|
| 40 |
-
((num >> 16) &
|
| 41 |
-
((num >> 8)
|
| 42 |
-
(num &
|
| 43 |
];
|
| 44 |
} catch (e) {
|
| 45 |
alert("hexToRgbaArray error: " + e);
|
|
@@ -49,10 +49,49 @@ function hexToRgbaArray(hex) {
|
|
| 49 |
|
| 50 |
// ----- Utility: Recursive scene traversal -----
|
| 51 |
function traverse(entity, callback) {
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
let pc;
|
|
@@ -73,6 +112,8 @@ let sogsUrl, glbUrl, presentoirUrl;
|
|
| 73 |
let color_bg_hex, color_bg, espace_expo_bool;
|
| 74 |
|
| 75 |
export async function initializeViewer(config, instanceId) {
|
|
|
|
|
|
|
| 76 |
if (viewerInitialized) return;
|
| 77 |
|
| 78 |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
|
|
@@ -80,38 +121,57 @@ export async function initializeViewer(config, instanceId) {
|
|
| 80 |
|
| 81 |
//prends les differents arguments de config.json
|
| 82 |
sogsUrl = config.sogs_json_url;
|
| 83 |
-
glbUrl =
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
minZoom = parseFloat(config.minZoom || "1");
|
| 86 |
maxZoom = parseFloat(config.maxZoom || "20");
|
| 87 |
minAngle = parseFloat(config.minAngle || "-45");
|
| 88 |
maxAngle = parseFloat(config.maxAngle || "90");
|
| 89 |
-
minAzimuth =
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
minPivotY = parseFloat(config.minPivotY || "0");
|
| 92 |
-
minY =
|
| 93 |
-
|
| 94 |
-
modelX =
|
| 95 |
-
modelY =
|
| 96 |
-
modelZ =
|
| 97 |
-
modelScale =
|
| 98 |
-
modelRotationX =
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
color_bg = hexToRgbaArray(color_bg_hex);
|
| 116 |
|
| 117 |
chosenCameraX = isMobile ? cameraXPhone : cameraX;
|
|
@@ -119,82 +179,99 @@ export async function initializeViewer(config, instanceId) {
|
|
| 119 |
chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
|
| 120 |
|
| 121 |
//cree le nouveau canva (instanceId est une valeur aléatoire pour gérer plusieurs canvas sur la meme page)
|
| 122 |
-
const canvasId =
|
| 123 |
-
const progressDialog = document.getElementById(
|
| 124 |
-
const progressIndicator = document.getElementById(
|
| 125 |
-
const viewerContainer = document.getElementById(
|
| 126 |
|
| 127 |
let oldCanvas = document.getElementById(canvasId);
|
| 128 |
if (oldCanvas) oldCanvas.remove();
|
| 129 |
|
| 130 |
-
const canvas = document.createElement(
|
| 131 |
canvas.id = canvasId;
|
| 132 |
-
canvas.className =
|
| 133 |
canvas.style.width = "100%";
|
| 134 |
canvas.style.height = "100%";
|
| 135 |
-
canvas.setAttribute(
|
| 136 |
viewerContainer.insertBefore(canvas, progressDialog);
|
| 137 |
|
| 138 |
canvas.style.touchAction = "none";
|
| 139 |
canvas.style.webkitTouchCallout = "none";
|
| 140 |
-
canvas.addEventListener(
|
| 141 |
-
canvas.addEventListener(
|
| 142 |
-
canvas.addEventListener(
|
| 143 |
-
canvas.addEventListener(
|
| 144 |
-
canvas.addEventListener(
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
// --- Clavier : bloquer le scroll seulement quand le pointeur est au-dessus du canvas ---
|
| 148 |
const scrollKeys = new Set([
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
]);
|
| 152 |
-
|
| 153 |
let isPointerOverCanvas = false;
|
| 154 |
-
|
| 155 |
const focusCanvas = () => canvas.focus({ preventScroll: true });
|
| 156 |
|
| 157 |
const onPointerEnter = () => {
|
| 158 |
isPointerOverCanvas = true;
|
| 159 |
focusCanvas();
|
| 160 |
};
|
| 161 |
-
|
| 162 |
const onPointerLeave = () => {
|
| 163 |
isPointerOverCanvas = false;
|
| 164 |
// rendre immédiatement le scroll à la page sans recliquer
|
| 165 |
-
if (document.activeElement === canvas)
|
| 166 |
-
canvas.blur();
|
| 167 |
-
}
|
| 168 |
};
|
| 169 |
|
| 170 |
-
|
| 171 |
-
canvas.addEventListener(
|
| 172 |
-
canvas.addEventListener(
|
| 173 |
-
canvas.addEventListener(
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
// Capture au niveau fenêtre pour bloquer le scroll, mais seulement si pointeur dessus
|
| 185 |
const onKeyDownCapture = (e) => {
|
| 186 |
if (!isPointerOverCanvas) return;
|
| 187 |
if (scrollKeys.has(e.key) || scrollKeys.has(e.code)) {
|
| 188 |
e.preventDefault(); // bloque le scroll page
|
| 189 |
-
// ne pas stopPropagation
|
| 190 |
}
|
| 191 |
};
|
| 192 |
-
window.addEventListener(
|
| 193 |
|
| 194 |
-
|
| 195 |
-
// Ça évite de devoir “recliquer” pour retrouver le scroll.
|
| 196 |
-
|
| 197 |
-
progressDialog.style.display = 'block';
|
| 198 |
|
| 199 |
if (!pc) {
|
| 200 |
pc = await import("https://esm.run/playcanvas");
|
|
@@ -214,7 +291,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 214 |
opts.graphicsDevice = device;
|
| 215 |
opts.mouse = new pc.Mouse(canvas);
|
| 216 |
opts.touch = new pc.TouchDevice(canvas);
|
| 217 |
-
opts.keyboard = new pc.Keyboard(canvas);
|
| 218 |
opts.componentSystems = [
|
| 219 |
pc.RenderComponentSystem,
|
| 220 |
pc.CameraComponentSystem,
|
|
@@ -235,50 +312,60 @@ export async function initializeViewer(config, instanceId) {
|
|
| 235 |
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 236 |
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 237 |
|
| 238 |
-
resizeObserver = new ResizeObserver(entries => {
|
| 239 |
-
entries.forEach(entry => {
|
| 240 |
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
| 241 |
});
|
| 242 |
});
|
| 243 |
resizeObserver.observe(viewerContainer);
|
| 244 |
|
| 245 |
-
window.addEventListener(
|
|
|
|
|
|
|
| 246 |
|
| 247 |
// Nettoyage complet à la destruction
|
| 248 |
-
app.on(
|
| 249 |
resizeObserver.disconnect();
|
| 250 |
if (opts.keyboard && opts.keyboard.detach) opts.keyboard.detach();
|
| 251 |
|
| 252 |
-
window.removeEventListener(
|
| 253 |
|
| 254 |
-
canvas.removeEventListener(
|
| 255 |
-
canvas.removeEventListener(
|
| 256 |
-
canvas.removeEventListener(
|
| 257 |
-
canvas.removeEventListener(
|
| 258 |
-
canvas.removeEventListener(
|
| 259 |
-
canvas.removeEventListener(
|
| 260 |
-
canvas.removeEventListener(
|
| 261 |
});
|
| 262 |
|
| 263 |
// Assets after app exists
|
|
|
|
|
|
|
| 264 |
const assets = {
|
| 265 |
-
sogs: new pc.Asset(
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
presentoir: new pc.Asset('presentoir', 'container', { url: presentoirUrl }),
|
| 269 |
};
|
| 270 |
|
| 271 |
for (const key in assets) app.assets.add(assets[key]);
|
| 272 |
|
| 273 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 274 |
-
loader.load(() => {
|
|
|
|
|
|
|
|
|
|
| 275 |
app.start();
|
| 276 |
-
progressDialog.style.display =
|
| 277 |
|
| 278 |
-
modelEntity = new pc.Entity(
|
| 279 |
-
modelEntity.addComponent(
|
| 280 |
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 281 |
-
modelEntity.setLocalEulerAngles(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 283 |
app.root.addChild(modelEntity);
|
| 284 |
|
|
@@ -286,7 +373,11 @@ export async function initializeViewer(config, instanceId) {
|
|
| 286 |
app.root.addChild(glbEntity);
|
| 287 |
|
| 288 |
const presentoirEntity = assets.presentoir.resource.instantiateRenderEntity();
|
| 289 |
-
presentoirEntity.setLocalScale(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
app.root.addChild(presentoirEntity);
|
| 291 |
|
| 292 |
if (!espace_expo_bool) {
|
|
@@ -297,7 +388,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 297 |
matSol.useLighting = false;
|
| 298 |
matSol.update();
|
| 299 |
|
| 300 |
-
traverse(presentoirEntity, node => {
|
| 301 |
if (node.render && node.render.meshInstances) {
|
| 302 |
for (let mi of node.render.meshInstances) {
|
| 303 |
mi.material = matSol;
|
|
@@ -305,7 +396,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 305 |
}
|
| 306 |
});
|
| 307 |
|
| 308 |
-
traverse(glbEntity, node => {
|
| 309 |
if (node.render && node.render.meshInstances) {
|
| 310 |
for (let mi of node.render.meshInstances) {
|
| 311 |
mi.material = matSol;
|
|
@@ -314,17 +405,16 @@ export async function initializeViewer(config, instanceId) {
|
|
| 314 |
});
|
| 315 |
}
|
| 316 |
|
| 317 |
-
cameraEntity = new pc.Entity(
|
| 318 |
-
cameraEntity.addComponent(
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
});
|
| 324 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 325 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 326 |
-
cameraEntity.addComponent(
|
| 327 |
-
cameraEntity.script.create(
|
| 328 |
attributes: {
|
| 329 |
focusEntity: modelEntity,
|
| 330 |
inertiaFactor: 0.2,
|
|
@@ -338,32 +428,39 @@ export async function initializeViewer(config, instanceId) {
|
|
| 338 |
frameOnStart: false
|
| 339 |
}
|
| 340 |
});
|
| 341 |
-
cameraEntity.script.create(
|
| 342 |
-
cameraEntity.script.create(
|
| 343 |
-
cameraEntity.script.create(
|
| 344 |
attributes: {
|
| 345 |
forwardSpeed: 1.2, // vertical (relatif à la distance)
|
| 346 |
-
strafeSpeed: 1.2
|
| 347 |
}
|
| 348 |
});
|
| 349 |
app.root.addChild(cameraEntity);
|
| 350 |
|
| 351 |
-
app.resizeCanvas(
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
-
// Tooltips
|
| 355 |
try {
|
| 356 |
if (config.tooltips_url) {
|
| 357 |
-
import(
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
});
|
| 366 |
-
}).catch(() => { /* optional */ });
|
| 367 |
}
|
| 368 |
} catch (e) {
|
| 369 |
// optional
|
|
@@ -384,10 +481,9 @@ export function resetViewerCamera() {
|
|
| 384 |
tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 385 |
tempEnt.lookAt(modelPos);
|
| 386 |
|
| 387 |
-
const dist = new pc.Vec3()
|
| 388 |
-
new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ),
|
| 389 |
-
|
| 390 |
-
).length();
|
| 391 |
|
| 392 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 393 |
cameraEntity.lookAt(modelPos);
|
|
|
|
| 2 |
// ==============================
|
| 3 |
|
| 4 |
async function loadImageAsTexture(url, app) {
|
| 5 |
+
return new Promise((resolve, reject) => {
|
| 6 |
+
const img = new window.Image();
|
| 7 |
+
img.crossOrigin = "anonymous";
|
| 8 |
+
img.onload = function () {
|
| 9 |
+
const tex = new pc.Texture(app.graphicsDevice, {
|
| 10 |
+
width: img.width,
|
| 11 |
+
height: img.height,
|
| 12 |
+
format: pc.PIXELFORMAT_R8_G8_B8_A8
|
| 13 |
+
});
|
| 14 |
+
tex.setSource(img);
|
| 15 |
+
resolve(tex);
|
| 16 |
+
};
|
| 17 |
+
img.onerror = reject;
|
| 18 |
+
img.src = url;
|
| 19 |
+
});
|
| 20 |
}
|
| 21 |
|
| 22 |
// --- Patch all window.Image to force crossOrigin="anonymous" ---
|
| 23 |
+
(function () {
|
| 24 |
const OriginalImage = window.Image;
|
| 25 |
+
window.Image = function (...args) {
|
| 26 |
const img = new OriginalImage(...args);
|
| 27 |
img.crossOrigin = "anonymous";
|
| 28 |
return img;
|
|
|
|
| 36 |
if (hex.length !== 8) return [1, 1, 1, 1];
|
| 37 |
const num = parseInt(hex, 16);
|
| 38 |
return [
|
| 39 |
+
((num >> 24) & 0xff) / 255,
|
| 40 |
+
((num >> 16) & 0xff) / 255,
|
| 41 |
+
((num >> 8) & 0xff) / 255,
|
| 42 |
+
(num & 0xff) / 255
|
| 43 |
];
|
| 44 |
} catch (e) {
|
| 45 |
alert("hexToRgbaArray error: " + e);
|
|
|
|
| 49 |
|
| 50 |
// ----- Utility: Recursive scene traversal -----
|
| 51 |
function traverse(entity, callback) {
|
| 52 |
+
callback(entity);
|
| 53 |
+
if (entity.children) {
|
| 54 |
+
entity.children.forEach((child) => traverse(child, callback));
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
// ---------- Charger orbit-camera.js une seule fois globalement ----------
|
| 59 |
+
async function ensureOrbitScripts(app) {
|
| 60 |
+
if (window.__orbitScriptsLoaded) return;
|
| 61 |
+
if (window.__orbitScriptsLoadingPromise) {
|
| 62 |
+
try {
|
| 63 |
+
await window.__orbitScriptsLoadingPromise;
|
| 64 |
+
} catch (e) {}
|
| 65 |
+
return;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
window.__orbitScriptsLoadingPromise = new Promise((resolve, reject) => {
|
| 69 |
+
const asset = new pc.Asset(
|
| 70 |
+
"orbit-script-" + Math.random().toString(36).slice(2),
|
| 71 |
+
"script",
|
| 72 |
+
{ url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" }
|
| 73 |
+
);
|
| 74 |
+
app.assets.add(asset);
|
| 75 |
+
|
| 76 |
+
const done = () => {
|
| 77 |
+
window.__orbitScriptsLoaded = true;
|
| 78 |
+
resolve(true);
|
| 79 |
+
};
|
| 80 |
+
const fail = (e) => {
|
| 81 |
+
console.error("[viewer.js] orbit-camera.js failed to load", e);
|
| 82 |
+
reject(e);
|
| 83 |
+
};
|
| 84 |
+
|
| 85 |
+
asset.once("load", done);
|
| 86 |
+
asset.once("error", fail);
|
| 87 |
+
app.assets.load(asset);
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
try {
|
| 91 |
+
await window.__orbitScriptsLoadingPromise;
|
| 92 |
+
} catch (e) {
|
| 93 |
+
// laisser l'app continuer sans scripts (mais la caméra ne marchera pas)
|
| 94 |
+
}
|
| 95 |
}
|
| 96 |
|
| 97 |
let pc;
|
|
|
|
| 112 |
let color_bg_hex, color_bg, espace_expo_bool;
|
| 113 |
|
| 114 |
export async function initializeViewer(config, instanceId) {
|
| 115 |
+
// Ce module est importé avec un query param unique par interface.js,
|
| 116 |
+
// donc 1 instance par import. On garde tout de même ce garde-fou.
|
| 117 |
if (viewerInitialized) return;
|
| 118 |
|
| 119 |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
|
|
|
|
| 121 |
|
| 122 |
//prends les differents arguments de config.json
|
| 123 |
sogsUrl = config.sogs_json_url;
|
| 124 |
+
glbUrl =
|
| 125 |
+
config.glb_url !== undefined
|
| 126 |
+
? config.glb_url
|
| 127 |
+
: "https://huggingface.co/datasets/MikaFil/viewer_gs/resolve/main/ressources/espace_expo/sol_blanc_2.glb";
|
| 128 |
+
presentoirUrl =
|
| 129 |
+
config.presentoir_url !== undefined
|
| 130 |
+
? config.presentoir_url
|
| 131 |
+
: "https://huggingface.co/datasets/MikaFil/viewer_gs/resolve/main/ressources/espace_expo/sol_blanc_2.glb";
|
| 132 |
minZoom = parseFloat(config.minZoom || "1");
|
| 133 |
maxZoom = parseFloat(config.maxZoom || "20");
|
| 134 |
minAngle = parseFloat(config.minAngle || "-45");
|
| 135 |
maxAngle = parseFloat(config.maxAngle || "90");
|
| 136 |
+
minAzimuth =
|
| 137 |
+
config.minAzimuth !== undefined ? parseFloat(config.minAzimuth) : -360;
|
| 138 |
+
maxAzimuth =
|
| 139 |
+
config.maxAzimuth !== undefined ? parseFloat(config.maxAzimuth) : 360;
|
| 140 |
minPivotY = parseFloat(config.minPivotY || "0");
|
| 141 |
+
minY = config.minY !== undefined ? parseFloat(config.minY) : 0;
|
| 142 |
+
|
| 143 |
+
modelX = config.modelX !== undefined ? parseFloat(config.modelX) : 0;
|
| 144 |
+
modelY = config.modelY !== undefined ? parseFloat(config.modelY) : 0;
|
| 145 |
+
modelZ = config.modelZ !== undefined ? parseFloat(config.modelZ) : 0;
|
| 146 |
+
modelScale = config.modelScale !== undefined ? parseFloat(config.modelScale) : 1;
|
| 147 |
+
modelRotationX =
|
| 148 |
+
config.modelRotationX !== undefined ? parseFloat(config.modelRotationX) : 0;
|
| 149 |
+
modelRotationY =
|
| 150 |
+
config.modelRotationY !== undefined ? parseFloat(config.modelRotationY) : 0;
|
| 151 |
+
modelRotationZ =
|
| 152 |
+
config.modelRotationZ !== undefined ? parseFloat(config.modelRotationZ) : 0;
|
| 153 |
+
|
| 154 |
+
presentoirScaleX =
|
| 155 |
+
config.presentoirScaleX !== undefined ? parseFloat(config.presentoirScaleX) : 0;
|
| 156 |
+
presentoirScaleY =
|
| 157 |
+
config.presentoirScaleY !== undefined ? parseFloat(config.presentoirScaleY) : 0;
|
| 158 |
+
presentoirScaleZ =
|
| 159 |
+
config.presentoirScaleZ !== undefined ? parseFloat(config.presentoirScaleZ) : 0;
|
| 160 |
+
|
| 161 |
+
const cameraX = config.cameraX !== undefined ? parseFloat(config.cameraX) : 0;
|
| 162 |
+
const cameraY = config.cameraY !== undefined ? parseFloat(config.cameraY) : 2;
|
| 163 |
+
const cameraZ = config.cameraZ !== undefined ? parseFloat(config.cameraZ) : 5;
|
| 164 |
+
const cameraXPhone =
|
| 165 |
+
config.cameraXPhone !== undefined ? parseFloat(config.cameraXPhone) : cameraX;
|
| 166 |
+
const cameraYPhone =
|
| 167 |
+
config.cameraYPhone !== undefined ? parseFloat(config.cameraYPhone) : cameraY;
|
| 168 |
+
const cameraZPhone =
|
| 169 |
+
config.cameraZPhone !== undefined ? parseFloat(config.cameraZPhone) : cameraZ * 1.5;
|
| 170 |
+
|
| 171 |
+
color_bg_hex =
|
| 172 |
+
config.canvas_background !== undefined ? config.canvas_background : "#FFFFFF";
|
| 173 |
+
espace_expo_bool =
|
| 174 |
+
config.espace_expo_bool !== undefined ? config.espace_expo_bool : false;
|
| 175 |
color_bg = hexToRgbaArray(color_bg_hex);
|
| 176 |
|
| 177 |
chosenCameraX = isMobile ? cameraXPhone : cameraX;
|
|
|
|
| 179 |
chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
|
| 180 |
|
| 181 |
//cree le nouveau canva (instanceId est une valeur aléatoire pour gérer plusieurs canvas sur la meme page)
|
| 182 |
+
const canvasId = "canvas-" + instanceId;
|
| 183 |
+
const progressDialog = document.getElementById("progress-dialog-" + instanceId);
|
| 184 |
+
const progressIndicator = document.getElementById("progress-indicator-" + instanceId);
|
| 185 |
+
const viewerContainer = document.getElementById("viewer-container-" + instanceId);
|
| 186 |
|
| 187 |
let oldCanvas = document.getElementById(canvasId);
|
| 188 |
if (oldCanvas) oldCanvas.remove();
|
| 189 |
|
| 190 |
+
const canvas = document.createElement("canvas");
|
| 191 |
canvas.id = canvasId;
|
| 192 |
+
canvas.className = "ply-canvas";
|
| 193 |
canvas.style.width = "100%";
|
| 194 |
canvas.style.height = "100%";
|
| 195 |
+
canvas.setAttribute("tabindex", "0");
|
| 196 |
viewerContainer.insertBefore(canvas, progressDialog);
|
| 197 |
|
| 198 |
canvas.style.touchAction = "none";
|
| 199 |
canvas.style.webkitTouchCallout = "none";
|
| 200 |
+
canvas.addEventListener("gesturestart", (e) => e.preventDefault());
|
| 201 |
+
canvas.addEventListener("gesturechange", (e) => e.preventDefault());
|
| 202 |
+
canvas.addEventListener("gestureend", (e) => e.preventDefault());
|
| 203 |
+
canvas.addEventListener("dblclick", (e) => e.preventDefault());
|
| 204 |
+
canvas.addEventListener(
|
| 205 |
+
"touchstart",
|
| 206 |
+
(e) => {
|
| 207 |
+
if (e.touches.length > 1) e.preventDefault();
|
| 208 |
+
},
|
| 209 |
+
{ passive: false }
|
| 210 |
+
);
|
| 211 |
+
canvas.addEventListener(
|
| 212 |
+
"wheel",
|
| 213 |
+
(e) => {
|
| 214 |
+
e.preventDefault();
|
| 215 |
+
},
|
| 216 |
+
{ passive: false }
|
| 217 |
+
);
|
| 218 |
|
| 219 |
// --- Clavier : bloquer le scroll seulement quand le pointeur est au-dessus du canvas ---
|
| 220 |
const scrollKeys = new Set([
|
| 221 |
+
"ArrowUp",
|
| 222 |
+
"ArrowDown",
|
| 223 |
+
"ArrowLeft",
|
| 224 |
+
"ArrowRight",
|
| 225 |
+
"PageUp",
|
| 226 |
+
"PageDown",
|
| 227 |
+
"Home",
|
| 228 |
+
"End",
|
| 229 |
+
" ",
|
| 230 |
+
"Space",
|
| 231 |
+
"Spacebar"
|
| 232 |
]);
|
|
|
|
| 233 |
let isPointerOverCanvas = false;
|
|
|
|
| 234 |
const focusCanvas = () => canvas.focus({ preventScroll: true });
|
| 235 |
|
| 236 |
const onPointerEnter = () => {
|
| 237 |
isPointerOverCanvas = true;
|
| 238 |
focusCanvas();
|
| 239 |
};
|
|
|
|
| 240 |
const onPointerLeave = () => {
|
| 241 |
isPointerOverCanvas = false;
|
| 242 |
// rendre immédiatement le scroll à la page sans recliquer
|
| 243 |
+
if (document.activeElement === canvas) canvas.blur();
|
|
|
|
|
|
|
| 244 |
};
|
| 245 |
|
| 246 |
+
canvas.addEventListener("pointerenter", onPointerEnter);
|
| 247 |
+
canvas.addEventListener("pointerleave", onPointerLeave);
|
| 248 |
+
canvas.addEventListener("mouseenter", onPointerEnter);
|
| 249 |
+
canvas.addEventListener("mouseleave", onPointerLeave);
|
| 250 |
+
|
| 251 |
+
canvas.addEventListener("mousedown", focusCanvas);
|
| 252 |
+
canvas.addEventListener(
|
| 253 |
+
"touchstart",
|
| 254 |
+
() => {
|
| 255 |
+
focusCanvas();
|
| 256 |
+
},
|
| 257 |
+
{ passive: false }
|
| 258 |
+
);
|
| 259 |
+
|
| 260 |
+
const onCanvasBlur = () => {
|
| 261 |
+
isPointerOverCanvas = false;
|
| 262 |
+
};
|
| 263 |
+
canvas.addEventListener("blur", onCanvasBlur);
|
| 264 |
|
|
|
|
| 265 |
const onKeyDownCapture = (e) => {
|
| 266 |
if (!isPointerOverCanvas) return;
|
| 267 |
if (scrollKeys.has(e.key) || scrollKeys.has(e.code)) {
|
| 268 |
e.preventDefault(); // bloque le scroll page
|
| 269 |
+
// ne pas stopPropagation -> PlayCanvas reçoit bien les touches
|
| 270 |
}
|
| 271 |
};
|
| 272 |
+
window.addEventListener("keydown", onKeyDownCapture, true);
|
| 273 |
|
| 274 |
+
progressDialog.style.display = "block";
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
if (!pc) {
|
| 277 |
pc = await import("https://esm.run/playcanvas");
|
|
|
|
| 291 |
opts.graphicsDevice = device;
|
| 292 |
opts.mouse = new pc.Mouse(canvas);
|
| 293 |
opts.touch = new pc.TouchDevice(canvas);
|
| 294 |
+
opts.keyboard = new pc.Keyboard(canvas); // clavier attaché au canvas
|
| 295 |
opts.componentSystems = [
|
| 296 |
pc.RenderComponentSystem,
|
| 297 |
pc.CameraComponentSystem,
|
|
|
|
| 312 |
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 313 |
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 314 |
|
| 315 |
+
resizeObserver = new ResizeObserver((entries) => {
|
| 316 |
+
entries.forEach((entry) => {
|
| 317 |
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
| 318 |
});
|
| 319 |
});
|
| 320 |
resizeObserver.observe(viewerContainer);
|
| 321 |
|
| 322 |
+
window.addEventListener("resize", () =>
|
| 323 |
+
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight)
|
| 324 |
+
);
|
| 325 |
|
| 326 |
// Nettoyage complet à la destruction
|
| 327 |
+
app.on("destroy", () => {
|
| 328 |
resizeObserver.disconnect();
|
| 329 |
if (opts.keyboard && opts.keyboard.detach) opts.keyboard.detach();
|
| 330 |
|
| 331 |
+
window.removeEventListener("keydown", onKeyDownCapture, true);
|
| 332 |
|
| 333 |
+
canvas.removeEventListener("pointerenter", onPointerEnter);
|
| 334 |
+
canvas.removeEventListener("pointerleave", onPointerLeave);
|
| 335 |
+
canvas.removeEventListener("mouseenter", onPointerEnter);
|
| 336 |
+
canvas.removeEventListener("mouseleave", onPointerLeave);
|
| 337 |
+
canvas.removeEventListener("mousedown", focusCanvas);
|
| 338 |
+
canvas.removeEventListener("touchstart", focusCanvas);
|
| 339 |
+
canvas.removeEventListener("blur", onCanvasBlur);
|
| 340 |
});
|
| 341 |
|
| 342 |
// Assets after app exists
|
| 343 |
+
// ⚠️ On NE charge PAS orbit-camera.js via AssetListLoader pour éviter les
|
| 344 |
+
// redéfinitions multiples de scripts. On l'assure via ensureOrbitScripts().
|
| 345 |
const assets = {
|
| 346 |
+
sogs: new pc.Asset("gsplat", "gsplat", { url: sogsUrl }),
|
| 347 |
+
glb: new pc.Asset("glb", "container", { url: glbUrl }),
|
| 348 |
+
presentoir: new pc.Asset("presentoir", "container", { url: presentoirUrl })
|
|
|
|
| 349 |
};
|
| 350 |
|
| 351 |
for (const key in assets) app.assets.add(assets[key]);
|
| 352 |
|
| 353 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 354 |
+
loader.load(async () => {
|
| 355 |
+
// Charger les scripts orbit (une seule fois globalement)
|
| 356 |
+
await ensureOrbitScripts(app);
|
| 357 |
+
|
| 358 |
app.start();
|
| 359 |
+
progressDialog.style.display = "none";
|
| 360 |
|
| 361 |
+
modelEntity = new pc.Entity("model");
|
| 362 |
+
modelEntity.addComponent("gsplat", { asset: assets.sogs });
|
| 363 |
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 364 |
+
modelEntity.setLocalEulerAngles(
|
| 365 |
+
modelRotationX,
|
| 366 |
+
modelRotationY,
|
| 367 |
+
modelRotationZ
|
| 368 |
+
);
|
| 369 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 370 |
app.root.addChild(modelEntity);
|
| 371 |
|
|
|
|
| 373 |
app.root.addChild(glbEntity);
|
| 374 |
|
| 375 |
const presentoirEntity = assets.presentoir.resource.instantiateRenderEntity();
|
| 376 |
+
presentoirEntity.setLocalScale(
|
| 377 |
+
presentoirScaleX,
|
| 378 |
+
presentoirScaleY,
|
| 379 |
+
presentoirScaleZ
|
| 380 |
+
);
|
| 381 |
app.root.addChild(presentoirEntity);
|
| 382 |
|
| 383 |
if (!espace_expo_bool) {
|
|
|
|
| 388 |
matSol.useLighting = false;
|
| 389 |
matSol.update();
|
| 390 |
|
| 391 |
+
traverse(presentoirEntity, (node) => {
|
| 392 |
if (node.render && node.render.meshInstances) {
|
| 393 |
for (let mi of node.render.meshInstances) {
|
| 394 |
mi.material = matSol;
|
|
|
|
| 396 |
}
|
| 397 |
});
|
| 398 |
|
| 399 |
+
traverse(glbEntity, (node) => {
|
| 400 |
if (node.render && node.render.meshInstances) {
|
| 401 |
for (let mi of node.render.meshInstances) {
|
| 402 |
mi.material = matSol;
|
|
|
|
| 405 |
});
|
| 406 |
}
|
| 407 |
|
| 408 |
+
cameraEntity = new pc.Entity("camera");
|
| 409 |
+
cameraEntity.addComponent("camera", {
|
| 410 |
+
clearColor: new pc.Color(color_bg),
|
| 411 |
+
nearClip: 0.001,
|
| 412 |
+
farClip: 100
|
| 413 |
+
});
|
|
|
|
| 414 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 415 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 416 |
+
cameraEntity.addComponent("script");
|
| 417 |
+
cameraEntity.script.create("orbitCamera", {
|
| 418 |
attributes: {
|
| 419 |
focusEntity: modelEntity,
|
| 420 |
inertiaFactor: 0.2,
|
|
|
|
| 428 |
frameOnStart: false
|
| 429 |
}
|
| 430 |
});
|
| 431 |
+
cameraEntity.script.create("orbitCameraInputMouse");
|
| 432 |
+
cameraEntity.script.create("orbitCameraInputTouch");
|
| 433 |
+
cameraEntity.script.create("orbitCameraInputKeyboard", {
|
| 434 |
attributes: {
|
| 435 |
forwardSpeed: 1.2, // vertical (relatif à la distance)
|
| 436 |
+
strafeSpeed: 1.2 // gauche/droite (relatif à la distance)
|
| 437 |
}
|
| 438 |
});
|
| 439 |
app.root.addChild(cameraEntity);
|
| 440 |
|
| 441 |
+
app.resizeCanvas(
|
| 442 |
+
viewerContainer.clientWidth,
|
| 443 |
+
viewerContainer.clientHeight
|
| 444 |
+
);
|
| 445 |
+
app.once("update", () => resetViewerCamera());
|
| 446 |
|
| 447 |
+
// Tooltips support
|
| 448 |
try {
|
| 449 |
if (config.tooltips_url) {
|
| 450 |
+
import("./tooltips.js")
|
| 451 |
+
.then((tooltipsModule) => {
|
| 452 |
+
tooltipsModule.initializeTooltips({
|
| 453 |
+
app,
|
| 454 |
+
cameraEntity,
|
| 455 |
+
modelEntity,
|
| 456 |
+
tooltipsUrl: config.tooltips_url,
|
| 457 |
+
defaultVisible: !!config.showTooltipsDefault,
|
| 458 |
+
moveDuration: config.tooltipMoveDuration || 0.6
|
| 459 |
+
});
|
| 460 |
+
})
|
| 461 |
+
.catch(() => {
|
| 462 |
+
/* optional */
|
| 463 |
});
|
|
|
|
| 464 |
}
|
| 465 |
} catch (e) {
|
| 466 |
// optional
|
|
|
|
| 481 |
tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 482 |
tempEnt.lookAt(modelPos);
|
| 483 |
|
| 484 |
+
const dist = new pc.Vec3()
|
| 485 |
+
.sub2(new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ), modelPos)
|
| 486 |
+
.length();
|
|
|
|
| 487 |
|
| 488 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 489 |
cameraEntity.lookAt(modelPos);
|