Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
export let app = null;
|
| 3 |
let cameraEntity = null;
|
| 4 |
let modelEntity = null;
|
|
@@ -8,323 +9,223 @@ let resizeObserver = null;
|
|
| 8 |
let chosenCameraX, chosenCameraY, chosenCameraZ;
|
| 9 |
let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
|
| 10 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 11 |
-
let sogsUrl
|
| 12 |
-
|
| 13 |
-
function hexToRgbaArray(hex) {
|
| 14 |
-
try {
|
| 15 |
-
hex = hex.replace("#", "");
|
| 16 |
-
if (hex.length === 6) hex += "FF";
|
| 17 |
-
if (hex.length !== 8) return [1, 1, 1, 1];
|
| 18 |
-
const num = parseInt(hex, 16);
|
| 19 |
-
return [
|
| 20 |
-
((num >> 24) & 0xFF) / 255,
|
| 21 |
-
((num >> 16) & 0xFF) / 255,
|
| 22 |
-
((num >> 8) & 0xFF) / 255,
|
| 23 |
-
(num & 0xFF) / 255
|
| 24 |
-
];
|
| 25 |
-
} catch (e) {
|
| 26 |
-
alert("hexToRgbaArray error: " + e);
|
| 27 |
-
return [1, 1, 1, 1];
|
| 28 |
-
}
|
| 29 |
-
}
|
| 30 |
|
| 31 |
export async function initializeViewer(config, instanceId) {
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
alert("Already initialized!");
|
| 36 |
-
return;
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
| 40 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
if (progressDialog) progressDialog.style.display = 'block';
|
| 108 |
-
|
| 109 |
-
alert("Canvas created. Loading PlayCanvas...");
|
| 110 |
|
|
|
|
| 111 |
if (!pc) {
|
| 112 |
-
pc = await import("https://
|
| 113 |
window.pc = pc;
|
| 114 |
-
alert("PlayCanvas imported.");
|
| 115 |
-
}
|
| 116 |
-
|
| 117 |
-
// Create app and graphics device
|
| 118 |
-
let device;
|
| 119 |
-
try {
|
| 120 |
-
device = await pc.createGraphicsDevice(canvas, {
|
| 121 |
-
deviceTypes: ["webgl2", "webgl1"],
|
| 122 |
-
glslangUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/glslang/glslang.js",
|
| 123 |
-
twgslUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/twgsl/twgsl.js",
|
| 124 |
-
antialias: false
|
| 125 |
-
});
|
| 126 |
-
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 127 |
-
alert("Graphics device created.");
|
| 128 |
-
} catch (e) {
|
| 129 |
-
alert("Graphics device error: " + e);
|
| 130 |
-
throw e;
|
| 131 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
pc.GSplatComponentSystem,
|
| 143 |
-
pc.CollisionComponentSystem,
|
| 144 |
-
pc.RigidbodyComponentSystem
|
| 145 |
-
];
|
| 146 |
-
opts.resourceHandlers = [
|
| 147 |
-
pc.TextureHandler,
|
| 148 |
-
pc.ContainerHandler,
|
| 149 |
-
pc.ScriptHandler,
|
| 150 |
-
pc.GSplatHandler
|
| 151 |
-
];
|
| 152 |
-
|
| 153 |
-
app = new pc.Application(canvas, opts);
|
| 154 |
-
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 155 |
-
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 156 |
-
|
| 157 |
-
alert("App created.");
|
| 158 |
-
|
| 159 |
-
// Resizing
|
| 160 |
-
resizeObserver = new ResizeObserver(entries => {
|
| 161 |
-
entries.forEach(entry => {
|
| 162 |
-
try {
|
| 163 |
-
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
| 164 |
-
} catch (e) { alert("resizeCanvas error: " + e); }
|
| 165 |
-
});
|
| 166 |
-
});
|
| 167 |
-
resizeObserver.observe(viewerContainer);
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
});
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
// Add SOGS model
|
| 210 |
-
try {
|
| 211 |
-
modelEntity = new pc.Entity('model');
|
| 212 |
-
modelEntity.addComponent('gsplat', { asset: assets.sogs });
|
| 213 |
-
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 214 |
-
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 215 |
-
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 216 |
-
app.root.addChild(modelEntity);
|
| 217 |
-
alert("Model entity added!");
|
| 218 |
-
} catch (e) {
|
| 219 |
-
alert("Model entity error: " + e);
|
| 220 |
-
throw e;
|
| 221 |
-
}
|
| 222 |
|
| 223 |
-
|
| 224 |
-
try {
|
| 225 |
-
if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
|
| 226 |
-
const glbEntity = assets.glb.resource.instantiateRenderEntity();
|
| 227 |
-
app.root.addChild(glbEntity);
|
| 228 |
-
alert("GLB entity added.");
|
| 229 |
-
}
|
| 230 |
-
} catch (e) {
|
| 231 |
-
alert("GLB entity error: " + e);
|
| 232 |
-
}
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
cameraEntity.script.create('orbitCamera', {
|
| 249 |
-
attributes: {
|
| 250 |
-
focusEntity: modelEntity,
|
| 251 |
-
inertiaFactor: 0.2,
|
| 252 |
-
distanceMax: maxZoom,
|
| 253 |
-
distanceMin: minZoom,
|
| 254 |
-
pitchAngleMax: maxAngle,
|
| 255 |
-
pitchAngleMin: minAngle,
|
| 256 |
-
yawAngleMax: maxAzimuth,
|
| 257 |
-
yawAngleMin: minAzimuth,
|
| 258 |
-
minY: minY,
|
| 259 |
-
frameOnStart: false
|
| 260 |
-
}
|
| 261 |
});
|
| 262 |
-
cameraEntity.script.create('orbitCameraInputMouse');
|
| 263 |
-
cameraEntity.script.create('orbitCameraInputTouch');
|
| 264 |
-
app.root.addChild(cameraEntity);
|
| 265 |
-
|
| 266 |
-
alert("Camera added.");
|
| 267 |
-
} catch (e) {
|
| 268 |
-
alert("Camera error: " + e);
|
| 269 |
-
}
|
| 270 |
-
|
| 271 |
-
try {
|
| 272 |
-
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 273 |
-
alert("resizeCanvas called.");
|
| 274 |
-
} catch (e) {
|
| 275 |
-
alert("resizeCanvas error: " + e);
|
| 276 |
-
}
|
| 277 |
-
|
| 278 |
-
try {
|
| 279 |
-
app.once('update', () => resetViewerCamera());
|
| 280 |
-
alert("resetViewerCamera scheduled.");
|
| 281 |
-
} catch (e) {
|
| 282 |
-
alert("resetViewerCamera schedule error: " + e);
|
| 283 |
-
}
|
| 284 |
-
|
| 285 |
-
// Tooltips support (optional)
|
| 286 |
-
try {
|
| 287 |
-
if (config.tooltips_url) {
|
| 288 |
-
import('https://mikafil-viewer-sgos.static.hf.space/tooltips.js').then(tooltipsModule => {
|
| 289 |
-
tooltipsModule.initializeTooltips({
|
| 290 |
-
app,
|
| 291 |
-
cameraEntity,
|
| 292 |
-
modelEntity,
|
| 293 |
-
tooltipsUrl: config.tooltips_url,
|
| 294 |
-
defaultVisible: !!config.showTooltipsDefault,
|
| 295 |
-
moveDuration: config.tooltipMoveDuration || 0.6
|
| 296 |
-
});
|
| 297 |
-
alert("Tooltips loaded.");
|
| 298 |
-
}).catch(e => { alert("Tooltips module error: " + e); });
|
| 299 |
-
}
|
| 300 |
-
} catch (e) {
|
| 301 |
-
alert("Tooltips try-catch error: " + e);
|
| 302 |
}
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
} catch (e) {
|
| 310 |
-
alert("Main try-catch error: " + e);
|
| 311 |
-
throw e;
|
| 312 |
-
}
|
| 313 |
}
|
| 314 |
|
| 315 |
-
// Resets the viewer camera (as in your PLY example)
|
| 316 |
export function resetViewerCamera() {
|
| 317 |
-
alert("resetViewerCamera called.");
|
| 318 |
try {
|
| 319 |
-
if (!cameraEntity || !modelEntity || !app)
|
| 320 |
-
alert("resetViewerCamera: cameraEntity/modelEntity/app not ready");
|
| 321 |
-
return;
|
| 322 |
-
}
|
| 323 |
const orbitCam = cameraEntity.script.orbitCamera;
|
| 324 |
-
if (!orbitCam)
|
| 325 |
-
alert("resetViewerCamera: orbitCam missing");
|
| 326 |
-
return;
|
| 327 |
-
}
|
| 328 |
|
| 329 |
const modelPos = modelEntity.getPosition();
|
| 330 |
const tempEnt = new pc.Entity();
|
|
@@ -361,8 +262,7 @@ export function resetViewerCamera() {
|
|
| 361 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 362 |
|
| 363 |
tempEnt.destroy();
|
| 364 |
-
alert("resetViewerCamera: success");
|
| 365 |
} catch (e) {
|
| 366 |
-
|
| 367 |
}
|
| 368 |
}
|
|
|
|
| 1 |
+
// viewer.js (for SOGS, no PLY fallback)
|
| 2 |
+
let pc;
|
| 3 |
export let app = null;
|
| 4 |
let cameraEntity = null;
|
| 5 |
let modelEntity = null;
|
|
|
|
| 9 |
let chosenCameraX, chosenCameraY, chosenCameraZ;
|
| 10 |
let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
|
| 11 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 12 |
+
let sogsUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
export async function initializeViewer(config, instanceId) {
|
| 15 |
+
if (viewerInitialized) return;
|
| 16 |
|
| 17 |
+
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 19 |
|
| 20 |
+
// Use ONLY SOGS file
|
| 21 |
+
sogsUrl = config.sogs_url; // Make sure your config provides this!
|
| 22 |
+
if (!sogsUrl) {
|
| 23 |
+
alert("No sogs_url found in config!");
|
| 24 |
+
throw new Error("No sogs_url found in config");
|
| 25 |
+
}
|
| 26 |
+
minZoom = parseFloat(config.minZoom || "1");
|
| 27 |
+
maxZoom = parseFloat(config.maxZoom || "20");
|
| 28 |
+
minAngle = parseFloat(config.minAngle || "-45");
|
| 29 |
+
maxAngle = parseFloat(config.maxAngle || "90");
|
| 30 |
+
minAzimuth = (config.minAzimuth !== undefined) ? parseFloat(config.minAzimuth) : -360;
|
| 31 |
+
maxAzimuth = (config.maxAzimuth !== undefined) ? parseFloat(config.maxAzimuth) : 360;
|
| 32 |
+
minPivotY = parseFloat(config.minPivotY || "0");
|
| 33 |
+
minY = (config.minY !== undefined) ? parseFloat(config.minY) : 0;
|
| 34 |
+
|
| 35 |
+
modelX = (config.modelX !== undefined) ? parseFloat(config.modelX) : 0;
|
| 36 |
+
modelY = (config.modelY !== undefined) ? parseFloat(config.modelY) : 0;
|
| 37 |
+
modelZ = (config.modelZ !== undefined) ? parseFloat(config.modelZ) : 0;
|
| 38 |
+
modelScale = (config.modelScale !== undefined) ? parseFloat(config.modelScale) : 1;
|
| 39 |
+
modelRotationX = (config.modelRotationX !== undefined) ? parseFloat(config.modelRotationX) : 0;
|
| 40 |
+
modelRotationY = (config.modelRotationY !== undefined) ? parseFloat(config.modelRotationY) : 0;
|
| 41 |
+
modelRotationZ = (config.modelRotationZ !== undefined) ? parseFloat(config.modelRotationZ) : 0;
|
| 42 |
+
|
| 43 |
+
const cameraX = (config.cameraX !== undefined) ? parseFloat(config.cameraX) : 0;
|
| 44 |
+
const cameraY = (config.cameraY !== undefined) ? parseFloat(config.cameraY) : 2;
|
| 45 |
+
const cameraZ = (config.cameraZ !== undefined) ? parseFloat(config.cameraZ) : 5;
|
| 46 |
+
const cameraXPhone = (config.cameraXPhone !== undefined) ? parseFloat(config.cameraXPhone) : cameraX;
|
| 47 |
+
const cameraYPhone = (config.cameraYPhone !== undefined) ? parseFloat(config.cameraYPhone) : cameraY;
|
| 48 |
+
const cameraZPhone = (config.cameraZPhone !== undefined) ? parseFloat(config.cameraZPhone) : (cameraZ * 1.5);
|
| 49 |
+
|
| 50 |
+
chosenCameraX = isMobile ? cameraXPhone : cameraX;
|
| 51 |
+
chosenCameraY = isMobile ? cameraYPhone : cameraY;
|
| 52 |
+
chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
|
| 53 |
+
|
| 54 |
+
const canvasId = 'canvas-' + instanceId;
|
| 55 |
+
const progressDialog = document.getElementById('progress-dialog-' + instanceId);
|
| 56 |
+
const progressIndicator = document.getElementById('progress-indicator-' + instanceId);
|
| 57 |
+
const viewerContainer = document.getElementById('viewer-container-' + instanceId);
|
| 58 |
+
|
| 59 |
+
let oldCanvas = document.getElementById(canvasId);
|
| 60 |
+
if (oldCanvas) oldCanvas.remove();
|
| 61 |
+
|
| 62 |
+
const canvas = document.createElement('canvas');
|
| 63 |
+
canvas.id = canvasId;
|
| 64 |
+
canvas.className = 'sogs-canvas';
|
| 65 |
+
canvas.style.width = "100%";
|
| 66 |
+
canvas.style.height = "100%";
|
| 67 |
+
canvas.setAttribute('tabindex', '0');
|
| 68 |
+
viewerContainer.insertBefore(canvas, progressDialog);
|
| 69 |
+
|
| 70 |
+
canvas.style.touchAction = "none";
|
| 71 |
+
canvas.style.webkitTouchCallout = "none";
|
| 72 |
+
canvas.addEventListener('gesturestart', e => e.preventDefault());
|
| 73 |
+
canvas.addEventListener('gesturechange', e => e.preventDefault());
|
| 74 |
+
canvas.addEventListener('gestureend', e => e.preventDefault());
|
| 75 |
+
canvas.addEventListener('dblclick', e => e.preventDefault());
|
| 76 |
+
canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
|
| 77 |
+
|
| 78 |
+
// --- Block scroll only when over canvas
|
| 79 |
+
canvas.addEventListener('wheel', (e) => {
|
| 80 |
+
e.preventDefault();
|
| 81 |
+
}, { passive: false });
|
| 82 |
+
|
| 83 |
+
progressDialog.style.display = 'block';
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
try {
|
| 86 |
if (!pc) {
|
| 87 |
+
pc = await import("https://esm.run/playcanvas");
|
| 88 |
window.pc = pc;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
+
} catch (err) {
|
| 91 |
+
alert("Could not load PlayCanvas module!");
|
| 92 |
+
throw err;
|
| 93 |
+
}
|
| 94 |
|
| 95 |
+
// ---- Check/require SOGS support
|
| 96 |
+
if (
|
| 97 |
+
!pc.GSplatHandler ||
|
| 98 |
+
!pc.GSplatComponentSystem ||
|
| 99 |
+
!pc.GSplatComponent
|
| 100 |
+
) {
|
| 101 |
+
alert("PlayCanvas build does not include SOGS/GSplat support! Check your import/build.");
|
| 102 |
+
throw new Error("PlayCanvas build missing GSplat support");
|
| 103 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
// Create app
|
| 106 |
+
const device = await pc.createGraphicsDevice(canvas, {
|
| 107 |
+
deviceTypes: ["webgl2"],
|
| 108 |
+
glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
|
| 109 |
+
twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
|
| 110 |
+
antialias: false
|
| 111 |
+
});
|
| 112 |
+
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 113 |
+
|
| 114 |
+
const opts = new pc.AppOptions();
|
| 115 |
+
opts.graphicsDevice = device;
|
| 116 |
+
opts.mouse = new pc.Mouse(canvas);
|
| 117 |
+
opts.touch = new pc.TouchDevice(canvas);
|
| 118 |
+
opts.componentSystems = [
|
| 119 |
+
pc.RenderComponentSystem,
|
| 120 |
+
pc.CameraComponentSystem,
|
| 121 |
+
pc.LightComponentSystem,
|
| 122 |
+
pc.ScriptComponentSystem,
|
| 123 |
+
pc.GSplatComponentSystem, // <-- MUST BE PRESENT
|
| 124 |
+
pc.CollisionComponentSystem,
|
| 125 |
+
pc.RigidbodyComponentSystem
|
| 126 |
+
];
|
| 127 |
+
opts.resourceHandlers = [
|
| 128 |
+
pc.TextureHandler,
|
| 129 |
+
pc.ContainerHandler,
|
| 130 |
+
pc.ScriptHandler,
|
| 131 |
+
pc.GSplatHandler // <-- MUST BE PRESENT
|
| 132 |
+
];
|
| 133 |
+
|
| 134 |
+
app = new pc.Application(canvas, opts);
|
| 135 |
+
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 136 |
+
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 137 |
+
|
| 138 |
+
// Resize handling
|
| 139 |
+
resizeObserver = new ResizeObserver(entries => {
|
| 140 |
+
entries.forEach(entry => {
|
| 141 |
+
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
| 142 |
});
|
| 143 |
+
});
|
| 144 |
+
resizeObserver.observe(viewerContainer);
|
| 145 |
+
window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
|
| 146 |
+
app.on('destroy', () => resizeObserver.disconnect());
|
| 147 |
+
|
| 148 |
+
// SOGS asset
|
| 149 |
+
const sogsAsset = new pc.Asset('gsplat', 'gsplat', { url: sogsUrl });
|
| 150 |
+
app.assets.add(sogsAsset);
|
| 151 |
+
|
| 152 |
+
// Orbit/controls script
|
| 153 |
+
const orbitAsset = new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" });
|
| 154 |
+
app.assets.add(orbitAsset);
|
| 155 |
+
|
| 156 |
+
// Loader
|
| 157 |
+
const loader = new pc.AssetListLoader([sogsAsset, orbitAsset], app.assets);
|
| 158 |
+
loader.load(() => {
|
| 159 |
+
app.start();
|
| 160 |
+
progressDialog.style.display = 'none';
|
| 161 |
+
|
| 162 |
+
// Add SOGS/GSplat model
|
| 163 |
+
modelEntity = new pc.Entity('model');
|
| 164 |
+
modelEntity.addComponent('gsplat', { asset: sogsAsset });
|
| 165 |
+
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 166 |
+
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 167 |
+
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 168 |
+
app.root.addChild(modelEntity);
|
| 169 |
+
|
| 170 |
+
cameraEntity = new pc.Entity('camera');
|
| 171 |
+
cameraEntity.addComponent('camera', { clearColor: new pc.Color(1, 1, 1, 1) });
|
| 172 |
+
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 173 |
+
cameraEntity.lookAt(modelEntity.getPosition());
|
| 174 |
+
cameraEntity.addComponent('script');
|
| 175 |
+
cameraEntity.script.create('orbitCamera', {
|
| 176 |
+
attributes: {
|
| 177 |
+
focusEntity: modelEntity,
|
| 178 |
+
inertiaFactor: 0.2,
|
| 179 |
+
distanceMax: maxZoom,
|
| 180 |
+
distanceMin: minZoom,
|
| 181 |
+
pitchAngleMax: maxAngle,
|
| 182 |
+
pitchAngleMin: minAngle,
|
| 183 |
+
yawAngleMax: maxAzimuth,
|
| 184 |
+
yawAngleMin: minAzimuth,
|
| 185 |
+
minY: minY,
|
| 186 |
+
frameOnStart: false
|
| 187 |
}
|
| 188 |
+
});
|
| 189 |
+
cameraEntity.script.create('orbitCameraInputMouse');
|
| 190 |
+
cameraEntity.script.create('orbitCameraInputTouch');
|
| 191 |
+
app.root.addChild(cameraEntity);
|
| 192 |
|
| 193 |
+
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
+
app.once('update', () => resetViewerCamera());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
// Tooltips
|
| 198 |
+
try {
|
| 199 |
+
if (config.tooltips_url) {
|
| 200 |
+
import('./tooltips.js').then(tooltipsModule => {
|
| 201 |
+
tooltipsModule.initializeTooltips({
|
| 202 |
+
app,
|
| 203 |
+
cameraEntity,
|
| 204 |
+
modelEntity,
|
| 205 |
+
tooltipsUrl: config.tooltips_url,
|
| 206 |
+
defaultVisible: !!config.showTooltipsDefault,
|
| 207 |
+
moveDuration: config.tooltipMoveDuration || 0.6
|
| 208 |
+
});
|
| 209 |
+
}).catch(e => {
|
| 210 |
+
// Tooltips optional: fail silently if missing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
}
|
| 213 |
+
} catch (e) {
|
| 214 |
+
// Tooltips optional, fail silently
|
| 215 |
+
}
|
| 216 |
|
| 217 |
+
viewerInitialized = true;
|
| 218 |
+
}, (err) => {
|
| 219 |
+
alert("Error loading SOGS or control assets! " + (err && err.message));
|
| 220 |
+
throw err;
|
| 221 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
}
|
| 223 |
|
|
|
|
| 224 |
export function resetViewerCamera() {
|
|
|
|
| 225 |
try {
|
| 226 |
+
if (!cameraEntity || !modelEntity || !app) return;
|
|
|
|
|
|
|
|
|
|
| 227 |
const orbitCam = cameraEntity.script.orbitCamera;
|
| 228 |
+
if (!orbitCam) return;
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
const modelPos = modelEntity.getPosition();
|
| 231 |
const tempEnt = new pc.Entity();
|
|
|
|
| 262 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 263 |
|
| 264 |
tempEnt.destroy();
|
|
|
|
| 265 |
} catch (e) {
|
| 266 |
+
// Silent fail
|
| 267 |
}
|
| 268 |
}
|