Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
| 1 |
-
// viewer.js
|
| 2 |
-
// ==============================
|
| 3 |
-
|
| 4 |
let pc;
|
| 5 |
export let app = null;
|
| 6 |
let cameraEntity = null;
|
|
@@ -13,27 +10,37 @@ let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, min
|
|
| 13 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 14 |
let sogsUrl, glbUrl, canvasBg;
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
hex = hex.replace("#", "");
|
| 19 |
if (hex.length === 6) hex += "FF";
|
|
|
|
| 20 |
const num = parseInt(hex, 16);
|
| 21 |
return [
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
];
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
|
| 29 |
export async function initializeViewer(config, instanceId) {
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
sogsUrl = config.sogs_json_url;
|
| 38 |
glbUrl = config.glb_url;
|
| 39 |
canvasBg = config.canvas_background || "#ffffff";
|
|
@@ -81,7 +88,9 @@ export async function initializeViewer(config, instanceId) {
|
|
| 81 |
canvas.setAttribute('tabindex', '0');
|
| 82 |
viewerContainer.insertBefore(canvas, progressDialog);
|
| 83 |
|
|
|
|
| 84 |
canvas.style.background = canvasBg;
|
|
|
|
| 85 |
canvas.style.touchAction = "none";
|
| 86 |
canvas.style.webkitTouchCallout = "none";
|
| 87 |
canvas.addEventListener('gesturestart', e => e.preventDefault());
|
|
@@ -90,208 +99,270 @@ export async function initializeViewer(config, instanceId) {
|
|
| 90 |
canvas.addEventListener('dblclick', e => e.preventDefault());
|
| 91 |
canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
|
| 92 |
|
| 93 |
-
//
|
| 94 |
canvas.addEventListener('wheel', (e) => {
|
| 95 |
-
|
| 96 |
}, { passive: false });
|
| 97 |
|
| 98 |
-
progressDialog.style.display = 'block';
|
|
|
|
|
|
|
| 99 |
|
| 100 |
if (!pc) {
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 103 |
}
|
| 104 |
|
| 105 |
-
//
|
| 106 |
-
let device
|
| 107 |
-
let lastError = null;
|
| 108 |
try {
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
| 115 |
} catch (e) {
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
// Fallback: forcibly try WebGL1 if WebGL2 fails (especially for older iOS)
|
| 119 |
-
if (!device) {
|
| 120 |
-
try {
|
| 121 |
-
device = await pc.createGraphicsDevice(canvas, {
|
| 122 |
-
deviceTypes: ["webgl1"],
|
| 123 |
-
glslangUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/glslang/glslang.js",
|
| 124 |
-
twgslUrl: "https://mikafil-viewer-sgos.static.hf.space/static/lib/twgsl/twgsl.js",
|
| 125 |
-
antialias: false
|
| 126 |
-
});
|
| 127 |
-
} catch (e) {
|
| 128 |
-
progressDialog.style.display = 'none';
|
| 129 |
-
progressIndicator.value = 0;
|
| 130 |
-
progressIndicator.removeAttribute('max');
|
| 131 |
-
if (window.confirm("WebGL failed to initialize. Your browser/device may not support it. Reload?")) location.reload();
|
| 132 |
-
throw new Error("Failed to create WebGL context: " + (lastError || e));
|
| 133 |
-
}
|
| 134 |
}
|
| 135 |
-
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 136 |
|
| 137 |
const opts = new pc.AppOptions();
|
| 138 |
opts.graphicsDevice = device;
|
| 139 |
opts.mouse = new pc.Mouse(canvas);
|
| 140 |
opts.touch = new pc.TouchDevice(canvas);
|
| 141 |
opts.componentSystems = [
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
];
|
| 150 |
opts.resourceHandlers = [
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
];
|
| 156 |
|
| 157 |
app = new pc.Application(canvas, opts);
|
| 158 |
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 159 |
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 160 |
|
| 161 |
-
|
|
|
|
|
|
|
| 162 |
resizeObserver = new ResizeObserver(entries => {
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
| 166 |
});
|
| 167 |
resizeObserver.observe(viewerContainer);
|
| 168 |
|
| 169 |
-
window.addEventListener('resize', () =>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
app.on('destroy', () => resizeObserver.disconnect());
|
| 171 |
|
| 172 |
-
//
|
| 173 |
const assets = {
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
};
|
| 178 |
for (const key in assets) app.assets.add(assets[key]);
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 181 |
|
|
|
|
|
|
|
| 182 |
loader.load(() => {
|
|
|
|
|
|
|
|
|
|
| 183 |
app.start();
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
-
|
|
|
|
| 187 |
modelEntity = new pc.Entity('model');
|
| 188 |
modelEntity.addComponent('gsplat', { asset: assets.sogs });
|
| 189 |
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 190 |
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 191 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 192 |
app.root.addChild(modelEntity);
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
|
| 196 |
-
|
| 197 |
-
|
|
|
|
| 198 |
}
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
-
|
|
|
|
| 201 |
cameraEntity = new pc.Entity('camera');
|
| 202 |
-
//
|
| 203 |
let bg = [1, 1, 1, 1];
|
| 204 |
if (canvasBg && /^#?[0-9a-f]{6,8}$/i.test(canvasBg.replace("#", ""))) {
|
| 205 |
-
|
| 206 |
}
|
| 207 |
cameraEntity.addComponent('camera', { clearColor: new pc.Color(bg[0], bg[1], bg[2], bg[3]) });
|
| 208 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 209 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 210 |
cameraEntity.addComponent('script');
|
| 211 |
|
|
|
|
| 212 |
cameraEntity.script.create('orbitCamera', {
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
});
|
| 226 |
cameraEntity.script.create('orbitCameraInputMouse');
|
| 227 |
cameraEntity.script.create('orbitCameraInputTouch');
|
| 228 |
app.root.addChild(cameraEntity);
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
app.once('update', () => resetViewerCamera());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
if (config.tooltips_url) {
|
| 236 |
-
import('https://mikafil-viewer-sgos.static.hf.space/tooltips.js').then(tooltipsModule => {
|
| 237 |
-
tooltipsModule.initializeTooltips({
|
| 238 |
-
app,
|
| 239 |
-
cameraEntity,
|
| 240 |
-
modelEntity,
|
| 241 |
-
tooltipsUrl: config.tooltips_url,
|
| 242 |
-
defaultVisible: !!config.showTooltipsDefault,
|
| 243 |
-
moveDuration: config.tooltipMoveDuration || 0.6
|
| 244 |
-
});
|
| 245 |
-
}).catch(e => {});
|
| 246 |
-
}
|
| 247 |
-
} catch (e) {}
|
| 248 |
-
|
| 249 |
-
viewerInitialized = true;
|
| 250 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
}
|
| 252 |
|
|
|
|
| 253 |
export function resetViewerCamera() {
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
const modelPos = modelEntity.getPosition();
|
| 260 |
-
const tempEnt = new pc.Entity();
|
| 261 |
-
tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 262 |
-
tempEnt.lookAt(modelPos);
|
| 263 |
-
|
| 264 |
-
const dist = new pc.Vec3().sub2(
|
| 265 |
-
new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ),
|
| 266 |
-
modelPos
|
| 267 |
-
).length();
|
| 268 |
-
|
| 269 |
-
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 270 |
-
cameraEntity.lookAt(modelPos);
|
| 271 |
-
|
| 272 |
-
orbitCam.pivotPoint = modelPos.clone();
|
| 273 |
-
orbitCam._targetDistance = dist;
|
| 274 |
-
orbitCam._distance = dist;
|
| 275 |
-
|
| 276 |
-
const rot = tempEnt.getRotation();
|
| 277 |
-
const fwd = new pc.Vec3();
|
| 278 |
-
rot.transformVector(pc.Vec3.FORWARD, fwd);
|
| 279 |
-
|
| 280 |
-
const yaw = Math.atan2(-fwd.x, -fwd.z) * pc.math.RAD_TO_DEG;
|
| 281 |
-
const yawQuat = new pc.Quat().setFromEulerAngles(0, -yaw, 0);
|
| 282 |
-
const rotNoYaw = new pc.Quat().mul2(yawQuat, rot);
|
| 283 |
-
const fNoYaw = new pc.Vec3();
|
| 284 |
-
rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
|
| 285 |
-
const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
|
| 286 |
-
|
| 287 |
-
orbitCam._targetYaw = yaw;
|
| 288 |
-
orbitCam._yaw = yaw;
|
| 289 |
-
orbitCam._targetPitch = pitch;
|
| 290 |
-
orbitCam._pitch = pitch;
|
| 291 |
-
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 292 |
-
|
| 293 |
-
tempEnt.destroy();
|
| 294 |
-
} catch (e) {
|
| 295 |
-
// Silent fail
|
| 296 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
let pc;
|
| 2 |
export let app = null;
|
| 3 |
let cameraEntity = null;
|
|
|
|
| 10 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 11 |
let sogsUrl, glbUrl, canvasBg;
|
| 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 |
+
alert("initializeViewer: start");
|
| 33 |
+
|
| 34 |
+
if (viewerInitialized) {
|
| 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 |
+
try {
|
| 43 |
+
// Parse config
|
| 44 |
sogsUrl = config.sogs_json_url;
|
| 45 |
glbUrl = config.glb_url;
|
| 46 |
canvasBg = config.canvas_background || "#ffffff";
|
|
|
|
| 88 |
canvas.setAttribute('tabindex', '0');
|
| 89 |
viewerContainer.insertBefore(canvas, progressDialog);
|
| 90 |
|
| 91 |
+
// Apply background color from config if present
|
| 92 |
canvas.style.background = canvasBg;
|
| 93 |
+
|
| 94 |
canvas.style.touchAction = "none";
|
| 95 |
canvas.style.webkitTouchCallout = "none";
|
| 96 |
canvas.addEventListener('gesturestart', e => e.preventDefault());
|
|
|
|
| 99 |
canvas.addEventListener('dblclick', e => e.preventDefault());
|
| 100 |
canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
|
| 101 |
|
| 102 |
+
// --- Mouse wheel suppression
|
| 103 |
canvas.addEventListener('wheel', (e) => {
|
| 104 |
+
e.preventDefault(); // Only block page scroll if mouse is over viewer
|
| 105 |
}, { passive: false });
|
| 106 |
|
| 107 |
+
if (progressDialog) progressDialog.style.display = 'block';
|
| 108 |
+
|
| 109 |
+
alert("Canvas created. Loading PlayCanvas...");
|
| 110 |
|
| 111 |
if (!pc) {
|
| 112 |
+
pc = await import("https://cdn.jsdelivr.net/npm/playcanvas@latest/+esm");
|
| 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 |
const opts = new pc.AppOptions();
|
| 134 |
opts.graphicsDevice = device;
|
| 135 |
opts.mouse = new pc.Mouse(canvas);
|
| 136 |
opts.touch = new pc.TouchDevice(canvas);
|
| 137 |
opts.componentSystems = [
|
| 138 |
+
pc.RenderComponentSystem,
|
| 139 |
+
pc.CameraComponentSystem,
|
| 140 |
+
pc.LightComponentSystem,
|
| 141 |
+
pc.ScriptComponentSystem,
|
| 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 |
+
window.addEventListener('resize', () => {
|
| 170 |
+
try {
|
| 171 |
+
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 172 |
+
} catch (e) { alert("resizeCanvas error: " + e); }
|
| 173 |
+
});
|
| 174 |
app.on('destroy', () => resizeObserver.disconnect());
|
| 175 |
|
| 176 |
+
// Prepare asset loading (SOGS as 'gsplat')
|
| 177 |
const assets = {
|
| 178 |
+
sogs: new pc.Asset('gsplat', 'gsplat', { url: sogsUrl }),
|
| 179 |
+
orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" }),
|
| 180 |
+
glb: new pc.Asset('glb', 'container', { url: glbUrl }),
|
| 181 |
};
|
| 182 |
for (const key in assets) app.assets.add(assets[key]);
|
| 183 |
|
| 184 |
+
// Individual asset error listeners
|
| 185 |
+
assets.sogs.on('error', (err) => { alert("SOGS asset error: " + err); });
|
| 186 |
+
assets.orbit.on('error', (err) => { alert("Orbit-camera.js asset error: " + err); });
|
| 187 |
+
if (assets.glb) assets.glb.on('error', (err) => { alert("GLB asset error: " + err); });
|
| 188 |
+
|
| 189 |
+
assets.sogs.ready(() => { alert("SOGS asset ready."); });
|
| 190 |
+
assets.orbit.ready(() => { alert("orbit-camera.js asset ready."); });
|
| 191 |
+
if (assets.glb) assets.glb.ready(() => { alert("GLB asset ready."); });
|
| 192 |
+
|
| 193 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 194 |
|
| 195 |
+
loader.on('error', (err) => { alert("Asset loader error: " + err); });
|
| 196 |
+
|
| 197 |
loader.load(() => {
|
| 198 |
+
alert("All assets loaded. Starting app...");
|
| 199 |
+
|
| 200 |
+
try {
|
| 201 |
app.start();
|
| 202 |
+
} catch (e) {
|
| 203 |
+
alert("app.start() error: " + e);
|
| 204 |
+
throw e;
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
if (progressDialog) progressDialog.style.display = 'none';
|
| 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 |
+
// Add the GLB entity if provided
|
| 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 |
+
// CAMERA
|
| 235 |
+
try {
|
| 236 |
cameraEntity = new pc.Entity('camera');
|
| 237 |
+
// Support background color
|
| 238 |
let bg = [1, 1, 1, 1];
|
| 239 |
if (canvasBg && /^#?[0-9a-f]{6,8}$/i.test(canvasBg.replace("#", ""))) {
|
| 240 |
+
bg = hexToRgbaArray(canvasBg);
|
| 241 |
}
|
| 242 |
cameraEntity.addComponent('camera', { clearColor: new pc.Color(bg[0], bg[1], bg[2], bg[3]) });
|
| 243 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 244 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 245 |
cameraEntity.addComponent('script');
|
| 246 |
|
| 247 |
+
// Pass all attributes to Orbit Camera script
|
| 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 |
+
viewerInitialized = true;
|
| 305 |
+
alert("Viewer initialized success!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
});
|
| 307 |
+
|
| 308 |
+
alert("Asset loader started.");
|
| 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();
|
| 331 |
+
tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 332 |
+
tempEnt.lookAt(modelPos);
|
| 333 |
+
|
| 334 |
+
const dist = new pc.Vec3().sub2(
|
| 335 |
+
new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ),
|
| 336 |
+
modelPos
|
| 337 |
+
).length();
|
| 338 |
+
|
| 339 |
+
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 340 |
+
cameraEntity.lookAt(modelPos);
|
| 341 |
+
|
| 342 |
+
orbitCam.pivotPoint = modelPos.clone();
|
| 343 |
+
orbitCam._targetDistance = dist;
|
| 344 |
+
orbitCam._distance = dist;
|
| 345 |
+
|
| 346 |
+
const rot = tempEnt.getRotation();
|
| 347 |
+
const fwd = new pc.Vec3();
|
| 348 |
+
rot.transformVector(pc.Vec3.FORWARD, fwd);
|
| 349 |
+
|
| 350 |
+
const yaw = Math.atan2(-fwd.x, -fwd.z) * pc.math.RAD_TO_DEG;
|
| 351 |
+
const yawQuat = new pc.Quat().setFromEulerAngles(0, -yaw, 0);
|
| 352 |
+
const rotNoYaw = new pc.Quat().mul2(yawQuat, rot);
|
| 353 |
+
const fNoYaw = new pc.Vec3();
|
| 354 |
+
rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
|
| 355 |
+
const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
|
| 356 |
+
|
| 357 |
+
orbitCam._targetYaw = yaw;
|
| 358 |
+
orbitCam._yaw = yaw;
|
| 359 |
+
orbitCam._targetPitch = pitch;
|
| 360 |
+
orbitCam._pitch = pitch;
|
| 361 |
+
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 362 |
+
|
| 363 |
+
tempEnt.destroy();
|
| 364 |
+
alert("resetViewerCamera: success");
|
| 365 |
+
} catch (e) {
|
| 366 |
+
alert("resetViewerCamera: error: " + e);
|
| 367 |
+
}
|
| 368 |
}
|