mujoco-react 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/README.md +510 -0
- package/dist/index.d.ts +1080 -0
- package/dist/index.js +3518 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -0
- package/src/components/ContactListener.tsx +26 -0
- package/src/components/ContactMarkers.tsx +81 -0
- package/src/components/Debug.tsx +227 -0
- package/src/components/DragInteraction.tsx +227 -0
- package/src/components/FlexRenderer.tsx +102 -0
- package/src/components/IkGizmo.tsx +146 -0
- package/src/components/SceneLights.tsx +131 -0
- package/src/components/SceneRenderer.tsx +104 -0
- package/src/components/SelectionHighlight.tsx +69 -0
- package/src/components/TendonRenderer.tsx +84 -0
- package/src/components/TrajectoryPlayer.tsx +44 -0
- package/src/core/GenericIK.ts +339 -0
- package/src/core/MujocoCanvas.tsx +72 -0
- package/src/core/MujocoProvider.tsx +78 -0
- package/src/core/MujocoSimProvider.tsx +1201 -0
- package/src/core/SceneLoader.ts +275 -0
- package/src/hooks/useActuators.ts +36 -0
- package/src/hooks/useBodyState.ts +56 -0
- package/src/hooks/useContacts.ts +125 -0
- package/src/hooks/useCtrl.ts +40 -0
- package/src/hooks/useCtrlNoise.ts +59 -0
- package/src/hooks/useGamepad.ts +77 -0
- package/src/hooks/useGravityCompensation.ts +22 -0
- package/src/hooks/useJointState.ts +64 -0
- package/src/hooks/useKeyboardTeleop.ts +97 -0
- package/src/hooks/usePolicy.ts +56 -0
- package/src/hooks/useSensor.ts +83 -0
- package/src/hooks/useSitePosition.ts +62 -0
- package/src/hooks/useTrajectoryPlayer.ts +105 -0
- package/src/hooks/useTrajectoryRecorder.ts +97 -0
- package/src/hooks/useVideoRecorder.ts +82 -0
- package/src/index.ts +108 -0
- package/src/rendering/CapsuleGeometry.ts +35 -0
- package/src/rendering/GeomBuilder.ts +140 -0
- package/src/rendering/Reflector.ts +225 -0
- package/src/types.ts +619 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
# mujoco-react
|
|
2
|
+
|
|
3
|
+
Composable [React Three Fiber](https://docs.pmnd.rs/react-three-fiber) building blocks for [MuJoCo](https://mujoco.org/) WASM simulations. Works with **any robot, any scene** — you provide the MJCF model, the library handles physics, rendering, and IK.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install mujoco-react three @react-three/fiber @react-three/drei
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Quick Start
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import {
|
|
16
|
+
MujocoProvider,
|
|
17
|
+
MujocoCanvas,
|
|
18
|
+
SceneRenderer,
|
|
19
|
+
IkGizmo,
|
|
20
|
+
} from 'mujoco-react';
|
|
21
|
+
import type { SceneConfig, MujocoSimAPI } from 'mujoco-react';
|
|
22
|
+
import { OrbitControls } from '@react-three/drei';
|
|
23
|
+
|
|
24
|
+
const config: SceneConfig = {
|
|
25
|
+
robotId: 'franka_emika_panda',
|
|
26
|
+
sceneFile: 'scene.xml',
|
|
27
|
+
numArmJoints: 7,
|
|
28
|
+
tcpSiteName: 'tcp',
|
|
29
|
+
gripperActuatorName: 'gripper',
|
|
30
|
+
homeJoints: [1.707, -1.754, 0.003, -2.702, 0.003, 0.951, 2.490],
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function App() {
|
|
34
|
+
const apiRef = useRef<MujocoSimAPI | null>(null);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<MujocoProvider>
|
|
38
|
+
<MujocoCanvas
|
|
39
|
+
config={config}
|
|
40
|
+
onReady={(api) => { apiRef.current = api; }}
|
|
41
|
+
camera={{ position: [2, -1.5, 2.5], up: [0, 0, 1], fov: 45 }}
|
|
42
|
+
shadows
|
|
43
|
+
style={{ width: '100%', height: '100vh' }}
|
|
44
|
+
>
|
|
45
|
+
<OrbitControls enableDamping makeDefault />
|
|
46
|
+
<SceneRenderer />
|
|
47
|
+
<IkGizmo />
|
|
48
|
+
<ambientLight intensity={0.7} />
|
|
49
|
+
<directionalLight position={[1, 2, 5]} intensity={1.2} castShadow />
|
|
50
|
+
</MujocoCanvas>
|
|
51
|
+
</MujocoProvider>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Architecture
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
<MujocoProvider> ← WASM module lifecycle
|
|
60
|
+
<MujocoCanvas config={...}> ← Thin R3F Canvas wrapper + physics context
|
|
61
|
+
<OrbitControls /> ← You add your own controls
|
|
62
|
+
<SceneRenderer /> ← Syncs MuJoCo bodies to Three.js meshes
|
|
63
|
+
<IkGizmo /> ← PivotControls-based IK handle
|
|
64
|
+
<YourLights /> ← You compose your own scene
|
|
65
|
+
<YourGrid />
|
|
66
|
+
<YourCustomLogic /> ← Your hooks + components
|
|
67
|
+
</MujocoCanvas>
|
|
68
|
+
</MujocoProvider>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The library provides **only MuJoCo engine concerns**: WASM lifecycle, physics stepping, body rendering, IK solving. Everything else (lighting, grid, markers, game logic) is composed by the consumer as R3F children.
|
|
72
|
+
|
|
73
|
+
## Loading Models
|
|
74
|
+
|
|
75
|
+
Models are loaded from any HTTP source via `SceneConfig.baseUrl`. Defaults to [MuJoCo Menagerie](https://github.com/google-deepmind/mujoco_menagerie) on GitHub.
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
// Menagerie robots — just set robotId
|
|
79
|
+
const franka: SceneConfig = {
|
|
80
|
+
robotId: 'franka_emika_panda',
|
|
81
|
+
sceneFile: 'scene.xml',
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// Any GitHub repo
|
|
85
|
+
const so101: SceneConfig = {
|
|
86
|
+
robotId: 'so101',
|
|
87
|
+
sceneFile: 'SO101.xml',
|
|
88
|
+
baseUrl: 'https://raw.githubusercontent.com/Vector-Wangel/MuJoCo-GS-Web/main/assets/robots/xlerobot/',
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// Self-hosted
|
|
92
|
+
const custom: SceneConfig = {
|
|
93
|
+
robotId: 'my_robot',
|
|
94
|
+
sceneFile: 'robot.xml',
|
|
95
|
+
baseUrl: 'http://localhost:3000/models/my_robot/',
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The loader fetches the scene XML, parses it for dependencies (meshes, textures, includes), recursively fetches those too, applies any XML patches, and writes everything to MuJoCo's in-memory WASM filesystem.
|
|
100
|
+
|
|
101
|
+
## SceneConfig
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
interface SceneConfig {
|
|
105
|
+
robotId: string; // e.g. 'franka_emika_panda'
|
|
106
|
+
sceneFile: string; // Entry XML file, e.g. 'scene.xml'
|
|
107
|
+
baseUrl?: string; // Base URL for fetching model files
|
|
108
|
+
sceneObjects?: SceneObject[]; // Objects injected into scene XML at load time
|
|
109
|
+
tcpSiteName?: string; // MuJoCo site for IK. Default: 'tcp'
|
|
110
|
+
gripperActuatorName?: string; // Gripper actuator name. Default: 'gripper'
|
|
111
|
+
numArmJoints?: number; // Number of arm joints for IK. Default: 7
|
|
112
|
+
homeJoints?: number[]; // Initial joint positions
|
|
113
|
+
xmlPatches?: XmlPatch[]; // Patches applied to XML files during loading
|
|
114
|
+
onReset?: (model, data) => void; // Called during reset after mj_resetData
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Adding Objects to Any Scene
|
|
119
|
+
|
|
120
|
+
```tsx
|
|
121
|
+
const config: SceneConfig = {
|
|
122
|
+
robotId: 'franka_emika_panda',
|
|
123
|
+
sceneFile: 'scene.xml',
|
|
124
|
+
sceneObjects: [
|
|
125
|
+
{ name: 'ball', type: 'sphere', size: [0.03, 0.03, 0.03],
|
|
126
|
+
position: [0.5, 0, 0.1], rgba: [1, 0, 0, 1], mass: 0.1, freejoint: true },
|
|
127
|
+
{ name: 'platform', type: 'box', size: [0.2, 0.2, 0.01],
|
|
128
|
+
position: [0.4, 0.3, 0], rgba: [0.5, 0.5, 0.5, 1] },
|
|
129
|
+
],
|
|
130
|
+
};
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### XML Patching
|
|
134
|
+
|
|
135
|
+
```tsx
|
|
136
|
+
xmlPatches: [{
|
|
137
|
+
target: 'panda.xml',
|
|
138
|
+
replace: ['name="actuator8"', 'name="gripper"'],
|
|
139
|
+
inject: '<site name="tcp" pos="0 0 0.1" size="0.01"/>',
|
|
140
|
+
injectAfter: '<body name="hand"',
|
|
141
|
+
}]
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Components
|
|
145
|
+
|
|
146
|
+
### `<MujocoProvider>`
|
|
147
|
+
|
|
148
|
+
Loads the MuJoCo WASM module. Wrap your entire app in this.
|
|
149
|
+
|
|
150
|
+
| Prop | Type | Description |
|
|
151
|
+
|------|------|-------------|
|
|
152
|
+
| `wasmUrl` | `string?` | Custom WASM URL override |
|
|
153
|
+
| `onError` | `(error: Error) => void` | Called if WASM fails to load |
|
|
154
|
+
|
|
155
|
+
### `<MujocoCanvas>`
|
|
156
|
+
|
|
157
|
+
Thin wrapper around R3F `<Canvas>`. Accepts all R3F Canvas props plus:
|
|
158
|
+
|
|
159
|
+
| Prop | Type | Description |
|
|
160
|
+
|------|------|-------------|
|
|
161
|
+
| `config` | `SceneConfig` | **Required.** Scene/robot configuration |
|
|
162
|
+
| `onReady` | `(api: MujocoSimAPI) => void` | Fires when model is loaded |
|
|
163
|
+
| `onError` | `(error: Error) => void` | Fires on scene load failure |
|
|
164
|
+
| `onStep` | `(time: number) => void` | Called each physics step |
|
|
165
|
+
| `onSelection` | `(bodyId: number, name: string) => void` | Called on double-click |
|
|
166
|
+
| `gravity` | `[number, number, number]` | Override model gravity |
|
|
167
|
+
| `timestep` | `number` | Override model.opt.timestep |
|
|
168
|
+
| `substeps` | `number` | mj_step calls per frame |
|
|
169
|
+
| `paused` | `boolean` | Declarative pause |
|
|
170
|
+
| `speed` | `number` | Simulation speed multiplier |
|
|
171
|
+
| `interpolate` | `boolean` | Interpolate body transforms between physics frames |
|
|
172
|
+
| `gravityCompensation` | `boolean` | Auto-apply gravity compensation |
|
|
173
|
+
| `mjcfLights` | `boolean` | Auto-create lights from MJCF model |
|
|
174
|
+
|
|
175
|
+
### `<SceneRenderer />`
|
|
176
|
+
|
|
177
|
+
Syncs MuJoCo bodies to Three.js meshes every frame. Must be inside `<MujocoCanvas>`.
|
|
178
|
+
|
|
179
|
+
### `<IkGizmo />`
|
|
180
|
+
|
|
181
|
+
drei PivotControls gizmo that tracks a MuJoCo site and drives IK on drag.
|
|
182
|
+
|
|
183
|
+
| Prop | Type | Default | Description |
|
|
184
|
+
|------|------|---------|-------------|
|
|
185
|
+
| `siteName` | `string?` | config.tcpSiteName | MuJoCo site to track |
|
|
186
|
+
| `scale` | `number?` | `0.18` | Gizmo handle scale |
|
|
187
|
+
| `onDrag` | `(pos, quat) => void` | — | Custom drag handler (disables auto-IK) |
|
|
188
|
+
|
|
189
|
+
### `<DragInteraction />`
|
|
190
|
+
|
|
191
|
+
Click-drag to apply spring forces to bodies. Raycasts to find bodies, applies `F = (mouseWorld - grabWorld) * body_mass * stiffness` via `mj_applyFT`.
|
|
192
|
+
|
|
193
|
+
### `<ContactMarkers />`
|
|
194
|
+
|
|
195
|
+
InstancedMesh showing MuJoCo contact points for debugging.
|
|
196
|
+
|
|
197
|
+
| Prop | Type | Default | Description |
|
|
198
|
+
|------|------|---------|-------------|
|
|
199
|
+
| `maxContacts` | `number?` | `100` | Max contacts to display |
|
|
200
|
+
| `size` | `number?` | `0.005` | Marker sphere radius |
|
|
201
|
+
| `color` | `string?` | `'red'` | Marker color |
|
|
202
|
+
|
|
203
|
+
### `<SceneLights />`
|
|
204
|
+
|
|
205
|
+
Auto-creates Three.js lights from MJCF `<light>` elements.
|
|
206
|
+
|
|
207
|
+
### `<Debug />`
|
|
208
|
+
|
|
209
|
+
Visualization overlays:
|
|
210
|
+
|
|
211
|
+
| Prop | Type | Default | Description |
|
|
212
|
+
|------|------|---------|-------------|
|
|
213
|
+
| `showGeoms` | `boolean?` | `false` | Wireframe collision geoms |
|
|
214
|
+
| `showSites` | `boolean?` | `false` | Site markers |
|
|
215
|
+
| `showJoints` | `boolean?` | `false` | Joint axes |
|
|
216
|
+
| `showContacts` | `boolean?` | `false` | Contact force vectors |
|
|
217
|
+
| `showCOM` | `boolean?` | `false` | Center of mass markers |
|
|
218
|
+
|
|
219
|
+
### `<TendonRenderer />`
|
|
220
|
+
|
|
221
|
+
Renders tendons as tube geometry from wrap paths.
|
|
222
|
+
|
|
223
|
+
### `<FlexRenderer />`
|
|
224
|
+
|
|
225
|
+
Renders deformable flex bodies from `flexvert_xpos`.
|
|
226
|
+
|
|
227
|
+
### `<ContactListener />`
|
|
228
|
+
|
|
229
|
+
Component wrapper for contact events:
|
|
230
|
+
|
|
231
|
+
```tsx
|
|
232
|
+
<ContactListener
|
|
233
|
+
body="block_1"
|
|
234
|
+
onContactEnter={(info) => console.log('contact!', info)}
|
|
235
|
+
onContactExit={(info) => console.log('released', info)}
|
|
236
|
+
/>
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### `<SelectionHighlight />`
|
|
240
|
+
|
|
241
|
+
Emissive highlight on selected body meshes.
|
|
242
|
+
|
|
243
|
+
### `<TrajectoryPlayer />`
|
|
244
|
+
|
|
245
|
+
Plays back recorded qpos trajectories with scrubbing.
|
|
246
|
+
|
|
247
|
+
## Hooks
|
|
248
|
+
|
|
249
|
+
### `useMujocoSim()`
|
|
250
|
+
|
|
251
|
+
Access the simulation API and internal refs:
|
|
252
|
+
|
|
253
|
+
```tsx
|
|
254
|
+
const { api, mjModelRef, mjDataRef } = useMujocoSim();
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### `useBeforePhysicsStep(callback)`
|
|
258
|
+
|
|
259
|
+
Run logic **before** `mj_step` each frame. Write to `data.ctrl`, apply forces, drive automation.
|
|
260
|
+
|
|
261
|
+
```tsx
|
|
262
|
+
useBeforePhysicsStep((model, data) => {
|
|
263
|
+
data.ctrl[0] = Math.sin(data.time);
|
|
264
|
+
});
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### `useAfterPhysicsStep(callback)`
|
|
268
|
+
|
|
269
|
+
Run logic **after** `mj_step` each frame. Read results, compute rewards, log telemetry.
|
|
270
|
+
|
|
271
|
+
### `useSensor(name)` / `useSensors()`
|
|
272
|
+
|
|
273
|
+
Read sensor values by name (ref-based, no re-renders):
|
|
274
|
+
|
|
275
|
+
```tsx
|
|
276
|
+
const { value, size, type } = useSensor('force_sensor_1');
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### `useBodyState(name)`
|
|
280
|
+
|
|
281
|
+
Position, quaternion, linear/angular velocity of a body (ref-based):
|
|
282
|
+
|
|
283
|
+
```tsx
|
|
284
|
+
const { position, quaternion, linearVelocity, angularVelocity } = useBodyState('block_1');
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### `useJointState(name)`
|
|
288
|
+
|
|
289
|
+
Joint position and velocity:
|
|
290
|
+
|
|
291
|
+
```tsx
|
|
292
|
+
const { position, velocity } = useJointState('joint1');
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### `useCtrl(name)`
|
|
296
|
+
|
|
297
|
+
Read/write actuator control by name:
|
|
298
|
+
|
|
299
|
+
```tsx
|
|
300
|
+
const [value, setValue] = useCtrl('gripper');
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### `useContacts(bodyName?)` / `useContactEvents(bodyName, handlers)`
|
|
304
|
+
|
|
305
|
+
Query contacts or subscribe to enter/exit events:
|
|
306
|
+
|
|
307
|
+
```tsx
|
|
308
|
+
useContactEvents('block_1', {
|
|
309
|
+
onEnter: (info) => console.log('contact!', info),
|
|
310
|
+
onExit: (info) => console.log('released', info),
|
|
311
|
+
});
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### `useKeyboardTeleop(config)`
|
|
315
|
+
|
|
316
|
+
Map keyboard keys to actuators:
|
|
317
|
+
|
|
318
|
+
```tsx
|
|
319
|
+
useKeyboardTeleop({
|
|
320
|
+
bindings: {
|
|
321
|
+
'w': { actuator: 'forward', delta: 0.1 },
|
|
322
|
+
's': { actuator: 'forward', delta: -0.1 },
|
|
323
|
+
'v': { actuator: 'gripper', toggle: [0, 0.04] },
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### `useGamepad(config)`
|
|
329
|
+
|
|
330
|
+
Map gamepad axes/buttons to actuators:
|
|
331
|
+
|
|
332
|
+
```tsx
|
|
333
|
+
useGamepad({
|
|
334
|
+
axes: { 0: 'joint1', 1: 'joint2' },
|
|
335
|
+
buttons: { 0: 'gripper' },
|
|
336
|
+
deadzone: 0.1,
|
|
337
|
+
});
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
### `usePolicy(config)`
|
|
341
|
+
|
|
342
|
+
Framework-agnostic decimation loop for RL policies:
|
|
343
|
+
|
|
344
|
+
```tsx
|
|
345
|
+
const { step, isRunning } = usePolicy({
|
|
346
|
+
frequency: 50,
|
|
347
|
+
onObservation: (model, data) => buildObs(model, data),
|
|
348
|
+
onAction: (action, model, data) => applyAction(action, data),
|
|
349
|
+
});
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### `useTrajectoryRecorder(config)` / `useTrajectoryPlayer(trajectory, config)`
|
|
353
|
+
|
|
354
|
+
Record and play back simulation trajectories:
|
|
355
|
+
|
|
356
|
+
```tsx
|
|
357
|
+
const recorder = useTrajectoryRecorder({ fields: ['qpos', 'qvel', 'ctrl'] });
|
|
358
|
+
// recorder.start(), recorder.stop(), recorder.downloadJSON(), recorder.downloadCSV()
|
|
359
|
+
|
|
360
|
+
const player = useTrajectoryPlayer(trajectory, { fps: 30, loop: true });
|
|
361
|
+
// player.play(), player.pause(), player.seek(frameIdx)
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### `useVideoRecorder(config)`
|
|
365
|
+
|
|
366
|
+
Record the canvas as video:
|
|
367
|
+
|
|
368
|
+
```tsx
|
|
369
|
+
const video = useVideoRecorder({ fps: 30, mimeType: 'video/webm' });
|
|
370
|
+
// video.start(), video.stop() → returns Blob
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### `useCtrlNoise(config)`
|
|
374
|
+
|
|
375
|
+
Apply Gaussian noise to controls for robustness testing:
|
|
376
|
+
|
|
377
|
+
```tsx
|
|
378
|
+
useCtrlNoise({ rate: 0.01, std: 0.05 });
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
### `useGravityCompensation(enabled?)`
|
|
382
|
+
|
|
383
|
+
Applies `qfrc_bias` to `qfrc_applied` so joints hold position against gravity.
|
|
384
|
+
|
|
385
|
+
### `useActuators()`
|
|
386
|
+
|
|
387
|
+
Returns actuator metadata for building control UIs.
|
|
388
|
+
|
|
389
|
+
### `useSitePosition(siteName)`
|
|
390
|
+
|
|
391
|
+
Ref-based site position/quaternion tracking.
|
|
392
|
+
|
|
393
|
+
## MujocoSimAPI
|
|
394
|
+
|
|
395
|
+
The full API object returned by `onReady` and available via `useMujocoSim().api`:
|
|
396
|
+
|
|
397
|
+
### Simulation Control
|
|
398
|
+
|
|
399
|
+
| Method | Description |
|
|
400
|
+
|--------|-------------|
|
|
401
|
+
| `reset()` | Reset sim, re-apply home joints |
|
|
402
|
+
| `setPaused(paused)` | Set pause state |
|
|
403
|
+
| `togglePause()` | Toggle pause, returns new state |
|
|
404
|
+
| `setSpeed(multiplier)` | Set simulation speed |
|
|
405
|
+
| `step(n?)` | Advance exactly n steps while paused |
|
|
406
|
+
| `getTime()` | Current simulation time |
|
|
407
|
+
| `getTimestep()` | Current timestep |
|
|
408
|
+
|
|
409
|
+
### State Management
|
|
410
|
+
|
|
411
|
+
| Method | Description |
|
|
412
|
+
|--------|-------------|
|
|
413
|
+
| `saveState()` | Snapshot qpos, qvel, ctrl, time, act |
|
|
414
|
+
| `restoreState(snapshot)` | Restore from snapshot |
|
|
415
|
+
| `setQpos(values)` / `getQpos()` | Direct qpos access |
|
|
416
|
+
| `setQvel(values)` / `getQvel()` | Direct qvel access |
|
|
417
|
+
| `setCtrl(nameOrValues, value?)` | Set control by name or batch |
|
|
418
|
+
| `getCtrl(name?)` | Get control values |
|
|
419
|
+
| `applyKeyframe(nameOrIndex)` | Apply a keyframe |
|
|
420
|
+
| `getKeyframeNames()` / `getKeyframeCount()` | Keyframe introspection |
|
|
421
|
+
|
|
422
|
+
### Forces
|
|
423
|
+
|
|
424
|
+
| Method | Description |
|
|
425
|
+
|--------|-------------|
|
|
426
|
+
| `applyForce(bodyName, force, point?)` | Apply force via `mj_applyFT` |
|
|
427
|
+
| `applyTorque(bodyName, torque)` | Apply torque via `mj_applyFT` |
|
|
428
|
+
| `setExternalForce(bodyName, force, torque)` | Write to `xfrc_applied` |
|
|
429
|
+
| `applyGeneralizedForce(values)` | Write to `qfrc_applied` |
|
|
430
|
+
|
|
431
|
+
### Model Introspection
|
|
432
|
+
|
|
433
|
+
| Method | Description |
|
|
434
|
+
|--------|-------------|
|
|
435
|
+
| `getBodies()` | All bodies with id, name, mass, parentId |
|
|
436
|
+
| `getJoints()` | All joints with id, name, type, range, bodyId |
|
|
437
|
+
| `getGeoms()` | All geoms with id, name, type, size, bodyId |
|
|
438
|
+
| `getSites()` | All sites with id, name, bodyId |
|
|
439
|
+
| `getActuators()` | All actuators with id, name, range |
|
|
440
|
+
| `getSensors()` | All sensors with id, name, type, dim |
|
|
441
|
+
| `getSensorData(name)` | Read sensor value by name |
|
|
442
|
+
| `getContacts()` | All active contacts |
|
|
443
|
+
| `getModelOption()` | Timestep, gravity, integrator |
|
|
444
|
+
|
|
445
|
+
### Model Mutation
|
|
446
|
+
|
|
447
|
+
| Method | Description |
|
|
448
|
+
|--------|-------------|
|
|
449
|
+
| `setGravity(g)` | Set gravity vector |
|
|
450
|
+
| `setTimestep(dt)` | Set timestep |
|
|
451
|
+
| `setBodyMass(name, mass)` | Domain randomization |
|
|
452
|
+
| `setGeomFriction(name, friction)` | Domain randomization |
|
|
453
|
+
| `setGeomSize(name, size)` | Domain randomization |
|
|
454
|
+
|
|
455
|
+
### Spatial Queries
|
|
456
|
+
|
|
457
|
+
| Method | Description |
|
|
458
|
+
|--------|-------------|
|
|
459
|
+
| `raycast(origin, direction, maxDist?)` | Physics raycast via `mj_ray` |
|
|
460
|
+
| `project2DTo3D(x, y, camPos, lookAt)` | Screen-to-world raycast (returns bodyId + geomId) |
|
|
461
|
+
| `getCameraState()` | Camera position and orbit target |
|
|
462
|
+
| `moveCameraTo(pos, target, ms)` | Smooth camera animation |
|
|
463
|
+
|
|
464
|
+
### IK Control
|
|
465
|
+
|
|
466
|
+
| Method | Description |
|
|
467
|
+
|--------|-------------|
|
|
468
|
+
| `setIkEnabled(enabled)` | Enable/disable IK tracking |
|
|
469
|
+
| `moveTarget(pos, duration?)` | Move IK target with optional animation |
|
|
470
|
+
| `syncTargetToSite()` | Snap IK target to current TCP position |
|
|
471
|
+
| `solveIK(pos, quat, currentQ)` | Solve IK for a target pose |
|
|
472
|
+
|
|
473
|
+
### Scene Management
|
|
474
|
+
|
|
475
|
+
| Method | Description |
|
|
476
|
+
|--------|-------------|
|
|
477
|
+
| `loadScene(newConfig)` | Runtime model swap |
|
|
478
|
+
| `getCanvasSnapshot(w?, h?, mime?)` | Base64 screenshot |
|
|
479
|
+
|
|
480
|
+
## useFrame Priority
|
|
481
|
+
|
|
482
|
+
| Priority | Owner | Purpose |
|
|
483
|
+
|----------|-------|---------|
|
|
484
|
+
| -1 | MujocoSimProvider | beforeStep, IK, mj_step, afterStep |
|
|
485
|
+
| 0 (default) | SceneRenderer, your code | Body mesh sync, rendering |
|
|
486
|
+
|
|
487
|
+
## Roadmap
|
|
488
|
+
|
|
489
|
+
Features planned but not yet implemented:
|
|
490
|
+
|
|
491
|
+
| Feature | Priority | Description |
|
|
492
|
+
|---------|----------|-------------|
|
|
493
|
+
| **User-uploaded model loading** | P2 | `loadFromFiles(FileList)` — detect meshdir, write to VFS |
|
|
494
|
+
| **URDF loading** | P2 | Load URDF models via MuJoCo's built-in URDF compiler |
|
|
495
|
+
| **XML mutation / recompile** | P1 | `addBody()`, `removeBody()`, `recompile()` for runtime XML editing |
|
|
496
|
+
| **Observation builder utilities** | P2 | Helpers for projected gravity, joint positions/velocities for RL |
|
|
497
|
+
| **Physics interpolation** | P1 | Smooth rendering between physics ticks for 120Hz+ displays |
|
|
498
|
+
| **Instanced geom rendering** | P2 | `<InstancedGeomRenderer />` for particle/granular sims |
|
|
499
|
+
| **Web Worker physics** | P2 | Run `mj_step` off main thread via SharedArrayBuffer |
|
|
500
|
+
|
|
501
|
+
### WASM Limitations (mujoco-js 0.0.7)
|
|
502
|
+
|
|
503
|
+
These MuJoCo features are not yet exposed in the WASM binding:
|
|
504
|
+
|
|
505
|
+
- `flex_faceadr` / `flex_facenum` / `flex_face` — FlexRenderer renders vertices without face indices
|
|
506
|
+
- `ten_rgba` / `ten_width` — TendonRenderer uses default color/width
|
|
507
|
+
|
|
508
|
+
## License
|
|
509
|
+
|
|
510
|
+
Apache-2.0
|