viewer_sgos / viewer.js
MikaFil's picture
Update viewer.js
5ad9c19 verified
raw
history blame
5.97 kB
// viewer.js
// ==============================
let pc;
export let app = null;
let cameraEntity = null;
let modelEntity = null;
let viewerInitialized = false;
let resizeObserver = null;
let chosenCameraX, chosenCameraY, chosenCameraZ;
let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
let plyUrl, glbUrl;
export async function initializeViewer(config, instanceId) {
if (viewerInitialized) return;
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
plyUrl = config.ply_url;
glbUrl = config.glb_url;
minZoom = parseFloat(config.minZoom || "1");
maxZoom = parseFloat(config.maxZoom || "20");
minAngle = parseFloat(config.minAngle || "-45");
maxAngle = parseFloat(config.maxAngle || "90");
minAzimuth = (config.minAzimuth !== undefined) ? parseFloat(config.minAzimuth) : -360;
maxAzimuth = (config.maxAzimuth !== undefined) ? parseFloat(config.maxAzimuth) : 360;
minPivotY = parseFloat(config.minPivotY || "0");
minY = (config.minY !== undefined) ? parseFloat(config.minY) : 0;
modelX = (config.modelX !== undefined) ? parseFloat(config.modelX) : 0;
modelY = (config.modelY !== undefined) ? parseFloat(config.modelY) : 0;
modelZ = (config.modelZ !== undefined) ? parseFloat(config.modelZ) : 0;
modelScale = (config.modelScale !== undefined) ? parseFloat(config.modelScale) : 1;
modelRotationX = (config.modelRotationX !== undefined) ? parseFloat(config.modelRotationX) : 0;
modelRotationY = (config.modelRotationY !== undefined) ? parseFloat(config.modelRotationY) : 0;
modelRotationZ = (config.modelRotationZ !== undefined) ? parseFloat(config.modelRotationZ) : 0;
const cameraX = parseFloat(config.cameraX || "0");
const cameraY = parseFloat(config.cameraY || "2");
const cameraZ = parseFloat(config.cameraZ || "5");
const cameraXPhone = parseFloat(config.cameraXPhone || cameraX);
const cameraYPhone = parseFloat(config.cameraYPhone || cameraY);
const cameraZPhone = parseFloat(config.cameraZPhone || cameraZ * 1.5);
chosenCameraX = isMobile ? cameraXPhone : cameraX;
chosenCameraY = isMobile ? cameraYPhone : cameraY;
chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
const canvasId = 'canvas-' + instanceId;
const progressDialog = document.getElementById('progress-dialog-' + instanceId);
const viewerContainer = document.getElementById('viewer-container-' + instanceId);
let oldCanvas = document.getElementById(canvasId);
if (oldCanvas) oldCanvas.remove();
const canvas = document.createElement('canvas');
canvas.id = canvasId;
canvas.className = 'ply-canvas';
canvas.style.width = "100%";
canvas.style.height = "100%";
viewerContainer.insertBefore(canvas, progressDialog);
canvas.style.touchAction = "none";
canvas.style.webkitTouchCallout = "none";
progressDialog.style.display = 'block';
if (!pc) {
pc = await import("https://esm.run/playcanvas");
window.pc = pc;
}
// Explicitly ensure arraybuffer for PLY on iOS Safari
const plyAsset = new pc.Asset('gsplat', 'gsplat', {
url: plyUrl,
responseType: 'arraybuffer' // Crucial explicit fix
});
const assets = {
model: plyAsset,
galerie: new pc.Asset('galerie', 'container', { url: glbUrl }),
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 })
};
const device = await pc.createGraphicsDevice(canvas, {
deviceTypes: ["webgl2"],
antialias: false
});
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
const opts = new pc.AppOptions();
opts.graphicsDevice = device;
opts.mouse = new pc.Mouse(document.body);
opts.touch = new pc.TouchDevice(document.body);
app = new pc.Application(canvas, opts);
app.setCanvasFillMode(pc.FILLMODE_NONE);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
resizeObserver = new ResizeObserver(entries => {
for (const entry of entries) {
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
}
});
resizeObserver.observe(viewerContainer);
app.assets.add(assets.model);
app.assets.add(assets.galerie);
app.assets.add(assets.hdr);
assets.model.ready(() => {
modelEntity = new pc.Entity('model');
modelEntity.addComponent('gsplat', { asset: assets.model });
modelEntity.setLocalPosition(modelX, modelY, modelZ);
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
app.root.addChild(modelEntity);
progressDialog.style.display = 'none';
});
app.start();
cameraEntity = new pc.Entity('camera');
cameraEntity.addComponent('camera', { clearColor: new pc.Color(0.2, 0.2, 0.2, 1) });
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
cameraEntity.lookAt(pc.Vec3.ZERO);
cameraEntity.addComponent('script');
cameraEntity.script.create('orbitCamera', {
attributes: {
inertiaFactor: 0.2,
focusEntity: modelEntity,
distanceMax: maxZoom,
distanceMin: minZoom,
pitchAngleMax: maxAngle,
pitchAngleMin: minAngle,
yawAngleMax: maxAzimuth,
yawAngleMin: minAzimuth,
minPivotY: minPivotY,
minY: minY,
frameOnStart: true
}
});
cameraEntity.script.create('orbitCameraInputMouse');
cameraEntity.script.create('orbitCameraInputTouch');
app.root.addChild(cameraEntity);
viewerInitialized = true;
}
export function resetViewerCamera() {
if (cameraEntity && cameraEntity.script.orbitCamera) {
cameraEntity.script.orbitCamera.reset();
}
}
export function cleanupViewer() {
if (app) app.destroy();
app = null;
viewerInitialized = false;
if (resizeObserver) resizeObserver.disconnect();
}