// 3D view of the robot step: the robot model (baked by tools/bake_robots.py), its reachable space // and the user's work area. app.js sends what to show through the "advisor:view" event. import * as THREE from 'three'; import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js'; import { STLLoader } from 'three/addons/loaders/STLLoader.js'; import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; import { USDZLoader } from 'three/addons/loaders/USDZLoader.js'; const canvas = document.getElementById('scene'); const msg = document.getElementById('vmsg'); const shellBox = document.getElementById('vShell'); const moveBox = document.getElementById('vMove'); const renderer = new THREE.WebGLRenderer({canvas, antialias:true, alpha:true}); renderer.setPixelRatio(Math.min(devicePixelRatio, 2)); const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(38, 1, 0.01, 100); camera.up.set(0, 0, 1); // URDF frame: x forward, z up const controls = new OrbitControls(camera, canvas); controls.enableDamping = true; const hemi = new THREE.HemisphereLight(0xffffff, 0x8a8a8a, 1.8); hemi.position.set(0, 0, 1); const sun = new THREE.DirectionalLight(0xffffff, 1.6); sun.position.set(2, -2.5, 4); scene.add(hemi, sun); const loader = new GLTFLoader().setMeshoptDecoder(MeshoptDecoder); const cache = new Map(); const css = n => getComputedStyle(document.documentElement).getPropertyValue(n).trim(); const color = n => new THREE.Color(css(n) || '#888'); let env = null, dyn = null, robots = [], current = null, fitted = null, token = 0; let things = null, thingsKey = ''; const uploads = new Map(); // uid -> Object3D parsed from the user's file const clock = new THREE.Clock(); function loadModel(id) { if (!cache.has(id)) { cache.set(id, Promise.all([ loader.loadAsync(`robots/${id}.glb`), fetch(`robots/${id}.json`).then(r => r.json()), ])); } return cache.get(id); } function dispose(obj) { if (!obj) return; scene.remove(obj); obj.traverse(o => { if (o.geometry) o.geometry.dispose(); if (o.material) [].concat(o.material).forEach(m => m.dispose()); }); } // Reach profile -> a lathe around the arm's base axis (front half, so the robot stays visible). function profilePoints(prof, R) { if (!prof) { const dz = R / 24, rmin = [], rmax = []; for (let z = 0; z < R; z += dz) { rmax.push(Math.sqrt(Math.max(0, R * R - (z + dz / 2) ** 2))); rmin.push(0); } prof = {dz, rmin, rmax}; } const outer = [], inner = []; prof.rmax.forEach((hi, k) => { if (hi == null) return; const z = (k + 0.5) * prof.dz; outer.push(new THREE.Vector2(hi * 0.97, z)); inner.push(new THREE.Vector2(Math.max(0.001, prof.rmin[k]), z)); }); if (!outer.length) return null; const top = outer[outer.length - 1].y + prof.dz / 2, bottom = Math.max(0, outer[0].y - prof.dz / 2); return [new THREE.Vector2(inner[0].x, bottom), new THREE.Vector2(outer[0].x, bottom), ...outer, new THREE.Vector2(outer[outer.length - 1].x, top), new THREE.Vector2(inner[inner.length - 1].x, top), ...inner.reverse()]; } function shell(points, mount) { const g = new THREE.Group(); const geo = new THREE.LatheGeometry(points, 48, 0, Math.PI).rotateX(Math.PI / 2); const fill = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({color:color('--s1'), transparent:true, opacity:0.07, side:THREE.DoubleSide, depthWrite:false})); // a light cage: a few meridians and rings, not every triangle const lines = [], step = Math.max(1, Math.round(points.length / 10)); for (let i = 0; i <= 6; i++) { const a = Math.PI * i / 6; for (let k = 0; k < points.length - 1; k++) { const p = points[k], q = points[k + 1]; lines.push(p.x * Math.sin(a), -p.x * Math.cos(a), p.y, q.x * Math.sin(a), -q.x * Math.cos(a), q.y); } } for (let k = 0; k < points.length; k += step) { const p = points[k]; for (let i = 0; i < 24; i++) { const a = Math.PI * i / 24, b = Math.PI * (i + 1) / 24; lines.push(p.x * Math.sin(a), -p.x * Math.cos(a), p.y, p.x * Math.sin(b), -p.x * Math.cos(b), p.y); } } const cageGeo = new THREE.BufferGeometry().setAttribute('position', new THREE.Float32BufferAttribute(lines, 3)); const cage = new THREE.LineSegments(cageGeo, new THREE.LineBasicMaterial({color:color('--s1'), transparent:true, opacity:0.35})); fill.userData.role = cage.userData.role = 'shell'; g.add(fill, cage); g.position.set(mount.x, mount.y, 0); return g; } function buildDyn(d) { const g = new THREE.Group(); const {w, d: depth, h, g: gap} = d.ws; if (!d.interaction) { const area = new THREE.Mesh(new THREE.BoxGeometry(depth, w, h), new THREE.MeshBasicMaterial({color:color('--accent'), transparent:true, opacity:0.28, depthWrite:false})); area.position.set(gap + depth / 2, 0, (d.table || 0) + h / 2); const edges = new THREE.LineSegments(new THREE.EdgesGeometry(area.geometry), new THREE.LineBasicMaterial({color:color('--accent')})); edges.position.copy(area.position); area.userData.role = edges.userData.role = 'area'; g.add(area, edges); if (d.table) { // the table the work area sits on const top = new THREE.Mesh(new THREE.BoxGeometry(depth + 0.1, w + 0.1, 0.02), new THREE.MeshStandardMaterial({color:color('--faint'), roughness:0.9})); top.position.set(gap + depth / 2, 0, d.table - 0.01); g.add(top); } } // only the parts of the work area out of reach are marked const out = d.pts.filter(p => !p[3]).flatMap(p => [p[0], p[1], p[2] + (d.table || 0)]); if (out.length) { const geo = new THREE.BufferGeometry().setAttribute('position', new THREE.Float32BufferAttribute(out, 3)); g.add(new THREE.Points(geo, new THREE.PointsMaterial({size:6, sizeAttenuation:false, color:color('--bad')}))); } if (d.reach && !d.moves) { const pts = profilePoints(d.prof, d.R); if (pts) d.mounts.forEach(m => g.add(shell(pts, m))); } g.traverse(o => { if (o.userData.role === 'shell') o.visible = shellBox.checked; }); return g; } function buildEnv(size) { const g = new THREE.Group(); const n = Math.max(4, Math.ceil(size * 2) * 2); const grid = new THREE.GridHelper(n / 2, n, color('--line'), color('--line')); grid.rotation.x = Math.PI / 2; grid.material.transparent = true; grid.material.opacity = 0.9; g.add(grid); return g; } // A simplified arm for robots without a kinematic model: base, upper arm, forearm. function placeholder(R) { const g = new THREE.Group(), mat = new THREE.MeshStandardMaterial({color:0x9a9ca3, roughness:0.6}); const r = Math.max(0.02, R * 0.06); const base = new THREE.Mesh(new THREE.CylinderGeometry(r * 1.4, r * 1.6, r * 1.5, 24), mat); base.rotation.x = Math.PI / 2; base.position.z = r * 0.75; const upper = new THREE.Mesh(new THREE.CapsuleGeometry(r * 0.8, R * 0.45, 6, 16), mat); upper.position.set(R * 0.12, 0, r * 1.5 + R * 0.24); upper.rotation.y = 0.5; const fore = new THREE.Mesh(new THREE.CapsuleGeometry(r * 0.65, R * 0.45, 6, 16), mat); fore.position.set(R * 0.42, 0, r * 1.5 + R * 0.36); fore.rotation.y = Math.PI / 2 - 0.15; g.add(base, upper, fore); return {obj:g, joints:[], spin:true}; } function rig(gltf, info) { const obj = gltf.scene.clone(true); obj.traverse(o => { if (o.isMesh) o.material = Object.assign(o.material.clone(), {side:THREE.DoubleSide}); }); const joints = []; (info.joints || []).forEach((j, i) => { const node = obj.getObjectByName(j.n); if (!node || !j.arm || j.t !== 'r') return; const amp = Math.min(0.45, 0.3 * (j.hi - j.lo)); const mid = Math.min(Math.max(0, j.lo + amp), j.hi - amp); joints.push({node, rest:node.quaternion.clone(), axis:new THREE.Vector3(...j.a).normalize(), mid, amp, w:0.35 + 0.13 * (i % 4), ph:i * 1.3}); }); return {obj, joints}; } function frame(force) { const b = new THREE.Box3(); [dyn, ...robots.map(r => r.obj)].forEach(o => o && b.expandByObject(o)); if (b.isEmpty()) return; const sphere = b.getBoundingSphere(new THREE.Sphere()); if (!force && fitted && sphere.radius < fitted * 1.15 && sphere.radius > fitted * 0.6) return; fitted = sphere.radius; const dist = sphere.radius / Math.sin(THREE.MathUtils.degToRad(camera.fov / 2)) * 1.05; const dir = new THREE.Vector3(0.75, -1.5, 0.85).normalize(); controls.target.copy(sphere.center); camera.position.copy(sphere.center).addScaledVector(dir, dist); camera.near = dist / 100; camera.far = dist * 10; camera.updateProjectionMatrix(); dispose(env); env = buildEnv(sphere.radius * 1.6); scene.add(env); } // The objects the robot handles, sitting in the work area. async function showObjects(d) { const key = JSON.stringify([d.objects, d.table]); if (key === thingsKey) return; thingsKey = key; dispose(things); things = new THREE.Group(); scene.add(things); const group = things; for (const o of d.objects || []) { let obj; try { if (o.src === 'simready' && o.glb) obj = (await loader.loadAsync(o.glb)).scene; else if (o.src === 'file' && uploads.has(o.uid)) obj = uploads.get(o.uid).clone(true); } catch (e) { obj = null; } if (group !== things) return; // a newer list replaced this one if (!obj) { obj = new THREE.Mesh(new THREE.BoxGeometry(...o.size), new THREE.MeshStandardMaterial({color:color('--s2'), roughness:0.7})); obj.position.z = o.size[2] / 2; obj = new THREE.Group().add(obj); } obj.position.set(o.x, o.y, d.table || 0); group.add(obj); } } async function show(d) { showObjects(d); const robotChanged = !current || current.id !== d.id || JSON.stringify(current.models) !== JSON.stringify(d.models); current = d; const t = robotChanged ? ++token : token; // only a new robot cancels a model still loading dispose(dyn); dyn = buildDyn(d); scene.add(dyn); if (robotChanged) { robots.forEach(r => dispose(r.obj)); robots = []; msg.textContent = ''; if (d.interaction) { msg.textContent = 'Interaction robots have no work area to show in 3D.'; frame(true); return; } if (d.model) { msg.textContent = 'Loading the 3D model…'; try { const [gltf, info] = await loadModel(d.model); if (t !== token) return; robots = d.models.map(m => { const r = rig(gltf, info); r.obj.position.y = m.y; return r; }); robots.forEach(r => { scene.add(r.obj); const b = new THREE.Box3().setFromObject(r.obj); if (b.min.z < -0.01) r.obj.position.z -= b.min.z; // humanoids: feet on the ground if (d.moves) r.obj.position.x = -Math.max(0.15, b.max.x); // mobile robots stand behind the work area }); msg.textContent = ''; } catch (e) { if (t !== token) return; msg.textContent = 'The 3D model could not load.'; } } else if (d.reach) { robots = d.models.map(m => { const r = placeholder(d.R / 0.9); r.obj.position.y = m.y; scene.add(r.obj); return r; }); } frame(true); } else frame(false); } function resize() { const w = canvas.clientWidth, h = canvas.clientHeight; if (!w || !h) return; renderer.setSize(w, h, false); camera.aspect = w / h; camera.updateProjectionMatrix(); } new ResizeObserver(resize).observe(canvas); const q = new THREE.Quaternion(); renderer.setAnimationLoop(() => { if (!canvas.offsetParent) return; // step not visible const t = clock.getElapsedTime(); if (moveBox.checked) robots.forEach(r => { r.joints.forEach(j => { j.node.quaternion.copy(j.rest).multiply(q.setFromAxisAngle(j.axis, j.mid + j.amp * Math.sin(j.w * t + j.ph))); }); if (r.spin) r.obj.rotation.z = 0.5 * Math.sin(0.4 * t); }); controls.update(); renderer.render(scene, camera); }); // The user's own 3D file: measure it, keep it for the view, and add it to the object list. document.getElementById('objFile').addEventListener('change', async e => { const file = e.target.files[0]; e.target.value = ''; if (!file) return; const ext = file.name.split('.').pop().toLowerCase(); const say = window.advisorObjectMsg; if (['usd', 'usda', 'usdc'].includes(ext)) { say('Raw USD, USDA and USDC stages are not supported in this preview. Export a self-contained GLB or supported USDZ, or choose a SimReady object. Keep the original USD files for simulation.'); return; } const url = URL.createObjectURL(file); try { let obj, yUp = true; if (ext === 'glb' || ext === 'gltf') obj = (await loader.loadAsync(url)).scene; else if (ext === 'stl') { obj = new THREE.Mesh(await new STLLoader().loadAsync(url), new THREE.MeshStandardMaterial({color:0xb8b2a6, roughness:0.7})); yUp = false; } else if (ext === 'obj') obj = await new OBJLoader().loadAsync(url); else if (ext === 'usdz') obj = await new USDZLoader().loadAsync(url); else { say('Use a GLB, glTF, STL, OBJ or USDZ file.'); return; } const holder = new THREE.Group(); if (yUp) obj.rotation.x = Math.PI / 2; // glTF, OBJ and USDZ are y-up; the scene is z-up holder.add(obj); let b = new THREE.Box3().setFromObject(holder), size = b.getSize(new THREE.Vector3()); let unit = ''; if (Math.max(size.x, size.y, size.z) > 3) { holder.scale.setScalar(0.001); unit = ' Read as millimetres.'; } b = new THREE.Box3().setFromObject(holder); size = b.getSize(new THREE.Vector3()); const c = b.getCenter(new THREE.Vector3()); const out = new THREE.Group(); holder.position.set(-c.x, -c.y, -b.min.z); out.add(holder); const uid = 'o' + Date.now(); uploads.set(uid, out); // mass guess: a quarter of the bounding box filled with plastic const kg = Math.max(0.01, +(size.x * size.y * size.z * 0.25 * 1100).toFixed(3)); window.advisorAddObject({uid, name:file.name.replace(/\.[^.]+$/, ''), size:[size.x, size.y, size.z].map(v => +v.toFixed(4)), kg, src:'file', est:true}); say(`Added ${file.name}: ${Math.round(size.x * 100)}×${Math.round(size.y * 100)}×${Math.round(size.z * 100)} cm.${unit} Set its mass in the list.`); } catch (err) { say('That file could not be read as a 3D model.'); } finally { URL.revokeObjectURL(url); } }); shellBox.addEventListener('change', () => dyn && dyn.traverse(o => { if (o.userData.role === 'shell') o.visible = shellBox.checked; })); window.addEventListener('advisor:view', e => show(e.detail)); window.addEventListener('advisor:step', e => { if (e.detail === 'robot') requestAnimationFrame(() => { resize(); frame(true); }); }); const recolor = () => { if (current) { dispose(dyn); dyn = buildDyn(current); scene.add(dyn); frame(true); } }; window.addEventListener('advisor:theme', recolor); matchMedia('(prefers-color-scheme: dark)').addEventListener('change', recolor); if (window.__view) show(window.__view);