Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -13,129 +13,89 @@ let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, min
|
|
| 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
|
| 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 =
|
| 81 |
-
const progressDialog = document.getElementById(
|
| 82 |
-
const progressIndicator = document.getElementById(
|
| 83 |
-
const viewerContainer = document.getElementById(
|
| 84 |
|
| 85 |
-
// Remove existing canvas if any
|
| 86 |
let oldCanvas = document.getElementById(canvasId);
|
| 87 |
if (oldCanvas) oldCanvas.remove();
|
| 88 |
|
| 89 |
-
const canvas = document.createElement(
|
| 90 |
canvas.id = canvasId;
|
| 91 |
-
canvas.className =
|
| 92 |
canvas.style.width = "100%";
|
| 93 |
canvas.style.height = "100%";
|
| 94 |
-
canvas.setAttribute(
|
| 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(
|
| 101 |
-
canvas.addEventListener(
|
| 102 |
-
canvas.addEventListener(
|
| 103 |
-
canvas.addEventListener(
|
| 104 |
-
canvas.addEventListener(
|
| 105 |
-
|
| 106 |
-
|
| 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 =
|
| 114 |
|
| 115 |
if (!pc) {
|
| 116 |
pc = await import("https://esm.run/playcanvas");
|
| 117 |
window.pc = pc;
|
| 118 |
}
|
| 119 |
|
| 120 |
-
//
|
| 121 |
-
|
| 122 |
-
|
| 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);
|
|
@@ -160,14 +120,6 @@ export async function initializeViewer(config, instanceId) {
|
|
| 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,51 +127,53 @@ export async function initializeViewer(config, instanceId) {
|
|
| 175 |
});
|
| 176 |
resizeObserver.observe(viewerContainer);
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 181 |
-
});
|
| 182 |
-
app.on("destroy", () => resizeObserver.disconnect());
|
| 183 |
|
| 184 |
-
//
|
| 185 |
const assets = {
|
| 186 |
-
|
| 187 |
-
orbit: new pc.Asset(
|
| 188 |
-
glb: new pc.Asset(
|
| 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 =
|
| 196 |
|
| 197 |
-
// Add
|
| 198 |
-
modelEntity = new pc.Entity(
|
| 199 |
-
modelEntity.addComponent(
|
| 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 |
-
//
|
| 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 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 219 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 220 |
-
cameraEntity.addComponent(
|
| 221 |
|
| 222 |
-
cameraEntity.script.create(
|
| 223 |
attributes: {
|
| 224 |
focusEntity: modelEntity,
|
| 225 |
inertiaFactor: 0.2,
|
|
@@ -233,18 +187,18 @@ export async function initializeViewer(config, instanceId) {
|
|
| 233 |
frameOnStart: false
|
| 234 |
}
|
| 235 |
});
|
| 236 |
-
cameraEntity.script.create(
|
| 237 |
-
cameraEntity.script.create(
|
| 238 |
app.root.addChild(cameraEntity);
|
| 239 |
|
| 240 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 241 |
|
| 242 |
-
app.once(
|
| 243 |
|
| 244 |
-
// Tooltips
|
| 245 |
try {
|
| 246 |
if (config.tooltips_url) {
|
| 247 |
-
import(
|
| 248 |
tooltipsModule.initializeTooltips({
|
| 249 |
app,
|
| 250 |
cameraEntity,
|
|
@@ -253,7 +207,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 253 |
defaultVisible: !!config.showTooltipsDefault,
|
| 254 |
moveDuration: config.tooltipMoveDuration || 0.6
|
| 255 |
});
|
| 256 |
-
}).catch(
|
| 257 |
}
|
| 258 |
} catch (e) {}
|
| 259 |
|
|
@@ -282,7 +236,7 @@ export function resetViewerCamera() {
|
|
| 282 |
|
| 283 |
orbitCam.pivotPoint = modelPos.clone();
|
| 284 |
orbitCam._targetDistance = dist;
|
| 285 |
-
orbitCam._distance
|
| 286 |
|
| 287 |
const rot = tempEnt.getRotation();
|
| 288 |
const fwd = new pc.Vec3();
|
|
@@ -295,10 +249,10 @@ export function resetViewerCamera() {
|
|
| 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
|
| 299 |
-
orbitCam._yaw
|
| 300 |
orbitCam._targetPitch = pitch;
|
| 301 |
-
orbitCam._pitch
|
| 302 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 303 |
|
| 304 |
tempEnt.destroy();
|
|
|
|
| 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 |
+
// ==== MOBILE DETECTION EXACTLY AS IN PLY VIEWER ====
|
| 20 |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
| 21 |
+
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
|
|
|
|
| 22 |
|
| 23 |
+
// ==== ASSET URLS (SOGS/GSPLAT INSTEAD OF PLY) ====
|
| 24 |
sogsUrl = config.sogs_json_url;
|
| 25 |
glbUrl = config.glb_url;
|
|
|
|
| 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 = 'ply-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 |
+
// Mouse wheel suppression to canvas only
|
| 79 |
+
canvas.addEventListener('wheel', (e) => {
|
|
|
|
|
|
|
| 80 |
e.preventDefault();
|
| 81 |
}, { passive: false });
|
| 82 |
|
| 83 |
+
progressDialog.style.display = 'block';
|
| 84 |
|
| 85 |
if (!pc) {
|
| 86 |
pc = await import("https://esm.run/playcanvas");
|
| 87 |
window.pc = pc;
|
| 88 |
}
|
| 89 |
|
| 90 |
+
// === EXACT iOS/Android/Windows HANDLING AS WORKING PLY VIEWER ===
|
| 91 |
+
const device = await pc.createGraphicsDevice(canvas, {
|
| 92 |
+
deviceTypes: ["webgl2"],
|
|
|
|
| 93 |
glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
|
| 94 |
twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
|
| 95 |
antialias: false
|
| 96 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 98 |
|
|
|
|
| 99 |
const opts = new pc.AppOptions();
|
| 100 |
opts.graphicsDevice = device;
|
| 101 |
opts.mouse = new pc.Mouse(canvas);
|
|
|
|
| 120 |
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 121 |
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
resizeObserver = new ResizeObserver(entries => {
|
| 124 |
entries.forEach(entry => {
|
| 125 |
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
|
|
|
| 127 |
});
|
| 128 |
resizeObserver.observe(viewerContainer);
|
| 129 |
|
| 130 |
+
window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
|
| 131 |
+
app.on('destroy', () => resizeObserver.disconnect());
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
// === SOGS (not PLY) asset ===
|
| 134 |
const assets = {
|
| 135 |
+
model: new pc.Asset('gsplat', 'gsplat', { url: sogsUrl }),
|
| 136 |
+
orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-sgos.static.hf.space/orbit-camera.js" }),
|
| 137 |
+
glb: new pc.Asset('glb', 'container', { url: glbUrl }),
|
| 138 |
};
|
| 139 |
for (const key in assets) app.assets.add(assets[key]);
|
| 140 |
|
| 141 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 142 |
loader.load(() => {
|
| 143 |
app.start();
|
| 144 |
+
progressDialog.style.display = 'none';
|
| 145 |
|
| 146 |
+
// Add GSplat model
|
| 147 |
+
modelEntity = new pc.Entity('model');
|
| 148 |
+
modelEntity.addComponent('gsplat', { asset: assets.model });
|
| 149 |
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
| 150 |
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
|
| 151 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 152 |
app.root.addChild(modelEntity);
|
| 153 |
|
| 154 |
+
// --- Add the GLB entity ---
|
| 155 |
if (assets.glb && assets.glb.resource && assets.glb.resource.instantiateRenderEntity) {
|
| 156 |
const glbEntity = assets.glb.resource.instantiateRenderEntity();
|
| 157 |
app.root.addChild(glbEntity);
|
| 158 |
}
|
| 159 |
|
| 160 |
+
cameraEntity = new pc.Entity('camera');
|
| 161 |
+
// Default background color (white) or config value
|
| 162 |
+
let bgColor = new pc.Color(1, 1, 1, 1);
|
| 163 |
+
if (config.canvas_background && /^#[A-Fa-f0-9]{6}$/.test(config.canvas_background)) {
|
| 164 |
+
let hex = config.canvas_background.replace("#", "");
|
| 165 |
+
bgColor = new pc.Color(
|
| 166 |
+
parseInt(hex.substr(0,2),16)/255,
|
| 167 |
+
parseInt(hex.substr(2,2),16)/255,
|
| 168 |
+
parseInt(hex.substr(4,2),16)/255
|
| 169 |
+
);
|
| 170 |
+
}
|
| 171 |
+
cameraEntity.addComponent('camera', { clearColor: bgColor });
|
| 172 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 173 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 174 |
+
cameraEntity.addComponent('script');
|
| 175 |
|
| 176 |
+
cameraEntity.script.create('orbitCamera', {
|
| 177 |
attributes: {
|
| 178 |
focusEntity: modelEntity,
|
| 179 |
inertiaFactor: 0.2,
|
|
|
|
| 187 |
frameOnStart: false
|
| 188 |
}
|
| 189 |
});
|
| 190 |
+
cameraEntity.script.create('orbitCameraInputMouse');
|
| 191 |
+
cameraEntity.script.create('orbitCameraInputTouch');
|
| 192 |
app.root.addChild(cameraEntity);
|
| 193 |
|
| 194 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 195 |
|
| 196 |
+
app.once('update', () => resetViewerCamera());
|
| 197 |
|
| 198 |
+
// Tooltips supported if tooltips_url set
|
| 199 |
try {
|
| 200 |
if (config.tooltips_url) {
|
| 201 |
+
import('https://mikafil-viewer-sgos.static.hf.space/tooltips.js').then(tooltipsModule => {
|
| 202 |
tooltipsModule.initializeTooltips({
|
| 203 |
app,
|
| 204 |
cameraEntity,
|
|
|
|
| 207 |
defaultVisible: !!config.showTooltipsDefault,
|
| 208 |
moveDuration: config.tooltipMoveDuration || 0.6
|
| 209 |
});
|
| 210 |
+
}).catch(e => {});
|
| 211 |
}
|
| 212 |
} catch (e) {}
|
| 213 |
|
|
|
|
| 236 |
|
| 237 |
orbitCam.pivotPoint = modelPos.clone();
|
| 238 |
orbitCam._targetDistance = dist;
|
| 239 |
+
orbitCam._distance = dist;
|
| 240 |
|
| 241 |
const rot = tempEnt.getRotation();
|
| 242 |
const fwd = new pc.Vec3();
|
|
|
|
| 249 |
rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
|
| 250 |
const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
|
| 251 |
|
| 252 |
+
orbitCam._targetYaw = yaw;
|
| 253 |
+
orbitCam._yaw = yaw;
|
| 254 |
orbitCam._targetPitch = pitch;
|
| 255 |
+
orbitCam._pitch = pitch;
|
| 256 |
if (orbitCam._updatePosition) orbitCam._updatePosition();
|
| 257 |
|
| 258 |
tempEnt.destroy();
|