Spaces:
Running
Running
Joint dots in the 3D viewer: hover to highlight, drag to move (part 2)
Browse files140 unmodified lines141142143144145146140 unmodified lines.viewer3d { position: relative; }.viewer3d-stage { position: relative; aspect-ratio: 4 / 3; touch-action: none; }.viewer3d-stage canvas { position: absolute; inset: 0; width: 100% !important; height: 100% !important; display: block; }.viewer3d-status { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 16px; font-size: 13px; color: var(--muted); pointer-events: none; }.joints { display: grid; gap: 8px; }.joint { display: grid; gap: 2px; }140 unmodified lines141142143144145146147148149150151152153154155140 unmodified lines.viewer3d { position: relative; }.viewer3d-stage { position: relative; aspect-ratio: 4 / 3; touch-action: none; }.viewer3d-stage canvas { position: absolute; inset: 0; width: 100% !important; height: 100% !important; display: block; }.viewer3d-overlay { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }.joint-dot { position: absolute; left: 0; top: 0; width: 16px; height: 16px; margin: -8px 0 0 -8px; padding: 0; border-radius: 50%; border: 3px solid #f29b38; background: #1d1e20; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35); cursor: grab; pointer-events: auto; transition: width 0.12s, height 0.12s, margin 0.12s, background 0.12s, border-color 0.12s; }.joint-dot:hover, .joint-dot.active { width: 22px; height: 22px; margin: -11px 0 0 -11px; background: #f29b38; border-color: #ffffff; box-shadow: 0 0 0 5px rgba(242, 155, 56, 0.28); }.viewer3d-stage.dragging, .viewer3d-stage.dragging .joint-dot { cursor: grabbing; }.viewer3d-tip { position: absolute; left: 0; top: 0; pointer-events: none; white-space: nowrap; font: 500 12px/1 var(--mono); color: #f3eee8; background: rgba(20, 21, 23, 0.88); border: 1px solid #b8742c; border-radius: 5px; padding: 6px 8px; }.viewer3d-toggle { position: absolute; top: 10px; left: 10px; width: 28px; height: 28px; padding: 0; border-radius: 6px; border: 2px solid rgba(255, 255, 255, 0.75); background: rgba(20, 21, 23, 0.35); cursor: pointer; display: grid; place-items: center; }.viewer3d-toggle::after { content: ""; width: 10px; height: 10px; border-radius: 50%; border: 2.5px solid #f29b38; background: #1d1e20; opacity: 0; }.viewer3d-toggle[aria-pressed="true"]::after { opacity: 1; }.viewer3d-toggle:hover { border-color: #ffffff; }.viewer3d-status { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 16px; font-size: 13px; color: var(--muted); pointer-events: none; }.joints { display: grid; gap: 8px; }.joint { display: grid; gap: 2px; }
12345146 unmodified lines15215315415515615715827 unmodified lines18618718818919019119223 unmodified lines2162172182192202212222232242252262275 unmodified lines2332342352362372382392402412422 unmodified lines2452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812821 unmodified line2842852862872882892902912922932942952961 unmodified line2982993003013023032 unmodified lines306307308309310311312// Interactive 3D preview: loads previews/<asset>.glb and its rig (previews/<asset>.json),// then lets the viewer orbit, drag parts, and move each joint with a slider.// One renderer is reused for every asset; app.js moves its element into the detail panel.import * as THREE from "three";import { OrbitControls } from "three/addons/controls/OrbitControls.js";146 unmodified lines}new ResizeObserver(resize).observe(stage);function clear() {if (model) {scene.remove(model);model.traverse((o) => {27 unmodified lines[gltf, data] = await Promise.all([loader.loadAsync(glbUrl), fetch(rigUrl).then((r) => r.ok ? r.json() : { joints: [] })]);} catch (e) {console.error("3D preview failed to load", glbUrl, e);if (mine === gen) status.textContent = `The 3D preview didn't load (${String((e && e.message) || e).slice(0, 120)}). Try the image view.`;return [];}if (mine !== gen) return [];23 unmodified linesconst free = j.lower == null || j.upper == null;const lo = free ? (j.type === "hinge" ? -180 : -0.25 * sizeM) : j.lower;const hi = free ? (j.type === "hinge" ? 180 : 0.25 * sizeM) : j.upper;const r = { index: rig.length, name: j.name, type: j.type, group, axis: new THREE.Vector3(...j.axis).normalize(),base: group.position.clone(), lo, hi, free, val: 0 };node.traverse((c) => { if (c.isMesh) { c.userData.rig = r; grabMeshes.push(c); } });rig.push(r);}rig.forEach((r) => setJoint(r, Math.min(r.hi, Math.max(r.lo, 0))));const box = new THREE.Box3().setFromObject(model);const c = box.getCenter(new THREE.Vector3());5 unmodified linescamera.updateProjectionMatrix();placeGrid();status.hidden = true;return rig.map((r) => ({ name: r.name, type: r.type, lo: r.lo, hi: r.hi, free: r.free, val: r.val }));}// Grab a part: hinges swing around their axis, sliders follow the pointer along theirs.const ray = new THREE.Raycaster();const ptr = new THREE.Vector2();function aim(e) {2 unmodified linesptr.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;ray.setFromCamera(ptr, camera);}renderer.domElement.addEventListener("pointerdown", (e) => {if (!grabMeshes.length) return;aim(e);const hit = ray.intersectObjects(grabMeshes, false)[0];if (!hit) return;const r = hit.object.userData.rig;const pivot = r.group.getWorldPosition(new THREE.Vector3());const axis = r.axis.clone().transformDirection(r.group.parent.matrixWorld).normalize();if (r.type === "hinge") {const v0 = hit.point.clone().sub(pivot).projectOnPlane(axis);if (v0.lengthSq() < 1e-10) return;drag = { r, kind: "hinge", pivot, axis, v0: v0.normalize(), start: r.val };} else {const n = camera.getWorldDirection(new THREE.Vector3());drag = { r, kind: "slider", axis, start: r.val, p0: hit.point.clone(),plane: new THREE.Plane().setFromNormalAndCoplanarPoint(n, hit.point) };}controls.enabled = false;renderer.domElement.setPointerCapture(e.pointerId);renderer.domElement.style.cursor = "grabbing";});renderer.domElement.addEventListener("pointermove", (e) => {aim(e);if (!drag) {renderer.domElement.style.cursor = grabMeshes.length && ray.intersectObjects(grabMeshes, false).length ? "grab" : "";return;}const p = new THREE.Vector3();if (drag.kind === "hinge") {if (!ray.ray.intersectPlane(new THREE.Plane().setFromNormalAndCoplanarPoint(drag.axis, drag.pivot), p)) return;const v1 = p.sub(drag.pivot).projectOnPlane(drag.axis);if (v1.lengthSq() < 1e-10) return;v1.normalize();const delta = Math.atan2(new THREE.Vector3().crossVectors(drag.v0, v1).dot(drag.axis), drag.v0.dot(v1));setJoint(drag.r, drag.start + THREE.MathUtils.radToDeg(delta));1 unmodified lineif (!ray.ray.intersectPlane(drag.plane, p)) return;setJoint(drag.r, drag.start + p.sub(drag.p0).dot(drag.axis));}});const release = () => {if (!drag) return;drag = null; controls.enabled = true; renderer.domElement.style.cursor = "";};renderer.domElement.addEventListener("pointerup", release);renderer.domElement.addEventListener("pointercancel", release);let running = true;(function tick() {1 unmodified linerequestAnimationFrame(tick);controls.update();renderer.render(scene, camera);})();setTheme(theme);2 unmodified linesload,setTheme,setJoint: (i, v) => rig[i] && setJoint(rig[i], v),resetJoints: () => rig.forEach((r) => setJoint(r, Math.min(r.hi, Math.max(r.lo, 0)))),openJoints: () => rig.forEach((r) => setJoint(r, r.free ? r.val : r.lo + (r.hi - r.lo) * (Math.abs(r.hi) >= Math.abs(r.lo) ? 0.85 : 0.15))),onJoint: (fn) => { onJoint = fn; },resize,12345146 unmodified lines15215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121227 unmodified lines24024124224324424524623 unmodified lines2702712722732742752762772782792802812822832842852862872882892902912922932942952962972982995 unmodified lines3053063073083093103113123133143153162 unmodified lines3193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533541 unmodified line3563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054061 unmodified line4084094104114124134142 unmodified lines417418419420421422423// Interactive 3D preview: loads previews/<asset>.glb and its rig (previews/<asset>.json),// then lets the viewer orbit, drag parts or their joint dots, and move each joint with a slider.// One renderer is reused for every asset; app.js moves its element into the detail panel.import * as THREE from "three";import { OrbitControls } from "three/addons/controls/OrbitControls.js";146 unmodified lines}new ResizeObserver(resize).observe(stage);// Joint dots (HTML overlay, like NVIDIA's viewer): one per moving part, at its joint.// Hover a dot or a part to highlight the part and see its state; drag either to move it.const overlay = document.createElement("div");overlay.className = "viewer3d-overlay";const tip = document.createElement("div");tip.className = "viewer3d-tip";tip.hidden = true;const toggle = document.createElement("button");toggle.type = "button";toggle.className = "viewer3d-toggle";toggle.setAttribute("aria-pressed", "true");toggle.setAttribute("aria-label", "Show joint markers");toggle.title = "Show joint markers";let showDots = true;toggle.addEventListener("click", () => {showDots = !showDots;toggle.setAttribute("aria-pressed", String(showDots));overlay.hidden = !showDots;if (!showDots) setHot(null);});stage.append(overlay, tip, toggle);const HIGHLIGHT = new THREE.Color(0xf29b38);let hot = null;function highlight(r, on) {for (const mesh of r.meshes) for (const m of [].concat(mesh.material)) {if (!m.emissive) continue;if (on) {if (!m.userData.e0) { m.userData.e0 = m.emissive.clone(); m.userData.ei0 = m.emissiveIntensity; }m.emissive.copy(HIGHLIGHT); m.emissiveIntensity = 0.45;} else if (m.userData.e0) {m.emissive.copy(m.userData.e0); m.emissiveIntensity = m.userData.ei0;}}if (r.dot) r.dot.classList.toggle("active", on);}function setHot(r) {if (hot === r) return;if (hot) highlight(hot, false);hot = r;if (hot) highlight(hot, true);tip.hidden = !hot;}function rest(r) { return Math.min(r.hi, Math.max(r.lo, 0)); }function label(r) {const len = (m) => Math.abs(m) >= 0.01 ? `${(Math.abs(m) * 100).toFixed(1)} cm` : `${(Math.abs(m) * 1000).toFixed(1)} mm`;if (r.free) return r.type === "hinge" ? `Rotating part · ${Math.round(r.val)}°` : `Sliding part · ${len(r.val)}`;const r0 = rest(r), span = Math.max(Math.abs(r.hi - r0), Math.abs(r.lo - r0)) || 1;const pct = Math.round((Math.abs(r.val - r0) / span) * 100);return r.type === "hinge" ? `Hinged part · ${Math.round(r.val)}° · ${pct}% open` : `Sliding part · ${pct}% open`;}function clear() {setHot(null);overlay.replaceChildren();if (model) {scene.remove(model);model.traverse((o) => {27 unmodified lines[gltf, data] = await Promise.all([loader.loadAsync(glbUrl), fetch(rigUrl).then((r) => r.ok ? r.json() : { joints: [] })]);} catch (e) {console.error("3D preview failed to load", glbUrl, e);if (mine === gen) status.textContent = `The 3D preview didn't load (${String((e && e.message) || e).slice(0, 120)}).`;return [];}if (mine !== gen) return [];23 unmodified linesconst free = j.lower == null || j.upper == null;const lo = free ? (j.type === "hinge" ? -180 : -0.25 * sizeM) : j.lower;const hi = free ? (j.type === "hinge" ? 180 : 0.25 * sizeM) : j.upper;const r = { index: rig.length, name: j.name, type: j.type, group, node, meshes: [],axis: new THREE.Vector3(...j.axis).normalize(), base: group.position.clone(), lo, hi, free, val: 0 };node.traverse((c) => {if (!c.isMesh) return;// own material copies, so highlighting this part doesn't tint parts that share a materialc.material = Array.isArray(c.material) ? c.material.map((m) => m.clone()) : c.material.clone();c.userData.rig = r; grabMeshes.push(c); r.meshes.push(c);});const dot = document.createElement("button");dot.type = "button";dot.className = "joint-dot";dot.setAttribute("aria-label", `${j.name}: drag to move`);dot.addEventListener("pointerenter", () => { if (!drag) setHot(r); });dot.addEventListener("pointerleave", () => { if (!drag) setHot(null); });dot.addEventListener("pointerdown", (e) => {e.preventDefault();const center = new THREE.Box3().setFromObject(r.node).getCenter(new THREE.Vector3());startDrag(r, e, center);});overlay.append(dot);r.dot = dot;rig.push(r);}rig.forEach((r) => setJoint(r, rest(r)));const box = new THREE.Box3().setFromObject(model);const c = box.getCenter(new THREE.Vector3());5 unmodified linescamera.updateProjectionMatrix();placeGrid();status.hidden = true;toggle.hidden = !rig.length;return rig.map((r) => ({ name: r.name, type: r.type, lo: r.lo, hi: r.hi, free: r.free, val: r.val }));}// Drag a part or its dot: hinges swing around their axis (following the pointer around// the pivot), sliders follow the pointer along their axis.const ray = new THREE.Raycaster();const ptr = new THREE.Vector2();function aim(e) {2 unmodified linesptr.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;ray.setFromCamera(ptr, camera);}function startDrag(r, e, point) {const pivot = r.group.getWorldPosition(new THREE.Vector3());const axis = r.axis.clone().transformDirection(r.group.parent.matrixWorld).normalize();if (r.type === "hinge") {const v0 = point.clone().sub(pivot).projectOnPlane(axis);// a part centred on its own axis (wheel, knob): turn it by horizontal pointer movementdrag = v0.lengthSq() < 1e-10 * sizeM * sizeM? { r, kind: "spin", x0: e.clientX, start: r.val }: { r, kind: "hinge", pivot, axis, v0: v0.normalize(), start: r.val };} else {const n = camera.getWorldDirection(new THREE.Vector3());drag = { r, kind: "slider", axis, start: r.val, p0: point.clone(),plane: new THREE.Plane().setFromNormalAndCoplanarPoint(n, point) };}setHot(r);controls.enabled = false;stage.classList.add("dragging");window.addEventListener("pointermove", moveDrag);window.addEventListener("pointerup", endDrag);window.addEventListener("pointercancel", endDrag);}function moveDrag(e) {if (!drag) return;if (drag.kind === "spin") { setJoint(drag.r, drag.start + (e.clientX - drag.x0) * 0.8); return; }aim(e);const p = new THREE.Vector3();if (drag.kind === "hinge") {if (!ray.ray.intersectPlane(new THREE.Plane().setFromNormalAndCoplanarPoint(drag.axis, drag.pivot), p)) return;const v1 = p.sub(drag.pivot).projectOnPlane(drag.axis);if (v1.lengthSq() < 1e-12) return;v1.normalize();const delta = Math.atan2(new THREE.Vector3().crossVectors(drag.v0, v1).dot(drag.axis), drag.v0.dot(v1));setJoint(drag.r, drag.start + THREE.MathUtils.radToDeg(delta));1 unmodified lineif (!ray.ray.intersectPlane(drag.plane, p)) return;setJoint(drag.r, drag.start + p.sub(drag.p0).dot(drag.axis));}}function endDrag() {if (!drag) return;drag = null;controls.enabled = true;stage.classList.remove("dragging");window.removeEventListener("pointermove", moveDrag);window.removeEventListener("pointerup", endDrag);window.removeEventListener("pointercancel", endDrag);setHot(null);}renderer.domElement.addEventListener("pointerdown", (e) => {if (!grabMeshes.length || e.button !== 0) return;aim(e);const hit = ray.intersectObjects(grabMeshes, false)[0];if (!hit) return;startDrag(hit.object.userData.rig, e, hit.point);});renderer.domElement.addEventListener("pointermove", (e) => {if (drag || !grabMeshes.length) return;aim(e);const hit = ray.intersectObjects(grabMeshes, false)[0];renderer.domElement.style.cursor = hit ? "grab" : "";setHot(hit && showDots ? hit.object.userData.rig : null);});renderer.domElement.addEventListener("pointerleave", () => { if (!drag) setHot(null); });// Keep dots and the label on their joints as the camera or the parts move.const tmp = new THREE.Vector3();function placeDots() {if (!rig.length || overlay.hidden) return;const w = stage.clientWidth, h = stage.clientHeight;for (const r of rig) {r.group.getWorldPosition(tmp).project(camera);const visible = tmp.z < 1 && Math.abs(tmp.x) <= 1.05 && Math.abs(tmp.y) <= 1.05;r.dot.hidden = !visible;if (!visible) continue;r.sx = (tmp.x + 1) / 2 * w; r.sy = (1 - tmp.y) / 2 * h;r.dot.style.transform = `translate(${r.sx}px, ${r.sy}px)`;}if (hot && !hot.dot.hidden) {tip.textContent = label(hot);tip.style.transform = `translate(${Math.min(hot.sx + 14, w - tip.offsetWidth - 6)}px, ${Math.max(hot.sy - 34, 6)}px)`;}}let running = true;(function tick() {1 unmodified linerequestAnimationFrame(tick);controls.update();renderer.render(scene, camera);placeDots();})();setTheme(theme);2 unmodified linesload,setTheme,setJoint: (i, v) => rig[i] && setJoint(rig[i], v),resetJoints: () => rig.forEach((r) => setJoint(r, rest(r))),openJoints: () => rig.forEach((r) => setJoint(r, r.free ? r.val : r.lo + (r.hi - r.lo) * (Math.abs(r.hi) >= Math.abs(r.lo) ? 0.85 : 0.15))),onJoint: (fn) => { onJoint = fn; },resize,
style.css+9
viewer.js+145-34