146 lines
4.8 KiB
JavaScript
146 lines
4.8 KiB
JavaScript
import * as THREE from "three";
|
|
import { applyWorldGrid } from "./WorldRenderer.js";
|
|
import { BodyVisuals } from "./BodyVisuals.js";
|
|
// Geometry is compiled MuJoCo geometry, not raw STL: compiler-applied mesh
|
|
// scale/centering is already represented by vertices + geom transforms.
|
|
export class RigRenderer {
|
|
constructor(model, data, { groups = [2], world } = {}) {
|
|
this.model = model;
|
|
this.data = data;
|
|
this.root = new THREE.Group();
|
|
this.root.rotation.x = -Math.PI / 2;
|
|
this.geometries = new Map();
|
|
this.materials = [];
|
|
this.items = [];
|
|
this.matrix = new THREE.Matrix4();
|
|
try {
|
|
for (let id = 0; id < model.ngeom; id++) {
|
|
if (!groups.includes(model.geom_group[id]) && model.geom_type[id] !== 0)
|
|
continue;
|
|
const type = model.geom_type[id],
|
|
size = Array.from(model.geom_size.slice(id * 3, id * 3 + 3));
|
|
let geometry,
|
|
key = type === 7 ? `mesh:${model.geom_dataid[id]}` : `geom:${id}`;
|
|
geometry = this.geometries.get(key);
|
|
if (!geometry) {
|
|
if (type === 7) {
|
|
const mesh = model.geom_dataid[id],
|
|
v0 = model.mesh_vertadr[mesh] * 3,
|
|
vn = model.mesh_vertnum[mesh] * 3;
|
|
const f0 = model.mesh_faceadr[mesh] * 3,
|
|
fn = model.mesh_facenum[mesh] * 3;
|
|
geometry = new THREE.BufferGeometry();
|
|
geometry.setAttribute(
|
|
"position",
|
|
new THREE.BufferAttribute(
|
|
new Float32Array(model.mesh_vert.slice(v0, v0 + vn)),
|
|
3,
|
|
),
|
|
);
|
|
geometry.setIndex(
|
|
new THREE.BufferAttribute(
|
|
new Uint32Array(model.mesh_face.slice(f0, f0 + fn)),
|
|
1,
|
|
),
|
|
);
|
|
geometry.computeVertexNormals();
|
|
} else if (type === 0) geometry = new THREE.PlaneGeometry(200, 200);
|
|
else if (type === 2)
|
|
geometry = new THREE.SphereGeometry(size[0], 20, 12);
|
|
else if (type === 6)
|
|
geometry = new THREE.BoxGeometry(...size.map((v) => v * 2));
|
|
else if (type === 4) {
|
|
geometry = new THREE.SphereGeometry(1, 20, 12);
|
|
geometry.scale(...size);
|
|
} else if (type === 5) {
|
|
geometry = new THREE.CylinderGeometry(
|
|
size[0],
|
|
size[0],
|
|
size[1] * 2,
|
|
20,
|
|
);
|
|
geometry.rotateX(Math.PI / 2);
|
|
} else if (type === 3) {
|
|
geometry = new THREE.CapsuleGeometry(size[0], size[1] * 2, 6, 12);
|
|
geometry.rotateX(Math.PI / 2);
|
|
} else throw new Error(`RigRenderer: unsupported geom type ${type}`);
|
|
this.geometries.set(key, geometry);
|
|
}
|
|
const mat = model.geom_matid[id],
|
|
rgba =
|
|
mat >= 0
|
|
? model.mat_rgba.slice(mat * 4, mat * 4 + 4)
|
|
: model.geom_rgba.slice(id * 4, id * 4 + 4);
|
|
const material = new THREE.MeshStandardMaterial({
|
|
color: new THREE.Color(rgba[0], rgba[1], rgba[2]),
|
|
opacity: rgba[3],
|
|
transparent: rgba[3] < 1,
|
|
roughness: 0.65,
|
|
metalness: 0.05,
|
|
side: type === 0 ? THREE.DoubleSide : THREE.FrontSide,
|
|
});
|
|
if (type === 0) applyWorldGrid(material, world?.floor?.grid);
|
|
this.materials.push(material);
|
|
const mesh = new THREE.Mesh(geometry, material);
|
|
mesh.userData.geomId = id;
|
|
this.root.add(mesh);
|
|
this.items.push({ id, mesh });
|
|
}
|
|
this.sync();
|
|
} catch (error) {
|
|
this.dispose();
|
|
throw error;
|
|
}
|
|
}
|
|
async loadBodyVisuals(mujoco, pkg) {
|
|
if (!pkg.config.render.body_visuals?.length) return;
|
|
this.bodyVisuals = await BodyVisuals.load(
|
|
mujoco,
|
|
this.model,
|
|
this.data,
|
|
pkg,
|
|
);
|
|
const replaced = new Set(this.bodyVisuals.items.map(({ id }) => id));
|
|
for (const { id, mesh } of this.items)
|
|
if (replaced.has(this.model.geom_bodyid[id])) mesh.visible = false;
|
|
this.root.add(this.bodyVisuals.root);
|
|
}
|
|
sync() {
|
|
this.bodyVisuals?.sync();
|
|
const p = this.data.geom_xpos,
|
|
r = this.data.geom_xmat,
|
|
m = this.matrix;
|
|
for (const { id, mesh } of this.items) {
|
|
mesh.position.set(p[id * 3], p[id * 3 + 1], p[id * 3 + 2]);
|
|
const a = id * 9;
|
|
m.set(
|
|
r[a],
|
|
r[a + 1],
|
|
r[a + 2],
|
|
0,
|
|
r[a + 3],
|
|
r[a + 4],
|
|
r[a + 5],
|
|
0,
|
|
r[a + 6],
|
|
r[a + 7],
|
|
r[a + 8],
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
1,
|
|
);
|
|
mesh.quaternion.setFromRotationMatrix(m);
|
|
}
|
|
}
|
|
dispose() {
|
|
this.bodyVisuals?.dispose();
|
|
for (const g of this.geometries.values()) g.dispose();
|
|
for (const m of this.materials) m.dispose();
|
|
this.geometries.clear();
|
|
this.materials = [];
|
|
this.root.removeFromParent();
|
|
}
|
|
}
|