Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
// viewer.js
|
| 2 |
// ==============================
|
| 3 |
|
| 4 |
-
let pc;
|
| 5 |
export let app = null;
|
| 6 |
-
let cameraEntity
|
| 7 |
-
let modelEntity
|
| 8 |
let viewerInitialized = false;
|
| 9 |
let resizeObserver = null;
|
| 10 |
|
|
@@ -13,38 +13,34 @@ let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, min
|
|
| 13 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 14 |
let plyUrl, glbUrl;
|
| 15 |
|
| 16 |
-
/**
|
| 17 |
-
* initializeViewer(config, instanceId)
|
| 18 |
-
*/
|
| 19 |
export async function initializeViewer(config, instanceId) {
|
| 20 |
if (viewerInitialized) return;
|
| 21 |
|
| 22 |
-
const isIOS
|
| 23 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 24 |
|
| 25 |
-
// 1. Read config
|
| 26 |
plyUrl = config.ply_url;
|
| 27 |
glbUrl = config.glb_url;
|
| 28 |
-
minZoom
|
| 29 |
-
maxZoom
|
| 30 |
-
minAngle
|
| 31 |
-
maxAngle
|
| 32 |
-
minAzimuth
|
| 33 |
-
maxAzimuth
|
| 34 |
-
minPivotY
|
| 35 |
-
minY
|
| 36 |
-
|
| 37 |
-
modelX
|
| 38 |
-
modelY
|
| 39 |
-
modelZ
|
| 40 |
-
modelScale
|
| 41 |
modelRotationX = (config.modelRotationX !== undefined) ? parseFloat(config.modelRotationX) : 0;
|
| 42 |
modelRotationY = (config.modelRotationY !== undefined) ? parseFloat(config.modelRotationY) : 0;
|
| 43 |
modelRotationZ = (config.modelRotationZ !== undefined) ? parseFloat(config.modelRotationZ) : 0;
|
| 44 |
|
| 45 |
-
const cameraX
|
| 46 |
-
const cameraY
|
| 47 |
-
const cameraZ
|
| 48 |
const cameraXPhone = (config.cameraXPhone !== undefined) ? parseFloat(config.cameraXPhone) : cameraX;
|
| 49 |
const cameraYPhone = (config.cameraYPhone !== undefined) ? parseFloat(config.cameraYPhone) : cameraY;
|
| 50 |
const cameraZPhone = (config.cameraZPhone !== undefined) ? parseFloat(config.cameraZPhone) : (cameraZ * 1.5);
|
|
@@ -53,304 +49,120 @@ export async function initializeViewer(config, instanceId) {
|
|
| 53 |
chosenCameraY = isMobile ? cameraYPhone : cameraY;
|
| 54 |
chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
|
| 55 |
|
| 56 |
-
|
| 57 |
-
const
|
| 58 |
-
const progressDialog = document.getElementById('progress-dialog-' + instanceId);
|
| 59 |
const progressIndicator = document.getElementById('progress-indicator-' + instanceId);
|
| 60 |
-
const viewerContainer
|
| 61 |
|
| 62 |
-
// 3. Create <canvas>
|
| 63 |
let oldCanvas = document.getElementById(canvasId);
|
| 64 |
if (oldCanvas) oldCanvas.remove();
|
|
|
|
| 65 |
const canvas = document.createElement('canvas');
|
| 66 |
canvas.id = canvasId;
|
| 67 |
canvas.className = 'ply-canvas';
|
| 68 |
-
canvas.style.
|
|
|
|
|
|
|
| 69 |
viewerContainer.insertBefore(canvas, progressDialog);
|
| 70 |
|
| 71 |
-
|
| 72 |
-
canvas.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
} else {
|
| 79 |
-
orbitCam.orthoHeight -= e.deltaY * 0.01 * sens * (orbitCam.orthoHeight * 0.1);
|
| 80 |
-
}
|
| 81 |
-
e.preventDefault();
|
| 82 |
-
e.stopPropagation();
|
| 83 |
-
}
|
| 84 |
-
}, { passive: false });
|
| 85 |
|
| 86 |
progressDialog.style.display = 'block';
|
| 87 |
|
| 88 |
-
// 5. Import PlayCanvas
|
| 89 |
if (!pc) {
|
| 90 |
pc = await import("https://esm.run/playcanvas");
|
| 91 |
window.pc = pc;
|
| 92 |
}
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
resizeObserver = new ResizeObserver(entries => {
|
| 130 |
-
for (const entry of entries) {
|
| 131 |
-
const { width, height } = entry.contentRect;
|
| 132 |
-
if (app) {
|
| 133 |
-
app.resizeCanvas(width, height);
|
| 134 |
-
}
|
| 135 |
-
}
|
| 136 |
-
});
|
| 137 |
-
resizeObserver.observe(viewerContainer);
|
| 138 |
-
|
| 139 |
-
window.addEventListener('resize', () => {
|
| 140 |
-
if (app) app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 141 |
-
});
|
| 142 |
-
|
| 143 |
-
app.on('destroy', () => {
|
| 144 |
-
window.removeEventListener('resize', resizeCanvas);
|
| 145 |
});
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
}
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
modelEntity = new pc.Entity('model');
|
| 181 |
-
modelEntity.addComponent('gsplat', { asset: assets.model });
|
| 182 |
-
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 183 |
-
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 184 |
-
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 185 |
-
app.root.addChild(modelEntity);
|
| 186 |
-
|
| 187 |
-
// Light
|
| 188 |
-
const dirLight = new pc.Entity('Cascaded Light');
|
| 189 |
-
dirLight.addComponent('light', {
|
| 190 |
-
type: 'directional',
|
| 191 |
-
color: pc.Color.WHITE,
|
| 192 |
-
shadowBias: 0.3,
|
| 193 |
-
normalOffsetBias: 0.2,
|
| 194 |
-
intensity: 1.0,
|
| 195 |
-
soft: true,
|
| 196 |
-
shadowResolution: 4096,
|
| 197 |
-
penumbraSize: 7,
|
| 198 |
-
penumbraFalloff: 1.5,
|
| 199 |
-
shadowSamples: 128,
|
| 200 |
-
shadowBlockerSamples: 16,
|
| 201 |
-
castShadows: true,
|
| 202 |
-
shadowType: pc.SHADOW_PCSS_32F,
|
| 203 |
-
shadowDistance: 1000
|
| 204 |
-
});
|
| 205 |
-
dirLight.setLocalEulerAngles(0, 0, 0);
|
| 206 |
-
app.root.addChild(dirLight);
|
| 207 |
-
|
| 208 |
-
// Gallery GLB
|
| 209 |
-
if (assets.galerie && assets.galerie.resource && assets.galerie.resource.instantiateRenderEntity) {
|
| 210 |
-
const galleryEntity = assets.galerie.resource.instantiateRenderEntity();
|
| 211 |
-
app.root.addChild(galleryEntity);
|
| 212 |
-
}
|
| 213 |
-
|
| 214 |
-
// Camera setup
|
| 215 |
-
cameraEntity = new pc.Entity('camera');
|
| 216 |
-
cameraEntity.addComponent('camera', {
|
| 217 |
-
clearColor: config.canvas_background
|
| 218 |
-
? parseInt(config.canvas_background.substr(1, 2), 16) / 255
|
| 219 |
-
: 0
|
| 220 |
-
});
|
| 221 |
-
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 222 |
-
cameraEntity.lookAt(modelEntity.getPosition());
|
| 223 |
-
|
| 224 |
-
cameraEntity.addComponent('script');
|
| 225 |
-
cameraEntity.script.create('orbitCamera', {
|
| 226 |
-
attributes: {
|
| 227 |
-
inertiaFactor: 0.2,
|
| 228 |
-
focusEntity: modelEntity,
|
| 229 |
-
distanceMax: maxZoom,
|
| 230 |
-
distanceMin: minZoom,
|
| 231 |
-
pitchAngleMax: maxAngle,
|
| 232 |
-
pitchAngleMin: minAngle,
|
| 233 |
-
yawAngleMax: maxAzimuth,
|
| 234 |
-
yawAngleMin: minAzimuth,
|
| 235 |
-
minPivotY: minPivotY,
|
| 236 |
-
frameOnStart: false
|
| 237 |
-
}
|
| 238 |
-
});
|
| 239 |
-
cameraEntity.script.create('orbitCameraInputMouse', {
|
| 240 |
-
attributes: {
|
| 241 |
-
orbitSensitivity: isMobile ? 0.6 : 0.3,
|
| 242 |
-
distanceSensitivity: isMobile ? 0.5 : 0.4
|
| 243 |
-
}
|
| 244 |
-
});
|
| 245 |
-
if (cameraEntity.script.orbitCameraInputMouse) {
|
| 246 |
-
cameraEntity.script.orbitCameraInputMouse.onMouseWheel = function() {};
|
| 247 |
-
}
|
| 248 |
-
cameraEntity.script.create('orbitCameraInputTouch', {
|
| 249 |
-
attributes: {
|
| 250 |
-
orbitSensitivity: 0.6,
|
| 251 |
-
distanceSensitivity: 0.5
|
| 252 |
-
}
|
| 253 |
-
});
|
| 254 |
-
app.root.addChild(cameraEntity);
|
| 255 |
-
|
| 256 |
-
// Reset & constrain updates
|
| 257 |
-
app.once('update', () => resetViewerCamera());
|
| 258 |
-
app.on('update', dt => {
|
| 259 |
-
if (cameraEntity) {
|
| 260 |
-
const pos = cameraEntity.getPosition();
|
| 261 |
-
if (pos.y < minY) {
|
| 262 |
-
cameraEntity.setPosition(pos.x, minY, pos.z);
|
| 263 |
-
}
|
| 264 |
-
}
|
| 265 |
-
});
|
| 266 |
-
|
| 267 |
-
// Final resize
|
| 268 |
-
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 269 |
-
|
| 270 |
-
// Tooltips
|
| 271 |
-
try {
|
| 272 |
-
const tooltipsModule = await import('./tooltips.js');
|
| 273 |
-
tooltipsModule.initializeTooltips({
|
| 274 |
-
app,
|
| 275 |
-
cameraEntity,
|
| 276 |
-
modelEntity,
|
| 277 |
-
tooltipsUrl: config.tooltips_url,
|
| 278 |
-
defaultVisible: !!config.showTooltipsDefault,
|
| 279 |
-
moveDuration: config.tooltipMoveDuration || 0.6
|
| 280 |
-
});
|
| 281 |
-
} catch (e) {
|
| 282 |
-
// Tooltips are optional
|
| 283 |
}
|
| 284 |
-
|
| 285 |
-
progressDialog.style.display = 'none';
|
| 286 |
-
viewerInitialized = true;
|
| 287 |
});
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
}
|
| 292 |
}
|
| 293 |
|
| 294 |
export function resetViewerCamera() {
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
if (!orbitCam) return;
|
| 299 |
-
|
| 300 |
-
const modelPos = modelEntity.getPosition();
|
| 301 |
-
const tempEnt = new pc.Entity();
|
| 302 |
-
tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 303 |
-
tempEnt.lookAt(modelPos);
|
| 304 |
-
|
| 305 |
-
const dist = new pc.Vec3().sub2(
|
| 306 |
-
new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ),
|
| 307 |
-
modelPos
|
| 308 |
-
).length();
|
| 309 |
-
|
| 310 |
-
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 311 |
-
cameraEntity.lookAt(modelPos);
|
| 312 |
-
|
| 313 |
-
orbitCam.pivotPoint = modelPos.clone();
|
| 314 |
-
orbitCam._targetDistance = dist;
|
| 315 |
-
orbitCam._distance = dist;
|
| 316 |
-
|
| 317 |
-
const rot = tempEnt.getRotation();
|
| 318 |
-
const fwd = new pc.Vec3();
|
| 319 |
-
rot.transformVector(pc.Vec3.FORWARD, fwd);
|
| 320 |
-
|
| 321 |
-
const yaw = Math.atan2(-fwd.x, -fwd.z) * pc.math.RAD_TO_DEG;
|
| 322 |
-
const yawQuat = new pc.Quat().setFromEulerAngles(0, -yaw, 0);
|
| 323 |
-
const rotNoYaw = new pc.Quat().mul2(yawQuat, rot);
|
| 324 |
-
const fNoYaw = new pc.Vec3();
|
| 325 |
-
rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
|
| 326 |
-
const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
|
| 327 |
-
|
| 328 |
-
orbitCam._targetYaw = yaw;
|
| 329 |
-
orbitCam._yaw = yaw;
|
| 330 |
-
orbitCam._targetPitch = pitch;
|
| 331 |
-
orbitCam._pitch = pitch;
|
| 332 |
-
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 333 |
-
|
| 334 |
-
tempEnt.destroy();
|
| 335 |
-
} catch (e) {
|
| 336 |
-
// Silent fail: resetting camera should never crash app
|
| 337 |
-
}
|
| 338 |
-
}
|
| 339 |
-
|
| 340 |
-
export function cleanupViewer() {
|
| 341 |
-
if (app) {
|
| 342 |
-
try {
|
| 343 |
-
app.destroy();
|
| 344 |
-
} catch {}
|
| 345 |
-
app = null;
|
| 346 |
-
}
|
| 347 |
-
cameraEntity = null;
|
| 348 |
-
modelEntity = null;
|
| 349 |
-
viewerInitialized = false;
|
| 350 |
-
|
| 351 |
-
// Disconnect the ResizeObserver to avoid leaks
|
| 352 |
-
if (resizeObserver) {
|
| 353 |
-
resizeObserver.disconnect();
|
| 354 |
-
resizeObserver = null;
|
| 355 |
-
}
|
| 356 |
}
|
|
|
|
| 1 |
// viewer.js
|
| 2 |
// ==============================
|
| 3 |
|
| 4 |
+
let pc;
|
| 5 |
export let app = null;
|
| 6 |
+
let cameraEntity = null;
|
| 7 |
+
let modelEntity = null;
|
| 8 |
let viewerInitialized = false;
|
| 9 |
let resizeObserver = null;
|
| 10 |
|
|
|
|
| 13 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 14 |
let plyUrl, glbUrl;
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
export async function initializeViewer(config, instanceId) {
|
| 17 |
if (viewerInitialized) return;
|
| 18 |
|
| 19 |
+
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
|
| 20 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 21 |
|
|
|
|
| 22 |
plyUrl = config.ply_url;
|
| 23 |
glbUrl = config.glb_url;
|
| 24 |
+
minZoom = parseFloat(config.minZoom || "1");
|
| 25 |
+
maxZoom = parseFloat(config.maxZoom || "20");
|
| 26 |
+
minAngle = parseFloat(config.minAngle || "-45");
|
| 27 |
+
maxAngle = parseFloat(config.maxAngle || "90");
|
| 28 |
+
minAzimuth = (config.minAzimuth !== undefined) ? parseFloat(config.minAzimuth) : -360;
|
| 29 |
+
maxAzimuth = (config.maxAzimuth !== undefined) ? parseFloat(config.maxAzimuth) : 360;
|
| 30 |
+
minPivotY = parseFloat(config.minPivotY || "0");
|
| 31 |
+
minY = (config.minY !== undefined) ? parseFloat(config.minY) : 0;
|
| 32 |
+
|
| 33 |
+
modelX = (config.modelX !== undefined) ? parseFloat(config.modelX) : 0;
|
| 34 |
+
modelY = (config.modelY !== undefined) ? parseFloat(config.modelY) : 0;
|
| 35 |
+
modelZ = (config.modelZ !== undefined) ? parseFloat(config.modelZ) : 0;
|
| 36 |
+
modelScale = (config.modelScale !== undefined) ? parseFloat(config.modelScale) : 1;
|
| 37 |
modelRotationX = (config.modelRotationX !== undefined) ? parseFloat(config.modelRotationX) : 0;
|
| 38 |
modelRotationY = (config.modelRotationY !== undefined) ? parseFloat(config.modelRotationY) : 0;
|
| 39 |
modelRotationZ = (config.modelRotationZ !== undefined) ? parseFloat(config.modelRotationZ) : 0;
|
| 40 |
|
| 41 |
+
const cameraX = (config.cameraX !== undefined) ? parseFloat(config.cameraX) : 0;
|
| 42 |
+
const cameraY = (config.cameraY !== undefined) ? parseFloat(config.cameraY) : 2;
|
| 43 |
+
const cameraZ = (config.cameraZ !== undefined) ? parseFloat(config.cameraZ) : 5;
|
| 44 |
const cameraXPhone = (config.cameraXPhone !== undefined) ? parseFloat(config.cameraXPhone) : cameraX;
|
| 45 |
const cameraYPhone = (config.cameraYPhone !== undefined) ? parseFloat(config.cameraYPhone) : cameraY;
|
| 46 |
const cameraZPhone = (config.cameraZPhone !== undefined) ? parseFloat(config.cameraZPhone) : (cameraZ * 1.5);
|
|
|
|
| 49 |
chosenCameraY = isMobile ? cameraYPhone : cameraY;
|
| 50 |
chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
|
| 51 |
|
| 52 |
+
const canvasId = 'canvas-' + instanceId;
|
| 53 |
+
const progressDialog = document.getElementById('progress-dialog-' + instanceId);
|
|
|
|
| 54 |
const progressIndicator = document.getElementById('progress-indicator-' + instanceId);
|
| 55 |
+
const viewerContainer = document.getElementById('viewer-container-' + instanceId);
|
| 56 |
|
|
|
|
| 57 |
let oldCanvas = document.getElementById(canvasId);
|
| 58 |
if (oldCanvas) oldCanvas.remove();
|
| 59 |
+
|
| 60 |
const canvas = document.createElement('canvas');
|
| 61 |
canvas.id = canvasId;
|
| 62 |
canvas.className = 'ply-canvas';
|
| 63 |
+
canvas.style.width = "100%";
|
| 64 |
+
canvas.style.height = "100%";
|
| 65 |
+
canvas.setAttribute('tabindex', '0');
|
| 66 |
viewerContainer.insertBefore(canvas, progressDialog);
|
| 67 |
|
| 68 |
+
canvas.style.touchAction = "none";
|
| 69 |
+
canvas.style.webkitTouchCallout = "none";
|
| 70 |
+
canvas.addEventListener('gesturestart', e => e.preventDefault());
|
| 71 |
+
canvas.addEventListener('gesturechange', e => e.preventDefault());
|
| 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 |
progressDialog.style.display = 'block';
|
| 77 |
|
|
|
|
| 78 |
if (!pc) {
|
| 79 |
pc = await import("https://esm.run/playcanvas");
|
| 80 |
window.pc = pc;
|
| 81 |
}
|
| 82 |
|
| 83 |
+
const device = await pc.createGraphicsDevice(canvas, {
|
| 84 |
+
deviceTypes: ["webgl2"],
|
| 85 |
+
glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
|
| 86 |
+
twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
|
| 87 |
+
antialias: false
|
| 88 |
+
});
|
| 89 |
+
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 90 |
+
|
| 91 |
+
const opts = new pc.AppOptions();
|
| 92 |
+
opts.graphicsDevice = device;
|
| 93 |
+
opts.mouse = new pc.Mouse(document.body);
|
| 94 |
+
opts.touch = new pc.TouchDevice(document.body);
|
| 95 |
+
opts.componentSystems = [
|
| 96 |
+
pc.RenderComponentSystem,
|
| 97 |
+
pc.CameraComponentSystem,
|
| 98 |
+
pc.LightComponentSystem,
|
| 99 |
+
pc.ScriptComponentSystem,
|
| 100 |
+
pc.GSplatComponentSystem,
|
| 101 |
+
pc.CollisionComponentSystem,
|
| 102 |
+
pc.RigidbodyComponentSystem
|
| 103 |
+
];
|
| 104 |
+
opts.resourceHandlers = [
|
| 105 |
+
pc.TextureHandler,
|
| 106 |
+
pc.ContainerHandler,
|
| 107 |
+
pc.ScriptHandler,
|
| 108 |
+
pc.GSplatHandler
|
| 109 |
+
];
|
| 110 |
+
|
| 111 |
+
app = new pc.Application(canvas, opts);
|
| 112 |
+
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 113 |
+
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 114 |
+
|
| 115 |
+
resizeObserver = new ResizeObserver(entries => {
|
| 116 |
+
entries.forEach(entry => {
|
| 117 |
+
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
});
|
| 119 |
+
});
|
| 120 |
+
resizeObserver.observe(viewerContainer);
|
| 121 |
+
|
| 122 |
+
window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
|
| 123 |
+
app.on('destroy', () => resizeObserver.disconnect());
|
| 124 |
+
|
| 125 |
+
const assets = {
|
| 126 |
+
model: new pc.Asset('gsplat', 'gsplat', { url: plyUrl }),
|
| 127 |
+
orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
|
| 128 |
+
galerie: new pc.Asset('galerie', 'container', { url: glbUrl }),
|
| 129 |
+
hdr: new pc.Asset('hdr', 'texture', { url: "https://huggingface.co/datasets/bilca/ply_files/resolve/main/galeries/blanc.png" }, { type: pc.TEXTURETYPE_RGBP, mipmaps: false })
|
| 130 |
+
};
|
| 131 |
+
|
| 132 |
+
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 133 |
+
loader.load(() => {
|
| 134 |
+
app.start();
|
| 135 |
+
progressDialog.style.display = 'none';
|
| 136 |
+
|
| 137 |
+
modelEntity = new pc.Entity('model');
|
| 138 |
+
modelEntity.addComponent('gsplat', { asset: assets.model });
|
| 139 |
+
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 140 |
+
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 141 |
+
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 142 |
+
app.root.addChild(modelEntity);
|
| 143 |
+
|
| 144 |
+
cameraEntity = new pc.Entity('camera');
|
| 145 |
+
cameraEntity.addComponent('camera', { clearColor: new pc.Color(0.2, 0.2, 0.2, 1) });
|
| 146 |
+
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 147 |
+
cameraEntity.lookAt(modelEntity.getPosition());
|
| 148 |
+
cameraEntity.addComponent('script');
|
| 149 |
+
cameraEntity.script.create('orbitCamera', {
|
| 150 |
+
attributes: {
|
| 151 |
+
focusEntity: modelEntity, inertiaFactor: 0.2, distanceMax: maxZoom, distanceMin: minZoom,
|
| 152 |
+
pitchAngleMax: maxAngle, pitchAngleMin: minAngle, frameOnStart: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
}
|
|
|
|
|
|
|
|
|
|
| 154 |
});
|
| 155 |
+
cameraEntity.script.create('orbitCameraInputMouse');
|
| 156 |
+
cameraEntity.script.create('orbitCameraInputTouch');
|
| 157 |
+
app.root.addChild(cameraEntity);
|
| 158 |
|
| 159 |
+
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 160 |
+
viewerInitialized = true;
|
| 161 |
+
});
|
| 162 |
}
|
| 163 |
|
| 164 |
export function resetViewerCamera() {
|
| 165 |
+
if (!cameraEntity || !modelEntity || !app) return;
|
| 166 |
+
const orbitCam = cameraEntity.script.orbitCamera;
|
| 167 |
+
if (orbitCam) orbitCam.reset();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
}
|