Skip to content

Set object from JavaScript code #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/main.min.js

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js';
import { VRButton } from 'three/examples/jsm/webxr/VRButton.js';
import { XRButton } from 'three/examples/jsm/webxr/XRButton.js';
import { XRControllerModelFactory } from 'three/examples/jsm/webxr/XRControllerModelFactory';

import { Reflector } from 'three/examples/jsm/objects/Reflector.js';

// For exporting THREE's examples that are not included in the THREE namespace.
export const THREE_EXAMPLES = {
Reflector,
};

require('ccapture.js');

// These are bundled as data:// URIs via our webpack.config.js.
Expand Down Expand Up @@ -1491,6 +1499,32 @@ class Viewer {
}
}

set_object_from_code(path, code) {
let obj;
try {
const THREE = MeshCat.THREE;
const THREE_EXAMPLES = MeshCat.THREE_EXAMPLES;
const obj_factory = eval(code);
obj = obj_factory();
} catch (error) {
console.error("Error creating object from raw code:", error);
}

let meshes_cast_shadows = (node) => {
if (node.type === "Mesh") {
node.castShadow = true;
node.receiveShadow = true;
}
for (let i = 0; i < node.children.length; ++i) {
meshes_cast_shadows(node.children[i]);
}
};

meshes_cast_shadows(obj);
this.set_object(path, obj);
this.set_dirty();
}

delete_path(path) {
if (path.length == 0) {
console.error("Deleting the entire scene is not implemented")
Expand Down Expand Up @@ -1608,6 +1642,9 @@ class Viewer {
} else if (cmd.type == "set_object") {
let path = split_path(cmd.path);
this.set_object_from_json(path, cmd.object);
} else if (cmd.type == "set_object_from_code") {
let path = split_path(cmd.path);
this.set_object_from_code(path, cmd.code);
} else if (cmd.type == "set_property") {
let path = split_path(cmd.path);
this.set_property(path, cmd.property, cmd.value);
Expand Down
198 changes: 198 additions & 0 deletions test/mujoco_floor.gltf

Large diffs are not rendered by default.

157 changes: 157 additions & 0 deletions test/mujoco_floor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<!DOCTYPE html>
<html>

<head>
<meta charset=utf-8>
<title>MeshCat - MuJoCo Floor</title>
</head>

<body>
<div id="meshcat-pane">
</div>

<script src="../dist/main.min.js"></script>
<script>
var viewer = new MeshCat.Viewer(document.getElementById("meshcat-pane"));

// Hide the grid
viewer.handle_command({
type: "set_property",
path: "/Grid",
property: "visible",
value: false
});

// Set the background color to white
viewer.set_property(["Background", "<object>"], "top_color", [0.3, 0.5, 0.7]);
viewer.set_property(["Background", "<object>"], "bottom_color", [0, 0, 0]);

// Create a red cube
viewer.handle_command({
type: "set_object",
path: "/meshcat/red_cube",
object: {
metadata: { version: 4.5, type: "Object" },
geometries: [
{
uuid: "cube-geometry",
type: "BoxGeometry",
width: 2,
height: 2,
depth: 2
}
],
materials: [
{
uuid: "cube-material",
type: "MeshStandardMaterial",
color: 0xff0000,
roughness: 0.2,
metalness: 0.3,
side: 2
}
],
object: {
uuid: "cube-mesh",
type: "Mesh",
geometry: "cube-geometry",
material: "cube-material"
}
}
});

// Position the cube above the floor
viewer.handle_command({
type: "set_transform",
path: "/meshcat/red_cube",
matrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1]
});

// Load the floor from GLTF file
fetch('mujoco_floor.gltf')
.then(response => response.arrayBuffer())
.then(buffer => {
const decoder = new TextDecoder('utf-8');
const gltfContent = decoder.decode(buffer);

viewer.handle_command({
type: "set_object",
path: "/meshcat/floor",
object: {
metadata: { version: 4.5, type: "Object" },
geometries: [],
materials: [],
object: {
uuid: "floor-mesh",
type: "_meshfile_object",
format: "gltf",
data: gltfContent
}
}
});

// Position the floor appropriately and rotate it 90 degrees around X-axis
viewer.handle_command({
type: "set_transform",
path: "/meshcat/floor",
matrix: [
1, 0, 0, 0,
0, 0, 1, 0,
0, -1, 0, 0,
0, 0, 0, 1
]
});
})
.catch(error => console.error('Error loading GLTF file:', error));

// Add reflector using set_object_from_code
viewer.handle_command({
type: "set_object_from_code",
path: "/meshcat/reflector",
code: `() => {
const geometry = new THREE.PlaneGeometry(20, 20);
const reflector = new THREE_EXAMPLES.Reflector(geometry, {
clipBias: 0.003,
textureWidth: 2048,
textureHeight: 2048,
color: new THREE.Color(0.5, 0.5, 0.5),
});

// Position slightly below the floor
reflector.position.z = -0.001;

return reflector;
}`
});

</script>

<style>
body {
margin: 0;
}

#meshcat-pane {
width: 100vw;
height: 100vh;
overflow: hidden;
}

#test-message {
width: 75vw;
text-align: left;
background-color: rgb(232, 232, 232);
position: fixed;
left: 0%;
display: block;
padding: 10px;
}

#status-message {
font-size: large;
font-weight: bold;
}
</style>
</body>

</html>