diff --git a/.gitattributes b/.gitattributes index a6344aa..3b21053 100644 --- a/.gitattributes +++ b/.gitattributes @@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text *.zip filter=lfs diff=lfs merge=lfs -text *.zst filter=lfs diff=lfs merge=lfs -text *tfevents* filter=lfs diff=lfs merge=lfs -text +*.stl filter=lfs diff=lfs merge=lfs -text diff --git a/README.md b/README.md index c694ba3..48bf79b 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,45 @@ --- title: Microduck Sandbox -emoji: 🏆 -colorFrom: gray -colorTo: indigo +emoji: 🐤 +colorFrom: yellow +colorTo: gray sdk: static pinned: false --- -Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference +# Microduck RL playground + +Real trained RL policies for the Microduck robot, running fully in the +browser: MuJoCo compiled to WebAssembly steps the physics, onnxruntime-web +runs the policy network at 50 Hz. No server, no backend. + +## Policies + +| Button | Checkpoint | What it does | +|--------|-----------|--------------| +| Run | `BEST_alpha_walking.onnx` | Velocity-tracking locomotion (arrows / WASD to steer) | +| Sit | `BEST_alpha_sitstand.onnx` | Sits down on its hull, stands back up | +| Roulade | `roulade.onnx` | Rolls over and recovers | + +Policies and MJCF model from +[apirrone/microduck_runtime](https://github.com/apirrone/microduck_runtime) +and [apirrone/mjlab_microduck](https://github.com/apirrone/mjlab_microduck). + +## Controls + +- Arrow up / down: forward / back +- Arrow left / right: turn +- A / E: strafe +- R: reset +- Drag to orbit, scroll to zoom +- Colour dots: repaint the duck (it quacks) + +## How it works + +- `rl.js` fetches the MJCF (`robot_allcollisions.xml`), strips the visual + geoms, injects a floor and a STAND keyframe, and compiles it with the + official `@mujoco/mujoco` WASM bindings. +- The 61D observation (gyro, projected gravity, joint pos/vel, last action, + command) matches `mjlab_microduck/scripts/infer_policy.py`. +- Rendering is a three.js rig built from `kinematics.json` + decimated STL + meshes, driven directly from MuJoCo `qpos`. diff --git a/duck.js b/duck.js new file mode 100644 index 0000000..9fe614b --- /dev/null +++ b/duck.js @@ -0,0 +1,335 @@ +// Standalone JS port of microduck_app's src/duck/kinematics.ts. +// Loads kinematics.json (built from the MJCF by build_kinematics.py) and +// builds an Object3D tree with one Group per body. Joints become per-body +// local rotations driven by setJointAngles / setJoint. + +import * as THREE from "three"; +import { STLLoader } from "three/addons/loaders/STLLoader.js"; +import { mergeVertices, toCreasedNormals } from "three/addons/utils/BufferGeometryUtils.js"; + +// Cache-busting version appended to every STL request. python http.server +// sends no Cache-Control, so browsers apply heuristic caching keyed on +// Last-Modified and can keep serving stale mesh bytes even after the files +// change on disk. Bump this whenever the mesh files are regenerated. +export const MESH_VERSION = "8"; + +// Default model directory: the complete mjlab export (converted from +// robot_walk.xml by tools/mjcf_to_kinematics.py). robot/v1.5/ and +// robot/alpha/ stay on disk for reference but the code no longer +// targets them. +export const MODEL_DIR = "./robot/mjlab"; + +// Meshes fully occluded inside the shells at every demo camera angle, +// verified empirically by per-mesh pixel-diff (front 3/4, back, low +// front close-up): hiding each changes exactly 0 pixels. Skipped at +// load time so their bytes are never fetched. Load everything with +// `?all=1` for debugging. +const HIDDEN_MESHES = new Set([]); + +export async function loadKinematics(url) { + // Same cache-buster as the STLs: force-cache would otherwise keep + // serving a stale kinematics.json after the mesh list changes. + const r = await fetch(`${url}?v=${MESH_VERSION}`, { cache: "force-cache" }); + if (!r.ok) throw new Error(`kinematics fetch ${r.status}`); + const k = await r.json(); + // Full-resolution meshes by default (the reduction will be redone + // interactively in Blender later). `?lite=1` opts into a decimated + // meshes-lite/ sibling if present. + if (new URLSearchParams(location.search).get("lite") === "1") { + k.mesh_dir = k.mesh_dir.replace(/\/meshes$/, "/meshes-lite"); + } + return k; +} + +// Materials are PBR (MeshStandardMaterial), same family as the Reachy +// mobile app's glb viewer. Optional silhouette outlines via three's +// OutlineEffect - see main.js. + +export async function buildRig(k, opts = {}) { + // placer holds world-space position + yaw (no axis-conversion). + const placer = new THREE.Group(); + placer.name = "duck_placer"; + // root applies the MJCF +Z up -> three.js +Y up convention fix. + const root = new THREE.Group(); + root.name = "duck_root"; + root.rotation.x = -Math.PI / 2; + placer.add(root); + + const bodies = new Map(); + const joints = new Map(); + const loader = new STLLoader(); + + // STL files carry per-facet normals, which shade as a faceted mess. + // Weld vertices, then rebuild normals with a crease angle: curved + // surfaces smooth out, machined edges stay hard - the cel look needs + // both. Two scale/attribute pitfalls here: + // - mergeVertices hashes ALL attributes, so the per-facet STL normals + // must be dropped first or coincident vertices never merge. + // - toCreasedNormals groups vertices on a fixed 0.01-unit hash grid. + // Our meshes are in meters, so that grid is 1 cm: normals of vertices + // up to a centimeter apart get averaged together, which shades like a + // heavily decimated mesh. Scaling to mm makes the grid 10 um. + const CREASE = Math.PI / 5; // 36 deg + const meshCache = new Map(); + const loadMesh = (name) => { + if (!meshCache.has(name)) { + meshCache.set( + name, + loader.loadAsync(`${k.mesh_dir}/${name}?v=${MESH_VERSION}`).then((raw) => { + raw.deleteAttribute("normal"); + const welded = mergeVertices(raw, 1e-4); + welded.scale(1000, 1000, 1000); + const display = toCreasedNormals(welded, CREASE); + display.scale(1e-3, 1e-3, 1e-3); + welded.scale(1e-3, 1e-3, 1e-3); + return { display, welded }; + }), + ); + } + return meshCache.get(name); + }; + + for (const b of k.bodies) { + const g = new THREE.Group(); + g.name = b.name; + g.position.set(b.pos[0], b.pos[1], b.pos[2]); + g.quaternion.set(b.quat[1], b.quat[2], b.quat[3], b.quat[0]); + bodies.set(b.name, g); + } + for (const b of k.bodies) { + const g = bodies.get(b.name); + if (b.parent && bodies.has(b.parent)) bodies.get(b.parent).add(g); + else root.add(g); + } + for (const b of k.bodies) { + if (!b.joint || (b.joint.type && b.joint.type !== "hinge")) continue; + const g = bodies.get(b.name); + joints.set(b.joint.name, { + body: g, + axis: new THREE.Vector3(...b.joint.axis).normalize(), + baseQuat: g.quaternion.clone(), + range: b.joint.range ?? null, + }); + } + + // Cache materials by their resolved PBR props so identical parts share + // one GPU material instance. + const matCache = new Map(); + // Optional (meshName, bodyName, rgba) -> material spec hook. A spec is + // { color: [r,g,b], roughness, metalness, opacity? }; plain rgba arrays + // are also accepted for backwards compat. + const materialForMesh = opts.materialForMesh ?? null; + const matFor = (spec) => { + const color = spec.color; + const roughness = spec.roughness ?? 0.5; + const metalness = spec.metalness ?? 0.0; + const opacity = spec.opacity ?? 1; + const key = `${color.join(",")}|${roughness}|${metalness}|${opacity}`; + const cached = matCache.get(key); + if (cached) return cached; + const m = new THREE.MeshStandardMaterial({ + color: new THREE.Color(...color), + roughness, + metalness, + transparent: opacity < 1, + opacity, + }); + matCache.set(key, m); + return m; + }; + const toSpec = (v, fallbackRgba) => { + if (!v) return { color: fallbackRgba.slice(0, 3), opacity: fallbackRgba[3] ?? 1 }; + if (Array.isArray(v)) return { color: v.slice(0, 3), opacity: v[3] ?? 1 }; + return v; + }; + + // Optional interior ink lines: hard edges above the threshold angle + // drawn as line segments, comic style. Cached per mesh file. + const inkOpts = opts.inkEdges ?? null; + const inkMat = inkOpts + ? new THREE.LineBasicMaterial({ + color: inkOpts.color ?? 0x0a0a0e, + transparent: true, + opacity: inkOpts.opacity ?? 0.55, + }) + : null; + const edgeCache = new Map(); + const edgesFor = (name, welded) => { + if (!edgeCache.has(name)) { + edgeCache.set(name, new THREE.EdgesGeometry(welded, inkOpts.threshold ?? 40)); + } + return edgeCache.get(name); + }; + + const pending = []; + const loadAll = new URLSearchParams(location.search).get("all") === "1"; + // The MJCF lists a few geoms twice with identical transforms (visual + + // collision copies of power_support, soles, legs); drawing both would + // only z-fight, so exact duplicates are skipped. + const seenGeoms = new Set(); + for (const b of k.bodies) { + const g = bodies.get(b.name); + if (!g) continue; + for (const geom of b.geoms) { + if (geom.type && geom.type !== "mesh") continue; + if (!geom.mesh) continue; + if (!loadAll && HIDDEN_MESHES.has(geom.mesh)) continue; + const dupKey = `${b.name}|${geom.mesh}|${geom.pos}|${geom.quat}`; + if (seenGeoms.has(dupKey)) continue; + seenGeoms.add(dupKey); + pending.push( + loadMesh(geom.mesh).then(({ display, welded }) => { + const rgba = geom.color + ? [geom.color[0], geom.color[1], geom.color[2], geom.color[3] ?? 1] + : [0.85, 0.85, 0.85, 1]; + const spec = toSpec(materialForMesh?.(geom.mesh, b.name, rgba), rgba); + const m = new THREE.Mesh(display, matFor(spec)); + // Mesh filename tag so callers can re-skin materials in place + // (survives cloneRig: Object3D.copy deep-copies userData). + m.userData.meshName = geom.mesh; + if (geom.pos) m.position.set(...geom.pos); + if (geom.quat) m.quaternion.set(geom.quat[1], geom.quat[2], geom.quat[3], geom.quat[0]); + g.add(m); + if (inkMat) { + const lines = new THREE.LineSegments(edgesFor(geom.mesh, welded), inkMat); + lines.position.copy(m.position); + lines.quaternion.copy(m.quaternion); + g.add(lines); + } + }), + ); + } + } + await Promise.all(pending); + + const rig = { placer, root, bodies, joints }; + setupJawPivot(rig); + return rig; +} + +// ── Jaw hinge ─────────────────────────────────────────────────────────── +// The mjlab model has no passive jaw joints: jaw.stl / jaw_soft.stl are +// rigid geoms of the head body (named "jaw_soft" in the MJCF, it carries +// the head_roll joint). The quack re-creates the hinge in JS: both jaw +// meshes are reparented into a "jaw_pivot" group +// whose origin sits on the rear top edge of their combined bounding box +// (the physical hinge line), and setJawOpen rotates that pivot about the +// robot's left-right axis so the beak tip swings down. +const JAW_MESH_NAMES = new Set(["jaw.stl", "jaw_soft.stl"]); +export const JAW_MAX_OPEN = 0.32; // rad at openness 1 + +function setupJawPivot(rig) { + const meshes = []; + rig.root.traverse((o) => { + if (o.isMesh && JAW_MESH_NAMES.has(o.userData.meshName)) meshes.push(o); + }); + if (!meshes.length) return; + const body = meshes[0].parent; + // The placer is still untransformed right after buildRig, so world + // coords == placer coords here: robot forward is +X, up is +Y. + rig.placer.updateWorldMatrix(true, true); + const box = new THREE.Box3(); + for (const m of meshes) box.expandByObject(m); + const hingeW = new THREE.Vector3(box.min.x, box.max.y, (box.min.z + box.max.z) / 2); + // +angle about -Z rotates the +X beak tip toward -Y (down). + const axisW = new THREE.Vector3(0, 0, -1); + const bodyQuatInv = body.getWorldQuaternion(new THREE.Quaternion()).invert(); + const hingeL = body.worldToLocal(hingeW.clone()); + const axisL = axisW.applyQuaternion(bodyQuatInv).normalize(); + const pivot = new THREE.Group(); + pivot.name = "jaw_pivot"; + pivot.position.copy(hingeL); + // Plain array so Object3D.copy's JSON userData clone preserves it. + pivot.userData.jawAxis = axisL.toArray(); + body.add(pivot); + for (const m of meshes) { + m.position.sub(hingeL); + pivot.add(m); + } +} + +// Open the beak: 0 = closed, 1 = fully open (JAW_MAX_OPEN rad). The pivot +// is resolved lazily by name so clones from cloneRig work transparently. +const _jawAxis = new THREE.Vector3(); +export function setJawOpen(rig, open) { + if (rig._jawPivot === undefined) { + rig._jawPivot = rig.placer.getObjectByName("jaw_pivot") ?? null; + } + const pivot = rig._jawPivot; + if (!pivot) return; + _jawAxis.fromArray(pivot.userData.jawAxis); + pivot.quaternion.setFromAxisAngle(_jawAxis, JAW_MAX_OPEN * open); +} + +// Set one named joint, clamped to its MJCF range when known. +export function setJoint(rig, name, angle) { + const j = rig.joints.get(name); + if (!j) return; + let a = angle; + if (j.range) a = Math.min(j.range[1], Math.max(j.range[0], a)); + const rot = _q.setFromAxisAngle(j.axis, a); + j.body.quaternion.copy(j.baseQuat).multiply(rot); +} +const _q = new THREE.Quaternion(); + +export function applyPose(rig, pose) { + for (const [name, ang] of Object.entries(pose)) setJoint(rig, name, ang); +} + +// Deep-clone a built rig without re-parsing the STL files: Object3D.clone +// shares geometry and materials, so N clones cost almost nothing on top of +// the first buildRig. The bodies/joints maps are rebuilt by looking up the +// cloned nodes by name (body names are unique in the MJCF). +export function cloneRig(rig) { + const placer = rig.placer.clone(true); + const root = placer.getObjectByName("duck_root"); + const bodies = new Map(); + for (const name of rig.bodies.keys()) { + bodies.set(name, placer.getObjectByName(name)); + } + const joints = new Map(); + for (const [name, j] of rig.joints) { + joints.set(name, { + body: placer.getObjectByName(j.body.name), + axis: j.axis, // read-only, safe to share + baseQuat: j.baseQuat.clone(), + range: j.range, + }); + } + return { placer, root, bodies, joints }; +} + +// Ground using the whole rig's bounding box (sitting pose folds the legs +// under the trunk, so the feet are not the lowest point). +const _box = new THREE.Box3(); +export function groundFullBody(rig, floorY = 0) { + rig.placer.updateWorldMatrix(true, true); + _box.setFromObject(rig.placer); + if (!Number.isFinite(_box.min.y)) return 0; + rig.placer.position.y += floorY - _box.min.y; + return floorY - _box.min.y; +} + +// "SIT" keyframe. The mjlab model shares alpha's conventions (same +// onshape-to-robot pipeline): neck_pitch range max is 1.0472, so +// neck_pitch sits just under it (headroom for the breathing oscillation, +// +-0.025) and head_pitch compensates to keep the head level-ish with a +// slight upward tilt toward the camera. NOTE: head_pitch sign is +// inverted vs v1.5 (positive = head down), so the compensation is +// positive here (verified visually). +export const SITTING_POSE = { + left_hip_yaw: 0.0, + left_hip_roll: 0.0, + left_hip_pitch: -0.5236, + left_knee: 1.0472, + left_ankle: 0.0, + neck_pitch: 1.02, + head_pitch: 0.9, + head_yaw: 0.0, + head_roll: 0.0, + right_hip_yaw: 0.0, + right_hip_roll: 0.0, + right_hip_pitch: 0.5236, + right_knee: -1.0472, + right_ankle: 0.0, +}; diff --git a/index.html b/index.html index b0c4b36..1c8ce99 100644 --- a/index.html +++ b/index.html @@ -1,19 +1,175 @@ - - - - - My static Space - - - -
-

