Skip to content

Add InstancedMesh support for efficient multi-environment rendering - #2

Open
initmaks wants to merge 8 commits into
masterfrom
gifted-blackburn
Open

initmaks wants to merge 8 commits into
masterfrom
gifted-blackburn

Conversation

@initmaks

Copy link
Copy Markdown
Owner

Summary

  • Adds InstancedMesh support for rendering thousands of identical objects with a single draw call
  • Enables efficient visualization of multiple robot trajectories (4000+) without performance degradation
  • ~100x faster than creating individual mesh objects

Changes

Python (meshcat-python):

  • geometry.py: New InstancedMesh class with set_matrix_at(), set_matrices(), set_color_at(), set_colors() methods
  • commands.py: New SetInstanceMatrices and SetInstanceColors commands for efficient updates
  • visualizer.py: New set_instance_matrices() and set_instance_colors() methods
  • zmqserver.py: Added new command types

JavaScript (meshcat viewer):

Usage Example

from meshcat.geometry import InstancedMesh, Box, MeshPhongMaterial

# Create 1000 instances with a single draw call
instanced = InstancedMesh(Box([0.1, 0.1, 0.1]), MeshPhongMaterial(), count=1000)

# Set transforms for each instance
for i in range(1000):
    matrix = np.eye(4)
    matrix[:3, 3] = [i * 0.2, 0, 0]
    instanced.set_matrix_at(i, matrix)

vis["robots"].set_object(instanced)

# Efficient updates - only sends transform data, not geometry
new_matrices = compute_new_transforms()
vis["robots"].set_instance_matrices(new_matrices)

# Optional per-instance colors
vis["robots"].set_instance_colors(color_array)

Test plan

  • Run examples/instanced_mesh_simple.py - basic grid animation
  • Run examples/instanced_mesh_demo.py - full feature demo with performance comparison

🤖 Generated with Claude Code

initmaks and others added 8 commits April 13, 2025 22:57
This enables rendering thousands of identical objects (e.g., robot trajectories)
with a single draw call using Three.js InstancedMesh.

Features:
- New InstancedMesh class in geometry.py for creating instanced objects
- set_instance_matrices() for efficient batch transform updates
- set_instance_colors() for per-instance coloring
- Full JavaScript viewer support with dynamic buffer updates

Performance: ~100x faster than individual objects for 1000+ instances

Example usage:
```python
from meshcat.geometry import InstancedMesh, Box, MeshPhongMaterial

# Create 1000 instances
instanced = InstancedMesh(Box([0.1, 0.1, 0.1]), MeshPhongMaterial(), count=1000)
for i in range(1000):
    instanced.set_matrix_at(i, translation_matrix)
vis["robots"].set_object(instanced)

# Efficient updates (only sends transforms, not geometry)
vis["robots"].set_instance_matrices(new_matrices)
```

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant