feat: relief as gentle slopes baked into the level itself
Replace the plateau boxes with a MuJoCo heightfield over the arena and vertex displacement of the grid floor shader, both driven by the same analytic cosine-bump function so physics and visuals are the same surface. Raising/sinking ramps one scalar (hfield z-size + uTopoScale uniform). Trigger still uncommitted: T key or rl.setRelief(). Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+35
-2
@@ -2,7 +2,9 @@
|
||||
// planes, ported verbatim from the pre-React rl.js. Both carry a uReveal
|
||||
// uniform for the entrance draw-in (driven by ceremony.js).
|
||||
import * as THREE from "three";
|
||||
import { ARENA_HALF, ARENA_WALL_H, ARENA_WALL_T, GRID_SECTION } from "./constants.js";
|
||||
import {
|
||||
ARENA_HALF, ARENA_WALL_H, ARENA_WALL_T, GRID_SECTION, RELIEF_BUMPS,
|
||||
} from "./constants.js";
|
||||
|
||||
// Infinite shader grid, ported from drei's <Grid>: anti-aliased world-space
|
||||
// lines at cell/section frequencies with a radial fade around the duck.
|
||||
@@ -24,11 +26,38 @@ export function makeInfiniteGrid() {
|
||||
// Starts at 0: the world stays hidden behind the welcome modal and
|
||||
// the BIOS readout until playBios cues startEntrance.
|
||||
uReveal: { value: 0.0 },
|
||||
// Relief: 0 = flat, 1 = bumps at full height. Driven by the game's
|
||||
// relief toggle, mirroring the MuJoCo heightfield's z-size so the
|
||||
// visual surface and the physics surface are the same function.
|
||||
uTopoScale: { value: 0.0 },
|
||||
// (cx, cz, height, radius) per bump, in three.js world coords
|
||||
// (MJCF y -> -z done here once).
|
||||
uBumps: {
|
||||
value: RELIEF_BUMPS.map(
|
||||
([cx, cy, h, r]) => new THREE.Vector4(cx, -cy, h, r),
|
||||
),
|
||||
},
|
||||
},
|
||||
vertexShader: /* glsl */ `
|
||||
#define NBUMPS ${RELIEF_BUMPS.length}
|
||||
varying vec3 vWorld;
|
||||
uniform float uTopoScale;
|
||||
uniform vec4 uBumps[NBUMPS];
|
||||
// Same analytic relief as the physics heightfield (game.js topoH):
|
||||
// a sum of cosine bumps, displacing the grid surface itself so the
|
||||
// floor genuinely deforms - lines flow up the slopes for free since
|
||||
// the fragment shader draws them from world xz.
|
||||
float topoH(vec2 p) {
|
||||
float H = 0.0;
|
||||
for (int i = 0; i < NBUMPS; i++) {
|
||||
float u = distance(p, uBumps[i].xy) / uBumps[i].w;
|
||||
if (u < 1.0) H += uBumps[i].z * (0.5 + 0.5 * cos(3.14159265 * u));
|
||||
}
|
||||
return H;
|
||||
}
|
||||
void main() {
|
||||
vec4 w = modelMatrix * vec4(position, 1.0);
|
||||
if (uTopoScale > 0.0) w.y += topoH(w.xz) * uTopoScale;
|
||||
vWorld = w.xyz;
|
||||
gl_Position = projectionMatrix * viewMatrix * w;
|
||||
}
|
||||
@@ -105,7 +134,11 @@ export function makeInfiniteGrid() {
|
||||
}
|
||||
`,
|
||||
});
|
||||
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(30, 30), material);
|
||||
// Subdivided so the relief displacement has vertices to push: ~12 cm
|
||||
// steps, plenty for the gentle bump radii (>= 0.5 m). The displacement
|
||||
// is computed in world space, so the per-frame recentering under the
|
||||
// camera target doesn't move the bumps.
|
||||
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(30, 30, 256, 256), material);
|
||||
mesh.rotation.x = -Math.PI / 2;
|
||||
return mesh;
|
||||
}
|
||||
|
||||
+15
-12
@@ -76,19 +76,22 @@ export const ARCADE_D = ARCADE_H * 0.5876; // footprint depth, ~0.247 m
|
||||
export const ARCADE_GAP = 0.02; // gap between neighbouring cabinets
|
||||
export const ARCADE_WALL_GAP = 0.01; // clearance between backs and the wall
|
||||
|
||||
// Relief terraces (prototype): grid cells that rise into plateaus when
|
||||
// toggled, adding topology to the arena. Each entry is [cx, cy, height]
|
||||
// in MJCF coords, aligned on GRID_SECTION cell centers. Two mirrored
|
||||
// two-step terraces on the side walls; the spawn cell, arena center and
|
||||
// the arcade row stay clear. Policies are blind (no terrain in the obs),
|
||||
// so raised cells act as obstacles, not walkable slopes.
|
||||
export const RELIEF_CELLS = [
|
||||
[0, -1.2, 0.06], [0.6, -1.2, 0.12],
|
||||
[0, 1.2, 0.06], [-0.6, 1.2, 0.12],
|
||||
// Relief (prototype): gentle cosine bumps baked into the level itself.
|
||||
// One shared analytic height function drives BOTH the physics (a MuJoCo
|
||||
// heightfield covering the arena) and the visuals (vertex displacement
|
||||
// of the grid floor shader), so the grid genuinely deforms instead of
|
||||
// boxes popping out of it. Entries are [cx, cy, height, radius] in MJCF
|
||||
// coords; slopes stay mild (max grade ~20%) so the blind walking policy
|
||||
// has a chance, and the spawn cell + arcade row stay flat.
|
||||
export const RELIEF_BUMPS = [
|
||||
[0.3, -0.9, 0.06, 0.55],
|
||||
[-0.9, 0.75, 0.05, 0.5],
|
||||
[0.6, 0.9, 0.07, 0.55],
|
||||
];
|
||||
export const RELIEF_LIP = 0.02; // extra box below the plateau top, m
|
||||
export const RELIEF_EPS = 0.005; // burial clearance under the floor plane
|
||||
export const RELIEF_SPEED = 0.25; // rise/sink rate, m/s
|
||||
export const RELIEF_HMAX = 0.07; // tallest bump, scales the hfield z-size
|
||||
export const RELIEF_GRID = 65; // hfield rows/cols over the 3x3 m arena
|
||||
export const RELIEF_SINK = 0.003; // hfield geom buried below the floor
|
||||
export const RELIEF_RATE = 0.6; // raise/sink rate, scale units per second
|
||||
|
||||
// Spawn: center of the middle section cell in the SECOND ROW FROM THE
|
||||
// BACK wall. The duck faces +X (identity freejoint quat, walks toward
|
||||
|
||||
+60
-80
@@ -30,9 +30,9 @@ import {
|
||||
ACTION_SCALE, TIMESTEP, DECIMATION, CTRL_DT,
|
||||
VEL_FWD, VEL_BACK, VEL_ANG, RVEL_FWD, RVEL_BACK, RVEL_ANG,
|
||||
CROUCH_PERIOD_S, CROUCH_END_PHASE,
|
||||
BALL_RADIUS, BALL_PARK_POS, ARENA_HALF, SPAWN_X, SPAWN_Y, GRID_SECTION,
|
||||
BALL_RADIUS, BALL_PARK_POS, ARENA_HALF, SPAWN_X, SPAWN_Y,
|
||||
ARCADE_H, ARCADE_W, ARCADE_D, ARCADE_GAP, ARCADE_WALL_GAP,
|
||||
RELIEF_CELLS, RELIEF_LIP, RELIEF_EPS, RELIEF_SPEED,
|
||||
RELIEF_BUMPS, RELIEF_HMAX, RELIEF_GRID, RELIEF_SINK, RELIEF_RATE,
|
||||
} from "./constants.js";
|
||||
import {
|
||||
buildRig, cloneRig, loadKinematics, setJoint, setJawOpen, MODEL_DIR, MESH_VERSION,
|
||||
@@ -205,47 +205,35 @@ async function boot({ scene, camera, renderer }) {
|
||||
// step, like the ball). q = 0 parks the box fully below the floor;
|
||||
// q = h + RELIEF_EPS puts its top exactly at h. Appended after the
|
||||
// ball so the keyframe layout stays robot + ball + relief.
|
||||
// The boxes overlap the floor plane by design (buried when lowered),
|
||||
// so their contacts with the world are excluded: otherwise the floor
|
||||
// pushes them out and fights the kinematic drive. Light mass + heavy
|
||||
// joint damping keep them effectively rigid under the duck's weight
|
||||
// between two qpos writes.
|
||||
const contact = el("contact", {});
|
||||
for (let i = 0; i < RELIEF_CELLS.length; i++) {
|
||||
const [cx, cy, h] = RELIEF_CELLS[i];
|
||||
const hz = (h + RELIEF_LIP) / 2;
|
||||
const body = el("body", { name: `relief_${i}`, pos: `${cx} ${cy} 0` });
|
||||
body.appendChild(el("joint", {
|
||||
name: `relief_${i}`, type: "slide", axis: "0 0 1", limited: "false",
|
||||
damping: "100",
|
||||
}));
|
||||
body.appendChild(el("geom", {
|
||||
name: `relief_${i}`, type: "box", mass: "1",
|
||||
size: `${GRID_SECTION / 2} ${GRID_SECTION / 2} ${hz}`,
|
||||
pos: `0 0 ${-RELIEF_EPS - hz}`,
|
||||
}));
|
||||
doc.querySelector("worldbody").appendChild(body);
|
||||
contact.appendChild(el("exclude", { body1: "world", body2: `relief_${i}` }));
|
||||
}
|
||||
root.appendChild(contact);
|
||||
// Relief heightfield: a static hfield over the whole arena, elevation
|
||||
// data filled at runtime from the shared analytic bump function (see
|
||||
// driveRelief). It compiles flat (no file/elevation = zeros) and has
|
||||
// no joints, so qpos and the keyframe are untouched. The geom sits
|
||||
// RELIEF_SINK below the floor so a near-zero z-size is fully buried;
|
||||
// raising the terrain = scaling model.hfield_size z at runtime.
|
||||
doc.querySelector("asset").appendChild(el("hfield", {
|
||||
name: "terrain", nrow: String(RELIEF_GRID), ncol: String(RELIEF_GRID),
|
||||
size: `${ARENA_HALF} ${ARENA_HALF} ${RELIEF_HMAX} 0.1`,
|
||||
}));
|
||||
doc.querySelector("worldbody").appendChild(el("geom", {
|
||||
name: "terrain", type: "hfield", hfield: "terrain",
|
||||
pos: `0 0 ${-RELIEF_SINK}`,
|
||||
}));
|
||||
// STAND keyframe from mjlab's scene_walk.xml. qpos must cover every
|
||||
// joint in document order: the 14 actuated hinges take DEFAULT_POSE by
|
||||
// name, anything else (the roller variant's passive wheels) starts at
|
||||
// zero. The ball's 7 free-joint values MUST be appended or nq won't
|
||||
// match; parked 50 m away = effectively absent. Relief slides come
|
||||
// last (they sit after the ball in document order - the selector
|
||||
// below must skip them) and start buried at 0.
|
||||
// match; parked 50 m away = effectively absent.
|
||||
const qposFree = `${SPAWN_X} ${SPAWN_Y} 0.12 1 0 0 0`;
|
||||
const poseByName = new Map(JOINT_NAMES.map((n, i) => [n, DEFAULT_POSE[i]]));
|
||||
const qposJoints = [...doc.querySelectorAll("body > joint")]
|
||||
.filter((j) => !(j.getAttribute("name") || "").startsWith("relief_"))
|
||||
.map((j) => poseByName.get(j.getAttribute("name")) ?? 0)
|
||||
.join(" ");
|
||||
const pose14 = Array.from(DEFAULT_POSE).join(" ");
|
||||
const kf = doc.createElement("keyframe");
|
||||
kf.appendChild(el("key", {
|
||||
name: "STAND",
|
||||
qpos: `${qposFree} ${qposJoints} ${BALL_PARK_POS} 1 0 0 0${" 0".repeat(RELIEF_CELLS.length)}`,
|
||||
qpos: `${qposFree} ${qposJoints} ${BALL_PARK_POS} 1 0 0 0`,
|
||||
ctrl: pose14,
|
||||
}));
|
||||
root.appendChild(kf);
|
||||
@@ -630,60 +618,51 @@ async function boot({ scene, camera, renderer }) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Relief terraces (prototype: toggleable grid topology) ────────────
|
||||
// Kinematic drive: every control step each cell's slide qpos glides
|
||||
// toward its target (raised or buried) at RELIEF_SPEED, qvel carrying
|
||||
// the true rate so contacts push the duck/ball along instead of
|
||||
// tunnelling. The state survives resets and loco switches: the keyframe
|
||||
// re-buries the cells and the drive raises them back if the toggle is
|
||||
// on. Trigger for now: window.rl.setRelief(bool) or the T key
|
||||
// (prototype - no UI commitment yet).
|
||||
// ── Relief (prototype: the level itself gains gentle slopes) ────────
|
||||
// One analytic height function (cosine bumps, RELIEF_BUMPS) drives both
|
||||
// surfaces: the MuJoCo heightfield gets it sampled into hfield_data
|
||||
// once per compiled model, and the grid floor shader displaces its
|
||||
// vertices with the same function (uTopoScale uniform). Raising or
|
||||
// sinking the terrain = ramping one scalar that scales the hfield
|
||||
// z-size and the shader uniform together, so physics and visuals stay
|
||||
// the same surface at every moment of the transition. Trigger for now:
|
||||
// window.rl.setRelief(bool) or the T key (prototype - no UI yet).
|
||||
let reliefOn = false;
|
||||
const reliefAddrCache = new WeakMap();
|
||||
function reliefAddrs(m) {
|
||||
let a = reliefAddrCache.get(m);
|
||||
if (!a) {
|
||||
a = RELIEF_CELLS.map((_, i) => ({
|
||||
q: m.jnt(`relief_${i}`).qposadr,
|
||||
d: m.jnt(`relief_${i}`).dofadr,
|
||||
}));
|
||||
reliefAddrCache.set(m, a);
|
||||
let reliefScale = 0;
|
||||
let reliefGridMat = null; // assigned at scene wiring (grid built below)
|
||||
const reliefFilled = new WeakSet();
|
||||
function topoH(x, y) {
|
||||
let H = 0;
|
||||
for (const [cx, cy, h, r] of RELIEF_BUMPS) {
|
||||
const u = Math.hypot(x - cx, y - cy) / r;
|
||||
if (u < 1) H += h * (0.5 + 0.5 * Math.cos(Math.PI * u));
|
||||
}
|
||||
return a;
|
||||
return H;
|
||||
}
|
||||
const reliefMeshes = RELIEF_CELLS.map(([cx, cy, h]) => {
|
||||
const boxH = h + RELIEF_LIP;
|
||||
const mesh = new THREE.Mesh(
|
||||
new THREE.BoxGeometry(GRID_SECTION, boxH, GRID_SECTION),
|
||||
new THREE.MeshStandardMaterial({ color: 0x0d0f16, roughness: 0.9 }),
|
||||
);
|
||||
// Orange edge lines: reads as a holographic tile popping out of the
|
||||
// grid, cheap and consistent with the arena DA.
|
||||
mesh.add(new THREE.LineSegments(
|
||||
new THREE.EdgesGeometry(mesh.geometry),
|
||||
new THREE.LineBasicMaterial({ color: 0xff7a2f }),
|
||||
));
|
||||
mesh.position.set(cx, -boxH, -cy); // buried until the drive raises it
|
||||
mesh.visible = false;
|
||||
scene.add(mesh);
|
||||
return mesh;
|
||||
});
|
||||
function driveRelief(dt) {
|
||||
const addrs = reliefAddrs(model);
|
||||
const qpos = data.qpos, qvel = data.qvel;
|
||||
for (let i = 0; i < RELIEF_CELLS.length; i++) {
|
||||
const h = RELIEF_CELLS[i][2];
|
||||
const hz = (h + RELIEF_LIP) / 2;
|
||||
const target = reliefOn ? h + RELIEF_EPS : 0;
|
||||
const prev = qpos[addrs[i].q];
|
||||
const dq = Math.max(-RELIEF_SPEED * dt, Math.min(RELIEF_SPEED * dt, target - prev));
|
||||
const q = prev + dq;
|
||||
qpos[addrs[i].q] = q;
|
||||
qvel[addrs[i].d] = dq / dt;
|
||||
// Mirror the visual box on the physics geom (three y = MJCF z).
|
||||
reliefMeshes[i].position.y = q - RELIEF_EPS - hz;
|
||||
reliefMeshes[i].visible = q > RELIEF_EPS + 1e-4; // top above the floor
|
||||
function fillHfield(m) {
|
||||
if (reliefFilled.has(m)) return;
|
||||
// Re-read the view on every fill: heap growth detaches TypedArrays.
|
||||
const n = RELIEF_GRID, hdata = m.hfield_data;
|
||||
for (let r = 0; r < n; r++) {
|
||||
const y = -ARENA_HALF + (2 * ARENA_HALF * r) / (n - 1);
|
||||
for (let c = 0; c < n; c++) {
|
||||
const x = -ARENA_HALF + (2 * ARENA_HALF * c) / (n - 1);
|
||||
hdata[r * n + c] = topoH(x, y) / RELIEF_HMAX;
|
||||
}
|
||||
}
|
||||
reliefFilled.add(m);
|
||||
}
|
||||
function driveRelief(dt) {
|
||||
fillHfield(model); // no-op once per compiled model (legs / rollers)
|
||||
const target = reliefOn ? 1 : 0;
|
||||
if (reliefScale !== target) {
|
||||
const d = Math.max(-RELIEF_RATE * dt, Math.min(RELIEF_RATE * dt, target - reliefScale));
|
||||
reliefScale += d;
|
||||
}
|
||||
// z-size scales every bump; the floor keeps it strictly positive and,
|
||||
// combined with the geom's RELIEF_SINK offset, fully buried when off.
|
||||
model.hfield_size[2] = Math.max(reliefScale * RELIEF_HMAX, 1e-4);
|
||||
if (reliefGridMat) reliefGridMat.uniforms.uTopoScale.value = reliefScale;
|
||||
}
|
||||
// Prototype trigger (no UI yet): T toggles the terrain.
|
||||
window.addEventListener("keydown", (e) => {
|
||||
@@ -715,6 +694,7 @@ async function boot({ scene, camera, renderer }) {
|
||||
// the R3F layer.
|
||||
const grid = makeInfiniteGrid();
|
||||
scene.add(grid);
|
||||
reliefGridMat = grid.material; // relief drive mirrors uTopoScale into it
|
||||
const { wallMats, wallMeshes } = makeArenaWalls();
|
||||
for (const m of wallMeshes) scene.add(m);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user