Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -11,94 +11,131 @@ let resizeObserver = null;
|
|
| 11 |
let chosenCameraX, chosenCameraY, chosenCameraZ;
|
| 12 |
let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
|
| 13 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 14 |
-
let sogsUrl, 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
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
sogsUrl
|
| 24 |
-
glbUrl
|
| 25 |
-
|
| 26 |
-
minZoom
|
| 27 |
-
maxZoom
|
| 28 |
-
minAngle
|
| 29 |
-
maxAngle
|
| 30 |
-
minAzimuth=
|
| 31 |
-
maxAzimuth=
|
| 32 |
minPivotY = parseFloat(config.minPivotY || "0");
|
| 33 |
-
minY
|
| 34 |
-
|
| 35 |
-
modelX
|
| 36 |
-
modelY
|
| 37 |
-
modelZ
|
| 38 |
-
modelScale
|
| 39 |
-
modelRotationX
|
| 40 |
-
modelRotationY
|
| 41 |
-
modelRotationZ
|
| 42 |
-
|
| 43 |
-
const cameraX
|
| 44 |
-
const cameraY
|
| 45 |
-
const cameraZ
|
| 46 |
-
const cameraXPhone =
|
| 47 |
-
const cameraYPhone =
|
| 48 |
-
const cameraZPhone =
|
| 49 |
|
| 50 |
chosenCameraX = isMobile ? cameraXPhone : cameraX;
|
| 51 |
chosenCameraY = isMobile ? cameraYPhone : cameraY;
|
| 52 |
chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
|
| 53 |
|
| 54 |
-
const canvasId
|
| 55 |
-
const progressDialog
|
| 56 |
-
const progressIndicator= document.getElementById(
|
| 57 |
-
const viewerContainer
|
| 58 |
|
|
|
|
| 59 |
let oldCanvas = document.getElementById(canvasId);
|
| 60 |
if (oldCanvas) oldCanvas.remove();
|
| 61 |
|
| 62 |
-
const canvas = document.createElement(
|
| 63 |
canvas.id = canvasId;
|
| 64 |
-
canvas.className =
|
| 65 |
canvas.style.width = "100%";
|
| 66 |
canvas.style.height = "100%";
|
| 67 |
-
canvas.setAttribute(
|
| 68 |
viewerContainer.insertBefore(canvas, progressDialog);
|
| 69 |
|
| 70 |
-
//
|
| 71 |
-
canvas.style.background = canvasBg;
|
| 72 |
-
|
| 73 |
canvas.style.touchAction = "none";
|
| 74 |
canvas.style.webkitTouchCallout = "none";
|
| 75 |
-
canvas.addEventListener(
|
| 76 |
-
canvas.addEventListener(
|
| 77 |
-
canvas.addEventListener(
|
| 78 |
-
canvas.addEventListener(
|
| 79 |
-
canvas.addEventListener(
|
| 80 |
-
|
| 81 |
-
// --- Mouse wheel suppression
|
| 82 |
-
canvas.addEventListener('wheel', (e) => {
|
| 83 |
-
e.preventDefault(); // Only block page scroll if mouse is over viewer
|
| 84 |
}, { passive: false });
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
if (!pc) {
|
| 89 |
-
pc = await import("https://
|
| 90 |
window.pc = pc;
|
| 91 |
}
|
| 92 |
|
| 93 |
-
//
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
antialias: false
|
| 99 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 101 |
|
|
|
|
| 102 |
const opts = new pc.AppOptions();
|
| 103 |
opts.graphicsDevice = device;
|
| 104 |
opts.mouse = new pc.Mouse(canvas);
|
|
@@ -108,7 +145,6 @@ export async function initializeViewer(config, instanceId) {
|
|
| 108 |
pc.CameraComponentSystem,
|
| 109 |
pc.LightComponentSystem,
|
| 110 |
pc.ScriptComponentSystem,
|
| 111 |
-
// "GSplat" component supports SOGS and GSplat
|
| 112 |
pc.GSplatComponentSystem,
|
| 113 |
pc.CollisionComponentSystem,
|
| 114 |
pc.RigidbodyComponentSystem
|
|
@@ -117,7 +153,6 @@ export async function initializeViewer(config, instanceId) {
|
|
| 117 |
pc.TextureHandler,
|
| 118 |
pc.ContainerHandler,
|
| 119 |
pc.ScriptHandler,
|
| 120 |
-
// GSplatHandler handles GSplat and SOGS
|
| 121 |
pc.GSplatHandler
|
| 122 |
];
|
| 123 |
|
|
@@ -125,6 +160,14 @@ export async function initializeViewer(config, instanceId) {
|
|
| 125 |
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 126 |
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
resizeObserver = new ResizeObserver(entries => {
|
| 129 |
entries.forEach(entry => {
|
| 130 |
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
|
@@ -132,59 +175,51 @@ export async function initializeViewer(config, instanceId) {
|
|
| 132 |
});
|
| 133 |
resizeObserver.observe(viewerContainer);
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
//
|
| 139 |
const assets = {
|
| 140 |
-
sogs: new pc.Asset(
|
| 141 |
-
orbit: new pc.Asset(
|
| 142 |
-
glb: new pc.Asset(
|
| 143 |
};
|
| 144 |
for (const key in assets) app.assets.add(assets[key]);
|
| 145 |
|
| 146 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 147 |
loader.load(() => {
|
| 148 |
app.start();
|
| 149 |
-
progressDialog.style.display =
|
| 150 |
|
| 151 |
-
// Add SOGS model
|
| 152 |
-
modelEntity = new pc.Entity(
|
| 153 |
-
modelEntity.addComponent(
|
| 154 |
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 155 |
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 156 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 157 |
app.root.addChild(modelEntity);
|
| 158 |
|
| 159 |
-
//
|
| 160 |
if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
|
| 161 |
const glbEntity = assets.glb.resource.instantiateRenderEntity();
|
| 162 |
app.root.addChild(glbEntity);
|
| 163 |
}
|
| 164 |
|
| 165 |
-
//
|
| 166 |
-
cameraEntity = new pc.Entity(
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
//
|
| 171 |
-
|
| 172 |
-
if (hex.length === 6) hex += "FF";
|
| 173 |
-
const num = parseInt(hex, 16);
|
| 174 |
-
bg = [
|
| 175 |
-
((num >> 24) & 0xFF) / 255,
|
| 176 |
-
((num >> 16) & 0xFF) / 255,
|
| 177 |
-
((num >> 8) & 0xFF) / 255,
|
| 178 |
-
(num & 0xFF) / 255
|
| 179 |
-
];
|
| 180 |
-
}
|
| 181 |
-
cameraEntity.addComponent('camera', { clearColor: new pc.Color(bg[0], bg[1], bg[2], bg[3]) });
|
| 182 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 183 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 184 |
-
cameraEntity.addComponent(
|
| 185 |
|
| 186 |
-
|
| 187 |
-
cameraEntity.script.create('orbitCamera', {
|
| 188 |
attributes: {
|
| 189 |
focusEntity: modelEntity,
|
| 190 |
inertiaFactor: 0.2,
|
|
@@ -198,18 +233,18 @@ export async function initializeViewer(config, instanceId) {
|
|
| 198 |
frameOnStart: false
|
| 199 |
}
|
| 200 |
});
|
| 201 |
-
cameraEntity.script.create(
|
| 202 |
-
cameraEntity.script.create(
|
| 203 |
app.root.addChild(cameraEntity);
|
| 204 |
|
| 205 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 206 |
|
| 207 |
-
app.once(
|
| 208 |
|
| 209 |
-
// Tooltips support
|
| 210 |
try {
|
| 211 |
if (config.tooltips_url) {
|
| 212 |
-
import(
|
| 213 |
tooltipsModule.initializeTooltips({
|
| 214 |
app,
|
| 215 |
cameraEntity,
|
|
@@ -218,7 +253,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 218 |
defaultVisible: !!config.showTooltipsDefault,
|
| 219 |
moveDuration: config.tooltipMoveDuration || 0.6
|
| 220 |
});
|
| 221 |
-
}).catch(
|
| 222 |
}
|
| 223 |
} catch (e) {}
|
| 224 |
|
|
@@ -226,7 +261,6 @@ export async function initializeViewer(config, instanceId) {
|
|
| 226 |
});
|
| 227 |
}
|
| 228 |
|
| 229 |
-
// Resets the viewer camera (as in your PLY example)
|
| 230 |
export function resetViewerCamera() {
|
| 231 |
try {
|
| 232 |
if (!cameraEntity || !modelEntity || !app) return;
|
|
@@ -248,7 +282,7 @@ export function resetViewerCamera() {
|
|
| 248 |
|
| 249 |
orbitCam.pivotPoint = modelPos.clone();
|
| 250 |
orbitCam._targetDistance = dist;
|
| 251 |
-
orbitCam._distance
|
| 252 |
|
| 253 |
const rot = tempEnt.getRotation();
|
| 254 |
const fwd = new pc.Vec3();
|
|
@@ -261,10 +295,10 @@ export function resetViewerCamera() {
|
|
| 261 |
rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
|
| 262 |
const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
|
| 263 |
|
| 264 |
-
orbitCam._targetYaw
|
| 265 |
-
orbitCam._yaw
|
| 266 |
orbitCam._targetPitch = pitch;
|
| 267 |
-
orbitCam._pitch
|
| 268 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 269 |
|
| 270 |
tempEnt.destroy();
|
|
|
|
| 11 |
let chosenCameraX, chosenCameraY, chosenCameraZ;
|
| 12 |
let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
|
| 13 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 14 |
+
let sogsUrl, glbUrl;
|
| 15 |
+
|
| 16 |
+
function getColorFromHexOrString(value, fallback) {
|
| 17 |
+
if (!value) return fallback;
|
| 18 |
+
try {
|
| 19 |
+
// If value is hex (e.g. "#FFFFFF")
|
| 20 |
+
if (value.startsWith("#")) {
|
| 21 |
+
// Remove # and convert to rgb
|
| 22 |
+
let hex = value.replace("#", "");
|
| 23 |
+
if (hex.length === 3) {
|
| 24 |
+
hex = hex.split("").map((c) => c + c).join("");
|
| 25 |
+
}
|
| 26 |
+
if (hex.length !== 6) return fallback;
|
| 27 |
+
const num = parseInt(hex, 16);
|
| 28 |
+
return new pc.Color(
|
| 29 |
+
((num >> 16) & 255) / 255,
|
| 30 |
+
((num >> 8) & 255) / 255,
|
| 31 |
+
(num & 255) / 255
|
| 32 |
+
);
|
| 33 |
+
} else {
|
| 34 |
+
// Try parsing as CSS color string (not supported by pc.Color)
|
| 35 |
+
return fallback;
|
| 36 |
+
}
|
| 37 |
+
} catch {
|
| 38 |
+
return fallback;
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
|
| 42 |
export async function initializeViewer(config, instanceId) {
|
| 43 |
if (viewerInitialized) return;
|
| 44 |
|
| 45 |
+
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
| 46 |
+
const isAndroid = /Android/i.test(navigator.userAgent);
|
| 47 |
+
const isMobile = isIOS || isAndroid;
|
| 48 |
+
|
| 49 |
+
sogsUrl = config.sogs_json_url;
|
| 50 |
+
glbUrl = config.glb_url;
|
| 51 |
+
|
| 52 |
+
minZoom = parseFloat(config.minZoom || "1");
|
| 53 |
+
maxZoom = parseFloat(config.maxZoom || "20");
|
| 54 |
+
minAngle = parseFloat(config.minAngle || "-45");
|
| 55 |
+
maxAngle = parseFloat(config.maxAngle || "90");
|
| 56 |
+
minAzimuth = config.minAzimuth !== undefined ? parseFloat(config.minAzimuth) : -360;
|
| 57 |
+
maxAzimuth = config.maxAzimuth !== undefined ? parseFloat(config.maxAzimuth) : 360;
|
| 58 |
minPivotY = parseFloat(config.minPivotY || "0");
|
| 59 |
+
minY = config.minY !== undefined ? parseFloat(config.minY) : 0;
|
| 60 |
+
|
| 61 |
+
modelX = config.modelX !== undefined ? parseFloat(config.modelX) : 0;
|
| 62 |
+
modelY = config.modelY !== undefined ? parseFloat(config.modelY) : 0;
|
| 63 |
+
modelZ = config.modelZ !== undefined ? parseFloat(config.modelZ) : 0;
|
| 64 |
+
modelScale = config.modelScale !== undefined ? parseFloat(config.modelScale) : 1;
|
| 65 |
+
modelRotationX = config.modelRotationX !== undefined ? parseFloat(config.modelRotationX) : 0;
|
| 66 |
+
modelRotationY = config.modelRotationY !== undefined ? parseFloat(config.modelRotationY) : 0;
|
| 67 |
+
modelRotationZ = config.modelRotationZ !== undefined ? parseFloat(config.modelRotationZ) : 0;
|
| 68 |
+
|
| 69 |
+
const cameraX = config.cameraX !== undefined ? parseFloat(config.cameraX) : 0;
|
| 70 |
+
const cameraY = config.cameraY !== undefined ? parseFloat(config.cameraY) : 2;
|
| 71 |
+
const cameraZ = config.cameraZ !== undefined ? parseFloat(config.cameraZ) : 5;
|
| 72 |
+
const cameraXPhone = config.cameraXPhone !== undefined ? parseFloat(config.cameraXPhone) : cameraX;
|
| 73 |
+
const cameraYPhone = config.cameraYPhone !== undefined ? parseFloat(config.cameraYPhone) : cameraY;
|
| 74 |
+
const cameraZPhone = config.cameraZPhone !== undefined ? parseFloat(config.cameraZPhone) : cameraZ * 1.5;
|
| 75 |
|
| 76 |
chosenCameraX = isMobile ? cameraXPhone : cameraX;
|
| 77 |
chosenCameraY = isMobile ? cameraYPhone : cameraY;
|
| 78 |
chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
|
| 79 |
|
| 80 |
+
const canvasId = "canvas-" + instanceId;
|
| 81 |
+
const progressDialog = document.getElementById("progress-dialog-" + instanceId);
|
| 82 |
+
const progressIndicator = document.getElementById("progress-indicator-" + instanceId);
|
| 83 |
+
const viewerContainer = document.getElementById("viewer-container-" + instanceId);
|
| 84 |
|
| 85 |
+
// Remove existing canvas if any
|
| 86 |
let oldCanvas = document.getElementById(canvasId);
|
| 87 |
if (oldCanvas) oldCanvas.remove();
|
| 88 |
|
| 89 |
+
const canvas = document.createElement("canvas");
|
| 90 |
canvas.id = canvasId;
|
| 91 |
+
canvas.className = "ply-canvas";
|
| 92 |
canvas.style.width = "100%";
|
| 93 |
canvas.style.height = "100%";
|
| 94 |
+
canvas.setAttribute("tabindex", "0");
|
| 95 |
viewerContainer.insertBefore(canvas, progressDialog);
|
| 96 |
|
| 97 |
+
// Touch-action policies (for iOS and general mobile)
|
|
|
|
|
|
|
| 98 |
canvas.style.touchAction = "none";
|
| 99 |
canvas.style.webkitTouchCallout = "none";
|
| 100 |
+
canvas.addEventListener("gesturestart", (e) => e.preventDefault());
|
| 101 |
+
canvas.addEventListener("gesturechange", (e) => e.preventDefault());
|
| 102 |
+
canvas.addEventListener("gestureend", (e) => e.preventDefault());
|
| 103 |
+
canvas.addEventListener("dblclick", (e) => e.preventDefault());
|
| 104 |
+
canvas.addEventListener("touchstart", (e) => {
|
| 105 |
+
if (e.touches.length > 1) e.preventDefault();
|
|
|
|
|
|
|
|
|
|
| 106 |
}, { passive: false });
|
| 107 |
|
| 108 |
+
// Prevent scrolling/zooming the page while interacting with canvas
|
| 109 |
+
canvas.addEventListener("wheel", (e) => {
|
| 110 |
+
e.preventDefault();
|
| 111 |
+
}, { passive: false });
|
| 112 |
+
|
| 113 |
+
progressDialog.style.display = "block";
|
| 114 |
|
| 115 |
if (!pc) {
|
| 116 |
+
pc = await import("https://esm.run/playcanvas");
|
| 117 |
window.pc = pc;
|
| 118 |
}
|
| 119 |
|
| 120 |
+
// Fix for iOS context loss / restore: Try to always use 'webgl2' and fall back if fails
|
| 121 |
+
let device;
|
| 122 |
+
let gfxOptions = {
|
| 123 |
+
deviceTypes: ["webgl2", "webgl1"],
|
| 124 |
+
glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
|
| 125 |
+
twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
|
| 126 |
antialias: false
|
| 127 |
+
};
|
| 128 |
+
|
| 129 |
+
try {
|
| 130 |
+
device = await pc.createGraphicsDevice(canvas, gfxOptions);
|
| 131 |
+
} catch (e) {
|
| 132 |
+
// Fallback for strict iOS devices that might have context issues
|
| 133 |
+
gfxOptions.deviceTypes = ["webgl1"];
|
| 134 |
+
device = await pc.createGraphicsDevice(canvas, gfxOptions);
|
| 135 |
+
}
|
| 136 |
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 137 |
|
| 138 |
+
// App options: Always use canvas for input to avoid iOS weirdness
|
| 139 |
const opts = new pc.AppOptions();
|
| 140 |
opts.graphicsDevice = device;
|
| 141 |
opts.mouse = new pc.Mouse(canvas);
|
|
|
|
| 145 |
pc.CameraComponentSystem,
|
| 146 |
pc.LightComponentSystem,
|
| 147 |
pc.ScriptComponentSystem,
|
|
|
|
| 148 |
pc.GSplatComponentSystem,
|
| 149 |
pc.CollisionComponentSystem,
|
| 150 |
pc.RigidbodyComponentSystem
|
|
|
|
| 153 |
pc.TextureHandler,
|
| 154 |
pc.ContainerHandler,
|
| 155 |
pc.ScriptHandler,
|
|
|
|
| 156 |
pc.GSplatHandler
|
| 157 |
];
|
| 158 |
|
|
|
|
| 160 |
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 161 |
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 162 |
|
| 163 |
+
// Set background color (from config if available)
|
| 164 |
+
let bgColor = new pc.Color(1, 1, 1, 1); // Default: white
|
| 165 |
+
if (config.canvas_background) {
|
| 166 |
+
const bgCol = getColorFromHexOrString(config.canvas_background, null);
|
| 167 |
+
if (bgCol) bgColor = bgCol;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// Use ResizeObserver to track viewerContainer size
|
| 171 |
resizeObserver = new ResizeObserver(entries => {
|
| 172 |
entries.forEach(entry => {
|
| 173 |
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
|
|
|
| 175 |
});
|
| 176 |
resizeObserver.observe(viewerContainer);
|
| 177 |
|
| 178 |
+
// Additional resize handler for global changes
|
| 179 |
+
window.addEventListener("resize", () => {
|
| 180 |
+
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 181 |
+
});
|
| 182 |
+
app.on("destroy", () => resizeObserver.disconnect());
|
| 183 |
|
| 184 |
+
// Load assets: SOGS model (meta.json) and GLB (if any)
|
| 185 |
const assets = {
|
| 186 |
+
sogs: new pc.Asset("sogs", "gsplat", { url: sogsUrl }),
|
| 187 |
+
orbit: new pc.Asset("script", "script", { url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" }),
|
| 188 |
+
glb: new pc.Asset("glb", "container", { url: glbUrl })
|
| 189 |
};
|
| 190 |
for (const key in assets) app.assets.add(assets[key]);
|
| 191 |
|
| 192 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 193 |
loader.load(() => {
|
| 194 |
app.start();
|
| 195 |
+
progressDialog.style.display = "none";
|
| 196 |
|
| 197 |
+
// Add SOGS/GSplat model entity
|
| 198 |
+
modelEntity = new pc.Entity("model");
|
| 199 |
+
modelEntity.addComponent("gsplat", { asset: assets.sogs });
|
| 200 |
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 201 |
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 202 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 203 |
app.root.addChild(modelEntity);
|
| 204 |
|
| 205 |
+
// Optionally add GLB entity
|
| 206 |
if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
|
| 207 |
const glbEntity = assets.glb.resource.instantiateRenderEntity();
|
| 208 |
app.root.addChild(glbEntity);
|
| 209 |
}
|
| 210 |
|
| 211 |
+
// Set up camera entity and orbit controls
|
| 212 |
+
cameraEntity = new pc.Entity("camera");
|
| 213 |
+
cameraEntity.addComponent("camera", {
|
| 214 |
+
clearColor: bgColor,
|
| 215 |
+
clearColorBuffer: true,
|
| 216 |
+
// toneMapping: pc.TONEMAP_ACES, // optionally for realistic look
|
| 217 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 219 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 220 |
+
cameraEntity.addComponent("script");
|
| 221 |
|
| 222 |
+
cameraEntity.script.create("orbitCamera", {
|
|
|
|
| 223 |
attributes: {
|
| 224 |
focusEntity: modelEntity,
|
| 225 |
inertiaFactor: 0.2,
|
|
|
|
| 233 |
frameOnStart: false
|
| 234 |
}
|
| 235 |
});
|
| 236 |
+
cameraEntity.script.create("orbitCameraInputMouse");
|
| 237 |
+
cameraEntity.script.create("orbitCameraInputTouch");
|
| 238 |
app.root.addChild(cameraEntity);
|
| 239 |
|
| 240 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 241 |
|
| 242 |
+
app.once("update", () => resetViewerCamera());
|
| 243 |
|
| 244 |
+
// Tooltips support
|
| 245 |
try {
|
| 246 |
if (config.tooltips_url) {
|
| 247 |
+
import("https://mikafil-viewer-sgos.static.hf.space/tooltips.js").then(tooltipsModule => {
|
| 248 |
tooltipsModule.initializeTooltips({
|
| 249 |
app,
|
| 250 |
cameraEntity,
|
|
|
|
| 253 |
defaultVisible: !!config.showTooltipsDefault,
|
| 254 |
moveDuration: config.tooltipMoveDuration || 0.6
|
| 255 |
});
|
| 256 |
+
}).catch(() => {});
|
| 257 |
}
|
| 258 |
} catch (e) {}
|
| 259 |
|
|
|
|
| 261 |
});
|
| 262 |
}
|
| 263 |
|
|
|
|
| 264 |
export function resetViewerCamera() {
|
| 265 |
try {
|
| 266 |
if (!cameraEntity || !modelEntity || !app) return;
|
|
|
|
| 282 |
|
| 283 |
orbitCam.pivotPoint = modelPos.clone();
|
| 284 |
orbitCam._targetDistance = dist;
|
| 285 |
+
orbitCam._distance = dist;
|
| 286 |
|
| 287 |
const rot = tempEnt.getRotation();
|
| 288 |
const fwd = new pc.Vec3();
|
|
|
|
| 295 |
rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
|
| 296 |
const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
|
| 297 |
|
| 298 |
+
orbitCam._targetYaw = yaw;
|
| 299 |
+
orbitCam._yaw = yaw;
|
| 300 |
orbitCam._targetPitch = pitch;
|
| 301 |
+
orbitCam._pitch = pitch;
|
| 302 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 303 |
|
| 304 |
tempEnt.destroy();
|