feat: release v0.5 with shared interactive arenas and textured RP1
Unify lighting and line-colour controls across robots, replace the football with a shared layout of draggable physical props, and import the supplied RP1 GLBs without changing trained physics or policies. Include reproducible visual import/provenance, regression coverage and verification records. Verified 66 unit tests, 55 browser tests and production-build checks.
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import * as THREE from "three";
|
||||
|
||||
// Same mass-scaled spring perturbation as Microduck, applied every physics
|
||||
// substep. Shared by generic robots for both their body and the arena toys.
|
||||
export class PhysicsDrag {
|
||||
constructor({
|
||||
sim,
|
||||
camera,
|
||||
controls,
|
||||
canvas,
|
||||
objects,
|
||||
addressFor,
|
||||
enabled,
|
||||
halfExtent,
|
||||
scale = 1,
|
||||
}) {
|
||||
Object.assign(this, {
|
||||
sim,
|
||||
camera,
|
||||
controls,
|
||||
canvas,
|
||||
objects,
|
||||
addressFor,
|
||||
enabled,
|
||||
halfExtent,
|
||||
scale,
|
||||
});
|
||||
this.ray = new THREE.Raycaster();
|
||||
this.ndc = new THREE.Vector2();
|
||||
this.plane = new THREE.Plane();
|
||||
this.point = new THREE.Vector3();
|
||||
this.direction = new THREE.Vector3();
|
||||
this.listeners = [];
|
||||
const listen = (type, fn, capture = false) => {
|
||||
window.addEventListener(type, fn, capture);
|
||||
this.listeners.push(() => window.removeEventListener(type, fn, capture));
|
||||
};
|
||||
listen(
|
||||
"pointerdown",
|
||||
(e) => {
|
||||
if (
|
||||
e.target !== canvas ||
|
||||
e.pointerType !== "mouse" ||
|
||||
e.button !== 0 ||
|
||||
!enabled()
|
||||
)
|
||||
return;
|
||||
this.setRay(e);
|
||||
const hit = this.pick();
|
||||
if (!hit) return;
|
||||
const address = addressFor(hit.object);
|
||||
if (!address) return;
|
||||
this.grab = { ...address, target: [0, 0, 0] };
|
||||
camera.getWorldDirection(this.direction);
|
||||
this.plane.setFromNormalAndCoplanarPoint(
|
||||
this.direction.negate(),
|
||||
hit.point,
|
||||
);
|
||||
this.updateTarget();
|
||||
controls.enabled = false;
|
||||
canvas.style.cursor = "grabbing";
|
||||
this.pointerId = e.pointerId;
|
||||
try {
|
||||
canvas.setPointerCapture(e.pointerId);
|
||||
} catch {
|
||||
/* unavailable */
|
||||
}
|
||||
},
|
||||
true,
|
||||
);
|
||||
listen("pointermove", (e) => {
|
||||
if (this.grab) {
|
||||
this.setRay(e);
|
||||
this.updateTarget();
|
||||
return;
|
||||
}
|
||||
if (
|
||||
e.target !== canvas ||
|
||||
e.pointerType !== "mouse" ||
|
||||
e.buttons ||
|
||||
!enabled()
|
||||
)
|
||||
return;
|
||||
if (performance.now() - (this.hoverAt ?? 0) < 80) return;
|
||||
this.hoverAt = performance.now();
|
||||
this.setRay(e);
|
||||
canvas.style.cursor = this.pick() ? "grab" : "";
|
||||
});
|
||||
for (const type of ["pointerup", "pointercancel", "blur"])
|
||||
listen(type, () => this.release());
|
||||
canvas.addEventListener(
|
||||
"lostpointercapture",
|
||||
(this.lostCapture = () => this.release()),
|
||||
);
|
||||
}
|
||||
setRay(e) {
|
||||
const rect = this.canvas.getBoundingClientRect();
|
||||
this.ndc.set(
|
||||
((e.clientX - rect.left) / rect.width) * 2 - 1,
|
||||
-((e.clientY - rect.top) / rect.height) * 2 + 1,
|
||||
);
|
||||
this.ray.setFromCamera(this.ndc, this.camera);
|
||||
}
|
||||
pick() {
|
||||
return this.ray.intersectObjects(this.objects(), true).find((hit) => {
|
||||
for (let o = hit.object; o; o = o.parent) if (!o.visible) return false;
|
||||
return !!this.addressFor(hit.object);
|
||||
});
|
||||
}
|
||||
updateTarget() {
|
||||
if (!this.ray.ray.intersectPlane(this.plane, this.point)) return;
|
||||
const lim = (this.halfExtent ?? 100) - 0.05 * this.scale;
|
||||
this.grab.target = [
|
||||
Math.max(-lim, Math.min(lim, this.point.x)),
|
||||
Math.max(-lim, Math.min(lim, -this.point.z)),
|
||||
Math.max(0.02 * this.scale, Math.min(0.45 * this.scale, this.point.y)),
|
||||
];
|
||||
}
|
||||
apply() {
|
||||
const g = this.grab;
|
||||
if (!g) return;
|
||||
const { data } = this.sim,
|
||||
force = [];
|
||||
for (let i = 0; i < 3; i++)
|
||||
force[i] =
|
||||
100 * (g.target[i] - data.qpos[g.qAdr + i]) -
|
||||
10 * data.qvel[g.dofAdr + i];
|
||||
const norm = Math.hypot(...force),
|
||||
clamp = Math.min(1, 200 / (norm || 1));
|
||||
for (let i = 0; i < 3; i++)
|
||||
data.xfrc_applied[g.bodyId * 6 + i] = g.mass * force[i] * clamp;
|
||||
}
|
||||
release() {
|
||||
if (this.grab)
|
||||
this.sim.data.xfrc_applied.fill(
|
||||
0,
|
||||
this.grab.bodyId * 6,
|
||||
this.grab.bodyId * 6 + 6,
|
||||
);
|
||||
this.grab = null;
|
||||
this.controls.enabled = true;
|
||||
this.canvas.style.cursor = "";
|
||||
const id = this.pointerId;
|
||||
this.pointerId = undefined;
|
||||
if (id !== undefined) {
|
||||
try {
|
||||
this.canvas.releasePointerCapture(id);
|
||||
} catch {
|
||||
/* already released */
|
||||
}
|
||||
}
|
||||
}
|
||||
dispose() {
|
||||
this.release();
|
||||
this.listeners.forEach((remove) => remove());
|
||||
this.canvas.removeEventListener("lostpointercapture", this.lostCapture);
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,7 @@ export class BodyVisuals {
|
||||
visuals.scenes.push(...gltf.scenes);
|
||||
const frame = new THREE.Group();
|
||||
frame.name = `body-visual:${spec.body}`;
|
||||
frame.userData.bodyId = ids[i];
|
||||
gltf.scene.position.fromArray(spec.position ?? [0, 0, 0]);
|
||||
gltf.scene.quaternion.fromArray(spec.quaternion ?? [0, 0, 0, 1]);
|
||||
frame.add(gltf.scene);
|
||||
|
||||
@@ -16,6 +16,8 @@ import { SkillManager } from "../skills/SkillManager.js";
|
||||
import { InputRouter } from "../input/InputRouter.js";
|
||||
import { RigRenderer } from "../render/RigRenderer.js";
|
||||
import { ChaseCamera } from "../render/ChaseCamera.js";
|
||||
import { PhysicsDrag } from "../input/PhysicsDrag.js";
|
||||
import { createInteractivePropVisuals } from "../../game/interactive-props.js";
|
||||
|
||||
export class RobotRuntime {
|
||||
static async load(options) {
|
||||
@@ -175,7 +177,14 @@ export class RobotRuntime {
|
||||
},
|
||||
});
|
||||
this.scene.add(this.rig.root);
|
||||
if (this.worldPresentation) this.scene.add(this.worldPresentation.root);
|
||||
this.lineColor = this.environmentSettings?.lineColor ?? "classic";
|
||||
if (this.worldPresentation) {
|
||||
this.scene.add(this.worldPresentation.root);
|
||||
this.worldPresentation.setLineColor(this.lineColor);
|
||||
}
|
||||
this.propsVisuals = createInteractivePropVisuals(this.sim.interactiveSpecs);
|
||||
this.scene.add(this.propsVisuals.root);
|
||||
this.syncProps();
|
||||
this.follow = new THREE.Vector3();
|
||||
}
|
||||
activate({ paused = false } = {}) {
|
||||
@@ -192,6 +201,35 @@ export class RobotRuntime {
|
||||
this.config.render.camera,
|
||||
);
|
||||
this.restoreCamera();
|
||||
this.drag = new PhysicsDrag({
|
||||
sim: this.sim,
|
||||
camera: this.camera,
|
||||
controls: this.controls,
|
||||
canvas: this.renderer.domElement,
|
||||
objects: () => [this.rig.root, this.propsVisuals.root],
|
||||
enabled: () => this.active && this.scheduler.running && !this.disposed,
|
||||
halfExtent: this.world.boundary?.half_extent,
|
||||
scale: this.world.scale ?? 1,
|
||||
addressFor: (object) => {
|
||||
const index = object.userData.interactiveIndex;
|
||||
if (Number.isInteger(index)) return this.sim.propAddresses[index];
|
||||
const id = object.userData.geomId;
|
||||
let visualBody = false;
|
||||
for (let node = object; node; node = node.parent)
|
||||
if (node.userData.bodyId > 0) visualBody = true;
|
||||
if (
|
||||
!visualBody &&
|
||||
(id === undefined || this.sim.model.geom_bodyid[id] === 0)
|
||||
)
|
||||
return null;
|
||||
return {
|
||||
bodyId: this.sim.root,
|
||||
qAdr: this.sim.rootQpos,
|
||||
dofAdr: this.sim.rootDof,
|
||||
mass: this.sim.model.body_subtreemass[this.sim.root],
|
||||
};
|
||||
},
|
||||
});
|
||||
this.input.init();
|
||||
if (!paused) this.resume();
|
||||
else this.onStatus({ paused: true });
|
||||
@@ -213,6 +251,11 @@ export class RobotRuntime {
|
||||
}
|
||||
status() {
|
||||
return {
|
||||
lineColor: this.lineColor,
|
||||
runtimeCapabilities: {
|
||||
environmentControls: !!this.worldPresentation,
|
||||
physicalDrag: true,
|
||||
},
|
||||
policyStatus: this.policyStatus(),
|
||||
actuationLabel:
|
||||
this.config.actuation.type === "joint_pd"
|
||||
@@ -220,6 +263,18 @@ export class RobotRuntime {
|
||||
: "",
|
||||
};
|
||||
}
|
||||
setLineColor(name) {
|
||||
if (!this.worldPresentation?.setLineColor(name)) return;
|
||||
this.lineColor = name;
|
||||
this.onStatus({ lineColor: name });
|
||||
}
|
||||
syncProps() {
|
||||
this.propsVisuals.sync(
|
||||
this.sim.data,
|
||||
this.sim.propAddresses,
|
||||
!this.sim.propsParked,
|
||||
);
|
||||
}
|
||||
policyStatus() {
|
||||
return this.policyState.episodes.size
|
||||
? `Skill: ${this.policyState.active} · ${this.policyState.state}`
|
||||
@@ -241,6 +296,7 @@ export class RobotRuntime {
|
||||
if (!paused) this.resume();
|
||||
}
|
||||
async pause() {
|
||||
this.drag?.release();
|
||||
this.input.clear();
|
||||
await this.scheduler.pause();
|
||||
this.onStatus({ paused: true });
|
||||
@@ -260,6 +316,7 @@ export class RobotRuntime {
|
||||
async #reset() {
|
||||
const running = this.scheduler.running;
|
||||
await this.scheduler.pause();
|
||||
this.drag?.release();
|
||||
this.input.clear();
|
||||
this.ctx.command.fill(0);
|
||||
for (const policy of this.policies.values()) policy.reset();
|
||||
@@ -278,6 +335,7 @@ export class RobotRuntime {
|
||||
this.onStatus({ fallen: false });
|
||||
this.publishPolicyStatus();
|
||||
this.rig.sync();
|
||||
this.syncProps();
|
||||
if (running) this.scheduler.start();
|
||||
}
|
||||
triggerAction(action) {
|
||||
@@ -313,8 +371,10 @@ export class RobotRuntime {
|
||||
}
|
||||
for (let i = 0; i < this.config.sim.decimation; i++) {
|
||||
this.actuation.apply();
|
||||
this.drag?.apply();
|
||||
this.sim.step();
|
||||
}
|
||||
this.sim.restoreEscapedProps(this.world.boundary?.half_extent);
|
||||
const q = this.sim.data.qpos,
|
||||
a = this.sim.rootQpos,
|
||||
quat = this.sim.data.xquat,
|
||||
@@ -356,6 +416,7 @@ export class RobotRuntime {
|
||||
frame() {
|
||||
if (this.disposed) return;
|
||||
this.rig.sync();
|
||||
this.syncProps();
|
||||
if (this.chaseCamera) {
|
||||
const yaw = this.cameraPose();
|
||||
this.worldPresentation?.update(this.follow);
|
||||
@@ -395,6 +456,17 @@ export class RobotRuntime {
|
||||
},
|
||||
scene: this.scene,
|
||||
world: this.world,
|
||||
get interactiveProps() {
|
||||
return self.sim.interactiveSpecs.map((spec, i) => ({
|
||||
...spec,
|
||||
...self.sim.propAddresses[i],
|
||||
}));
|
||||
},
|
||||
setPropsParked: (parked) => {
|
||||
this.drag?.release();
|
||||
this.sim.setPropsParked(parked);
|
||||
this.syncProps();
|
||||
},
|
||||
command: this.input.command,
|
||||
get lastAction() {
|
||||
return self.policies.get(self.activePolicy).ctx.lastAction;
|
||||
@@ -444,6 +516,8 @@ export class RobotRuntime {
|
||||
this.active = false;
|
||||
await this.scheduler.dispose();
|
||||
this.input.dispose();
|
||||
this.drag?.dispose();
|
||||
this.propsVisuals.dispose();
|
||||
this.chaseCamera?.dispose();
|
||||
this.controls?.dispose();
|
||||
this.worldPresentation?.dispose();
|
||||
|
||||
@@ -82,6 +82,9 @@ export class RuntimeManager {
|
||||
camera,
|
||||
renderer: this.renderer,
|
||||
autoStart: false,
|
||||
environmentSettings: {
|
||||
lineColor: this.getState().lineColor ?? "classic",
|
||||
},
|
||||
publish,
|
||||
onStatus: publish,
|
||||
onTelemetry: (telemetry) => publish({ telemetry }),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { arenaWalls } from "../worlds/arena.js";
|
||||
export function composeScene(xml, config, world) {
|
||||
import { appendInteractivePhysics } from "../../game/worlds/arena/session-environment.js";
|
||||
export function composeScene(xml, config, world, interactiveProps = []) {
|
||||
const doc = new DOMParser().parseFromString(xml, "text/xml");
|
||||
if (
|
||||
doc.querySelector("parsererror") ||
|
||||
@@ -9,6 +10,7 @@ export function composeScene(xml, config, world) {
|
||||
const option =
|
||||
doc.querySelector("mujoco > option") ?? doc.createElement("option");
|
||||
option.setAttribute("timestep", String(config.sim.timestep));
|
||||
if (interactiveProps.length) option.setAttribute("jacobian", "dense");
|
||||
if (!option.parentNode) doc.documentElement.append(option);
|
||||
if (world.type !== "flat")
|
||||
throw new Error(`World ${world.id}: generic composer requires flat World`);
|
||||
@@ -44,5 +46,6 @@ export function composeScene(xml, config, world) {
|
||||
worldbody.append(geom);
|
||||
}
|
||||
}
|
||||
appendInteractivePhysics(doc, interactiveProps);
|
||||
return new XMLSerializer().serializeToString(doc);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { fetchChecked } from "../loader/PackageLoader.js";
|
||||
import { composeScene } from "./SceneComposer.js";
|
||||
import { sharedInteractiveProps } from "../../game/worlds/arena/session-environment.js";
|
||||
import {
|
||||
resolvePropAddresses,
|
||||
resetProp,
|
||||
} from "../../game/interactive-props.js";
|
||||
export function resolveAddresses(mujoco, model, names) {
|
||||
const joints = new Map();
|
||||
for (const name of names) {
|
||||
@@ -42,14 +47,23 @@ export class SimCore {
|
||||
pkg.config.assets.forEach((a, i) =>
|
||||
vfs.addBuffer(a.vfs, new Uint8Array(buffers[i])),
|
||||
);
|
||||
const interactiveSpecs = sharedInteractiveProps(world);
|
||||
const xml = composeScene(
|
||||
await fetchChecked(pkg.asset(pkg.config.model)),
|
||||
pkg.config,
|
||||
world,
|
||||
interactiveSpecs,
|
||||
);
|
||||
model = mujoco.MjModel.from_xml_string(xml, vfs);
|
||||
data = new mujoco.MjData(model);
|
||||
return new SimCore(mujoco, model, data, vfs, pkg.config);
|
||||
return new SimCore(
|
||||
mujoco,
|
||||
model,
|
||||
data,
|
||||
vfs,
|
||||
pkg.config,
|
||||
interactiveSpecs,
|
||||
);
|
||||
} catch (error) {
|
||||
data?.delete();
|
||||
model?.delete();
|
||||
@@ -57,8 +71,10 @@ export class SimCore {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
constructor(mujoco, model, data, vfs, config) {
|
||||
Object.assign(this, { mujoco, model, data, vfs, config });
|
||||
constructor(mujoco, model, data, vfs, config, interactiveSpecs = []) {
|
||||
Object.assign(this, { mujoco, model, data, vfs, config, interactiveSpecs });
|
||||
this.propAddresses = resolvePropAddresses(mujoco, model, interactiveSpecs);
|
||||
this.propsParked = false;
|
||||
const names = [...new Set(Object.values(config.joints.groups).flat())];
|
||||
this.joints = resolveAddresses(mujoco, model, names);
|
||||
for (const [name, j] of this.joints) {
|
||||
@@ -119,8 +135,36 @@ export class SimCore {
|
||||
for (const [name, joint] of this.joints)
|
||||
data.qpos[joint.qpos] =
|
||||
config.base.initial?.[name] ?? defaults.get(name) ?? 0;
|
||||
this.resetProps();
|
||||
mujoco.mj_forward(model, data);
|
||||
}
|
||||
resetProps() {
|
||||
this.interactiveSpecs.forEach((spec, i) =>
|
||||
resetProp(this.data, this.propAddresses[i], spec, this.propsParked, i),
|
||||
);
|
||||
}
|
||||
setPropsParked(parked) {
|
||||
this.propsParked = parked;
|
||||
this.resetProps();
|
||||
this.mujoco.mj_forward(this.model, this.data);
|
||||
}
|
||||
restoreEscapedProps(halfExtent) {
|
||||
if (this.propsParked || !halfExtent) return;
|
||||
let restored = false;
|
||||
this.interactiveSpecs.forEach((spec, i) => {
|
||||
const a = this.propAddresses[i].qAdr,
|
||||
q = this.data.qpos;
|
||||
if (
|
||||
Math.abs(q[a]) > halfExtent + 0.1 ||
|
||||
Math.abs(q[a + 1]) > halfExtent + 0.1 ||
|
||||
q[a + 2] < -0.2
|
||||
) {
|
||||
resetProp(this.data, this.propAddresses[i], spec);
|
||||
restored = true;
|
||||
}
|
||||
});
|
||||
if (restored) this.mujoco.mj_forward(this.model, this.data);
|
||||
}
|
||||
step() {
|
||||
this.mujoco.mj_step(this.model, this.data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user