Welcome to your static Space!

-

You can modify this app directly by editing index.html in the Files and versions tab.

-

- Also don't forget to check the - Spaces documentation. -

-
- + + + + + Microduck RL playground + + + +
+
+
+
Microduck · real trained policies · MuJoCo + ONNX, live in your browser
+

This one actually learned to run

+
+
+ +
Compiling physics…
+ + + + diff --git a/policies/BEST_alpha_sitstand.onnx b/policies/BEST_alpha_sitstand.onnx new file mode 100644 index 0000000..6b26011 --- /dev/null +++ b/policies/BEST_alpha_sitstand.onnx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c6c40e35e726eabd803d633e090d112994f469921152448367953fbaf9799bc8 +size 793695 diff --git a/policies/BEST_alpha_walking.onnx b/policies/BEST_alpha_walking.onnx new file mode 100644 index 0000000..7c1d28a --- /dev/null +++ b/policies/BEST_alpha_walking.onnx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f5604bce126b01ebf341b0d1de2e409cb969895106b7715c27cefacccecd9972 +size 793705 diff --git a/policies/roulade.onnx b/policies/roulade.onnx new file mode 100644 index 0000000..060c707 --- /dev/null +++ b/policies/roulade.onnx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3d60da08fc13f29c1b57f41977aa898132c0d60042100149d8e775affcbca32b +size 793685 diff --git a/rl.js b/rl.js new file mode 100644 index 0000000..391d976 --- /dev/null +++ b/rl.js @@ -0,0 +1,518 @@ +// Microduck RL playground: the REAL trained policies, not a procedural waddle. +// +// Physics runs in MuJoCo compiled to WebAssembly (the official +// @mujoco/mujoco bindings), stepping the same MJCF the policies were +// trained on (apirrone/mjlab_microduck). The controller is one of the +// exported ONNX checkpoints from apirrone/microduck_runtime, executed with +// onnxruntime-web at 50 Hz (timestep 0.005 s, decimation 4) - exactly the +// loop from mjlab_microduck/scripts/infer_policy.py. +// +// Obs layout (61D, "new-cmd-obs" flavor, from the ONNX metadata): +// [base_ang_vel(3), projected_gravity(3), joint_pos(14), joint_vel(14), +// last_action(14), command(13)] +// command = [vx, vy, wz, head_pose(4), body_pose(6)]; for the sitstand +// policy, command[0] is the posture flag (1 = sit, 0 = stand). +// Action (14) = joint position targets relative to the default pose. + +import * as THREE from "three"; +import { OrbitControls } from "three/addons/controls/OrbitControls.js"; +import { RoomEnvironment } from "three/addons/environments/RoomEnvironment.js"; +import { buildRig, loadKinematics, setJoint, setJawOpen, MODEL_DIR, MESH_VERSION } from "./duck.js"; +import { VARIANTS, VARIANT_NAMES, materialHookFor, randomVariantName, applyVariant, specToHex } from "./variants.js"; +import loadMujoco from "https://cdn.jsdelivr.net/npm/@mujoco/mujoco@3.11.0/mujoco.js"; +import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.27.0/dist/ort.min.mjs"; + +ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.27.0/dist/"; +ort.env.wasm.numThreads = 1; // static hosting sends no COOP/COEP headers + +const POLICY_DIR = "./policies"; +const POLICIES = { + walk: `${POLICY_DIR}/BEST_alpha_walking.onnx`, + sitstand: `${POLICY_DIR}/BEST_alpha_sitstand.onnx`, + roulade: `${POLICY_DIR}/roulade.onnx`, +}; + +// From the ONNX metadata (identical for all alpha policies) and the STAND +// keyframe in mjlab's scene_walk.xml. Order matches the actuators in +// the MJCF. +const JOINT_NAMES = [ + "left_hip_yaw", "left_hip_roll", "left_hip_pitch", "left_knee", "left_ankle", + "neck_pitch", "head_pitch", "head_yaw", "head_roll", + "right_hip_yaw", "right_hip_roll", "right_hip_pitch", "right_knee", "right_ankle", +]; +const DEFAULT_POSE = new Float32Array([ + 0, -0.08726646259971647, -0.457924, -0.004940, 0.452984, + 0.3490658503988659, 0.3490658503988659, 0, 0, + 0, 0.08726646259971647, 0.457924, 0.004940, -0.452984, +]); +const NUM_JOINTS = 14; +const OBS_SIZE = 61; +const CMD_SIZE = 13; +const ACTION_SCALE = 1.0; +const TIMESTEP = 0.005; +const DECIMATION = 4; +const CTRL_DT = TIMESTEP * DECIMATION; // 50 Hz + +// Velocity command limits, same as infer_policy.py's keyboard mapping. +const VEL_FWD = 0.25, VEL_BACK = -0.2, VEL_LAT = 0.2, VEL_ANG = 1.0; + +const mount = document.getElementById("scene"); +const loadingEl = document.getElementById("loading"); +const hudEl = document.getElementById("hud"); +const statsEl = document.getElementById("stats"); +const verbEl = document.getElementById("verb"); +const setLoading = (msg) => { loadingEl.textContent = msg; }; + +// Surface boot failures on the page itself: a rejected top-level await +// otherwise leaves the loading screen up with no visible error. +window.addEventListener("unhandledrejection", (e) => { + const msg = e.reason?.stack || e.reason?.message || String(e.reason); + setLoading(`Boot failed: ${msg}`); + console.error("[rl] unhandled rejection", e.reason); +}); +window.addEventListener("error", (e) => { + setLoading(`Boot failed: ${e.message}`); +}); +const bootSteps = []; +const traced = (label, p) => { + bootSteps.push(label); + return p.then( + (v) => { + bootSteps.splice(bootSteps.indexOf(label), 1); + if (bootSteps.length) setLoading(`Loading ${bootSteps.join(", ")}\u2026`); + return v; + }, + (err) => { console.error(`[rl] ${label} FAILED`, err); throw err; }, + ); +}; + +// ── MJCF preparation ──────────────────────────────────────────────────── +// robot_allcollisions.xml is what infer_policy.py's scene.xml includes: it +// carries body/shell collision geoms that robot_walk.xml lacks, which the +// sitstand policy needs (a sit rests the trunk on the ground). +// The visual meshes are irrelevant to the dynamics: every body carries an +// explicit , and visual geoms have contype=0 conaffinity=0. +// Stripping them means the MuJoCo VFS only needs the ~10 meshes referenced +// by collision geoms. +async function buildPhysicsXml() { + const src = await (await fetch(`${MODEL_DIR}/robot_allcollisions.xml`)).text(); + const doc = new DOMParser().parseFromString(src, "text/xml"); + for (const g of [...doc.querySelectorAll('geom[class="visual"]')]) g.remove(); + const usedMeshes = new Set( + [...doc.querySelectorAll("geom[mesh]")].map((g) => g.getAttribute("mesh")), + ); + for (const m of [...doc.querySelectorAll("asset > mesh")]) { + const name = m.getAttribute("name") ?? m.getAttribute("file").replace(/\.stl$/i, ""); + if (!usedMeshes.has(name)) m.remove(); + } + const root = doc.documentElement; + const el = (tag, attrs) => { + const e = doc.createElement(tag); + for (const [k, v] of Object.entries(attrs)) e.setAttribute(k, v); + return e; + }; + root.appendChild(el("option", { timestep: String(TIMESTEP) })); + doc.querySelector("worldbody").appendChild( + el("geom", { name: "floor", type: "plane", size: "0 0 0.05", pos: "0 0 0" }), + ); + // STAND keyframe from mjlab's scene_walk.xml (STAND2 pose). + const qposFree = "0 0 0.12 1 0 0 0"; + const pose14 = Array.from(DEFAULT_POSE).join(" "); + const kf = doc.createElement("keyframe"); + kf.appendChild(el("key", { name: "STAND", qpos: `${qposFree} ${pose14}`, ctrl: pose14 })); + root.appendChild(kf); + const meshFiles = [...doc.querySelectorAll("asset > mesh")].map((m) => m.getAttribute("file")); + return { xml: new XMLSerializer().serializeToString(doc), meshFiles }; +} + +// ── Boot physics + policy in parallel with the render rig ────────────── +setLoading("Loading MuJoCo WASM, policies and meshes\u2026"); + +const [mujoco, { xml, meshFiles }, k] = await Promise.all([ + traced("mujoco wasm", loadMujoco()), + traced("physics xml", buildPhysicsXml()), + traced("kinematics", loadKinematics(`${MODEL_DIR}/kinematics.json`)), +]); + +const vfs = new mujoco.MjVFS(); +await Promise.all( + meshFiles.map(async (f) => { + // Same cache-busted URL as duck.js so the browser reuses the render + // meshes instead of downloading the collision subset a second time. + const buf = await (await fetch(`${MODEL_DIR}/meshes/${f}?v=${MESH_VERSION}`, { cache: "force-cache" })).arrayBuffer(); + // meshdir="assets" in the MJCF, so the compiler looks up "assets/". + vfs.addBuffer(`assets/${f}`, new Uint8Array(buf)); + }), +); + +const sessions = {}; +let currentVariant = randomVariantName(); +const rigPromise = (async () => { + return buildRig(k, { materialForMesh: materialHookFor(VARIANTS[currentVariant]) }); +})(); +const sessionOpts = { executionProviders: ["wasm"] }; +[sessions.walk, sessions.sitstand, sessions.roulade] = await Promise.all([ + ort.InferenceSession.create(POLICIES.walk, sessionOpts), + ort.InferenceSession.create(POLICIES.sitstand, sessionOpts), + ort.InferenceSession.create(POLICIES.roulade, sessionOpts), +]); + +setLoading("Compiling physics\u2026"); +const model = mujoco.MjModel.from_xml_string(xml, vfs); +const data = new mujoco.MjData(model); + +// Addresses resolved once. qpos/qvel/sensordata views are re-read at each +// use: the WASM heap can grow and detach earlier TypedArray views. +// NOTE: unlike the Python bindings, these accessor fields are plain numbers. +const qposAdr = JOINT_NAMES.map((n) => model.jnt(n).qposadr); +const dofAdr = JOINT_NAMES.map((n) => model.jnt(n).dofadr); +const gyroAdr = model.sensor("imu_ang_vel").adr; +const trunkId = mujoco.mj_name2id(model, mujoco.mjtObj.mjOBJ_BODY.value, "trunk_base"); +const standKeyId = mujoco.mj_name2id(model, mujoco.mjtObj.mjOBJ_KEY.value, "STAND"); + +let uiReady = false; +const lastAction = new Float32Array(NUM_JOINTS); +const obs = new Float32Array(OBS_SIZE); +const cmd = new Float32Array(CMD_SIZE); // [vx, vy, wz, head(4), body(6)] +const velCmd = new Float32Array(3); // twist command, driven by held keys +// Declared before the control loop starts: buildObs checks it to decide +// between the auto-run default and manual key control. +const held = new Set(); + +let mode = "walk"; // "walk" | "sitstand" | "roulade" +let sitFlag = 0; + +function resetSim() { + mujoco.mj_resetDataKeyframe(model, data, standKeyId); + mujoco.mj_forward(model, data); + lastAction.fill(0); + sitFlag = 0; + // Buttons reflect sitFlag; keep them honest after auto-resets. + if (uiReady) syncButtons(); +} +resetSim(); + +// ── Observation ───────────────────────────────────────────────────────── +const _q = new THREE.Quaternion(); +const _g = new THREE.Vector3(); + +function buildObs() { + const qpos = data.qpos, qvel = data.qvel, sens = data.sensordata; + let i = 0; + // base_ang_vel: gyro sensor at the IMU site + for (let a = 0; a < 3; a++) obs[i++] = sens[gyroAdr + a]; + // projected gravity: world -z rotated into the trunk frame + const xq = data.body(trunkId).xquat; // [w, x, y, z] + _q.set(xq[1], xq[2], xq[3], xq[0]).conjugate(); + _g.set(0, 0, -1).applyQuaternion(_q); + obs[i++] = _g.x; obs[i++] = _g.y; obs[i++] = _g.z; + for (let j = 0; j < NUM_JOINTS; j++) obs[i++] = qpos[qposAdr[j]] - DEFAULT_POSE[j]; + for (let j = 0; j < NUM_JOINTS; j++) obs[i++] = qvel[dofAdr[j]]; + for (let j = 0; j < NUM_JOINTS; j++) obs[i++] = lastAction[j]; + // command: walking/roulade use the twist; sitstand uses cmd[0] as the + // posture flag. "Run" means run: with no keys held the walking policy + // gets a forward velocity by default, keys override it. + cmd.fill(0, 0, 3); + if (mode === "walk") { + cmd[0] = held.size ? velCmd[0] : VEL_FWD; + cmd[1] = velCmd[1]; cmd[2] = velCmd[2]; + } else if (mode === "roulade") { + cmd[0] = velCmd[0]; cmd[1] = velCmd[1]; cmd[2] = velCmd[2]; + } else { + cmd[0] = sitFlag; + } + for (let c = 0; c < CMD_SIZE; c++) obs[i++] = cmd[c]; + return obs; +} + +// ── Control loop (50 Hz, async because ONNX inference is async) ──────── +let ctrlHz = 0; +let fallenSince = null; + +async function controlStep() { + const feeds = { obs: new ort.Tensor("float32", buildObs(), [1, OBS_SIZE]) }; + const out = await sessions[mode].run(feeds); + const act = out.actions.data; + lastAction.set(act); + const ctrl = data.ctrl; + for (let j = 0; j < NUM_JOINTS; j++) ctrl[j] = DEFAULT_POSE[j] + act[j] * ACTION_SCALE; + for (let s = 0; s < DECIMATION; s++) mujoco.mj_step(model, data); + + // Fall detection: walk/sitstand have no get-up skill, so auto-reset when + // down for good. Height alone would false-positive on a deep sit, so + // "fallen" = trunk tilted past ~60 deg (projected gravity z above -0.5) + // or sunk below the floor, sustained for over a second. The roulade rolls + // the trunk on purpose and recovers on its own, so it gets a much longer + // grace window before we call it stuck. + const z = data.qpos[2]; + const now = performance.now(); + const tipped = obs[5] > -0.5; // projected gravity z, from the last obs + const graceMs = mode === "roulade" ? 5000 : 1000; + if (tipped || z < 0.02) { + fallenSince ??= now; + if (now - fallenSince > graceMs) { resetSim(); fallenSince = null; } + } else { + fallenSince = null; + } +} + +let running = true; +(async function controlLoop() { + let next = performance.now(); + let count = 0, hzT0 = next; + while (running) { + await controlStep(); + count++; + const now = performance.now(); + if (now - hzT0 > 500) { + ctrlHz = (count * 1000) / (now - hzT0); + count = 0; hzT0 = now; + } + next += CTRL_DT * 1000; + const wait = next - performance.now(); + if (wait > 0) await new Promise((r) => setTimeout(r, wait)); + else next = performance.now(); // fell behind: don't spiral + } +})(); + +// ── Rendering (three.js rig driven by qpos) ───────────────────────────── +const scene = new THREE.Scene(); +scene.background = new THREE.Color(0x08080c); + +const camera = new THREE.PerspectiveCamera(40, 1, 0.02, 30); +camera.position.set(0.55, 0.35, 0.7); + +const renderer = new THREE.WebGLRenderer({ antialias: true }); +renderer.setPixelRatio(Math.min(2, window.devicePixelRatio)); +renderer.toneMapping = THREE.ACESFilmicToneMapping; +mount.appendChild(renderer.domElement); + +const pmrem = new THREE.PMREMGenerator(renderer); +scene.environment = pmrem.fromScene(new RoomEnvironment()).texture; +scene.environmentIntensity = 0.45; + +scene.add(new THREE.AmbientLight(0xffffff, 0.6)); +const keyLight = new THREE.DirectionalLight(0xffffff, 1.6); +keyLight.position.set(2, 4, 2); +scene.add(keyLight); +const fill = new THREE.DirectionalLight(0xffffff, 0.4); +fill.position.set(-2, 2, 1.5); +scene.add(fill); +const rim = new THREE.DirectionalLight(0xffb366, 0.7); +rim.position.set(0, 3, -2); +scene.add(rim); + +function makeFloorTexture() { + const N = 256; + const c = document.createElement("canvas"); + c.width = c.height = N; + const g = c.getContext("2d"); + g.fillStyle = "#131009"; + g.fillRect(0, 0, N, N); + g.strokeStyle = "rgba(255, 179, 102, 0.05)"; + g.lineWidth = 2; + g.strokeRect(1, 1, N - 2, N - 2); + g.fillStyle = "rgba(255, 198, 30, 0.10)"; + for (const x of [0, N]) { + for (const y of [0, N]) { + g.beginPath(); + g.arc(x, y, 5, 0, Math.PI * 2); + g.fill(); + } + } + const tex = new THREE.CanvasTexture(c); + tex.colorSpace = THREE.SRGBColorSpace; + tex.wrapS = tex.wrapT = THREE.RepeatWrapping; + tex.repeat.set(48, 48); + return tex; +} +const floor = new THREE.Mesh( + new THREE.PlaneGeometry(12, 12), + new THREE.MeshStandardMaterial({ map: makeFloorTexture(), roughness: 0.9, metalness: 0.0 }), +); +floor.rotation.x = -Math.PI / 2; +scene.add(floor); + +const rig = await rigPromise; +scene.add(rig.placer); +const trunkGroup = rig.bodies.get("trunk_base"); + +const controls = new OrbitControls(camera, renderer.domElement); +controls.enableDamping = true; +controls.dampingFactor = 0.08; +controls.minDistance = 0.25; +controls.maxDistance = 3; +controls.maxPolarAngle = Math.PI / 2 - 0.03; + +function resize() { + const w = mount.clientWidth, h = mount.clientHeight; + camera.aspect = w / h; + camera.updateProjectionMatrix(); + renderer.setSize(w, h); +} +new ResizeObserver(resize).observe(mount); +resize(); + +// The rig's root already applies the MJCF Z-up -> three Y-up fix, so the +// trunk group can take the freejoint pose in raw MJCF coordinates. +const _target = new THREE.Vector3(); +function syncRig() { + const qpos = data.qpos; + trunkGroup.position.set(qpos[0], qpos[1], qpos[2]); + trunkGroup.quaternion.set(qpos[4], qpos[5], qpos[6], qpos[3]); + for (let j = 0; j < NUM_JOINTS; j++) setJoint(rig, JOINT_NAMES[j], qpos[qposAdr[j]]); + // Camera target eases toward the trunk so the duck stays framed while + // it walks away. + _target.set(qpos[0], qpos[2], -qpos[1]); + controls.target.lerp(_target, 0.06); +} + +// Quack: a quick jaw flap on every mode/colour change. The jaw isn't a +// MuJoCo joint (duck.js re-creates the hinge in JS), so this is purely +// cosmetic and can't upset the policy. +const QUACK_MS = 480; +let quackAt = -Infinity; +const quack = () => { quackAt = performance.now(); }; +function syncJaw() { + const t = (performance.now() - quackAt) / QUACK_MS; + setJawOpen(rig, t >= 0 && t < 1 ? Math.sin(Math.PI * t) : 0); +} + +function renderStats() { + const [vx, vy, wz] = velCmd; + const posture = mode === "walk" + ? `running policy` + : mode === "roulade" + ? `roulade policy` + : `sitstand policy \u00b7 ${sitFlag ? "sit" : "stand"}`; + statsEl.innerHTML = + `${posture}
` + + `cmd vx ${vx.toFixed(2)} \u00b7 vy ${vy.toFixed(2)} \u00b7 wz ${wz.toFixed(2)}
` + + `ctrl ${ctrlHz.toFixed(0)} Hz \u00b7 sim t ${data.time.toFixed(1)} s`; +} + +function loop() { + requestAnimationFrame(loop); + syncRig(); + syncJaw(); + controls.update(); + renderStats(); + renderer.render(scene, camera); +} +loadingEl.style.display = "none"; +hudEl.hidden = false; +loop(); + +// ── Input: hold-to-command keys + HUD buttons ─────────────────────────── +function refreshVelCmd() { + velCmd[0] = held.has("fwd") ? VEL_FWD : held.has("back") ? VEL_BACK : 0; + velCmd[1] = held.has("left") ? VEL_LAT : held.has("right") ? -VEL_LAT : 0; + velCmd[2] = held.has("turnl") ? VEL_ANG : held.has("turnr") ? -VEL_ANG : 0; +} + +const KEYMAP = { + ArrowUp: "fwd", KeyW: "fwd", KeyZ: "fwd", + ArrowDown: "back", KeyS: "back", + ArrowLeft: "turnl", ArrowRight: "turnr", + KeyA: "left", KeyQ: "left", KeyE: "right", KeyD: "right", +}; + +window.addEventListener("keydown", (e) => { + if (e.repeat) return; + if (e.code === "KeyR") { resetSim(); return; } + const act = KEYMAP[e.code]; + if (!act) return; + e.preventDefault(); + held.add(act); + refreshVelCmd(); +}); +window.addEventListener("keyup", (e) => { + const act = KEYMAP[e.code]; + if (!act) return; + held.delete(act); + refreshVelCmd(); +}); +window.addEventListener("blur", () => { held.clear(); refreshVelCmd(); }); + +const btnWalk = document.getElementById("btn-walk"); +const btnSit = document.getElementById("btn-sit"); +const btnRoulade = document.getElementById("btn-roulade"); +document.getElementById("btn-reset").addEventListener("click", resetSim); + +// The headline verb tracks the active policy. +const VERBS = { walk: "run", sitstand: "sit", roulade: "roll over" }; + +function setMode(next) { + quack(); + if (next !== "sit") { + // Leaving a sit: let the sitstand policy stand the duck back up first. + if (mode === "sitstand" && sitFlag === 1) { + sitFlag = 0; + setTimeout(() => { mode = next; lastAction.fill(0); syncButtons(); }, 2000); + syncButtons(); + return; + } + mode = next; + lastAction.fill(0); + } else { + // Hand over gently: hold the stand under the sitstand policy for a + // moment before commanding the sit, or the abrupt session switch + // (walking's action history + instant flag) knocks the duck over. + mode = "sitstand"; + sitFlag = 0; + lastAction.fill(0); + setTimeout(() => { + if (mode === "sitstand") { sitFlag = 1; syncButtons(); } + }, 800); + } + syncButtons(); +} +function syncButtons() { + const sitting = mode === "sitstand" && sitFlag === 1; + btnWalk.classList.toggle("on", mode === "walk" || (mode === "sitstand" && !sitting)); + btnSit.classList.toggle("on", sitting); + btnRoulade.classList.toggle("on", mode === "roulade"); + verbEl.textContent = VERBS[mode]; +} +btnWalk.addEventListener("click", () => setMode("walk")); +btnSit.addEventListener("click", () => setMode("sit")); +btnRoulade.addEventListener("click", () => setMode("roulade")); + +// ── Colour swatches: re-skin the rig live, with a quack ───────────────── +// One representative colour per variant so the dots read at a glance. +const SWATCH_SLOT = { classic: "feet", charcoal: "headDome", purple: "feet", blue: "facePlate" }; +const swatchesEl = document.getElementById("swatches"); +const swatchBtns = new Map(); +for (const name of VARIANT_NAMES) { + const v = VARIANTS[name]; + const b = document.createElement("button"); + b.style.background = specToHex(v[SWATCH_SLOT[name] ?? "bodyShell"]); + b.setAttribute("aria-label", `${name} colours`); + b.addEventListener("click", () => { + if (name === currentVariant) return; + currentVariant = name; + applyVariant(rig, name); + quack(); + syncSwatches(); + }); + swatchesEl.appendChild(b); + swatchBtns.set(name, b); +} +function syncSwatches() { + for (const [name, b] of swatchBtns) b.classList.toggle("on", name === currentVariant); +} +syncSwatches(); +syncButtons(); +uiReady = true; + +// Deterministic hooks for automated verification (rAF pauses in +// background tabs, and the control loop is async). +window.rl = { + model, data, mujoco, + get mode() { return mode; }, + get sitFlag() { return sitFlag; }, + buildObs, cmd, + velCmd, lastAction, resetSim, + step: async (n = 1) => { for (let i = 0; i < n; i++) await controlStep(); }, + render: () => { syncRig(); renderer.render(scene, camera); }, +}; diff --git a/robot/mjlab/kinematics.json b/robot/mjlab/kinematics.json new file mode 100644 index 0000000..9090b3b --- /dev/null +++ b/robot/mjlab/kinematics.json @@ -0,0 +1,2013 @@ +{ + "bodies": [ + { + "name": "trunk_base", + "parent": null, + "pos": [ + 0.0, + 0.0, + 0.12 + ], + "quat": [ + 1.0, + 0.0, + 0.0, + 0.0 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + 0.006, + 0.0175, + -0.005 + ], + "quat": [ + 1.0, + -0.0, + -0.0, + 0.0 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "right_shell.stl", + "pos": [ + 0.004, + -0.0005, + -0.0175 + ], + "quat": [ + 0.707107, + -0.0, + -0.0, + 0.707107 + ], + "color": [ + 0.8, + 0.8, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "banana_pcb_locker.stl", + "pos": [ + 0.0042, + -0.0, + -0.0175215 + ], + "quat": [ + 0.707107, + -0.0, + 0.0, + 0.707107 + ], + "color": [ + 0.647059, + 0.647059, + 0.647059, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "trunk_base.stl", + "pos": [ + 0.004, + -0.0, + -0.0175 + ], + "quat": [ + 0.707107, + -0.0, + -0.0, + 0.707107 + ], + "color": [ + 0.34902, + 0.376471, + 0.4, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + 0.006, + 0.0175, + 0.0115 + ], + "quat": [ + 0.0, + 0.707107, + -0.0, + 0.707107 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "power_support.stl", + "pos": [ + 0.004, + -0.0, + -0.0175215 + ], + "quat": [ + 0.707107, + -0.0, + 0.0, + 0.707107 + ], + "color": [ + 0.901961, + 0.901961, + 0.901961, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "np_f970.stl", + "pos": [ + -0.0359693, + 0.0, + -0.00385049 + ], + "quat": [ + 0.00617059, + 0.70708, + -0.70708, + -0.00617059 + ], + "color": [ + 0.901961, + 0.901961, + 0.901961, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + 0.006, + -0.0175, + 0.0115 + ], + "quat": [ + 0.0, + 0.707107, + -0.0, + 0.707107 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "left_shell.stl", + "pos": [ + 0.004, + -0.0, + -0.0175785 + ], + "quat": [ + 0.707107, + 0.0, + -0.0, + 0.707107 + ], + "color": [ + 0.8, + 0.8, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + 0.006, + -0.0175, + -0.005 + ], + "quat": [ + 1.0, + -0.0, + -0.0, + 0.0 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + } + ], + "joint": null + }, + { + "name": "yaw2roll", + "parent": "trunk_base", + "pos": [ + 0.006, + 0.0175, + -0.005 + ], + "quat": [ + 0.0, + -0.707107, + -0.707107, + -0.0 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "yaw2roll.stl", + "pos": [ + -0.0175, + -0.002, + 0.0125 + ], + "quat": [ + 0.0, + 1.0, + 0.0, + 0.0 + ], + "color": [ + 0.647059, + 0.647059, + 0.647059, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + -0.0, + 0.0, + 0.0125 + ], + "quat": [ + 0.0, + -0.707107, + 0.707107, + -0.0 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + -0.0, + 0.0165, + 0.0125 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + -0.5 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "bearing_roll.stl", + "pos": [ + -0.0175, + -0.002, + 0.0125 + ], + "quat": [ + 0.0, + -1.0, + -0.0, + -0.0 + ], + "color": [ + 0.262745, + 0.282353, + 0.301961, + 1.0 + ] + } + ], + "joint": { + "name": "left_hip_yaw", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -0.4363323129985824, + 0.5235987755982988 + ], + "actuator_index": 0 + } + }, + { + "name": "hip_l", + "parent": "yaw2roll", + "pos": [ + 0.0, + 0.0165, + 0.0125 + ], + "quat": [ + 0.707107, + -0.707107, + -0.0, + -0.0 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + 0.021, + 0.0, + -0.0185 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + 0.5 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "hip_l.stl", + "pos": [ + -0.0175, + 0.0, + -0.0185 + ], + "quat": [ + 0.707107, + -0.707107, + 0.0, + -0.0 + ], + "color": [ + 0.901961, + 0.901961, + 0.901961, + 1.0 + ] + } + ], + "joint": { + "name": "left_hip_roll", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -0.3839724354387525, + 0.3839724354387525 + ], + "actuator_index": 1 + } + }, + { + "name": "upper_leg_left", + "parent": "hip_l", + "pos": [ + 0.025, + 0.0, + -0.0185 + ], + "quat": [ + 0.5, + -0.5, + 0.5, + -0.5 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "upper_leg_left.stl", + "pos": [ + -0.0, + -0.0, + -0.0425 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + -0.5 + ], + "color": [ + 0.866667, + 0.866667, + 0.866667, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + 0.022, + 0.0357771, + 0.0125 + ], + "quat": [ + 0.0, + -0.707107, + -0.0, + -0.707107 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "upper_leg_rigidity_plate.stl", + "pos": [ + 0.0, + -0.0, + -0.0425 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + -0.5 + ], + "color": [ + 0.537255, + 0.854902, + 0.827451, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + 0.022, + 0.0357771, + -0.004 + ], + "quat": [ + 0.0, + 0.0, + 0.0, + 1.0 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + -0.0, + -0.0, + 0.0125 + ], + "quat": [ + 0.5, + 0.5, + -0.5, + 0.5 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + } + ], + "joint": { + "name": "left_hip_pitch", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -1.570796326795005, + 1.5707963267947882 + ], + "actuator_index": 2 + } + }, + { + "name": "leg", + "parent": "upper_leg_left", + "pos": [ + 0.022, + 0.0357771, + -0.004 + ], + "quat": [ + 0.0, + 0.707107, + 0.707107, + 0.0 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "leg.stl", + "pos": [ + -0.022, + 0.0357771, + 0.0385 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + -0.5 + ], + "color": [ + 0.811765, + 0.858824, + 0.898039, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + 0.0, + 0.042, + -0.0115 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + -0.5 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + } + ], + "joint": { + "name": "left_knee", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -1.570796326795012, + 1.570796326794781 + ], + "actuator_index": 3 + } + }, + { + "name": "ankle_left", + "parent": "leg", + "pos": [ + 0.0, + 0.042, + -0.026 + ], + "quat": [ + 0.0, + 1.0, + -0.0, + 0.0 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "sole_left.stl", + "pos": [ + -0.022, + 0.00622291, + -0.0647 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + -0.5 + ], + "color": [ + 0.537255, + 0.854902, + 0.827451, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "foot_left.stl", + "pos": [ + -0.022, + 0.00622291, + -0.0647 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + -0.5 + ], + "color": [ + 0.980392, + 0.713725, + 0.00392157, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration_default.stl", + "pos": [ + 0.0, + 0.0, + -0.0322 + ], + "quat": [ + 0.0, + 0.0, + -0.0, + 1.0 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "ankle_left.stl", + "pos": [ + -0.022, + 0.00622291, + -0.0647 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + -0.5 + ], + "color": [ + 0.980392, + 0.713725, + 0.00392157, + 1.0 + ] + } + ], + "joint": { + "name": "left_ankle", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -1.5707963267949019, + 1.5707963267948912 + ], + "actuator_index": 4 + } + }, + { + "name": "neck", + "parent": "trunk_base", + "pos": [ + 0.026, + 0.0145, + 0.0324215 + ], + "quat": [ + 0.0, + 0.0, + 0.707107, + -0.707107 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "neck.stl", + "pos": [ + 0.022, + 0.05, + 0.028 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + -0.5 + ], + "color": [ + 0.8, + 0.8, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + 0.0, + 0.0, + 0.0145 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + -0.5 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + 0.0, + -0.05, + 0.0145 + ], + "quat": [ + 0.5, + 0.5, + -0.5, + 0.5 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "neck.stl", + "pos": [ + 0.022, + 0.05, + 0.003 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + -0.5 + ], + "color": [ + 0.8, + 0.8, + 0.8, + 1.0 + ] + } + ], + "joint": { + "name": "neck_pitch", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -1.5707963267948966, + 1.0471975511965976 + ], + "actuator_index": 5 + } + }, + { + "name": "neck_pitch", + "parent": "neck", + "pos": [ + 0.0, + -0.05, + -0.0 + ], + "quat": [ + 0.0, + 1.0, + 0.0, + 0.0 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "neck_pitch.stl", + "pos": [ + -0.0, + 0.0287931, + -0.0145 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + -0.5 + ], + "color": [ + 0.34902, + 0.376471, + 0.4, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + -0.0, + 0.0207931, + -0.0145 + ], + "quat": [ + 0.707107, + 0.707107, + 0.0, + 0.0 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + } + ], + "joint": { + "name": "head_pitch", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -1.5707963267948974, + 1.5707963267948957 + ], + "actuator_index": 6 + } + }, + { + "name": "yaw_roll_motion", + "parent": "neck_pitch", + "pos": [ + -0.0, + 0.0186931, + -0.0145 + ], + "quat": [ + 0.0, + 0.0, + -0.707107, + -0.707107 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "yaw_roll_motion.stl", + "pos": [ + 0.0, + 0.0, + 0.01 + ], + "quat": [ + 0.707107, + 0.0, + -0.0, + 0.707107 + ], + "color": [ + 0.768627, + 0.886275, + 0.952941, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + 0.0, + 0.0, + 0.0145 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + -0.5 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + -0.016, + 0.0, + 0.0145 + ], + "quat": [ + 0.707107, + -0.0, + -0.707107, + -0.0 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + 0.018, + 0.0, + 0.0145 + ], + "quat": [ + 0.707107, + -0.0, + -0.707107, + -0.0 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + } + ], + "joint": { + "name": "head_yaw", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -2.967059728390373, + 2.967059728390348 + ], + "actuator_index": 7 + } + }, + { + "name": "jaw_soft", + "parent": "yaw_roll_motion", + "pos": [ + -0.0179, + 0.0, + 0.0145 + ], + "quat": [ + 0.707107, + -0.0, + -0.707107, + -0.0 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "lens.stl", + "pos": [ + 0.0155, + -9.13778e-05, + -0.05548 + ], + "quat": [ + 0.5, + -0.5, + 0.5, + -0.5 + ], + "color": [ + 0.262745, + 0.282353, + 0.301961, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "face_part.stl", + "pos": [ + -0.0045, + -9.13778e-05, + -0.0178 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + 0.5 + ], + "color": [ + 0.34902, + 0.376471, + 0.4, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + 0.003, + 0.0255, + -0.018 + ], + "quat": [ + 0.707107, + 0.0, + -0.0, + -0.707107 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "jaw_soft.stl", + "pos": [ + -0.0046, + 0.0, + -0.0179996 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + 0.5 + ], + "color": [ + 0.85098, + 0.756863, + 0.866667, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "motor_support.stl", + "pos": [ + -0.0045, + 0.0, + -0.018 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + 0.5 + ], + "color": [ + 0.262745, + 0.282353, + 0.301961, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "top_head_shell.stl", + "pos": [ + -0.0045, + -0.0, + -0.018 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + 0.5 + ], + "color": [ + 0.901961, + 0.901961, + 0.901961, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + 0.0, + 0.0, + 0.0145 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + -0.5 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration_default.stl", + "pos": [ + 0.0032, + -0.04, + -0.018 + ], + "quat": [ + 0.5, + -0.5, + 0.5, + 0.5 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "elec_rpi_robot_hat_pcb.stl", + "pos": [ + 0.02694, + 0.0289936, + -0.0519 + ], + "quat": [ + 0.707107, + 0.0, + 0.0, + -0.707107 + ], + "color": [ + 0.313725, + 0.486275, + 0.411765, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "soft_mouth_top.stl", + "pos": [ + -0.00420071, + 1.7776e-08, + -0.0180814 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + 0.5 + ], + "color": [ + 0.85098, + 0.756863, + 0.866667, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "noenoeil.stl", + "pos": [ + -0.0045, + -9.13778e-05, + -0.0178 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + 0.5 + ], + "color": [ + 0.262745, + 0.282353, + 0.301961, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "jaw.stl", + "pos": [ + -0.0045, + 0.0, + -0.018 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + 0.5 + ], + "color": [ + 0.980392, + 0.713725, + 0.00392157, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "bottom_head_shell.stl", + "pos": [ + -0.0043, + -0.0, + -0.018 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + 0.5 + ], + "color": [ + 0.980392, + 0.713725, + 0.00392157, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "m12_lens_holder.stl", + "pos": [ + 0.0155, + -9.13778e-05, + -0.05558 + ], + "quat": [ + 0.5, + -0.5, + 0.5, + -0.5 + ], + "color": [ + 0.262745, + 0.282353, + 0.301961, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "pcb__raspberry_pi_zero_2_w.stl", + "pos": [ + 0.015435, + -1.13778e-05, + -0.06001 + ], + "quat": [ + 0.5, + -0.5, + -0.5, + 0.5 + ], + "color": [ + 0.498039, + 0.498039, + 0.498039, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "speaker.stl", + "pos": [ + 0.01, + -0.016786, + 0.0260226 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + 0.5 + ], + "color": [ + 0.294118, + 0.290196, + 0.290196, + 1.0 + ] + } + ], + "joint": { + "name": "head_roll", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -0.43633231299859127, + 0.4363323129985735 + ], + "actuator_index": 8 + } + }, + { + "name": "bearing_roll", + "parent": "trunk_base", + "pos": [ + 0.006, + -0.0175, + -0.005 + ], + "quat": [ + 0.0, + 0.707107, + 0.707107, + 0.0 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "yaw2roll.stl", + "pos": [ + -0.0175, + -0.002, + 0.0125 + ], + "quat": [ + 0.0, + 1.0, + 0.0, + 0.0 + ], + "color": [ + 0.647059, + 0.647059, + 0.647059, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + -0.0, + 0.0, + 0.0125 + ], + "quat": [ + 0.0, + -0.707107, + 0.707107, + -0.0 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "bearing_roll.stl", + "pos": [ + -0.0175, + -0.002, + 0.0125 + ], + "quat": [ + 0.0, + -1.0, + -0.0, + -0.0 + ], + "color": [ + 0.262745, + 0.282353, + 0.301961, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + -0.0, + 0.0165, + 0.0125 + ], + "quat": [ + 0.5, + 0.5, + 0.5, + -0.5 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + } + ], + "joint": { + "name": "right_hip_yaw", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -0.5235987755982988, + 0.4363323129985824 + ], + "actuator_index": 9 + } + }, + { + "name": "hip_l_2", + "parent": "bearing_roll", + "pos": [ + -0.0, + 0.0165, + 0.0125 + ], + "quat": [ + 0.0, + -0.0, + 0.707107, + 0.707107 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "hip_l.stl", + "pos": [ + -0.0175, + 0.0, + -0.0185 + ], + "quat": [ + 0.707107, + -0.707107, + -0.0, + -0.0 + ], + "color": [ + 0.901961, + 0.901961, + 0.901961, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + 0.025, + 0.0, + -0.0185 + ], + "quat": [ + 0.5, + 0.5, + -0.5, + -0.5 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + } + ], + "joint": { + "name": "right_hip_roll", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -0.3839724354387525, + 0.3839724354387525 + ], + "actuator_index": 10 + } + }, + { + "name": "upper_leg_right", + "parent": "hip_l_2", + "pos": [ + 0.025, + 0.0, + -0.0185 + ], + "quat": [ + 0.5, + -0.5, + 0.5, + -0.5 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + -0.022, + 0.0357771, + 0.0125 + ], + "quat": [ + 0.707107, + -0.0, + -0.707107, + -0.0 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration__22x16x4.stl", + "pos": [ + -0.022, + 0.0357771, + 0.0 + ], + "quat": [ + 0.0, + 1.0, + -0.0, + -0.0 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "upper_leg_rigidity_plate.stl", + "pos": [ + 0.0, + 0.0, + 0.0435 + ], + "quat": [ + 0.5, + -0.5, + 0.5, + 0.5 + ], + "color": [ + 0.537255, + 0.854902, + 0.827451, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "upper_leg_right.stl", + "pos": [ + -0.0, + 0.0, + -0.0425 + ], + "quat": [ + 0.5, + -0.5, + 0.5, + 0.5 + ], + "color": [ + 0.866667, + 0.866667, + 0.866667, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + 0.0, + 0.0, + 0.0125 + ], + "quat": [ + 0.5, + 0.5, + -0.5, + 0.5 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + } + ], + "joint": { + "name": "right_hip_pitch", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -1.5707963267949268, + 1.5707963267948664 + ], + "actuator_index": 11 + } + }, + { + "name": "leg_2", + "parent": "upper_leg_right", + "pos": [ + -0.022, + 0.0357771, + -0.004 + ], + "quat": [ + 0.0, + 1.0, + -0.0, + -0.0 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "xl330.stl", + "pos": [ + -0.042, + 0.0, + -0.0115 + ], + "quat": [ + 0.707107, + -0.0, + -0.707107, + -0.0 + ], + "color": [ + 0.286275, + 0.286275, + 0.286275, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "leg.stl", + "pos": [ + -0.0357771, + -0.022, + 0.0385 + ], + "quat": [ + 0.707107, + 0.0, + 0.707107, + -0.0 + ], + "color": [ + 0.811765, + 0.858824, + 0.898039, + 1.0 + ] + } + ], + "joint": { + "name": "right_knee", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -1.570796326794932, + 1.570796326794861 + ], + "actuator_index": 12 + } + }, + { + "name": "ankle_right", + "parent": "leg_2", + "pos": [ + -0.042, + 0.0, + -0.026 + ], + "quat": [ + 0.0, + -0.707107, + -0.707107, + 0.0 + ], + "geoms": [ + { + "type": "mesh", + "mesh": "ankle_right.stl", + "pos": [ + 0.022, + 0.00622291, + -0.0647 + ], + "quat": [ + 0.5, + -0.5, + 0.5, + 0.5 + ], + "color": [ + 0.980392, + 0.713725, + 0.00392157, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "sole_right.stl", + "pos": [ + 0.022, + 0.00622291, + -0.0647 + ], + "quat": [ + 0.5, + -0.5, + 0.5, + 0.5 + ], + "color": [ + 0.537255, + 0.854902, + 0.827451, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "seeed_bearing__configuration_default.stl", + "pos": [ + -0.0, + 0.0, + -0.0292 + ], + "quat": [ + 0.0, + 1.0, + -0.0, + -0.0 + ], + "color": [ + 0.713725, + 0.760784, + 0.8, + 1.0 + ] + }, + { + "type": "mesh", + "mesh": "foot_right.stl", + "pos": [ + 0.022, + 0.00622291, + -0.0647 + ], + "quat": [ + 0.5, + -0.5, + 0.5, + 0.5 + ], + "color": [ + 0.980392, + 0.713725, + 0.00392157, + 1.0 + ] + } + ], + "joint": { + "name": "right_ankle", + "axis": [ + 0.0, + 0.0, + 1.0 + ], + "type": "hinge", + "pos": [ + 0.0, + 0.0, + 0.0 + ], + "range": [ + -1.5707963267949054, + 1.5707963267948877 + ], + "actuator_index": 13 + } + } + ], + "actuated_joints": [ + "left_hip_yaw", + "left_hip_roll", + "left_hip_pitch", + "left_knee", + "left_ankle", + "neck_pitch", + "head_pitch", + "head_yaw", + "head_roll", + "right_hip_yaw", + "right_hip_roll", + "right_hip_pitch", + "right_knee", + "right_ankle" + ], + "mesh_dir": "/robot/mjlab/meshes" +} \ No newline at end of file diff --git a/robot/mjlab/meshes/ankle_left.stl b/robot/mjlab/meshes/ankle_left.stl new file mode 100644 index 0000000..4fc5629 --- /dev/null +++ b/robot/mjlab/meshes/ankle_left.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d8ad18f2d8c5c44542f1183c37f634e4fc526b13155bcc3aa7f64dcf6fe1014b +size 62284 diff --git a/robot/mjlab/meshes/ankle_right.stl b/robot/mjlab/meshes/ankle_right.stl new file mode 100644 index 0000000..b1ef96a --- /dev/null +++ b/robot/mjlab/meshes/ankle_right.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0710cfd347cedfc3895a32cd0b8f75179e511ffec838748289f396941a6344d8 +size 62284 diff --git a/robot/mjlab/meshes/banana_pcb_locker.stl b/robot/mjlab/meshes/banana_pcb_locker.stl new file mode 100644 index 0000000..6aeb84e --- /dev/null +++ b/robot/mjlab/meshes/banana_pcb_locker.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6ac5914456188fa1c181d8a06ac5b21274fdd3ed2409194552b2c87cd8afb165 +size 17384 diff --git a/robot/mjlab/meshes/bearing_roll.stl b/robot/mjlab/meshes/bearing_roll.stl new file mode 100644 index 0000000..86cb4b8 --- /dev/null +++ b/robot/mjlab/meshes/bearing_roll.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fa0da0a5eef428a41da6e9639f75120781594e600c5db7518621750221669849 +size 30284 diff --git a/robot/mjlab/meshes/bottom_head_shell.stl b/robot/mjlab/meshes/bottom_head_shell.stl new file mode 100644 index 0000000..f3007f5 --- /dev/null +++ b/robot/mjlab/meshes/bottom_head_shell.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3de5a0c729a3a8efaefda01f92c946f7954081269192f160c5f36543f30da29e +size 262084 diff --git a/robot/mjlab/meshes/elec_rpi_robot_hat_pcb.stl b/robot/mjlab/meshes/elec_rpi_robot_hat_pcb.stl new file mode 100644 index 0000000..be83cbb --- /dev/null +++ b/robot/mjlab/meshes/elec_rpi_robot_hat_pcb.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6cdf66cc2fbc6e4f3956c551ad1d460ad3cdb463521d05b98942972788f5e12f +size 235784 diff --git a/robot/mjlab/meshes/face_part.stl b/robot/mjlab/meshes/face_part.stl new file mode 100644 index 0000000..ddab143 --- /dev/null +++ b/robot/mjlab/meshes/face_part.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c75714620738fae64dc664efeb64563053b8b28cef88a823f180b96600790c84 +size 142684 diff --git a/robot/mjlab/meshes/foot_left.stl b/robot/mjlab/meshes/foot_left.stl new file mode 100644 index 0000000..8e1dd35 --- /dev/null +++ b/robot/mjlab/meshes/foot_left.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ebfb1d26924658de3c18bb61b48be39799d4aebb9f82ee19c68c4d4ae690f398 +size 249034 diff --git a/robot/mjlab/meshes/foot_right.stl b/robot/mjlab/meshes/foot_right.stl new file mode 100644 index 0000000..e605c06 --- /dev/null +++ b/robot/mjlab/meshes/foot_right.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:486f3b692e8987df7a18fcbcac7101b27d250ab4de87b86474823787be28c1c8 +size 248984 diff --git a/robot/mjlab/meshes/hip_l.stl b/robot/mjlab/meshes/hip_l.stl new file mode 100644 index 0000000..8737eab --- /dev/null +++ b/robot/mjlab/meshes/hip_l.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9258f4de8a58898a717c69db7a928acd41458531c15bb2ea02bfa3cc3cc5c6a7 +size 261784 diff --git a/robot/mjlab/meshes/jaw.stl b/robot/mjlab/meshes/jaw.stl new file mode 100644 index 0000000..f200f5f --- /dev/null +++ b/robot/mjlab/meshes/jaw.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:19f150f6b040be2fa6e7532028c0d64d30c9fe014ecd5c0c3e306d9d274c0bae +size 123984 diff --git a/robot/mjlab/meshes/jaw_soft.stl b/robot/mjlab/meshes/jaw_soft.stl new file mode 100644 index 0000000..675b1ef --- /dev/null +++ b/robot/mjlab/meshes/jaw_soft.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6c3ebb578ea5e6d7b995baca9b42ef7c9f9858b56dedf6f0d949f5e6ea9d3ef2 +size 84084 diff --git a/robot/mjlab/meshes/left_shell.stl b/robot/mjlab/meshes/left_shell.stl new file mode 100644 index 0000000..75bdeac --- /dev/null +++ b/robot/mjlab/meshes/left_shell.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bbce72c8fbbb1afb08600a000b102a5885a1fa3a7406056dbd6ead5fecef31a5 +size 259034 diff --git a/robot/mjlab/meshes/leg.stl b/robot/mjlab/meshes/leg.stl new file mode 100644 index 0000000..0594b48 --- /dev/null +++ b/robot/mjlab/meshes/leg.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8e746228a2b0b664fd011677bc1bb1a87446f7b66b7f003dbf6a55a8f2c79de9 +size 184784 diff --git a/robot/mjlab/meshes/lens.stl b/robot/mjlab/meshes/lens.stl new file mode 100644 index 0000000..2707472 --- /dev/null +++ b/robot/mjlab/meshes/lens.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b9b86fce70198040781a739e2c15d2996b549b25d73b7544005ae08a415b486e +size 23484 diff --git a/robot/mjlab/meshes/m12_lens_holder.stl b/robot/mjlab/meshes/m12_lens_holder.stl new file mode 100644 index 0000000..540d5f3 --- /dev/null +++ b/robot/mjlab/meshes/m12_lens_holder.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8760c127946f6be365f107053eaba5763aeb4e13d7278836f2e99dfcd8191f0e +size 137484 diff --git a/robot/mjlab/meshes/motor_support.stl b/robot/mjlab/meshes/motor_support.stl new file mode 100644 index 0000000..d831cb7 --- /dev/null +++ b/robot/mjlab/meshes/motor_support.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d6725004f0a85a5cf0161214ea0d39fb6f14ee3563c9a96cd5aad725b5e2a716 +size 45284 diff --git a/robot/mjlab/meshes/neck.stl b/robot/mjlab/meshes/neck.stl new file mode 100644 index 0000000..3496f18 --- /dev/null +++ b/robot/mjlab/meshes/neck.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e6b27ab0a20b9765465d6d20f8549718a552e1fb3fc867d43320e2190aec44d +size 18384 diff --git a/robot/mjlab/meshes/neck_pitch.stl b/robot/mjlab/meshes/neck_pitch.stl new file mode 100644 index 0000000..5d035cc --- /dev/null +++ b/robot/mjlab/meshes/neck_pitch.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eb9086636a26d9c5a7ebc8b0e44bd3b458c9c89142e8acffcc431b46b70e9a5d +size 89084 diff --git a/robot/mjlab/meshes/noenoeil.stl b/robot/mjlab/meshes/noenoeil.stl new file mode 100644 index 0000000..0e1d294 --- /dev/null +++ b/robot/mjlab/meshes/noenoeil.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:55f3167627f6fbd1a19966f92396adf2ccd55795b13ded3b9f52705ba8a7ab77 +size 12384 diff --git a/robot/mjlab/meshes/np_f970.stl b/robot/mjlab/meshes/np_f970.stl new file mode 100644 index 0000000..7bb564e --- /dev/null +++ b/robot/mjlab/meshes/np_f970.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:78db51461cff3f125a905042b6117b6ee15c5648c77ecfde2f061aaab4dd89c6 +size 260134 diff --git a/robot/mjlab/meshes/pcb__raspberry_pi_zero_2_w.stl b/robot/mjlab/meshes/pcb__raspberry_pi_zero_2_w.stl new file mode 100644 index 0000000..d02f0bc --- /dev/null +++ b/robot/mjlab/meshes/pcb__raspberry_pi_zero_2_w.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:50a365f079c423e5e35cb7b6edbe1bfa6abdea89dd3e17146d579005f8e5a511 +size 262184 diff --git a/robot/mjlab/meshes/power_support.stl b/robot/mjlab/meshes/power_support.stl new file mode 100644 index 0000000..a61b944 --- /dev/null +++ b/robot/mjlab/meshes/power_support.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:39d23ce00b9182ec01ad0a96c629225e501eb090f3cdc7074209b18f80359b0f +size 160484 diff --git a/robot/mjlab/meshes/right_shell.stl b/robot/mjlab/meshes/right_shell.stl new file mode 100644 index 0000000..fd77bf7 --- /dev/null +++ b/robot/mjlab/meshes/right_shell.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c2131ef20ffaf39bf0e33558e66f792265c1f26d40c203110f1c8dadbacb31e2 +size 259984 diff --git a/robot/mjlab/meshes/seeed_bearing__configuration__22x16x4.stl b/robot/mjlab/meshes/seeed_bearing__configuration__22x16x4.stl new file mode 100644 index 0000000..e123c75 --- /dev/null +++ b/robot/mjlab/meshes/seeed_bearing__configuration__22x16x4.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8551dc99cbdc012bd261c2a175f2cc766dd6aed0786781b35f8381cd3aa95b34 +size 260134 diff --git a/robot/mjlab/meshes/seeed_bearing__configuration_default.stl b/robot/mjlab/meshes/seeed_bearing__configuration_default.stl new file mode 100644 index 0000000..524b730 --- /dev/null +++ b/robot/mjlab/meshes/seeed_bearing__configuration_default.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e4338e0e6c7adeb52a35e02d6fa5babe761e8752ffa0fc2e31b0ba672c18630d +size 260084 diff --git a/robot/mjlab/meshes/soft_mouth_top.stl b/robot/mjlab/meshes/soft_mouth_top.stl new file mode 100644 index 0000000..a3bd23f --- /dev/null +++ b/robot/mjlab/meshes/soft_mouth_top.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:451f53d30350552cb6e26f777698272f7c3fa557f389adb24f94c75a201e67cd +size 98384 diff --git a/robot/mjlab/meshes/sole_left.stl b/robot/mjlab/meshes/sole_left.stl new file mode 100644 index 0000000..87ece49 --- /dev/null +++ b/robot/mjlab/meshes/sole_left.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:357f80bb46855e3ce50f817705acd80471dae90b4ee6b4a0a948614a7daeddb1 +size 197384 diff --git a/robot/mjlab/meshes/sole_right.stl b/robot/mjlab/meshes/sole_right.stl new file mode 100644 index 0000000..c57db33 --- /dev/null +++ b/robot/mjlab/meshes/sole_right.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:07801107a36dbc1c2b83b3b64ad98be39ba662a8a7fa0b62a29119b201f41fe6 +size 198684 diff --git a/robot/mjlab/meshes/speaker.stl b/robot/mjlab/meshes/speaker.stl new file mode 100644 index 0000000..7baa6fd --- /dev/null +++ b/robot/mjlab/meshes/speaker.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0ef54df86c6093c166d73e33dc0b8364ef5643a7a1b1dacaba577be5a9c06c32 +size 684 diff --git a/robot/mjlab/meshes/top_head_shell.stl b/robot/mjlab/meshes/top_head_shell.stl new file mode 100644 index 0000000..6d1eee3 --- /dev/null +++ b/robot/mjlab/meshes/top_head_shell.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:32c7499f12f9871e5471dfc8dd117a277b1efd9450c882913d44a0462af50003 +size 201584 diff --git a/robot/mjlab/meshes/trunk_base.stl b/robot/mjlab/meshes/trunk_base.stl new file mode 100644 index 0000000..c482ae5 --- /dev/null +++ b/robot/mjlab/meshes/trunk_base.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bca03970d8251c7a489e7f52fa48c5dca3e59185533f269aa1bc786875b479a8 +size 46284 diff --git a/robot/mjlab/meshes/upper_leg_left.stl b/robot/mjlab/meshes/upper_leg_left.stl new file mode 100644 index 0000000..0fc5a2f --- /dev/null +++ b/robot/mjlab/meshes/upper_leg_left.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0cad4994905a15fa6af9e890542d146c67533ab8baf767ec7f171a980e512c75 +size 153184 diff --git a/robot/mjlab/meshes/upper_leg_right.stl b/robot/mjlab/meshes/upper_leg_right.stl new file mode 100644 index 0000000..82636bd --- /dev/null +++ b/robot/mjlab/meshes/upper_leg_right.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5906134bcde398008361f80897bc1b037bca8a08cf73840720aeab60fa185b9f +size 153184 diff --git a/robot/mjlab/meshes/upper_leg_rigidity_plate.stl b/robot/mjlab/meshes/upper_leg_rigidity_plate.stl new file mode 100644 index 0000000..116e0eb --- /dev/null +++ b/robot/mjlab/meshes/upper_leg_rigidity_plate.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cc926157ae915beb0d87d06000f42ffd5680f656c941d5fc586324fcd8284b52 +size 45084 diff --git a/robot/mjlab/meshes/xl330.stl b/robot/mjlab/meshes/xl330.stl new file mode 100644 index 0000000..62e8974 --- /dev/null +++ b/robot/mjlab/meshes/xl330.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:49255c2f0e2e009b08c4f56792da35ff9caab0b3c00e6349d63a0fe59ca240d8 +size 51584 diff --git a/robot/mjlab/meshes/yaw2roll.stl b/robot/mjlab/meshes/yaw2roll.stl new file mode 100644 index 0000000..983d10f --- /dev/null +++ b/robot/mjlab/meshes/yaw2roll.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ec47cd77c456631a5fddabfaec1c7f634806a9343ca1fa3a70cdcac5ecfa3baf +size 155084 diff --git a/robot/mjlab/meshes/yaw_roll_motion.stl b/robot/mjlab/meshes/yaw_roll_motion.stl new file mode 100644 index 0000000..4caef6a --- /dev/null +++ b/robot/mjlab/meshes/yaw_roll_motion.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:41570530b2590e24ba38015cee853d3b17dd3059d782183299a23dea56c2c31c +size 72684 diff --git a/robot/mjlab/robot_allcollisions.xml b/robot/mjlab/robot_allcollisions.xml new file mode 100644 index 0000000..7f296e3 --- /dev/null +++ b/robot/mjlab/robot_allcollisions.xml @@ -0,0 +1,434 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/style.css b/style.css deleted file mode 100644 index 114adf4..0000000 --- a/style.css +++ /dev/null @@ -1,28 +0,0 @@ -body { - padding: 2rem; - font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif; -} - -h1 { - font-size: 16px; - margin-top: 0; -} - -p { - color: rgb(107, 114, 128); - font-size: 15px; - margin-bottom: 10px; - margin-top: 5px; -} - -.card { - max-width: 620px; - margin: 0 auto; - padding: 16px; - border: 1px solid lightgray; - border-radius: 16px; -} - -.card p:last-child { - margin-bottom: 0; -} diff --git a/variants.js b/variants.js new file mode 100644 index 0000000..10dd9c1 --- /dev/null +++ b/variants.js @@ -0,0 +1,212 @@ +// Colour variants of the real Microduck robots (see the reference photo of +// the four printed units). Each variant fills the same semantic slots; the +// mesh-filename -> slot mapping lives in meshMaterialsFor(). All colours are +// linear-space RGB and slightly oversaturated: the renderer tone-maps with +// ACESFilmicToneMapping under warm key/rim lights, which desaturates and +// shifts hues toward gold, so the base colours lean past the target to land +// right on screen. + +import * as THREE from "three"; + +// ── Shared slot specs (identical on all four robots) ─────────────────── +// sRGB targets from the reference photo, converted to linear with a +// slight saturation boost (x1.12) so ACES lands on the clean tint. +const AMBER_YELLOW = { color: [1.0, 0.413, 0.007], roughness: 0.4, metalness: 0.0 }; // #ffb52e +const BRIGHT_ORANGE = { color: [1.0, 0.144, 0.008], roughness: 0.45, metalness: 0.0 }; // #ff7a2f +const AMBER = { color: [0.847, 0.339, 0.022], roughness: 0.45, metalness: 0.0 }; // #eda63e +const CLEAN_YELLOW = { color: [1.0, 0.608, 0.021], roughness: 0.4, metalness: 0.0 }; // #ffd23f +const CREAM = { color: [0.888, 0.86, 0.798], roughness: 0.35, metalness: 0.0 }; // #f2efe8 +const DARK = { color: [0.012, 0.012, 0.014], roughness: 0.55, metalness: 0.3 }; // #1d1d1f +const GRAY = { color: [0.256, 0.256, 0.279], roughness: 0.5, metalness: 0.35 }; // #8b8b90 +// Camera-lens eye: very dark blue-black, glossy like coated glass. +const LENS = { color: [0.01, 0.012, 0.02], roughness: 0.05, metalness: 0.0 }; + +// Per-variant colours. +const WARM_GRAY = { color: [0.328, 0.312, 0.283], roughness: 0.35, metalness: 0.0 }; // #9b9892 +const AMBER_ORANGE = { color: [0.815, 0.321, 0.019], roughness: 0.5, metalness: 0.0 }; // #e9a23b +const CHARCOAL = { color: [0.028, 0.028, 0.033], roughness: 0.5, metalness: 0.0 }; // #2f2f33 +const CHARCOAL_FACE = { color: [0.044, 0.044, 0.051], roughness: 0.5, metalness: 0.0 }; // #3c3c40 +const CHARCOAL_BODY = { color: [0.017, 0.017, 0.019], roughness: 0.5, metalness: 0.0 }; // #232326 +const LIGHT_WARM_GRAY = { color: [0.485, 0.459, 0.416], roughness: 0.35, metalness: 0.0 };// #b9b5ae +const PURPLE_RING = { color: [0.114, 0.058, 0.578], roughness: 0.4, metalness: 0.0 }; // #6a52c8 +const PURPLE_FEET = { color: [0.164, 0.085, 0.584], roughness: 0.45, metalness: 0.0 }; // #7a5fc9 +const PALE_BLUE = { color: [0.441, 0.613, 0.723], roughness: 0.35, metalness: 0.0 }; // #b6cfdd +const MEDIUM_BLUE = { color: [0.13, 0.321, 0.552], roughness: 0.35, metalness: 0.0 }; // #6f9ec4 +const BLUE_GRAY = { color: [0.183, 0.379, 0.565], roughness: 0.35, metalness: 0.0 }; // #7fa9c6 +const FEET_YELLOW = { color: [0.784, 0.455, 0.023], roughness: 0.5, metalness: 0.0 }; // #e5b93e + +// Slots (validated against the four-robot reference photo): headDome +// (top shell), facePlate (around the eye), trim (band under the shell + +// upper beak base), beakUpper (mouth-roof plate), beakLower (jaw plates), +// eyeRing, lens, bodyShell (trunk shells), legShells (upper-leg shells), +// feet (foot blocks + ankle brackets), soles, mechDark, mechGray. +export const VARIANTS = { + // Front-center robot: warm gray head, cream body, amber eye, orange + // trim/upper beak over a lighter amber lower beak, amber-orange feet. + classic: { + headDome: WARM_GRAY, + facePlate: WARM_GRAY, + trim: BRIGHT_ORANGE, + beakUpper: BRIGHT_ORANGE, + beakLower: AMBER, + eyeRing: AMBER_YELLOW, + lens: LENS, + bodyShell: CREAM, + legShells: CREAM, + feet: AMBER_ORANGE, + soles: AMBER_ORANGE, + mechDark: DARK, + mechGray: GRAY, + }, + // Front-left robot: charcoal shells, slightly lighter face plate, + // amber eye, orange/amber beak, yellow soles peeking under black feet. + charcoal: { + headDome: CHARCOAL, + facePlate: CHARCOAL_FACE, + trim: BRIGHT_ORANGE, + beakUpper: BRIGHT_ORANGE, + beakLower: AMBER, + eyeRing: AMBER_YELLOW, + lens: LENS, + bodyShell: CHARCOAL_BODY, + legShells: CHARCOAL_BODY, + feet: CHARCOAL_BODY, + soles: CLEAN_YELLOW, + mechDark: DARK, + mechGray: GRAY, + }, + // Back-center robot: light warm gray head, purple eye, YELLOW trim and + // beak (both plates), cream body, purple feet. + purple: { + headDome: LIGHT_WARM_GRAY, + facePlate: LIGHT_WARM_GRAY, + trim: CLEAN_YELLOW, + beakUpper: CLEAN_YELLOW, + beakLower: CLEAN_YELLOW, + eyeRing: PURPLE_RING, + lens: LENS, + bodyShell: CREAM, + legShells: CREAM, + feet: PURPLE_FEET, + soles: PURPLE_FEET, + mechDark: DARK, + mechGray: GRAY, + }, + // Right robot: pale blue dome over a medium blue face plate (two + // different blues), amber eye, orange/amber beak, blue-gray body, + // clean yellow feet. + blue: { + headDome: PALE_BLUE, + facePlate: MEDIUM_BLUE, + trim: BRIGHT_ORANGE, + beakUpper: BRIGHT_ORANGE, + beakLower: AMBER, + eyeRing: AMBER_YELLOW, + lens: LENS, + bodyShell: BLUE_GRAY, + legShells: BLUE_GRAY, + feet: FEET_YELLOW, + soles: FEET_YELLOW, + mechDark: DARK, + mechGray: GRAY, + }, +}; + +export const VARIANT_NAMES = Object.keys(VARIANTS); +export const DEFAULT_VARIANT = "classic"; + +export const randomVariantName = () => + VARIANT_NAMES[Math.floor(Math.random() * VARIANT_NAMES.length)]; + +// Mesh filename -> material spec for one variant (mjlab model meshes). +// Anything not listed is small mechanical hardware and falls back to +// mechGray. Verified visually against the four-robot reference photo. +export function meshMaterialsFor(v) { + return { + // Head: dome, band under it, face plate around the eye. + "top_head_shell.stl": v.headDome, + "bottom_head_shell.stl": v.trim, + "face_part.stl": v.facePlate, + // The eye: printed ring + camera lens behind it. + "noenoeil.stl": v.eyeRing, + "lens.stl": v.lens, + "m12_lens_holder.stl": v.mechDark, + // Beak: mouth-roof plate on top, rigid jaw + soft pad below. + "soft_mouth_top.stl": v.beakUpper, + "jaw.stl": v.beakLower, + "jaw_soft.stl": v.beakLower, + // Body shells + "trunk_base.stl": v.bodyShell, + "left_shell.stl": v.bodyShell, + "right_shell.stl": v.bodyShell, + "upper_leg_left.stl": v.legShells, + "upper_leg_right.stl": v.legShells, + "hip_l.stl": v.trim, + // Feet: foot block + ankle bracket, soft sole pad below. + "foot_left.stl": v.feet, + "foot_right.stl": v.feet, + "ankle_left.stl": v.feet, + "ankle_right.stl": v.feet, + "sole_left.stl": v.soles, + "sole_right.stl": v.soles, + // Dark mechanics + "xl330.stl": v.mechDark, + "neck.stl": v.mechDark, + "np_f970.stl": v.mechDark, + "pcb__raspberry_pi_zero_2_w.stl": v.mechDark, + "elec_rpi_robot_hat_pcb.stl": v.mechDark, + "banana_pcb_locker.stl": v.mechDark, + "speaker.stl": v.mechDark, + // Gray mechanics + "upper_leg_rigidity_plate.stl": v.mechGray, + "leg.stl": v.mechGray, + "yaw2roll.stl": v.mechGray, + "yaw_roll_motion.stl": v.mechGray, + "neck_pitch.stl": v.mechGray, + "bearing_roll.stl": v.mechGray, + "motor_support.stl": v.mechGray, + "power_support.stl": v.mechGray, + "seeed_bearing__configuration__22x16x4.stl": v.mechGray, + "seeed_bearing__configuration_default.stl": v.mechGray, + }; +} + +// buildRig materialForMesh hook for one variant. +export const materialHookFor = (v) => { + const map = meshMaterialsFor(v); + return (mesh) => map[mesh] ?? v.mechGray; +}; + +// ── Live re-skin ──────────────────────────────────────────────────────── +// Swap materials on an already-built rig without reloading any STL. +// Meshes are identified via userData.meshName (set by duck.js buildRig and +// preserved by cloneRig). Materials are cached per resolved spec so clones +// and repeated switches share GPU material instances. +const matCache = new Map(); +function matFor(spec) { + const key = `${spec.color.join(",")}|${spec.roughness ?? 0.5}|${spec.metalness ?? 0}`; + let m = matCache.get(key); + if (!m) { + m = new THREE.MeshStandardMaterial({ + color: new THREE.Color(...spec.color), + roughness: spec.roughness ?? 0.5, + metalness: spec.metalness ?? 0.0, + }); + matCache.set(key, m); + } + return m; +} + +export function applyVariant(rig, variant) { + const v = typeof variant === "string" ? VARIANTS[variant] : variant; + const map = meshMaterialsFor(v); + rig.root.traverse((o) => { + if (!o.isMesh || !o.userData.meshName) return; + o.material = matFor(map[o.userData.meshName] ?? v.mechGray); + }); +} + +// Linear-space spec colour -> sRGB CSS hex, for swatch UI elements. +export function specToHex(spec) { + return `#${new THREE.Color(...spec.color).getHexString()}`; +}