Spaces:
Running
Running
Update deplacement_dans_env/viewer_pr_env.js
Browse files
deplacement_dans_env/viewer_pr_env.js
CHANGED
|
@@ -96,7 +96,8 @@ async function ensureOrbitScriptsLoaded() {
|
|
| 96 |
let pc;
|
| 97 |
export let app = null;
|
| 98 |
let cameraEntity = null;
|
| 99 |
-
let modelEntity = null;
|
|
|
|
| 100 |
let viewerInitialized = false;
|
| 101 |
let resizeObserver = null;
|
| 102 |
|
|
@@ -105,7 +106,7 @@ let chosenCameraX, chosenCameraY, chosenCameraZ;
|
|
| 105 |
let distanceMin, minAngle, maxAngle, minAzimuth, maxAzimuth, minY;
|
| 106 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 107 |
let presentoirScaleX, presentoirScaleY, presentoirScaleZ;
|
| 108 |
-
let sogsUrl, glbUrl
|
| 109 |
let color_bg_hex, color_bg, espace_expo_bool;
|
| 110 |
|
| 111 |
/* -------------------------------------------
|
|
@@ -120,7 +121,8 @@ export async function initializeViewer(config, instanceId) {
|
|
| 120 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 121 |
|
| 122 |
// --- Configuration ---
|
| 123 |
-
sogsUrl = config.sogs_json_url;
|
|
|
|
| 124 |
|
| 125 |
// plus de minZoom/maxZoom dans le JSON : on expose uniquement distanceMin (défaut 1)
|
| 126 |
distanceMin = config.minZoom !== undefined
|
|
@@ -143,9 +145,10 @@ export async function initializeViewer(config, instanceId) {
|
|
| 143 |
modelRotationY = config.modelRotationY !== undefined ? parseFloat(config.modelRotationY) : 0;
|
| 144 |
modelRotationZ = config.modelRotationZ !== undefined ? parseFloat(config.modelRotationZ) : 0;
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
| 149 |
|
| 150 |
const cameraX = config.cameraX !== undefined ? parseFloat(config.cameraX) : 0;
|
| 151 |
const cameraY = config.cameraY !== undefined ? parseFloat(config.cameraY) : 2;
|
|
@@ -319,9 +322,17 @@ export async function initializeViewer(config, instanceId) {
|
|
| 319 |
});
|
| 320 |
|
| 321 |
// --- Enregistre les assets (SAUF orbit script : chargé globalement) ---
|
| 322 |
-
const assets = {
|
| 323 |
-
|
| 324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
for (const k in assets) app.assets.add(assets[k]);
|
| 326 |
|
| 327 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
|
@@ -334,27 +345,48 @@ export async function initializeViewer(config, instanceId) {
|
|
| 334 |
progressDialog.style.display = "none";
|
| 335 |
|
| 336 |
// --- Modèle principal (gsplat) ---
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
// --- Caméra + scripts d’input ---
|
| 345 |
cameraEntity = new pc.Entity("camera");
|
| 346 |
cameraEntity.addComponent("camera", {
|
| 347 |
-
clearColor: new pc.Color(color_bg),
|
| 348 |
nearClip: 0.001,
|
| 349 |
farClip: 100
|
| 350 |
});
|
| 351 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 352 |
-
cameraEntity.lookAt(
|
| 353 |
cameraEntity.addComponent("script");
|
| 354 |
|
| 355 |
// Attributs orbitCamera — sans distanceMax, avec bbox optionnelle
|
| 356 |
const orbitAttrs = {
|
| 357 |
-
focusEntity:
|
| 358 |
inertiaFactor: 0.2,
|
| 359 |
distanceMin: distanceMin,
|
| 360 |
pitchAngleMax: maxAngle,
|
|
@@ -393,6 +425,8 @@ export async function initializeViewer(config, instanceId) {
|
|
| 393 |
app.root.addChild(cameraEntity);
|
| 394 |
|
| 395 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
|
|
|
|
|
|
| 396 |
app.once("update", () => resetViewerCamera());
|
| 397 |
|
| 398 |
// --- Tooltips (optionnels) ---
|
|
@@ -403,7 +437,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 403 |
tooltipsModule.initializeTooltips({
|
| 404 |
app,
|
| 405 |
cameraEntity,
|
| 406 |
-
modelEntity,
|
| 407 |
tooltipsUrl: config.tooltips_url,
|
| 408 |
defaultVisible: !!config.showTooltipsDefault,
|
| 409 |
moveDuration: config.tooltipMoveDuration || 0.6
|
|
@@ -427,23 +461,27 @@ export async function initializeViewer(config, instanceId) {
|
|
| 427 |
|
| 428 |
export function resetViewerCamera() {
|
| 429 |
try {
|
| 430 |
-
if (!cameraEntity || !
|
| 431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
if (!orbitCam) return;
|
| 433 |
|
| 434 |
-
const modelPos = modelEntity.getPosition();
|
| 435 |
const tempEnt = new pc.Entity();
|
| 436 |
tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 437 |
-
tempEnt.lookAt(
|
| 438 |
|
| 439 |
const dist = new pc.Vec3()
|
| 440 |
-
.sub2(new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ),
|
| 441 |
.length();
|
| 442 |
|
| 443 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 444 |
-
cameraEntity.lookAt(
|
| 445 |
|
| 446 |
-
orbitCam.pivotPoint =
|
| 447 |
orbitCam._targetDistance = Math.max(distanceMin, dist);
|
| 448 |
orbitCam._distance = Math.max(distanceMin, dist);
|
| 449 |
|
|
|
|
| 96 |
let pc;
|
| 97 |
export let app = null;
|
| 98 |
let cameraEntity = null;
|
| 99 |
+
let modelEntity = null; // gsplat principal
|
| 100 |
+
let envEntity = null; // scène GLB optionnelle (présentoir / environnement)
|
| 101 |
let viewerInitialized = false;
|
| 102 |
let resizeObserver = null;
|
| 103 |
|
|
|
|
| 106 |
let distanceMin, minAngle, maxAngle, minAzimuth, maxAzimuth, minY;
|
| 107 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 108 |
let presentoirScaleX, presentoirScaleY, presentoirScaleZ;
|
| 109 |
+
let sogsUrl, glbUrl;
|
| 110 |
let color_bg_hex, color_bg, espace_expo_bool;
|
| 111 |
|
| 112 |
/* -------------------------------------------
|
|
|
|
| 121 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 122 |
|
| 123 |
// --- Configuration ---
|
| 124 |
+
sogsUrl = config.sogs_json_url || null;
|
| 125 |
+
glbUrl = config.glb_url || null;
|
| 126 |
|
| 127 |
// plus de minZoom/maxZoom dans le JSON : on expose uniquement distanceMin (défaut 1)
|
| 128 |
distanceMin = config.minZoom !== undefined
|
|
|
|
| 145 |
modelRotationY = config.modelRotationY !== undefined ? parseFloat(config.modelRotationY) : 0;
|
| 146 |
modelRotationZ = config.modelRotationZ !== undefined ? parseFloat(config.modelRotationZ) : 0;
|
| 147 |
|
| 148 |
+
// défauts à 1 (et non 0) pour éviter l'invisibilité si le JSON n'en fournit pas
|
| 149 |
+
presentoirScaleX = config.presentoirScaleX !== undefined ? parseFloat(config.presentoirScaleX) : 1;
|
| 150 |
+
presentoirScaleY = config.presentoirScaleY !== undefined ? parseFloat(config.presentoirScaleY) : 1;
|
| 151 |
+
presentoirScaleZ = config.presentoirScaleZ !== undefined ? parseFloat(config.presentoirScaleZ) : 1;
|
| 152 |
|
| 153 |
const cameraX = config.cameraX !== undefined ? parseFloat(config.cameraX) : 0;
|
| 154 |
const cameraY = config.cameraY !== undefined ? parseFloat(config.cameraY) : 2;
|
|
|
|
| 322 |
});
|
| 323 |
|
| 324 |
// --- Enregistre les assets (SAUF orbit script : chargé globalement) ---
|
| 325 |
+
const assets = {};
|
| 326 |
+
|
| 327 |
+
if (sogsUrl) {
|
| 328 |
+
assets.sogs = new pc.Asset("gsplat", "gsplat", { url: sogsUrl });
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
// Ajoute le GLB d'environnement / présentoir si fourni
|
| 332 |
+
if (glbUrl) {
|
| 333 |
+
assets.env = new pc.Asset("env", "container", { url: glbUrl });
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
for (const k in assets) app.assets.add(assets[k]);
|
| 337 |
|
| 338 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
|
|
|
| 345 |
progressDialog.style.display = "none";
|
| 346 |
|
| 347 |
// --- Modèle principal (gsplat) ---
|
| 348 |
+
if (assets.sogs) {
|
| 349 |
+
modelEntity = new pc.Entity("model");
|
| 350 |
+
modelEntity.addComponent("gsplat", { asset: assets.sogs });
|
| 351 |
+
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 352 |
+
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 353 |
+
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 354 |
+
app.root.addChild(modelEntity);
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
// --- GLB Environnement / Présentoir (optionnel) ---
|
| 358 |
+
if (assets.env && assets.env.resource) {
|
| 359 |
+
const container = assets.env.resource; // pc.ContainerResource
|
| 360 |
+
envEntity = container.instantiateRenderEntity({
|
| 361 |
+
castShadows: false,
|
| 362 |
+
receiveShadows: true
|
| 363 |
+
});
|
| 364 |
+
|
| 365 |
+
// Position/rotation/échelle par défaut (adaptable selon besoin)
|
| 366 |
+
envEntity.setLocalPosition(0, 0, 0);
|
| 367 |
+
envEntity.setLocalEulerAngles(0, 0, 0);
|
| 368 |
+
envEntity.setLocalScale(presentoirScaleX, presentoirScaleY, presentoirScaleZ);
|
| 369 |
+
|
| 370 |
+
app.root.addChild(envEntity);
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
// Fallback: si pas de gsplat, mais un env GLB existe, utiliser l'env comme focus du viewer
|
| 374 |
+
const focusEntity = modelEntity || envEntity;
|
| 375 |
|
| 376 |
// --- Caméra + scripts d’input ---
|
| 377 |
cameraEntity = new pc.Entity("camera");
|
| 378 |
cameraEntity.addComponent("camera", {
|
| 379 |
+
clearColor: new pc.Color(color_bg[0], color_bg[1], color_bg[2], color_bg[3]),
|
| 380 |
nearClip: 0.001,
|
| 381 |
farClip: 100
|
| 382 |
});
|
| 383 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 384 |
+
if (focusEntity) cameraEntity.lookAt(focusEntity.getPosition());
|
| 385 |
cameraEntity.addComponent("script");
|
| 386 |
|
| 387 |
// Attributs orbitCamera — sans distanceMax, avec bbox optionnelle
|
| 388 |
const orbitAttrs = {
|
| 389 |
+
focusEntity: focusEntity || undefined,
|
| 390 |
inertiaFactor: 0.2,
|
| 391 |
distanceMin: distanceMin,
|
| 392 |
pitchAngleMax: maxAngle,
|
|
|
|
| 425 |
app.root.addChild(cameraEntity);
|
| 426 |
|
| 427 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 428 |
+
|
| 429 |
+
// Reset caméra une fois la première frame prête
|
| 430 |
app.once("update", () => resetViewerCamera());
|
| 431 |
|
| 432 |
// --- Tooltips (optionnels) ---
|
|
|
|
| 437 |
tooltipsModule.initializeTooltips({
|
| 438 |
app,
|
| 439 |
cameraEntity,
|
| 440 |
+
modelEntity: focusEntity, // focusEntity pour coller à ce qui est visible
|
| 441 |
tooltipsUrl: config.tooltips_url,
|
| 442 |
defaultVisible: !!config.showTooltipsDefault,
|
| 443 |
moveDuration: config.tooltipMoveDuration || 0.6
|
|
|
|
| 461 |
|
| 462 |
export function resetViewerCamera() {
|
| 463 |
try {
|
| 464 |
+
if (!cameraEntity || !app) return;
|
| 465 |
+
|
| 466 |
+
// Choisir la cible prioritaire : gsplat puis glb, sinon (0,0,0)
|
| 467 |
+
const targetEntity = modelEntity || envEntity;
|
| 468 |
+
const targetPos = targetEntity ? targetEntity.getPosition().clone() : new pc.Vec3(0, 0, 0);
|
| 469 |
+
|
| 470 |
+
const orbitCam = cameraEntity.script && cameraEntity.script.orbitCamera;
|
| 471 |
if (!orbitCam) return;
|
| 472 |
|
|
|
|
| 473 |
const tempEnt = new pc.Entity();
|
| 474 |
tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 475 |
+
tempEnt.lookAt(targetPos);
|
| 476 |
|
| 477 |
const dist = new pc.Vec3()
|
| 478 |
+
.sub2(new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ), targetPos)
|
| 479 |
.length();
|
| 480 |
|
| 481 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 482 |
+
cameraEntity.lookAt(targetPos);
|
| 483 |
|
| 484 |
+
orbitCam.pivotPoint = targetPos.clone();
|
| 485 |
orbitCam._targetDistance = Math.max(distanceMin, dist);
|
| 486 |
orbitCam._distance = Math.max(distanceMin, dist);
|
| 487 |
|