732 lines
35 KiB
HTML
732 lines
35 KiB
HTML
<!doctype html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>Real2Sim Scene Viewer</title>
|
||
<link rel="icon" href="data:,">
|
||
<style>
|
||
* { box-sizing: border-box; }
|
||
body { margin: 0; overflow: hidden; background: #080b0d; color: #e7edf2; font: 13px/1.4 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
|
||
#hud { position: fixed; z-index: 10; left: 12px; top: 12px; width: min(560px, calc(100vw - 24px)); min-width: 280px; min-height: 76px; max-width: calc(100vw - 24px); max-height: calc(100vh - 24px); padding: 10px 12px; border: 1px solid #2a343d; border-radius: 8px; background: rgba(12, 17, 22, 0.92); box-shadow: 0 12px 30px rgba(0, 0, 0, 0.25); overflow: hidden; }
|
||
#hudTitlebar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: -10px -12px 8px; padding: 9px 10px 7px 12px; border-bottom: 1px solid rgba(75, 91, 104, 0.45); cursor: grab; user-select: none; touch-action: none; }
|
||
#hudTitlebar:active { cursor: grabbing; }
|
||
#hud strong { display: block; margin: 0; }
|
||
#hudToggle { flex: 0 0 auto; margin: 0; min-width: 28px; min-height: 26px; padding: 2px 7px; font-size: 15px; line-height: 1; }
|
||
#hudContent { overflow: auto; max-height: calc(100vh - 116px); }
|
||
#hud.is-collapsed { width: auto !important; min-width: 0; min-height: 0; height: auto !important; padding-bottom: 10px; resize: none; }
|
||
#hud.is-collapsed #hudTitlebar { margin-bottom: -1px; border-bottom: 0; }
|
||
#hud.is-collapsed #hudContent, #hud.is-collapsed #hudResizer { display: none; }
|
||
#hudResizer { position: absolute; right: 1px; bottom: 1px; width: 17px; height: 17px; cursor: nwse-resize; touch-action: none; background: linear-gradient(135deg, transparent 46%, #52717a 47%, #52717a 55%, transparent 56%, transparent 64%, #52717a 65%, #52717a 73%, transparent 74%); opacity: 0.8; }
|
||
#hud div { color: #9aa8b4; overflow-wrap: anywhere; }
|
||
#hint { margin-top: 6px; color: #c4d0dc; }
|
||
#assetList { margin-top: 8px; max-height: 120px; overflow: auto; }
|
||
#assetList button[data-autoload="false"] { border-color: #6b5a33; }
|
||
#assetList button[data-renderable="false"] { opacity: 0.72; border-color: #33404a; }
|
||
#assetList button[data-quality="recommended"] { border-color: #28d8bd; box-shadow: inset 0 0 0 1px rgba(40, 216, 189, 0.18); }
|
||
#assetList button[data-quality="full"] { border-color: #c9a85d; }
|
||
#assetList button[data-quality="diagnostic"] { opacity: 0.72; }
|
||
button { margin: 3px 4px 0 0; border: 1px solid #2a343d; border-radius: 6px; background: #17232b; color: #e7edf2; padding: 5px 8px; cursor: pointer; }
|
||
canvas { display: block; width: 100vw; height: 100vh; }
|
||
@media (max-width: 640px) {
|
||
#hud { left: 8px; top: 8px; width: calc(100vw - 16px); min-width: 0; max-height: min(46vh, 360px); }
|
||
#hudContent { max-height: calc(min(46vh, 360px) - 56px); }
|
||
#hudResizer { display: none; }
|
||
}
|
||
</style>
|
||
<script type="importmap">
|
||
{
|
||
"imports": {
|
||
"three": "/static/vendor/three/three.module.js",
|
||
"three/addons/": "/static/vendor/three/addons/"
|
||
}
|
||
}
|
||
</script>
|
||
</head>
|
||
<body>
|
||
<div id="hud" aria-label="场景预览资源面板">
|
||
<div id="hudTitlebar">
|
||
<strong>Real2Sim Scene Viewer</strong>
|
||
<button id="hudToggle" type="button" title="收起资源面板" aria-label="收起资源面板">−</button>
|
||
</div>
|
||
<div id="hudContent">
|
||
<div id="status">loading</div>
|
||
<div id="hint"></div>
|
||
<div id="assetList"></div>
|
||
</div>
|
||
<div id="hudResizer" aria-label="调整面板大小" title="拖动调整面板大小"></div>
|
||
</div>
|
||
<script type="module">
|
||
import * as THREE from "three";
|
||
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
|
||
import { OBJLoader } from "three/addons/loaders/OBJLoader.js";
|
||
import { PLYLoader } from "three/addons/loaders/PLYLoader.js";
|
||
|
||
const status = document.getElementById("status");
|
||
const hint = document.getElementById("hint");
|
||
const assetList = document.getElementById("assetList");
|
||
const hud = document.getElementById("hud");
|
||
const hudTitlebar = document.getElementById("hudTitlebar");
|
||
const hudToggle = document.getElementById("hudToggle");
|
||
const hudResizer = document.getElementById("hudResizer");
|
||
const params = new URLSearchParams(location.search);
|
||
const sceneId = params.get("scene_id");
|
||
const embeddedMode = params.get("embedded") === "1";
|
||
const coverMode = params.get("cover") === "1";
|
||
const coverView = params.get("cover_view") || "hero";
|
||
const probeFull = params.get("probe_full") === "1";
|
||
if (coverMode) document.getElementById("hud").style.display = "none";
|
||
setupHudPanel();
|
||
if (embeddedMode || innerWidth <= 640) setHudCollapsed(true);
|
||
const nativeWarn = console.warn.bind(console);
|
||
console.warn = (...args) => {
|
||
const message = args.map((item) => item?.message || String(item)).join(" ");
|
||
if (message.includes("THREE.Clock: This module has been deprecated")) {
|
||
console.debug(...args);
|
||
return;
|
||
}
|
||
nativeWarn(...args);
|
||
};
|
||
|
||
const renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: coverMode });
|
||
const basePixelRatio = Math.min(devicePixelRatio, 2);
|
||
renderer.setPixelRatio(basePixelRatio);
|
||
renderer.setSize(innerWidth, innerHeight);
|
||
renderer.setClearColor(0x080b0d, 1);
|
||
document.body.appendChild(renderer.domElement);
|
||
|
||
const scene = new THREE.Scene();
|
||
const camera = new THREE.PerspectiveCamera(55, innerWidth / innerHeight, 0.01, 1000);
|
||
setCoverCamera();
|
||
const controls = new OrbitControls(camera, renderer.domElement);
|
||
controls.target.set(0, 0, 0);
|
||
controls.enableDamping = true;
|
||
|
||
const gridHelper = new THREE.GridHelper(4, 16, 0x2f6d68, 0x24313a);
|
||
const axesHelper = new THREE.AxesHelper(1.2);
|
||
scene.add(gridHelper);
|
||
scene.add(axesHelper);
|
||
const light = new THREE.DirectionalLight(0xffffff, 1.5);
|
||
light.position.set(3, 4, 2);
|
||
scene.add(light);
|
||
scene.add(new THREE.AmbientLight(0x9fb6c8, 0.8));
|
||
|
||
const fitRoot = new THREE.Group();
|
||
const contentRoot = new THREE.Group();
|
||
fitRoot.add(contentRoot);
|
||
scene.add(fitRoot);
|
||
|
||
let loadedObject = null;
|
||
let overlayObjects = [];
|
||
let manifestAssets = [];
|
||
let sourceUpAxis = "Z";
|
||
let sparkRenderer = null;
|
||
let sparkModule = null;
|
||
const debug = {
|
||
backend: "loading",
|
||
loaded: false,
|
||
assetName: "",
|
||
pointCount: 0,
|
||
overlayCount: 0,
|
||
errors: [],
|
||
warnings: [],
|
||
canvas: { width: 0, height: 0 },
|
||
};
|
||
window.real2simViewerDebug = debug;
|
||
window.real2simViewerDebug.snapshot = () => ({ ...debug, canvas: { ...debug.canvas } });
|
||
|
||
function setupHudPanel() {
|
||
let drag = null;
|
||
let resize = null;
|
||
const clamp = (value, min, max) => Math.max(min, Math.min(value, max));
|
||
const clearPointerState = () => {
|
||
drag = null;
|
||
resize = null;
|
||
};
|
||
|
||
hudToggle.addEventListener("click", (event) => {
|
||
event.stopPropagation();
|
||
setHudCollapsed(!hud.classList.contains("is-collapsed"));
|
||
});
|
||
|
||
hudTitlebar.addEventListener("pointerdown", (event) => {
|
||
if (event.target.closest("button") || hud.classList.contains("is-collapsed")) return;
|
||
const rect = hud.getBoundingClientRect();
|
||
drag = { offsetX: event.clientX - rect.left, offsetY: event.clientY - rect.top };
|
||
hudTitlebar.setPointerCapture(event.pointerId);
|
||
event.preventDefault();
|
||
});
|
||
hudResizer.addEventListener("pointerdown", (event) => {
|
||
if (hud.classList.contains("is-collapsed")) return;
|
||
const rect = hud.getBoundingClientRect();
|
||
resize = { startX: event.clientX, startY: event.clientY, width: rect.width, height: rect.height };
|
||
hudResizer.setPointerCapture(event.pointerId);
|
||
event.preventDefault();
|
||
});
|
||
addEventListener("pointermove", (event) => {
|
||
if (drag) {
|
||
const rect = hud.getBoundingClientRect();
|
||
const left = clamp(event.clientX - drag.offsetX, 8, innerWidth - rect.width - 8);
|
||
const top = clamp(event.clientY - drag.offsetY, 8, innerHeight - rect.height - 8);
|
||
hud.style.left = `${left}px`;
|
||
hud.style.top = `${top}px`;
|
||
hud.style.right = "auto";
|
||
hud.style.bottom = "auto";
|
||
}
|
||
if (resize) {
|
||
const width = clamp(resize.width + event.clientX - resize.startX, 280, innerWidth - hud.offsetLeft - 8);
|
||
const height = clamp(resize.height + event.clientY - resize.startY, 76, innerHeight - hud.offsetTop - 8);
|
||
hud.style.width = `${width}px`;
|
||
hud.style.height = `${height}px`;
|
||
document.getElementById("hudContent").style.maxHeight = `${Math.max(32, height - 56)}px`;
|
||
}
|
||
});
|
||
addEventListener("pointerup", clearPointerState);
|
||
addEventListener("pointercancel", clearPointerState);
|
||
}
|
||
|
||
function setHudCollapsed(collapsed) {
|
||
hud.classList.toggle("is-collapsed", collapsed);
|
||
hudToggle.textContent = collapsed ? "+" : "−";
|
||
hudToggle.title = collapsed ? "展开资源面板" : "收起资源面板";
|
||
hudToggle.setAttribute("aria-label", hudToggle.title);
|
||
}
|
||
|
||
async function main() {
|
||
if (!sceneId) {
|
||
addPlaceholder("missing scene_id");
|
||
return;
|
||
}
|
||
const manifest = await fetch(`/api/scenes/${encodeURIComponent(sceneId)}/manifest`).then((response) => {
|
||
if (!response.ok) throw new Error(`manifest request failed: ${response.status}`);
|
||
return response.json();
|
||
});
|
||
const assets = Array.isArray(manifest.assets) ? prioritizeAssets(manifest.assets) : [];
|
||
manifestAssets = assets;
|
||
sourceUpAxis = String(manifest.coordinate_frame?.up_axis || "Z").toUpperCase();
|
||
contentRoot.rotation.set(sourceUpAxis === "Z" ? -Math.PI / 2 : 0, 0, 0);
|
||
const renderable = assets.filter(isRenderableAsset);
|
||
assetList.innerHTML = assets.slice(0, 20).map((item, index) => {
|
||
const quality = assetQuality(item, index);
|
||
const renderable = isRenderableAsset(item);
|
||
const label = [assetQualityLabel(quality), assetTypeLabel(item), item.name || item.path || "asset", item.size_label || "", item.autoload === false ? "手动加载" : ""].filter(Boolean).join(" · ");
|
||
const title = renderable ? "加载此预览资产" : "查看资源说明,不会替换当前 3D 预览";
|
||
return `<button data-index="${index}" data-renderable="${renderable}" data-autoload="${item.autoload !== false}" data-quality="${quality}" title="${escapeHtml(title)}">${escapeHtml(label)}</button>`;
|
||
}).join("");
|
||
assetList.addEventListener("click", (event) => {
|
||
const button = event.target.closest("[data-index]");
|
||
if (!button) return;
|
||
if (button.dataset.renderable === "false") {
|
||
showAssetInfo(assets[Number(button.dataset.index)]);
|
||
return;
|
||
}
|
||
const selected = assets[Number(button.dataset.index)];
|
||
if (selected?.role === "added_asset") {
|
||
status.textContent = `已添加资产 · ${selected.name || "asset"} · 与 MotrixSim 场景使用同一坐标`;
|
||
return;
|
||
}
|
||
loadAssetWithSceneFallback(selected, assets, { manual: true }).then(() => loadSceneOverlays(assets)).catch(showError);
|
||
});
|
||
status.textContent = rendererStatusText(manifest, assets.length);
|
||
hint.textContent = viewerHint(assets, renderable);
|
||
const recommended = renderable[0] || null;
|
||
const previewImage = preferredScenePreviewImage(renderable);
|
||
const preferred = recommended?.autoload !== false
|
||
? recommended
|
||
: previewImage || renderable.find((item) => item.autoload !== false) || null;
|
||
if (preferred) {
|
||
// Large 3DGS assets remain an explicit user action. Their real cover is
|
||
// a safer first frame than an automatically generated diagnostic mesh.
|
||
await loadAssetWithSceneFallback(preferred, assets, { manual: preferred === previewImage });
|
||
await loadSceneOverlays(assets);
|
||
}
|
||
else if (renderable[0]) addPlaceholder(`可渲染大文件未自动加载:${renderable[0].name || "asset"} · ${renderable[0].size_label || "unknown size"} · 点击按钮手动加载`);
|
||
else addPlaceholder("当前清单没有可直接渲染的 PLY/SPZ/SPLAT/GLB/OBJ 资产");
|
||
if (probeFull) {
|
||
const full = assets.find((item) => {
|
||
const name = String(item.name || item.path || "").toLowerCase();
|
||
return name.endsWith(".spz") || name.endsWith(".splat") || name.endsWith(".ksplat") || name.endsWith(".sog");
|
||
}) || assets.find((item) => assetQuality(item, 1) === "full" && String(item.name || item.path || "").toLowerCase().includes("scene.ply"));
|
||
if (full) {
|
||
const started = performance.now();
|
||
try {
|
||
await loadAsset(full, { manual: true });
|
||
writeProbeResult({ loaded: debug.loaded, backend: debug.backend, assetName: debug.assetName, pointCount: debug.pointCount, elapsedMs: Math.round(performance.now() - started), canvas: debug.canvas, warnings: debug.warnings, errors: debug.errors });
|
||
} catch (error) {
|
||
writeProbeResult({ loaded: false, error: error.message || String(error), elapsedMs: Math.round(performance.now() - started), warnings: debug.warnings, errors: debug.errors });
|
||
}
|
||
} else {
|
||
writeProbeResult({ loaded: false, error: "full 3DGS asset not found" });
|
||
}
|
||
}
|
||
}
|
||
|
||
function writeProbeResult(result) {
|
||
let node = document.getElementById("probeResult");
|
||
if (!node) {
|
||
node = document.createElement("pre");
|
||
node.id = "probeResult";
|
||
node.style.display = "none";
|
||
document.body.appendChild(node);
|
||
}
|
||
node.textContent = JSON.stringify(result);
|
||
document.body.dataset.probeResult = node.textContent;
|
||
}
|
||
|
||
async function loadAsset(asset, options = {}) {
|
||
if (!asset?.url) {
|
||
addPlaceholder("asset has no preview URL");
|
||
return;
|
||
}
|
||
if (asset.autoload === false && !options.manual) {
|
||
addPlaceholder(`large asset skipped: ${asset.name || "asset"} · ${asset.size_label || "unknown size"} · click to load`);
|
||
return;
|
||
}
|
||
if (loadedObject) contentRoot.remove(loadedObject);
|
||
clearOverlays();
|
||
const suffix = (asset.name || asset.path || "").toLowerCase().split(".").pop();
|
||
const imageAsset = ["png", "jpg", "jpeg", "webp"].includes(suffix);
|
||
if (!imageAsset) prepareGeometryScene();
|
||
debug.loaded = false;
|
||
debug.assetName = asset.name || asset.path || "asset";
|
||
debug.pointCount = 0;
|
||
setRenderQualityForAsset(asset);
|
||
const size = asset.size_label ? ` · ${asset.size_label}` : "";
|
||
if (suffix === "ply" && shouldUseSparkForPly(asset)) {
|
||
await withLoadingTimeout(loadSparkOrFallback(asset, suffix), `loading timed out: ${asset.name || "asset"}${size}`, 90000);
|
||
} else if (suffix === "ply") {
|
||
await loadPlyFallback(asset);
|
||
} else if (["spz", "splat", "ksplat", "sog", "zip", "rad"].includes(suffix)) {
|
||
await withLoadingTimeout(loadSparkOrFallback(asset, suffix), `loading timed out: ${asset.name || "asset"}${size}`, 20000);
|
||
} else if (suffix === "glb" || suffix === "gltf") {
|
||
status.textContent = `Three.js fallback · loading ${asset.name}${size}`;
|
||
const { GLTFLoader } = await import("three/addons/loaders/GLTFLoader.js");
|
||
const gltf = await withLoadingTimeout(new GLTFLoader().loadAsync(asset.url), `loading timed out: ${asset.name || "asset"}${size}`);
|
||
loadedObject = gltf.scene;
|
||
contentRoot.add(loadedObject);
|
||
normalizeObjectFromBox(loadedObject);
|
||
debug.backend = "three_fallback";
|
||
debug.loaded = true;
|
||
status.textContent = `Three.js fallback · ${asset.name}`;
|
||
} else if (suffix === "obj") {
|
||
status.textContent = `Three.js fallback · loading ${asset.name}${size}`;
|
||
loadedObject = await withLoadingTimeout(new OBJLoader().loadAsync(asset.url), `loading timed out: ${asset.name || "asset"}${size}`);
|
||
contentRoot.add(loadedObject);
|
||
normalizeObjectFromBox(loadedObject);
|
||
debug.backend = "three_fallback";
|
||
debug.loaded = true;
|
||
status.textContent = `Three.js fallback · ${asset.name}`;
|
||
} else if (["xml", "urdf", "mjcf"].includes(suffix)) {
|
||
status.textContent = "这是仿真描述文件,用于 MotrixSim/MuJoCo 加载,不会替换当前 3D 预览";
|
||
} else if (suffix === "py") {
|
||
status.textContent = "这是控制脚本,用于运行策略或示例,不会替换当前 3D 预览";
|
||
} else if (imageAsset) {
|
||
await addImagePreview(asset);
|
||
} else {
|
||
addPlaceholder(`当前浏览器预览暂不支持 .${suffix || "file"} 文件`);
|
||
}
|
||
}
|
||
|
||
async function loadAssetWithSceneFallback(asset, assets, options = {}) {
|
||
try {
|
||
await loadAsset(asset, options);
|
||
return;
|
||
} catch (error) {
|
||
const suffix = String(asset?.name || asset?.path || "").toLowerCase().split(".").pop();
|
||
const splatFormat = ["spz", "splat", "ksplat", "sog", "zip", "rad"].includes(suffix);
|
||
const fallback = splatFormat
|
||
? assets.find((item) => item?.role === "visual_fallback" && ["glb", "gltf", "obj"].includes(String(item?.name || item?.path || "").toLowerCase().split(".").pop()))
|
||
: null;
|
||
if (!fallback || fallback === asset) throw error;
|
||
const reason = error?.message || String(error);
|
||
debug.warnings.push(`Spark ${asset.name || "asset"}: ${reason}`);
|
||
status.textContent = `Spark 未就绪,正在切换 Three.js fallback · ${fallback.name || "mesh"}`;
|
||
await loadAsset(fallback, { manual: true });
|
||
status.textContent = `Three.js fallback · ${fallback.name || "mesh"} · SPZ 可手动重试`;
|
||
}
|
||
}
|
||
|
||
function isRenderableAsset(asset) {
|
||
if (asset?.role === "added_asset" && asset?.viewer_primitive) return true;
|
||
const suffix = String(asset?.name || asset?.path || "").toLowerCase().split(".").pop();
|
||
return ["ply", "spz", "splat", "ksplat", "sog", "glb", "gltf", "obj", "png", "jpg", "jpeg", "webp"].includes(suffix);
|
||
}
|
||
|
||
function assetTypeLabel(asset) {
|
||
if (asset?.role === "added_asset") return "已添加资产";
|
||
const suffix = String(asset?.name || asset?.path || "").toLowerCase().split(".").pop();
|
||
const kind = String(asset?.kind || "");
|
||
if (["ply", "spz", "splat", "ksplat", "sog", "glb", "gltf", "obj"].includes(suffix)) return "可预览";
|
||
if (["png", "jpg", "jpeg", "webp"].includes(suffix)) return "图片";
|
||
if (["xml", "urdf", "mjcf"].includes(suffix) || kind === "simulator") return "仿真文件";
|
||
if (suffix === "py" || kind === "controller") return "控制脚本";
|
||
return "资源";
|
||
}
|
||
|
||
function viewerHint(assets, renderable) {
|
||
if (renderable.some((asset) => !["png", "jpg", "jpeg", "webp"].includes(String(asset.name || asset.path || "").toLowerCase().split(".").pop()))) {
|
||
return "可直接预览 PLY、SPZ/SPLAT、GLB/OBJ 等资产;XML 和 Python 只作为仿真/控制资源列出。";
|
||
}
|
||
if (assets.length) return "当前包主要是仿真 XML、控制脚本和参考图;需要生成 PLY/GLB/3DGS 后才能看到真实三维场景。";
|
||
return "当前场景清单为空。";
|
||
}
|
||
|
||
function showAssetInfo(asset) {
|
||
const suffix = String(asset?.name || asset?.path || "").toLowerCase().split(".").pop();
|
||
const name = asset?.name || asset?.path || "资源";
|
||
const size = asset?.size_label ? ` · ${asset.size_label}` : "";
|
||
const labels = {
|
||
xml: "仿真描述文件,用于 MuJoCo/MotrixSim 加载",
|
||
urdf: "机器人/场景描述文件,用于仿真器加载",
|
||
mjcf: "MuJoCo MJCF 文件,用于仿真器加载",
|
||
py: "控制脚本,用于运行策略或示例",
|
||
json: "结构化报告或轨迹数据,可在任务/清单中查看",
|
||
md: "说明文档",
|
||
txt: "文本资源",
|
||
gif: "rollout 回放资源,可在场景管理的资产区查看",
|
||
mp4: "视频资源,可在场景管理的资产区查看",
|
||
};
|
||
status.textContent = `${labels[suffix] || "资源文件"} · ${name}${size}`;
|
||
hint.textContent = "这个条目不会替换当前 3D 场景。真正可旋转预览的是 GLB/OBJ/PLY/3DGS 等几何资产。";
|
||
}
|
||
|
||
async function addImagePreview(asset) {
|
||
if (loadedObject) contentRoot.remove(loadedObject);
|
||
clearOverlays();
|
||
loadedObject = null;
|
||
status.textContent = `图片预览 · loading ${asset.name || "asset"}`;
|
||
const texture = await withLoadingTimeout(new THREE.TextureLoader().loadAsync(asset.url), `loading timed out: ${asset.name || "asset"}`);
|
||
texture.colorSpace = THREE.SRGBColorSpace;
|
||
scene.background = texture;
|
||
gridHelper.visible = false;
|
||
axesHelper.visible = false;
|
||
debug.backend = "image_preview";
|
||
debug.loaded = true;
|
||
debug.pointCount = 0;
|
||
status.textContent = `图片预览 · ${asset.name || "asset"}`;
|
||
}
|
||
|
||
function prepareGeometryScene() {
|
||
scene.background = null;
|
||
gridHelper.visible = true;
|
||
axesHelper.visible = true;
|
||
}
|
||
|
||
function setRenderQualityForAsset(asset) {
|
||
const sizeBytes = Number(asset?.size_bytes || 0);
|
||
const heavy = shouldUseSparkForPly(asset) || sizeBytes > 80 * 1024 * 1024;
|
||
renderer.setPixelRatio(heavy ? 1 : basePixelRatio);
|
||
renderer.setSize(innerWidth, innerHeight);
|
||
}
|
||
|
||
function shouldUseSparkForPly(asset) {
|
||
const name = String(asset?.name || "").toLowerCase();
|
||
const path = String(asset?.path || asset?.url || "").toLowerCase();
|
||
const sizeBytes = Number(asset?.size_bytes || 0);
|
||
if (name.endsWith("_threejs_rgb.ply")) return false;
|
||
if (name.includes("colmap_sparse") || name.includes("sparse_points")) return false;
|
||
if (name === "pose_init_trained_scene.ply") return true;
|
||
return name === "scene.ply" && (path.includes("/3dgs/") || sizeBytes > 50 * 1024 * 1024);
|
||
}
|
||
|
||
function isScenePreviewImage(asset) {
|
||
const name = String(asset?.name || asset?.path || "").toLowerCase();
|
||
return name === "scene_cover.png"
|
||
|| name.endsWith("/scene_cover.png")
|
||
|| name === "source_gs_preview.png"
|
||
|| name.endsWith("/source_gs_preview.png")
|
||
|| name === "pointcloud_projection_preview.png"
|
||
|| name.endsWith("/pointcloud_projection_preview.png");
|
||
}
|
||
|
||
function preferredScenePreviewImage(assets) {
|
||
const byName = (expected) => assets.find((asset) => {
|
||
const name = String(asset?.name || asset?.path || "").toLowerCase();
|
||
return name === expected || name.endsWith(`/${expected}`);
|
||
});
|
||
return byName("source_gs_preview.png")
|
||
|| byName("scene_cover.png")
|
||
|| byName("pointcloud_projection_preview.png")
|
||
|| assets.find(isScenePreviewImage)
|
||
|| null;
|
||
}
|
||
|
||
async function loadSparkOrFallback(asset, suffix) {
|
||
try {
|
||
const spark = await loadSparkModule();
|
||
if (!sparkRenderer) {
|
||
sparkRenderer = new spark.SparkRenderer({ renderer });
|
||
scene.add(sparkRenderer);
|
||
}
|
||
status.textContent = `Spark · loading ${asset.name}${asset.size_label ? ` · ${asset.size_label}` : ""}`;
|
||
const splatMesh = new spark.SplatMesh({ url: asset.url });
|
||
loadedObject = splatMesh;
|
||
contentRoot.add(splatMesh);
|
||
await splatMesh.initialized;
|
||
normalizeObjectFromBox(splatMesh);
|
||
const count = splatMesh.splats?.getNumSplats?.() || splatMesh.packedSplats?.numSplats || 0;
|
||
debug.backend = "spark";
|
||
debug.loaded = true;
|
||
debug.pointCount = count;
|
||
status.textContent = `Spark · ${asset.name}${count ? ` · ${count} splats` : ""}`;
|
||
} catch (error) {
|
||
console.info("Spark preview unavailable; using Three.js fallback", error);
|
||
debug.warnings.push(error.message || String(error));
|
||
if (loadedObject) {
|
||
contentRoot.remove(loadedObject);
|
||
loadedObject = null;
|
||
}
|
||
if (suffix !== "ply") throw error;
|
||
await loadPlyFallback(asset);
|
||
}
|
||
}
|
||
|
||
async function loadPlyFallback(asset) {
|
||
status.textContent = `Three.js fallback · loading ${asset.name}${asset.size_label ? ` · ${asset.size_label}` : ""}`;
|
||
const geometry = await withLoadingTimeout(new PLYLoader().loadAsync(asset.url), `loading timed out: ${asset.name || "asset"}${asset.size_label ? ` · ${asset.size_label}` : ""}`);
|
||
geometry.computeBoundingSphere();
|
||
geometry.computeVertexNormals();
|
||
const count = geometry.getAttribute("position")?.count || 0;
|
||
const material = new THREE.PointsMaterial({
|
||
size: pointSizeFor(count, asset),
|
||
sizeAttenuation: true,
|
||
vertexColors: geometry.hasAttribute("color"),
|
||
color: 0x58c4a8,
|
||
});
|
||
loadedObject = new THREE.Points(geometry, material);
|
||
contentRoot.add(loadedObject);
|
||
normalizeObject(loadedObject, geometry.boundingSphere);
|
||
debug.backend = "three_fallback";
|
||
debug.loaded = true;
|
||
debug.pointCount = count;
|
||
status.textContent = `Three.js fallback · ${asset.name} · ${count} points`;
|
||
}
|
||
|
||
async function loadSceneOverlays(assets) {
|
||
clearOverlays();
|
||
if (!loadedObject) return;
|
||
const overlays = assets.filter((asset) => asset?.role === "added_asset");
|
||
for (const asset of overlays) {
|
||
try {
|
||
const object = await createOverlayObject(asset);
|
||
if (!object) continue;
|
||
applyAssetTransform(object, asset.transform || {});
|
||
object.userData.real2simAsset = asset.name || asset.path || "asset";
|
||
contentRoot.add(object);
|
||
overlayObjects.push(object);
|
||
} catch (error) {
|
||
debug.warnings.push(`overlay ${asset.name || "asset"}: ${error.message || error}`);
|
||
console.warn("Unable to load scene overlay", asset, error);
|
||
}
|
||
}
|
||
debug.overlayCount = overlayObjects.length;
|
||
if (overlayObjects.length) hint.textContent = `已叠加 ${overlayObjects.length} 个 MotrixSim 数字资产;位置、旋转、缩放与仿真场景保持一致。`;
|
||
}
|
||
|
||
async function createOverlayObject(asset) {
|
||
const primitive = String(asset.viewer_primitive || "").toLowerCase();
|
||
const dimensions = Array.isArray(asset.physics?.dimensions) ? asset.physics.dimensions.map(Number) : [0.1, 0.1, 0.1];
|
||
const rgba = Array.isArray(asset.physics?.rgba) ? asset.physics.rgba.map(Number) : [0.13, 0.76, 0.67, 1];
|
||
const material = new THREE.MeshStandardMaterial({
|
||
color: new THREE.Color(rgba[0], rgba[1], rgba[2]),
|
||
transparent: rgba[3] < 1,
|
||
opacity: rgba[3],
|
||
roughness: 0.55,
|
||
metalness: 0.05,
|
||
});
|
||
if (primitive === "box") return new THREE.Mesh(new THREE.BoxGeometry(dimensions[0], dimensions[1], dimensions[2]), material);
|
||
if (primitive === "sphere") return new THREE.Mesh(new THREE.SphereGeometry(Math.max(...dimensions) / 2, 32, 20), material);
|
||
if (primitive === "cylinder") {
|
||
const geometry = new THREE.CylinderGeometry(Math.max(dimensions[0], dimensions[1]) / 2, Math.max(dimensions[0], dimensions[1]) / 2, dimensions[2], 32);
|
||
geometry.rotateX(Math.PI / 2);
|
||
return new THREE.Mesh(geometry, material);
|
||
}
|
||
if (primitive === "capsule") {
|
||
const radius = Math.max(dimensions[0], dimensions[1]) / 2;
|
||
const bodyLength = Math.max(0.001, dimensions[2] - radius * 2);
|
||
const group = new THREE.Group();
|
||
const cylinder = new THREE.CylinderGeometry(radius, radius, bodyLength, 32);
|
||
cylinder.rotateX(Math.PI / 2);
|
||
group.add(new THREE.Mesh(cylinder, material));
|
||
const capGeometry = new THREE.SphereGeometry(radius, 28, 18);
|
||
const first = new THREE.Mesh(capGeometry, material);
|
||
const second = new THREE.Mesh(capGeometry, material);
|
||
first.position.z = bodyLength / 2;
|
||
second.position.z = -bodyLength / 2;
|
||
group.add(first, second);
|
||
return group;
|
||
}
|
||
if (!asset.url) return null;
|
||
const suffix = String(asset.name || asset.path || "").toLowerCase().split(".").pop();
|
||
if (suffix === "glb" || suffix === "gltf") {
|
||
const { GLTFLoader } = await import("three/addons/loaders/GLTFLoader.js");
|
||
return (await new GLTFLoader().loadAsync(asset.url)).scene;
|
||
}
|
||
if (suffix === "obj") return new OBJLoader().loadAsync(asset.url);
|
||
if (suffix === "ply") {
|
||
const geometry = await new PLYLoader().loadAsync(asset.url);
|
||
geometry.computeVertexNormals();
|
||
return new THREE.Mesh(geometry, material);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
function applyAssetTransform(object, transform) {
|
||
const translation = Array.isArray(transform.translation) ? transform.translation.map(Number) : [0, 0, 0];
|
||
const rotation = Array.isArray(transform.rotation_euler_degrees) ? transform.rotation_euler_degrees.map(Number) : [0, 0, Number(transform.yaw_degrees || 0)];
|
||
const scale = Array.isArray(transform.scale) ? transform.scale.map(Number) : [Number(transform.scale || 1), Number(transform.scale || 1), Number(transform.scale || 1)];
|
||
object.position.set(translation[0] || 0, translation[1] || 0, translation[2] || 0);
|
||
object.rotation.set(THREE.MathUtils.degToRad(rotation[0] || 0), THREE.MathUtils.degToRad(rotation[1] || 0), THREE.MathUtils.degToRad(rotation[2] || 0));
|
||
object.scale.set(scale[0] || 1, scale[1] || 1, scale[2] || 1);
|
||
}
|
||
|
||
function clearOverlays() {
|
||
for (const object of overlayObjects) contentRoot.remove(object);
|
||
overlayObjects = [];
|
||
debug.overlayCount = 0;
|
||
}
|
||
|
||
function pointSizeFor(count, asset) {
|
||
const name = String(asset?.name || asset?.path || "").toLowerCase();
|
||
if (name.includes("colmap_sparse") || name.includes("sparse_points")) return 0.022;
|
||
if (count <= 100000) return 0.018;
|
||
if (count <= 500000) return 0.011;
|
||
return 0.006;
|
||
}
|
||
|
||
function withLoadingTimeout(promise, message, timeoutMs = 45000) {
|
||
let timer;
|
||
return Promise.race([
|
||
promise.finally(() => clearTimeout(timer)),
|
||
new Promise((_, reject) => {
|
||
timer = setTimeout(() => reject(new Error(message)), timeoutMs);
|
||
}),
|
||
]);
|
||
}
|
||
|
||
async function loadSparkModule() {
|
||
if (sparkModule) return sparkModule;
|
||
sparkModule = await import("/static/vendor/spark.module.min.js");
|
||
if (!sparkModule.SparkRenderer || !sparkModule.SplatMesh) throw new Error("Spark bundle missing SparkRenderer/SplatMesh exports");
|
||
return sparkModule;
|
||
}
|
||
|
||
function normalizeObject(object, _sphere) {
|
||
fitRoot.position.set(0, 0, 0);
|
||
fitRoot.scale.set(1, 1, 1);
|
||
object.updateMatrixWorld(true);
|
||
const box = new THREE.Box3().setFromObject(object);
|
||
if (!Number.isFinite(box.min.x) || !Number.isFinite(box.max.x)) return;
|
||
const sphere = new THREE.Sphere();
|
||
box.getBoundingSphere(sphere);
|
||
const scale = sphere.radius > 0 ? 1.6 / sphere.radius : 1;
|
||
fitRoot.scale.setScalar(scale);
|
||
fitRoot.position.copy(sphere.center).multiplyScalar(-scale);
|
||
controls.target.set(0, 0, 0);
|
||
setCoverCamera();
|
||
controls.update();
|
||
}
|
||
|
||
function setCoverCamera() {
|
||
if (!coverMode) {
|
||
camera.position.set(2.8, 2.2, 2.8);
|
||
return;
|
||
}
|
||
const views = {
|
||
hero: [2.6, 1.8, 2.4],
|
||
front: [0.0, 1.55, 3.2],
|
||
side: [3.2, 1.55, 0.2],
|
||
top: [0.9, 3.4, 1.1],
|
||
};
|
||
const pos = views[coverView] || views.hero;
|
||
camera.position.set(pos[0], pos[1], pos[2]);
|
||
}
|
||
|
||
function normalizeObjectFromBox(object) {
|
||
normalizeObject(object, null);
|
||
}
|
||
|
||
function addPlaceholder(message) {
|
||
if (loadedObject) contentRoot.remove(loadedObject);
|
||
clearOverlays();
|
||
const geometry = new THREE.BoxGeometry(0.8, 0.8, 0.8);
|
||
const material = new THREE.MeshStandardMaterial({ color: 0x58c4a8, roughness: 0.65 });
|
||
loadedObject = new THREE.Mesh(geometry, material);
|
||
contentRoot.add(loadedObject);
|
||
normalizeObjectFromBox(loadedObject);
|
||
debug.backend = "placeholder";
|
||
debug.loaded = false;
|
||
status.textContent = message;
|
||
}
|
||
|
||
function showError(error) {
|
||
console.error(error);
|
||
debug.errors.push(error.message || String(error));
|
||
addPlaceholder(error.message || String(error));
|
||
}
|
||
|
||
function rendererStatusText(manifest, assetCount) {
|
||
const renderer = manifest.renderer || "auto";
|
||
const rendererStatus = manifest.renderer_status || {};
|
||
if (rendererStatus.aholo_available && rendererStatus.selected === "aholo") return `Aholo · ${assetCount} assets`;
|
||
if (rendererStatus.spark_available) return `Spark · ${assetCount} assets · ${rendererStatus.aholo_reason || ""}`;
|
||
return `Three.js fallback · ${assetCount} assets · ${rendererStatus.fallback_reason || "Spark bundle unavailable"}`;
|
||
}
|
||
|
||
function prioritizeAssets(assets) {
|
||
return assets.slice().sort((left, right) => assetPriority(right) - assetPriority(left));
|
||
}
|
||
|
||
function assetPriority(asset) {
|
||
const name = String(asset?.name || asset?.path || "").toLowerCase();
|
||
const role = String(asset?.role || "").toLowerCase();
|
||
if (role === "viewer_preview" && name.endsWith(".glb")) return 20;
|
||
if (role === "rollout_gif" || role === "rollout_final_frame") return 14;
|
||
if (name.endsWith("_threejs_rgb.ply")) return 12;
|
||
if (name.endsWith(".spz") || name.endsWith(".splat") || name.endsWith(".ksplat") || name.endsWith(".sog")) return 11;
|
||
if (name.endsWith("/pose_init_trained_scene.ply") || name === "pose_init_trained_scene.ply") return 10;
|
||
if (name.endsWith("/scene.ply") || name === "scene.ply") return asset?.autoload === false ? 7 : 9;
|
||
if (name.includes("colmap_sparse") || name.includes("sparse_points")) return 2;
|
||
if (["added_asset", "collision", "visual", "simulator_import"].includes(role)) return 4;
|
||
const kind = String(asset?.kind || "");
|
||
if (["mesh", "splat_or_pointcloud"].includes(kind)) return 3;
|
||
if (kind === "simulator") return 2;
|
||
return 1;
|
||
}
|
||
|
||
function assetQuality(asset, index) {
|
||
const name = String(asset?.name || asset?.path || "").toLowerCase();
|
||
if (index === 0) return "recommended";
|
||
if (name === "scene.ply" || name.endsWith("/3dgs/scene.ply") || name.endsWith(".spz") || name.endsWith(".splat") || name.endsWith(".ksplat") || name.endsWith(".sog")) return "full";
|
||
if (name.includes("colmap_sparse") || name.includes("sparse_points")) return "diagnostic";
|
||
if (asset?.autoload === false) return "manual";
|
||
return "normal";
|
||
}
|
||
|
||
function assetQualityLabel(quality) {
|
||
return ({ recommended: "推荐", full: "完整3DGS", diagnostic: "诊断", manual: "大文件" })[quality] || "";
|
||
}
|
||
|
||
function escapeHtml(value) {
|
||
return String(value).replace(/[&<>"']/g, (ch) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[ch]));
|
||
}
|
||
|
||
function animate() {
|
||
controls.update();
|
||
debug.canvas = { width: renderer.domElement.width, height: renderer.domElement.height };
|
||
renderer.render(scene, camera);
|
||
requestAnimationFrame(animate);
|
||
}
|
||
|
||
addEventListener("resize", () => {
|
||
camera.aspect = innerWidth / innerHeight;
|
||
camera.updateProjectionMatrix();
|
||
renderer.setSize(innerWidth, innerHeight);
|
||
});
|
||
|
||
main().catch(showError);
|
||
animate();
|
||
</script>
|
||
</body>
|
||
</html>
|