} */
+ let meshes = {};
+ /** @type {THREE.Light[]} */
+ let lights = [];
+
+ // Default material definition.
+ let material = new THREE.MeshPhysicalMaterial();
+ material.color = new THREE.Color(1, 1, 1);
+
+ // Loop through the MuJoCo geoms and recreate them in three.js.
+ for (let g = 0; g < model.ngeom; g++) {
+ // Only visualize geom groups up to 2 (same default behavior as simulate).
+ if (!(model.geom_group[g] < 3)) { continue; }
+
+ // Get the body ID and type of the geom.
+ let b = model.geom_bodyid[g];
+ let type = model.geom_type [g];
+ let size = [
+ model.geom_size[(g*3) + 0],
+ model.geom_size[(g*3) + 1],
+ model.geom_size[(g*3) + 2]
+ ];
+
+ // Create the body if it doesn't exist.
+ if (!(b in bodies)) {
+ bodies[b] = new THREE.Group();
+
+ let start_idx = model.name_bodyadr[b];
+ let end_idx = start_idx;
+ while (end_idx < names_array.length && names_array[end_idx] !== 0) {
+ end_idx++;
+ }
+ let name_buffer = names_array.subarray(start_idx, end_idx);
+ bodies[b].name = textDecoder.decode(name_buffer);
+
+ bodies[b].bodyID = b;
+ bodies[b].has_custom_mesh = false;
+ }
+
+ // Set the default geometry. In MuJoCo, this is a sphere.
+ let geometry = new THREE.SphereGeometry(size[0] * 0.5);
+ if (type == mujoco.mjtGeom.mjGEOM_PLANE.value) {
+ // Special handling for plane later.
+ } else if (type == mujoco.mjtGeom.mjGEOM_HFIELD.value) {
+ // TODO: Implement this.
+ } else if (type == mujoco.mjtGeom.mjGEOM_SPHERE.value) {
+ geometry = new THREE.SphereGeometry(size[0]);
+ } else if (type == mujoco.mjtGeom.mjGEOM_CAPSULE.value) {
+ geometry = new THREE.CapsuleGeometry(size[0], size[1] * 2.0, 20, 20);
+ } else if (type == mujoco.mjtGeom.mjGEOM_ELLIPSOID.value) {
+ geometry = new THREE.SphereGeometry(1); // Stretch this below
+ } else if (type == mujoco.mjtGeom.mjGEOM_CYLINDER.value) {
+ geometry = new THREE.CylinderGeometry(size[0], size[0], size[1] * 2.0);
+ } else if (type == mujoco.mjtGeom.mjGEOM_BOX.value) {
+ geometry = new THREE.BoxGeometry(size[0] * 2.0, size[2] * 2.0, size[1] * 2.0);
+ } else if (type == mujoco.mjtGeom.mjGEOM_MESH.value) {
+ let meshID = model.geom_dataid[g];
+
+ if (!(meshID in meshes)) {
+ geometry = new THREE.BufferGeometry();
+
+ let vertex_buffer = model.mesh_vert.subarray(
+ model.mesh_vertadr[meshID] * 3,
+ (model.mesh_vertadr[meshID] + model.mesh_vertnum[meshID]) * 3);
+ for (let v = 0; v < vertex_buffer.length; v+=3){
+ //vertex_buffer[v + 0] = vertex_buffer[v + 0];
+ let temp = vertex_buffer[v + 1];
+ vertex_buffer[v + 1] = vertex_buffer[v + 2];
+ vertex_buffer[v + 2] = -temp;
+ }
+
+ let normal_buffer = model.mesh_normal.subarray(
+ model.mesh_normaladr[meshID] * 3,
+ (model.mesh_normaladr[meshID] + model.mesh_normalnum[meshID]) * 3);
+ for (let v = 0; v < normal_buffer.length; v+=3){
+ //normal_buffer[v + 0] = normal_buffer[v + 0];
+ let temp = normal_buffer[v + 1];
+ normal_buffer[v + 1] = normal_buffer[v + 2];
+ normal_buffer[v + 2] = -temp;
+ }
+
+ let uv_buffer = model.mesh_texcoord.subarray(
+ model.mesh_texcoordadr[meshID] * 2,
+ (model.mesh_texcoordadr[meshID] + model.mesh_texcoordnum[meshID]) * 2);
+
+ let face_to_vertex_buffer = model.mesh_face.subarray(
+ model.mesh_faceadr[meshID] * 3,
+ (model.mesh_faceadr[meshID] + model.mesh_facenum[meshID]) * 3);
+ let face_to_uv_buffer = model.mesh_facetexcoord.subarray(
+ model.mesh_faceadr[meshID] * 3,
+ (model.mesh_faceadr[meshID] + model.mesh_facenum[meshID]) * 3);
+ let face_to_normal_buffer = model.mesh_facenormal.subarray(
+ model.mesh_faceadr[meshID] * 3,
+ (model.mesh_faceadr[meshID] + model.mesh_facenum[meshID]) * 3);
+
+ // The UV and Normal Buffers are actually indexed by the triangle indices through the face_to_uv_buffer and face_to_normal_buffer.
+ // We need to swizzle them into a per-vertex format for three.js
+ let swizzled_uv_buffer = new Float32Array((vertex_buffer.length / 3) * 2);
+ let swizzled_normal_buffer = new Float32Array(vertex_buffer.length);
+ for (let t = 0; t < face_to_vertex_buffer.length / 3; t++) {
+ let vi0 = face_to_vertex_buffer[(t * 3) + 0];
+ let vi1 = face_to_vertex_buffer[(t * 3) + 1];
+ let vi2 = face_to_vertex_buffer[(t * 3) + 2];
+ let uvi0 = face_to_uv_buffer[(t * 3) + 0];
+ let uvi1 = face_to_uv_buffer[(t * 3) + 1];
+ let uvi2 = face_to_uv_buffer[(t * 3) + 2];
+ let nvi0 = face_to_normal_buffer[(t * 3) + 0];
+ let nvi1 = face_to_normal_buffer[(t * 3) + 1];
+ let nvi2 = face_to_normal_buffer[(t * 3) + 2];
+ swizzled_uv_buffer[(vi0 * 2) + 0] = uv_buffer[(uvi0 * 2) + 0];
+ swizzled_uv_buffer[(vi0 * 2) + 1] = uv_buffer[(uvi0 * 2) + 1];
+ swizzled_uv_buffer[(vi1 * 2) + 0] = uv_buffer[(uvi1 * 2) + 0];
+ swizzled_uv_buffer[(vi1 * 2) + 1] = uv_buffer[(uvi1 * 2) + 1];
+ swizzled_uv_buffer[(vi2 * 2) + 0] = uv_buffer[(uvi2 * 2) + 0];
+ swizzled_uv_buffer[(vi2 * 2) + 1] = uv_buffer[(uvi2 * 2) + 1];
+ swizzled_normal_buffer[(vi0 * 3) + 0] = normal_buffer[(nvi0 * 3) + 0];
+ swizzled_normal_buffer[(vi0 * 3) + 1] = normal_buffer[(nvi0 * 3) + 1];
+ swizzled_normal_buffer[(vi0 * 3) + 2] = normal_buffer[(nvi0 * 3) + 2];
+ swizzled_normal_buffer[(vi1 * 3) + 0] = normal_buffer[(nvi1 * 3) + 0];
+ swizzled_normal_buffer[(vi1 * 3) + 1] = normal_buffer[(nvi1 * 3) + 1];
+ swizzled_normal_buffer[(vi1 * 3) + 2] = normal_buffer[(nvi1 * 3) + 2];
+ swizzled_normal_buffer[(vi2 * 3) + 0] = normal_buffer[(nvi2 * 3) + 0];
+ swizzled_normal_buffer[(vi2 * 3) + 1] = normal_buffer[(nvi2 * 3) + 1];
+ swizzled_normal_buffer[(vi2 * 3) + 2] = normal_buffer[(nvi2 * 3) + 2];
+ }
+ geometry.setAttribute("position", new THREE.BufferAttribute(vertex_buffer, 3));
+ geometry.setAttribute("normal" , new THREE.BufferAttribute(swizzled_normal_buffer, 3));
+ geometry.setAttribute("uv" , new THREE.BufferAttribute(swizzled_uv_buffer, 2));
+ geometry.setIndex (Array.from(face_to_vertex_buffer));
+ geometry.computeVertexNormals(); // MuJoCo Normals acting strangely... just recompute them
+ meshes[meshID] = geometry;
+ } else {
+ geometry = meshes[meshID];
+ }
+
+ bodies[b].has_custom_mesh = true;
+ }
+ // Done with geometry creation.
+
+ // Set the Material Properties of incoming bodies
+ let texture = undefined;
+ let color = [
+ model.geom_rgba[(g * 4) + 0],
+ model.geom_rgba[(g * 4) + 1],
+ model.geom_rgba[(g * 4) + 2],
+ model.geom_rgba[(g * 4) + 3]];
+ if (model.geom_matid[g] != -1) {
+ let matId = model.geom_matid[g];
+ color = [
+ model.mat_rgba[(matId * 4) + 0],
+ model.mat_rgba[(matId * 4) + 1],
+ model.mat_rgba[(matId * 4) + 2],
+ model.mat_rgba[(matId * 4) + 3]];
+
+ // Construct Texture from model.tex_data
+ texture = undefined;
+ // mat_texid is now a matrix (nmat x mjNTEXROLE)
+ // We use mjTEXROLE_RGB (value 1) for standard diffuse/color textures
+ const mjNTEXROLE = 10; // Total number of texture roles
+ const mjTEXROLE_RGB = 1; // RGB texture role
+ let texId = model.mat_texid[(matId * mjNTEXROLE) + mjTEXROLE_RGB];
+
+ if (texId != -1) {
+ let width = model.tex_width [texId];
+ let height = model.tex_height[texId];
+ // tex_adr is an mjtSize (64-bit) field, exposed as a BigInt64Array.
+ let offset = Number(model.tex_adr[texId]);
+ let channels = model.tex_nchannel[texId];
+ let texData = model.tex_data;
+ let rgbaArray = new Uint8Array(width * height * 4);
+ for (let p = 0; p < width * height; p++){
+ rgbaArray[(p * 4) + 0] = texData[offset + ((p * channels) + 0)];
+ rgbaArray[(p * 4) + 1] = channels > 1 ? texData[offset + ((p * channels) + 1)] : rgbaArray[(p * 4) + 0];
+ rgbaArray[(p * 4) + 2] = channels > 2 ? texData[offset + ((p * channels) + 2)] : rgbaArray[(p * 4) + 0];
+ rgbaArray[(p * 4) + 3] = channels > 3 ? texData[offset + ((p * channels) + 3)] : 255;
+ }
+ texture = new THREE.DataTexture(rgbaArray, width, height, THREE.RGBAFormat, THREE.UnsignedByteType);
+ if (texId == 2) {
+ texture.repeat = new THREE.Vector2(50, 50);
+ texture.wrapS = THREE.RepeatWrapping;
+ texture.wrapT = THREE.RepeatWrapping;
+ } else {
+ texture.repeat = new THREE.Vector2(model.mat_texrepeat[(model.geom_matid[g] * 2) + 0],
+ model.mat_texrepeat[(model.geom_matid[g] * 2) + 1]);
+ texture.wrapS = THREE.RepeatWrapping;
+ texture.wrapT = THREE.RepeatWrapping;
+ }
+
+ texture.needsUpdate = true;
+ }
+ }
+
+ // Create a new material for each geom to avoid cross-contamination
+ let currentMaterial = new THREE.MeshPhysicalMaterial({
+ color: new THREE.Color(color[0], color[1], color[2]),
+ transparent: color[3] < 1.0,
+ opacity: color[3]/255.,
+ specularIntensity: model.geom_matid[g] != -1 ? model.mat_specular [model.geom_matid[g]] : undefined,
+ reflectivity : model.geom_matid[g] != -1 ? model.mat_reflectance[model.geom_matid[g]] : undefined,
+ roughness : model.geom_matid[g] != -1 ? 1.0 - model.mat_shininess [model.geom_matid[g]] : undefined,
+ metalness : model.geom_matid[g] != -1 ? 0.1 : undefined, //model.mat_metallic [model.geom_matid[g]]
+ map : texture
+ });
+
+ let mesh;// = new THREE.Mesh();
+ if (type == 0) {
+ mesh = new Reflector( new THREE.PlaneGeometry( 100, 100 ), { clipBias: 0.003, texture: texture } );
+ mesh.rotateX( - Math.PI / 2 );
+ } else {
+ mesh = new THREE.Mesh(geometry, currentMaterial);
+ }
+
+ mesh.castShadow = g == 0 ? false : true;
+ mesh.receiveShadow = type != 7;
+ mesh.bodyID = b;
+ bodies[b].add(mesh);
+ getPosition (model.geom_pos, g, mesh.position );
+ if (type != 0) { getQuaternion(model.geom_quat, g, mesh.quaternion); }
+ if (type == 4) { mesh.scale.set(size[0], size[2], size[1]); } // Stretch the Ellipsoid
+ }
+
+ // Parse tendons.
+ let tendonMat = new THREE.MeshPhongMaterial();
+ tendonMat.color = new THREE.Color(0.8, 0.3, 0.3);
+ mujocoRoot.cylinders = new THREE.InstancedMesh(
+ new THREE.CylinderGeometry(1, 1, 1),
+ tendonMat, 1023);
+ mujocoRoot.cylinders.receiveShadow = true;
+ mujocoRoot.cylinders.castShadow = true;
+ mujocoRoot.add(mujocoRoot.cylinders);
+ mujocoRoot.spheres = new THREE.InstancedMesh(
+ new THREE.SphereGeometry(1, 10, 10),
+ tendonMat, 1023);
+ mujocoRoot.spheres.receiveShadow = true;
+ mujocoRoot.spheres.castShadow = true;
+ mujocoRoot.add(mujocoRoot.spheres);
+
+ // Parse lights.
+ for (let l = 0; l < model.nlight; l++) {
+ let light = new THREE.DirectionalLight();
+ if (model.light_type[l] == 0) {
+ light = new THREE.SpotLight();
+ light.angle = 1.51;//model.light_cutoffangle[l];
+ } else if (model.light_type[l] == 1) {
+ light = new THREE.DirectionalLight();
+ } else if (model.light_type[l] == 2) {
+ light = new THREE.PointLight();
+ }else if (model.light_type[l] == 3) {
+ light = new THREE.HemisphereLight();
+ }
+
+ light.angle = 1.11;
+
+ light.decay = model.light_attenuation[l] * 100;
+ light.penumbra = 0.5;
+ light.castShadow = true; // default false
+ light.intensity = light.intensity * 3.14 * 1.0;
+
+ light.shadow.mapSize.width = 1024; // default
+ light.shadow.mapSize.height = 1024; // default
+ light.shadow.camera.near = 0.1; // default
+ light.shadow.camera.far = 10; // default
+ //bodies[model.light_bodyid()].add(light);
+ if (bodies[0]) {
+ bodies[0].add(light);
+ } else {
+ mujocoRoot.add(light);
+ }
+ lights.push(light);
+ }
+ if (model.nlight == 0) {
+ let light = new THREE.DirectionalLight();
+ mujocoRoot.add(light);
+ }
+
+ for (let b = 0; b < model.nbody; b++) {
+ //let parent_body = model.body_parentid()[b];
+ if (b == 0 || !bodies[0]) {
+ mujocoRoot.add(bodies[b]);
+ } else if(bodies[b]){
+ bodies[0].add(bodies[b]);
+ } else {
+ console.log("Body without Geometry detected; adding to bodies", b, bodies[b]);
+ bodies[b] = new THREE.Group(); bodies[b].name = names[b + 1]; bodies[b].bodyID = b; bodies[b].has_custom_mesh = false;
+ bodies[0].add(bodies[b]);
+ }
+ }
+
+ parent.mujocoRoot = mujocoRoot;
+
+ return [model, data, bodies, lights];
+}
+
+export function drawTendonsAndFlex(mujocoRoot, model, data) {
+ // Update tendon transforms.
+ let identityQuat = new THREE.Quaternion();
+ let numWraps = 0;
+ if (mujocoRoot && mujocoRoot.cylinders) {
+ let mat = new THREE.Matrix4();
+ for (let t = 0; t < model.ntendon; t++) {
+ let startW = data.ten_wrapadr[t];
+ let r = model.tendon_width[t];
+ for (let w = startW; w < startW + data.ten_wrapnum[t] -1 ; w++) {
+ let tendonStart = getPosition(data.wrap_xpos, w , new THREE.Vector3());
+ let tendonEnd = getPosition(data.wrap_xpos, w + 1, new THREE.Vector3());
+ let tendonAvg = new THREE.Vector3().addVectors(tendonStart, tendonEnd).multiplyScalar(0.5);
+
+ let validStart = tendonStart.length() > 0.01;
+ let validEnd = tendonEnd .length() > 0.01;
+
+ if (validStart) { mujocoRoot.spheres.setMatrixAt(numWraps , mat.compose(tendonStart, identityQuat, new THREE.Vector3(r, r, r))); }
+ if (validEnd ) { mujocoRoot.spheres.setMatrixAt(numWraps + 1, mat.compose(tendonEnd , identityQuat, new THREE.Vector3(r, r, r))); }
+ if (validStart && validEnd) {
+ mat.compose(tendonAvg, identityQuat.setFromUnitVectors(
+ new THREE.Vector3(0, 1, 0), tendonEnd.clone().sub(tendonStart).normalize()),
+ new THREE.Vector3(r, tendonStart.distanceTo(tendonEnd), r));
+ mujocoRoot.cylinders.setMatrixAt(numWraps, mat);
+ numWraps++;
+ }
+ }
+ }
+
+ let curFlexSphereInd = numWraps;
+ let tempvertPos = new THREE.Vector3();
+ let tempvertRad = new THREE.Vector3();
+ for (let i = 0; i < model.nflex; i++) {
+ for(let j = 0; j < model.flex_vertnum[i]; j++) {
+ let vertIndex = model.flex_vertadr[i] + j;
+ getPosition(data.flexvert_xpos, vertIndex, tempvertPos);
+ let r = 0.01;
+ mat.compose(tempvertPos, identityQuat, tempvertRad.set(r, r, r));
+
+ mujocoRoot.spheres.setMatrixAt(curFlexSphereInd, mat);
+ curFlexSphereInd++;
+ }
+ }
+ mujocoRoot.cylinders.count = numWraps;
+ mujocoRoot.spheres .count = curFlexSphereInd;
+ mujocoRoot.cylinders.instanceMatrix.needsUpdate = true;
+ mujocoRoot.spheres .instanceMatrix.needsUpdate = true;
+ }
+}
+
+/** Downloads the scenes/assets folder to MuJoCo's virtual filesystem
+ * @param {mujoco} mujoco */
+export async function downloadExampleScenesFolder(mujoco) {
+ let allFiles = [
+ "22_humanoids.xml",
+ "adhesion.xml",
+ "agility_cassie/assets/achilles-rod.obj",
+ "agility_cassie/assets/cassie-texture.png",
+ "agility_cassie/assets/foot-crank.obj",
+ "agility_cassie/assets/foot.obj",
+ "agility_cassie/assets/heel-spring.obj",
+ "agility_cassie/assets/hip-pitch.obj",
+ "agility_cassie/assets/hip-roll.obj",
+ "agility_cassie/assets/hip-yaw.obj",
+ "agility_cassie/assets/knee-spring.obj",
+ "agility_cassie/assets/knee.obj",
+ "agility_cassie/assets/pelvis.obj",
+ "agility_cassie/assets/plantar-rod.obj",
+ "agility_cassie/assets/shin.obj",
+ "agility_cassie/assets/tarsus.obj",
+ "agility_cassie/cassie.xml",
+ "agility_cassie/scene.xml",
+ "arm26.xml",
+ "balloons.xml",
+ "car.xml",
+ "flex.xml",
+ "hammock.xml",
+ "humanoid.xml",
+ "humanoid_body.xml",
+ "model.xml",
+ "mug.obj",
+ "mug.png",
+ "mug.xml",
+ "scene.xml",
+ "shadow_hand/assets/f_distal_pst.obj",
+ "shadow_hand/assets/f_knuckle.obj",
+ "shadow_hand/assets/f_middle.obj",
+ "shadow_hand/assets/f_proximal.obj",
+ "shadow_hand/assets/forearm_0.obj",
+ "shadow_hand/assets/forearm_1.obj",
+ "shadow_hand/assets/forearm_collision.obj",
+ "shadow_hand/assets/lf_metacarpal.obj",
+ "shadow_hand/assets/mounting_plate.obj",
+ "shadow_hand/assets/palm.obj",
+ "shadow_hand/assets/th_distal_pst.obj",
+ "shadow_hand/assets/th_middle.obj",
+ "shadow_hand/assets/th_proximal.obj",
+ "shadow_hand/assets/wrist.obj",
+ "shadow_hand/left_hand.xml",
+ "shadow_hand/right_hand.xml",
+ "shadow_hand/scene_left.xml",
+ "shadow_hand/scene_right.xml",
+ "simple.xml",
+ "slider_crank.xml",
+ "model_with_tendon.xml",
+ ];
+
+ let requests = allFiles.map((url) => fetch("./assets/scenes/" + url));
+ let responses = await Promise.all(requests);
+ for (let i = 0; i < responses.length; i++) {
+ let split = allFiles[i].split("/");
+ let working = '/working/';
+ for (let f = 0; f < split.length - 1; f++) {
+ working += split[f];
+ if (!mujoco.FS.analyzePath(working).exists) { mujoco.FS.mkdir(working); }
+ working += "/";
+ }
+
+ if (allFiles[i].endsWith(".png") || allFiles[i].endsWith(".stl") || allFiles[i].endsWith(".skn")) {
+ mujoco.FS.writeFile("/working/" + allFiles[i], new Uint8Array(await responses[i].arrayBuffer()));
+ } else {
+ mujoco.FS.writeFile("/working/" + allFiles[i], await responses[i].text());
+ }
+ }
+}
+
+/** Access the vector at index, swizzle for three.js, and apply to the target THREE.Vector3
+ * @param {Float32Array|Float64Array} buffer
+ * @param {number} index
+ * @param {THREE.Vector3} target */
+export function getPosition(buffer, index, target, swizzle = true) {
+ if (swizzle) {
+ return target.set(
+ buffer[(index * 3) + 0],
+ buffer[(index * 3) + 2],
+ -buffer[(index * 3) + 1]);
+ } else {
+ return target.set(
+ buffer[(index * 3) + 0],
+ buffer[(index * 3) + 1],
+ buffer[(index * 3) + 2]);
+ }
+}
+
+/** Access the quaternion at index, swizzle for three.js, and apply to the target THREE.Quaternion
+ * @param {Float32Array|Float64Array} buffer
+ * @param {number} index
+ * @param {THREE.Quaternion} target */
+export function getQuaternion(buffer, index, target, swizzle = true) {
+ if (swizzle) {
+ return target.set(
+ -buffer[(index * 4) + 1],
+ -buffer[(index * 4) + 3],
+ buffer[(index * 4) + 2],
+ -buffer[(index * 4) + 0]);
+ } else {
+ return target.set(
+ buffer[(index * 4) + 0],
+ buffer[(index * 4) + 1],
+ buffer[(index * 4) + 2],
+ buffer[(index * 4) + 3]);
+ }
+}
+
+/** Converts this Vector3's Handedness to MuJoCo's Coordinate Handedness
+ * @param {THREE.Vector3} target */
+export function toMujocoPos(target) { return target.set(target.x, -target.z, target.y); }
+
+/** Standard normal random number generator using Box-Muller transform */
+export function standardNormal() {
+ return Math.sqrt(-2.0 * Math.log( Math.random())) *
+ Math.cos ( 2.0 * Math.PI * Math.random()); }
+
diff --git a/src/utils/Debug.js b/src/utils/Debug.js
new file mode 100644
index 00000000..5b19cd68
--- /dev/null
+++ b/src/utils/Debug.js
@@ -0,0 +1,36 @@
+/** This class provides Debug Utilities. */
+class Debug {
+
+ /** Reroute Console Errors to the Main Screen (for mobile) */
+ constructor() {
+ // Intercept Main Window Errors as well
+ window.realConsoleError = console.error;
+ window.addEventListener('error', (event) => {
+ let path = event.filename.split("/");
+ this.display((path[path.length - 1] + ":" + event.lineno + " - " + event.message));
+ });
+ console.error = this.fakeError.bind(this);
+
+ // Record whether we're on Safari or Mobile (unused so far)
+ this.safari = /(Safari)/g.test( navigator.userAgent ) && ! /(Chrome)/g.test( navigator.userAgent );
+ this.mobile = /(Android|iPad|iPhone|iPod|Oculus)/g.test(navigator.userAgent) || this.safari;
+ }
+
+ // Log Errors as s over the main viewport
+ fakeError(...args) {
+ if (args.length > 0 && args[0]) { this.display(JSON.stringify(args[0])); }
+ window.realConsoleError.apply(console, arguments);
+ }
+
+ display(text) {
+ //if (this.mobile) {
+ let errorNode = window.document.getElementById("error");
+ errorNode.innerHTML += "\n\n"+text.fontcolor("red");
+ //window.document.getElementById("info").appendChild(errorNode);
+ //}
+ }
+
+}
+
+export { Debug };
+let debug = new Debug();
\ No newline at end of file
diff --git a/src/utils/DragStateManager.js b/src/utils/DragStateManager.js
new file mode 100644
index 00000000..84675035
--- /dev/null
+++ b/src/utils/DragStateManager.js
@@ -0,0 +1,135 @@
+import * as THREE from 'three';
+import { Vector3 } from 'three';
+
+export class DragStateManager {
+ constructor(scene, renderer, camera, container, controls) {
+ this.scene = scene;
+ this.renderer = renderer;
+ this.camera = camera;
+ this.mousePos = new THREE.Vector2();
+ this.raycaster = new THREE.Raycaster();
+ //this.raycaster.layers.set(1);
+ // this.raycaster.params.Mesh.threshold = 3;
+ this.raycaster.params.Line.threshold = 0.1;
+ this.grabDistance = 0.0;
+ this.active = false;
+ this.physicsObject = null;
+ this.controls = controls;
+
+ this.arrow = new THREE.ArrowHelper(new THREE.Vector3(0, 1, 0), new THREE.Vector3(0, 0, 0), 15, 0x666666);
+ this.arrow.setLength(15, 3, 1);
+ this.scene.add(this.arrow);
+ //this.residuals.push(arrow);
+ this.arrow.line.material.transparent = true;
+ this.arrow.cone.material.transparent = true;
+ this.arrow.line.material.opacity = 0.5;
+ this.arrow.cone.material.opacity = 0.5;
+ this.arrow.visible = false;
+
+ this.previouslySelected = null;
+ this.higlightColor = 0xff0000; // 0x777777
+
+ this.localHit = new Vector3();
+ this.worldHit = new Vector3();
+ this.currentWorld = new Vector3();
+
+ container.addEventListener( 'pointerdown', this.onPointer.bind(this), true );
+ document.addEventListener( 'pointermove', this.onPointer.bind(this), true );
+ document.addEventListener( 'pointerup' , this.onPointer.bind(this), true );
+ document.addEventListener( 'pointerout' , this.onPointer.bind(this), true );
+ container.addEventListener( 'dblclick', this.onPointer.bind(this), false );
+ }
+ updateRaycaster(x, y) {
+ var rect = this.renderer.domElement.getBoundingClientRect();
+ this.mousePos.x = ((x - rect.left) / rect.width) * 2 - 1;
+ this.mousePos.y = -((y - rect.top) / rect.height) * 2 + 1;
+ this.raycaster.setFromCamera(this.mousePos, this.camera);
+ }
+ start(x, y) {
+ this.physicsObject = null;
+ this.updateRaycaster(x, y);
+ let intersects = this.raycaster.intersectObjects(this.scene.children);
+ for (let i = 0; i < intersects.length; i++) {
+ let obj = intersects[i].object;
+ if (obj.bodyID && obj.bodyID > 0) {
+ this.physicsObject = obj;
+ this.grabDistance = intersects[0].distance;
+ let hit = this.raycaster.ray.origin.clone();
+ hit.addScaledVector(this.raycaster.ray.direction, this.grabDistance);
+ this.arrow.position.copy(hit);
+ //this.physicsObject.startGrab(hit);
+ this.active = true;
+ this.controls.enabled = false;
+ this.localHit = obj.worldToLocal(hit.clone());
+ this.worldHit.copy(hit);
+ this.currentWorld.copy(hit);
+ this.arrow.visible = true;
+ break;
+ }
+ }
+ }
+ move(x, y) {
+ if (this.active) {
+ this.updateRaycaster(x, y);
+ let hit = this.raycaster.ray.origin.clone();
+ hit.addScaledVector(this.raycaster.ray.direction, this.grabDistance);
+ this.currentWorld.copy(hit);
+
+ this.update();
+
+ if (this.physicsObject != null) {
+ //this.physicsObject.moveGrabbed(hit);
+ }
+ }
+ }
+ update() {
+ if (this.worldHit && this.localHit && this.currentWorld && this.arrow && this.physicsObject) {
+ this.worldHit.copy(this.localHit);
+ this.physicsObject.localToWorld(this.worldHit);
+ this.arrow.position.copy(this.worldHit);
+ this.arrow.setDirection(this.currentWorld.clone().sub(this.worldHit).normalize());
+ this.arrow.setLength(this.currentWorld.clone().sub(this.worldHit).length());
+ }
+ }
+ end(evt) {
+ //this.physicsObject.endGrab();
+ this.physicsObject = null;
+
+ this.active = false;
+ this.controls.enabled = true;
+ //this.controls.onPointerUp(evt);
+ this.arrow.visible = false;
+ this.mouseDown = false;
+ }
+ onPointer(evt) {
+ if (evt.type == "pointerdown") {
+ this.start(evt.clientX, evt.clientY);
+ this.mouseDown = true;
+ } else if (evt.type == "pointermove" && this.mouseDown) {
+ if (this.active) { this.move(evt.clientX, evt.clientY); }
+ } else if (evt.type == "pointerup" /*|| evt.type == "pointerout"*/) {
+ this.end(evt);
+ }
+ if (evt.type == "dblclick") {
+ this.start(evt.clientX, evt.clientY);
+ this.doubleClick = true;
+ if (this.physicsObject) {
+ if (this.physicsObject == this.previouslySelected) {
+ this.physicsObject.material.emissive.setHex(0x000000);
+ this.previouslySelected = null;
+ } else {
+ if (this.previouslySelected) {
+ this.previouslySelected.material.emissive.setHex(0x000000);
+ }
+ this.physicsObject.material.emissive.setHex(this.higlightColor);
+ this.previouslySelected = this.physicsObject;
+ }
+ } else {
+ if (this.previouslySelected) {
+ this.previouslySelected.material.emissive.setHex(0x000000);
+ this.previouslySelected = null;
+ }
+ }
+ }
+ }
+}
diff --git a/src/utils/Reflector.js b/src/utils/Reflector.js
new file mode 100644
index 00000000..9f7c808d
--- /dev/null
+++ b/src/utils/Reflector.js
@@ -0,0 +1,226 @@
+import {
+ Color,
+ Matrix4,
+ Mesh,
+ PerspectiveCamera,
+ Plane,
+ ShaderMaterial,
+ UniformsUtils,
+ Vector3,
+ Vector4,
+ WebGLRenderTarget,
+ HalfFloatType,
+ NoToneMapping,
+ LinearSRGBColorSpace,
+ MeshPhysicalMaterial
+} from 'three';
+
+class Reflector extends Mesh {
+
+ constructor( geometry, options = {} ) {
+
+ super( geometry );
+
+ this.isReflector = true;
+
+ this.type = 'Reflector';
+ this.camera = new PerspectiveCamera();
+
+ const scope = this;
+
+ const color = ( options.color !== undefined ) ? new Color( options.color ) : new Color( 0x7F7F7F );
+ const textureWidth = options.textureWidth || 1024;
+ const textureHeight = options.textureHeight || 1024;
+ const clipBias = options.clipBias || 0;
+ const shader = options.shader || Reflector.ReflectorShader;
+ const multisample = ( options.multisample !== undefined ) ? options.multisample : 4;
+ const blendTexture = options.texture || undefined;
+
+ //
+
+ const reflectorPlane = new Plane();
+ const normal = new Vector3();
+ const reflectorWorldPosition = new Vector3();
+ const cameraWorldPosition = new Vector3();
+ const rotationMatrix = new Matrix4();
+ const lookAtPosition = new Vector3( 0, 0, - 1 );
+ const clipPlane = new Vector4();
+
+ const view = new Vector3();
+ const target = new Vector3();
+ const q = new Vector4();
+
+ const textureMatrix = new Matrix4();
+ const virtualCamera = this.camera;
+
+ const renderTarget = new WebGLRenderTarget( textureWidth, textureHeight, { samples: multisample, type: HalfFloatType } );
+
+ this.material = new MeshPhysicalMaterial( { map: blendTexture });
+ this.material.uniforms = { tDiffuse : { value: renderTarget.texture },
+ textureMatrix: { value: textureMatrix }};
+ this.material.onBeforeCompile = ( shader ) => {
+
+ // Vertex Shader: Set Vertex Positions to the Unwrapped UV Positions
+ let bodyStart = shader.vertexShader.indexOf( 'void main() {' );
+ shader.vertexShader =
+ shader.vertexShader.slice(0, bodyStart) +
+ '\nuniform mat4 textureMatrix;\nvarying vec4 vUv3;\n' +
+ shader.vertexShader.slice( bodyStart - 1, - 1 ) +
+ ' vUv3 = textureMatrix * vec4( position, 1.0 ); }';
+
+ // Fragment Shader: Set Pixels to 9-tap box blur the current frame's Shadows
+ bodyStart = shader.fragmentShader.indexOf( 'void main() {' );
+ shader.fragmentShader =
+ //'#define USE_UV\n' +
+ '\nuniform sampler2D tDiffuse; \n varying vec4 vUv3;\n' +
+ shader.fragmentShader.slice( 0, bodyStart ) +
+ shader.fragmentShader.slice( bodyStart - 1, - 1 ) +
+ ` gl_FragColor = vec4( mix( texture2DProj( tDiffuse, vUv3 ).rgb, gl_FragColor.rgb , 0.5), 1.0 );
+ }`;
+
+ // Set the LightMap Accumulation Buffer
+ shader.uniforms.tDiffuse = { value: renderTarget.texture };
+ shader.uniforms.textureMatrix = { value: textureMatrix };
+ this.material.uniforms = shader.uniforms;
+
+ // Set the new Shader to this
+ this.material.userData.shader = shader;
+ };
+ this.receiveShadow = true;
+
+
+ this.onBeforeRender = function ( renderer, scene, camera ) {
+
+ reflectorWorldPosition.setFromMatrixPosition( scope.matrixWorld );
+ cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
+
+ rotationMatrix.extractRotation( scope.matrixWorld );
+
+ normal.set( 0, 0, 1 );
+ normal.applyMatrix4( rotationMatrix );
+
+ view.subVectors( reflectorWorldPosition, cameraWorldPosition );
+
+ // Avoid rendering when reflector is facing away
+
+ if ( view.dot( normal ) > 0 ) return;
+
+ view.reflect( normal ).negate();
+ view.add( reflectorWorldPosition );
+
+ rotationMatrix.extractRotation( camera.matrixWorld );
+
+ lookAtPosition.set( 0, 0, - 1 );
+ lookAtPosition.applyMatrix4( rotationMatrix );
+ lookAtPosition.add( cameraWorldPosition );
+
+ target.subVectors( reflectorWorldPosition, lookAtPosition );
+ target.reflect( normal ).negate();
+ target.add( reflectorWorldPosition );
+
+ virtualCamera.position.copy( view );
+ virtualCamera.up.set( 0, 1, 0 );
+ virtualCamera.up.applyMatrix4( rotationMatrix );
+ virtualCamera.up.reflect( normal );
+ virtualCamera.lookAt( target );
+
+ virtualCamera.far = camera.far; // Used in WebGLBackground
+
+ virtualCamera.updateMatrixWorld();
+ virtualCamera.projectionMatrix.copy( camera.projectionMatrix );
+
+ // Update the texture matrix
+ textureMatrix.set(
+ 0.5, 0.0, 0.0, 0.5,
+ 0.0, 0.5, 0.0, 0.5,
+ 0.0, 0.0, 0.5, 0.5,
+ 0.0, 0.0, 0.0, 1.0
+ );
+ textureMatrix.multiply( virtualCamera.projectionMatrix );
+ textureMatrix.multiply( virtualCamera.matrixWorldInverse );
+ textureMatrix.multiply( scope.matrixWorld );
+
+ // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
+ // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
+ reflectorPlane.setFromNormalAndCoplanarPoint( normal, reflectorWorldPosition );
+ reflectorPlane.applyMatrix4( virtualCamera.matrixWorldInverse );
+
+ clipPlane.set( reflectorPlane.normal.x, reflectorPlane.normal.y, reflectorPlane.normal.z, reflectorPlane.constant );
+
+ const projectionMatrix = virtualCamera.projectionMatrix;
+
+ q.x = ( Math.sign( clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
+ q.y = ( Math.sign( clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
+ q.z = - 1.0;
+ q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
+
+ // Calculate the scaled plane vector
+ clipPlane.multiplyScalar( 2.0 / clipPlane.dot( q ) );
+
+ // Replacing the third row of the projection matrix
+ projectionMatrix.elements[ 2 ] = clipPlane.x;
+ projectionMatrix.elements[ 6 ] = clipPlane.y;
+ projectionMatrix.elements[ 10 ] = clipPlane.z + 1.0 - clipBias;
+ projectionMatrix.elements[ 14 ] = clipPlane.w;
+
+ // Render
+ scope.visible = false;
+
+ const currentRenderTarget = renderer.getRenderTarget();
+
+ const currentXrEnabled = renderer.xr.enabled;
+ const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
+ const currentOutputEncoding = renderer.outputColorSpace;
+ const currentToneMapping = renderer.toneMapping;
+
+ renderer.xr.enabled = false; // Avoid camera modification
+ renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
+ renderer.outputColorSpace = LinearSRGBColorSpace;
+ renderer.toneMapping = NoToneMapping;
+
+ renderer.setRenderTarget( renderTarget );
+
+ renderer.state.buffers.depth.setMask( true ); // make sure the depth buffer is writable so it can be properly cleared, see #18897
+
+ if ( renderer.autoClear === false ) renderer.clear();
+ renderer.render( scene, virtualCamera );
+
+ renderer.xr.enabled = currentXrEnabled;
+ renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
+ renderer.outputColorSpace = currentOutputEncoding;
+ renderer.toneMapping = currentToneMapping;
+
+ renderer.setRenderTarget( currentRenderTarget );
+
+ // Restore viewport
+
+ const viewport = camera.viewport;
+
+ if ( viewport !== undefined ) {
+
+ renderer.state.viewport( viewport );
+
+ }
+
+ scope.visible = true;
+
+ };
+
+ this.getRenderTarget = function () {
+
+ return renderTarget;
+
+ };
+
+ this.dispose = function () {
+
+ renderTarget.dispose();
+ scope.material.dispose();
+
+ };
+
+ }
+
+}
+
+export { Reflector };
diff --git a/test_load.mjs b/test_load.mjs
new file mode 100644
index 00000000..1ec1ece8
--- /dev/null
+++ b/test_load.mjs
@@ -0,0 +1,50 @@
+// Smoke test: compile every demo scene with the new @mujoco/mujoco WASM module.
+import load_mujoco from './node_modules/@mujoco/mujoco/mujoco.js';
+import { readFileSync, readdirSync, statSync } from 'fs';
+import { join, relative } from 'path';
+
+const mujoco = await load_mujoco();
+mujoco.FS.mkdir('/working');
+mujoco.FS.mount(mujoco.MEMFS, { root: '.' }, '/working');
+
+// Copy the whole assets/scenes tree into the VFS.
+function copyDir(dir) {
+ for (const entry of readdirSync(dir)) {
+ const full = join(dir, entry);
+ const rel = relative('assets/scenes', full);
+ if (statSync(full).isDirectory()) {
+ mujoco.FS.mkdir('/working/' + rel);
+ copyDir(full);
+ } else {
+ mujoco.FS.writeFile('/working/' + rel, readFileSync(full));
+ }
+ }
+}
+copyDir('assets/scenes');
+
+const scenes = [
+ "22_humanoids.xml", "adhesion.xml", "agility_cassie/scene.xml", "arm26.xml",
+ "balloons.xml", "car.xml", "flex.xml", "hammock.xml", "humanoid.xml",
+ "model.xml", "mug.xml", "scene.xml", "shadow_hand/scene_right.xml",
+ "shadow_hand/scene_left.xml", "simple.xml", "slider_crank.xml",
+ "model_with_tendon.xml",
+];
+
+let failures = 0;
+for (const scene of scenes) {
+ try {
+ const model = mujoco.MjModel.mj_loadXML('/working/' + scene);
+ const data = new mujoco.MjData(model);
+ for (let i = 0; i < 100; i++) mujoco.mj_step(model, data);
+ // Touch the fields the renderer reads.
+ const probes = [model.names, model.name_bodyadr, model.geom_rgba, model.mat_texid,
+ model.tex_data, data.xpos, data.xquat, data.ten_wrapadr, data.wrap_xpos];
+ if (probes.some(p => p === undefined)) throw new Error('missing field');
+ console.log(`OK ${scene} (nbody=${model.nbody} ngeom=${model.ngeom} nu=${model.nu} nkey=${model.nkey} time=${data.time.toFixed(3)})`);
+ data.delete(); model.delete();
+ } catch (e) {
+ failures++;
+ console.log(`FAIL ${scene}: ${e.message ?? e}`);
+ }
+}
+process.exit(failures ? 1 : 0);