mujoco-react 8.2.0 → 8.2.1
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/README.md +13 -38
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
# mujoco-react
|
|
6
6
|
|
|
7
|
-
> **Beta** — This library is under active development. The API may change between minor versions until
|
|
7
|
+
> **Beta** — This library is under active development. The API may change between minor versions until 10.0.
|
|
8
8
|
|
|
9
9
|
Composable [React Three Fiber](https://docs.pmnd.rs/react-three-fiber) wrapper around [mujoco-js](https://www.npmjs.com/package/mujoco-js). Load any MuJoCo model, step physics, render bodies, and write controllers as React components.
|
|
10
10
|
|
|
@@ -87,64 +87,37 @@ function MyComponent() {
|
|
|
87
87
|
|
|
88
88
|
## Writing a Controller
|
|
89
89
|
|
|
90
|
-
A controller is a
|
|
90
|
+
A controller is a hook that reads sensors and writes actuators each physics step:
|
|
91
91
|
|
|
92
92
|
```tsx
|
|
93
93
|
import { useCtrl, useSensor, useBeforePhysicsStep } from "mujoco-react";
|
|
94
94
|
|
|
95
|
-
function
|
|
95
|
+
function useMyController(gain: number) {
|
|
96
96
|
const shoulder = useCtrl("shoulder");
|
|
97
97
|
const elbow = useCtrl("elbow");
|
|
98
98
|
const force = useSensor("force_sensor");
|
|
99
99
|
|
|
100
100
|
useBeforePhysicsStep(() => {
|
|
101
|
-
shoulder.write(Math.sin(Date.now() / 1000));
|
|
101
|
+
shoulder.write(gain * Math.sin(Date.now() / 1000));
|
|
102
102
|
elbow.write(force.read()[0] * -0.5);
|
|
103
103
|
});
|
|
104
|
-
return null;
|
|
105
104
|
}
|
|
106
105
|
```
|
|
107
106
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
```tsx
|
|
111
|
-
<MujocoCanvas config={config}>
|
|
112
|
-
<MyController />
|
|
113
|
-
</MujocoCanvas>
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
The `createControllerHook` factory produces a typed hook with config stabilization and default merging. Pass `null` to disable:
|
|
107
|
+
For reusable controllers with typed config, default merging, and children, use the `createController` factory:
|
|
117
108
|
|
|
118
109
|
```tsx
|
|
119
|
-
import {
|
|
120
|
-
|
|
121
|
-
export const useMyController = createControllerHook<{ gain: number }, { value: number }>(
|
|
122
|
-
{ name: "useMyController", defaultConfig: { gain: 1.0 } },
|
|
123
|
-
(config) => {
|
|
124
|
-
useBeforePhysicsStep((_model, data) => {
|
|
125
|
-
if (!config) return;
|
|
126
|
-
data.ctrl[0] = config.gain * Math.sin(data.time);
|
|
127
|
-
});
|
|
128
|
-
if (!config) return null;
|
|
129
|
-
return { value: config.gain };
|
|
130
|
-
},
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
// const result = useMyController({ gain: 2.0 });
|
|
134
|
-
// const disabled = useMyController(null); // returns null
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
The `createController` factory is the component equivalent — same config stabilization, but returns a component that can render children:
|
|
138
|
-
|
|
139
|
-
```tsx
|
|
140
|
-
import { createController, useBeforePhysicsStep, Debug } from "mujoco-react";
|
|
110
|
+
import { createController, useCtrl, useBeforePhysicsStep } from "mujoco-react";
|
|
141
111
|
|
|
142
112
|
export const MyController = createController<{ gain: number }>(
|
|
143
113
|
{ name: "MyController", defaultConfig: { gain: 1.0 } },
|
|
144
114
|
({ config, children }) => {
|
|
145
|
-
|
|
146
|
-
|
|
115
|
+
const shoulder = useCtrl("shoulder");
|
|
116
|
+
|
|
117
|
+
useBeforePhysicsStep(() => {
|
|
118
|
+
shoulder.write(config.gain * Math.sin(Date.now() / 1000));
|
|
147
119
|
});
|
|
120
|
+
|
|
148
121
|
return <>{children}</>;
|
|
149
122
|
},
|
|
150
123
|
);
|
|
@@ -154,6 +127,8 @@ export const MyController = createController<{ gain: number }>(
|
|
|
154
127
|
// </MyController>
|
|
155
128
|
```
|
|
156
129
|
|
|
130
|
+
A `createControllerHook` factory is also available for the hook equivalent — see the [Building Controllers](https://dadd.mintlify.app/guides/building-controllers) guide.
|
|
131
|
+
|
|
157
132
|
## Architecture
|
|
158
133
|
|
|
159
134
|
`<MujocoCanvas>` wraps R3F `<Canvas>` and forwards all Canvas props (`camera`, `shadows`, `gl`, etc.). For full control over the Canvas, use `<MujocoPhysics>` inside your own:
|