mujoco-react 2.0.0 → 4.0.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/README.md +36 -53
- package/dist/index.d.ts +5 -5
- package/dist/index.js +32 -32
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/ContactMarkers.tsx +2 -2
- package/src/components/Debug.tsx +2 -2
- package/src/components/DragInteraction.tsx +2 -2
- package/src/components/FlexRenderer.tsx +2 -2
- package/src/components/IkController.tsx +2 -2
- package/src/components/IkGizmo.tsx +2 -2
- package/src/components/SceneRenderer.tsx +2 -2
- package/src/components/TendonRenderer.tsx +2 -2
- package/src/core/MujocoCanvas.tsx +2 -2
- package/src/core/MujocoPhysics.tsx +2 -2
- package/src/core/MujocoProvider.tsx +1 -1
- package/src/core/MujocoSimProvider.tsx +4 -4
- package/src/core/SceneLoader.ts +1 -1
- package/src/core/createController.tsx +1 -1
- package/src/hooks/useActuators.ts +2 -2
- package/src/hooks/useBodyState.ts +2 -2
- package/src/hooks/useContacts.ts +2 -2
- package/src/hooks/useCtrl.ts +2 -2
- package/src/hooks/useCtrlNoise.ts +2 -2
- package/src/hooks/useGamepad.ts +2 -2
- package/src/hooks/useJointState.ts +2 -2
- package/src/hooks/useKeyboardTeleop.ts +2 -2
- package/src/hooks/usePolicy.ts +2 -2
- package/src/hooks/useSceneLights.ts +2 -2
- package/src/hooks/useSensor.ts +3 -3
- package/src/hooks/useSitePosition.ts +2 -2
- package/src/hooks/useTrajectoryPlayer.ts +2 -2
- package/src/hooks/useTrajectoryRecorder.ts +2 -2
- package/src/index.ts +2 -2
- package/src/types.ts +1 -1
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@ import { useRef } from 'react';
|
|
|
12
12
|
import { useFrame } from '@react-three/fiber';
|
|
13
13
|
import type { ThreeElements } from '@react-three/fiber';
|
|
14
14
|
import * as THREE from 'three';
|
|
15
|
-
import {
|
|
15
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
16
16
|
import { getContact } from '../types';
|
|
17
17
|
|
|
18
18
|
const _dummy = new THREE.Object3D();
|
|
@@ -35,7 +35,7 @@ export function ContactMarkers({
|
|
|
35
35
|
visible = true,
|
|
36
36
|
...groupProps
|
|
37
37
|
}: ContactMarkersProps & Omit<ThreeElements['group'], 'ref' | 'visible'> = {}) {
|
|
38
|
-
const { mjDataRef, status } =
|
|
38
|
+
const { mjDataRef, status } = useMujoco();
|
|
39
39
|
const meshRef = useRef<THREE.InstancedMesh>(null);
|
|
40
40
|
|
|
41
41
|
useFrame(() => {
|
package/src/components/Debug.tsx
CHANGED
|
@@ -9,7 +9,7 @@ import { useEffect, useMemo, useRef } from 'react';
|
|
|
9
9
|
import { useFrame, useThree } from '@react-three/fiber';
|
|
10
10
|
import type { ThreeElements } from '@react-three/fiber';
|
|
11
11
|
import * as THREE from 'three';
|
|
12
|
-
import {
|
|
12
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
13
13
|
import { getName } from '../core/SceneLoader';
|
|
14
14
|
import { getContact } from '../types';
|
|
15
15
|
import type { DebugProps } from '../types';
|
|
@@ -43,7 +43,7 @@ export function Debug({
|
|
|
43
43
|
showTendons = false,
|
|
44
44
|
...groupProps
|
|
45
45
|
}: DebugProps & Omit<ThreeElements['group'], 'ref'>) {
|
|
46
|
-
const { mjModelRef, mjDataRef, status } =
|
|
46
|
+
const { mjModelRef, mjDataRef, status } = useMujoco();
|
|
47
47
|
const { scene } = useThree();
|
|
48
48
|
const groupRef = useRef<THREE.Group>(null);
|
|
49
49
|
|
|
@@ -7,7 +7,7 @@ import { useFrame, useThree } from '@react-three/fiber';
|
|
|
7
7
|
import type { ThreeElements } from '@react-three/fiber';
|
|
8
8
|
import { useEffect, useRef } from 'react';
|
|
9
9
|
import * as THREE from 'three';
|
|
10
|
-
import {
|
|
10
|
+
import { useMujoco, useBeforePhysicsStep } from '../core/MujocoSimProvider';
|
|
11
11
|
import type { DragInteractionProps } from '../types';
|
|
12
12
|
|
|
13
13
|
// Preallocated temps to avoid GC pressure
|
|
@@ -38,7 +38,7 @@ export function DragInteraction({
|
|
|
38
38
|
showArrow = true,
|
|
39
39
|
...groupProps
|
|
40
40
|
}: DragInteractionProps & Omit<ThreeElements['group'], 'ref'>) {
|
|
41
|
-
const { mjDataRef, mujocoRef, mjModelRef, status } =
|
|
41
|
+
const { mjDataRef, mujocoRef, mjModelRef, status } = useMujoco();
|
|
42
42
|
const { gl, camera, scene, controls } = useThree();
|
|
43
43
|
|
|
44
44
|
const draggingRef = useRef(false);
|
|
@@ -9,14 +9,14 @@ import { useEffect, useRef } from 'react';
|
|
|
9
9
|
import { useFrame } from '@react-three/fiber';
|
|
10
10
|
import type { ThreeElements } from '@react-three/fiber';
|
|
11
11
|
import * as THREE from 'three';
|
|
12
|
-
import {
|
|
12
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Renders MuJoCo flex (deformable) bodies as dynamic meshes.
|
|
16
16
|
* Vertices are updated every frame from flexvert_xpos.
|
|
17
17
|
*/
|
|
18
18
|
export function FlexRenderer(props: Omit<ThreeElements['group'], 'ref'>) {
|
|
19
|
-
const { mjModelRef, mjDataRef, status } =
|
|
19
|
+
const { mjModelRef, mjDataRef, status } = useMujoco();
|
|
20
20
|
const groupRef = useRef<THREE.Group>(null);
|
|
21
21
|
const meshesRef = useRef<THREE.Mesh[]>([]);
|
|
22
22
|
|
|
@@ -11,7 +11,7 @@ import { useFrame } from '@react-three/fiber';
|
|
|
11
11
|
import * as THREE from 'three';
|
|
12
12
|
import { createController } from '../core/createController';
|
|
13
13
|
import { IkContext, type IkContextValue } from '../core/IkContext';
|
|
14
|
-
import {
|
|
14
|
+
import { useMujoco, useBeforePhysicsStep } from '../core/MujocoSimProvider';
|
|
15
15
|
import { GenericIK } from '../core/GenericIK';
|
|
16
16
|
import { findSiteByName } from '../core/SceneLoader';
|
|
17
17
|
import type { IkConfig, IKSolveFn, MujocoData } from '../types';
|
|
@@ -41,7 +41,7 @@ function IkControllerImpl({
|
|
|
41
41
|
children?: React.ReactNode;
|
|
42
42
|
}) {
|
|
43
43
|
const { mjModelRef, mjDataRef, mujocoRef, configRef, resetCallbacks, status } =
|
|
44
|
-
|
|
44
|
+
useMujoco();
|
|
45
45
|
|
|
46
46
|
// All IK state lives here, NOT in the provider
|
|
47
47
|
const ikEnabledRef = useRef(false);
|
|
@@ -7,7 +7,7 @@ import { PivotControls } from '@react-three/drei';
|
|
|
7
7
|
import { useFrame, useThree } from '@react-three/fiber';
|
|
8
8
|
import { useEffect, useRef } from 'react';
|
|
9
9
|
import * as THREE from 'three';
|
|
10
|
-
import {
|
|
10
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
11
11
|
import { useIk } from '../core/IkContext';
|
|
12
12
|
import { findSiteByName } from '../core/SceneLoader';
|
|
13
13
|
import type { IkGizmoProps } from '../types';
|
|
@@ -31,7 +31,7 @@ const _scale = new THREE.Vector3(1, 1, 1);
|
|
|
31
31
|
* When provided, the consumer handles what happens during drag.
|
|
32
32
|
*/
|
|
33
33
|
export function IkGizmo({ siteName, scale = 0.18, onDrag }: IkGizmoProps) {
|
|
34
|
-
const { mjModelRef, mjDataRef, status } =
|
|
34
|
+
const { mjModelRef, mjDataRef, status } = useMujoco();
|
|
35
35
|
const { ikTargetRef, siteIdRef, ikEnabledRef, setIkEnabled } = useIk();
|
|
36
36
|
|
|
37
37
|
const wrapperRef = useRef<THREE.Group>(null);
|
|
@@ -10,14 +10,14 @@ import * as THREE from 'three';
|
|
|
10
10
|
import { GeomBuilder } from '../rendering/GeomBuilder';
|
|
11
11
|
import { MujocoModel } from '../types';
|
|
12
12
|
import { getName } from '../core/SceneLoader';
|
|
13
|
-
import {
|
|
13
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* SceneRenderer — creates and syncs MuJoCo body meshes every frame.
|
|
17
17
|
* Accepts standard R3F group props (position, rotation, scale, visible, etc.).
|
|
18
18
|
*/
|
|
19
19
|
export function SceneRenderer(props: Omit<ThreeElements['group'], 'ref'>) {
|
|
20
|
-
const { mjModelRef, mjDataRef, mujocoRef, onSelectionRef, status } =
|
|
20
|
+
const { mjModelRef, mjDataRef, mujocoRef, onSelectionRef, status } = useMujoco();
|
|
21
21
|
const groupRef = useRef<THREE.Group>(null);
|
|
22
22
|
const bodyRefs = useRef<(THREE.Group | null)[]>([]);
|
|
23
23
|
const prevModelRef = useRef<MujocoModel | null>(null);
|
|
@@ -15,7 +15,7 @@ import { useEffect, useRef } from 'react';
|
|
|
15
15
|
import { useFrame } from '@react-three/fiber';
|
|
16
16
|
import type { ThreeElements } from '@react-three/fiber';
|
|
17
17
|
import * as THREE from 'three';
|
|
18
|
-
import {
|
|
18
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
19
19
|
|
|
20
20
|
const DEFAULT_TENDON_COLOR = new THREE.Color(0.3, 0.3, 0.8);
|
|
21
21
|
const DEFAULT_TENDON_WIDTH = 0.002;
|
|
@@ -24,7 +24,7 @@ const DEFAULT_TENDON_WIDTH = 0.002;
|
|
|
24
24
|
const _tmpVec = new THREE.Vector3();
|
|
25
25
|
|
|
26
26
|
export function TendonRenderer(props: Omit<ThreeElements['group'], 'ref'>) {
|
|
27
|
-
const { mjModelRef, mjDataRef, status } =
|
|
27
|
+
const { mjModelRef, mjDataRef, status } = useMujoco();
|
|
28
28
|
const groupRef = useRef<THREE.Group>(null);
|
|
29
29
|
const meshesRef = useRef<THREE.Mesh[]>([]);
|
|
30
30
|
const curvesRef = useRef<THREE.CatmullRomCurve3[]>([]);
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import { Canvas } from '@react-three/fiber';
|
|
7
7
|
import { forwardRef, useEffect } from 'react';
|
|
8
|
-
import {
|
|
8
|
+
import { useMujocoWasm } from './MujocoProvider';
|
|
9
9
|
import { MujocoSimProvider } from './MujocoSimProvider';
|
|
10
10
|
import { MujocoCanvasProps, MujocoSimAPI } from '../types';
|
|
11
11
|
|
|
@@ -35,7 +35,7 @@ export const MujocoCanvas = forwardRef<MujocoSimAPI, MujocoCanvasProps>(
|
|
|
35
35
|
},
|
|
36
36
|
ref
|
|
37
37
|
) {
|
|
38
|
-
const { mujoco, status: wasmStatus, error: wasmError } =
|
|
38
|
+
const { mujoco, status: wasmStatus, error: wasmError } = useMujocoWasm();
|
|
39
39
|
|
|
40
40
|
useEffect(() => {
|
|
41
41
|
if (wasmStatus === 'error' && onError) {
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { forwardRef, useEffect } from 'react';
|
|
7
|
-
import {
|
|
7
|
+
import { useMujocoWasm } from './MujocoProvider';
|
|
8
8
|
import { MujocoSimProvider } from './MujocoSimProvider';
|
|
9
9
|
import type { MujocoSimAPI, SceneConfig } from '../types';
|
|
10
10
|
|
|
@@ -53,7 +53,7 @@ export interface MujocoPhysicsProps {
|
|
|
53
53
|
*/
|
|
54
54
|
export const MujocoPhysics = forwardRef<MujocoSimAPI, MujocoPhysicsProps>(
|
|
55
55
|
function MujocoPhysics({ onError, children, ...props }, ref) {
|
|
56
|
-
const { mujoco, status: wasmStatus, error: wasmError } =
|
|
56
|
+
const { mujoco, status: wasmStatus, error: wasmError } = useMujocoWasm();
|
|
57
57
|
|
|
58
58
|
useEffect(() => {
|
|
59
59
|
if (wasmStatus === 'error' && onError) {
|
|
@@ -16,7 +16,7 @@ const MujocoContext = createContext<MujocoContextValue>({
|
|
|
16
16
|
/**
|
|
17
17
|
* Hook to access the MuJoCo WASM module.
|
|
18
18
|
*/
|
|
19
|
-
export function
|
|
19
|
+
export function useMujocoWasm(): MujocoContextValue {
|
|
20
20
|
return useContext(MujocoContext);
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -96,15 +96,15 @@ export interface MujocoSimContextValue {
|
|
|
96
96
|
|
|
97
97
|
const MujocoSimContext = createContext<MujocoSimContextValue | null>(null);
|
|
98
98
|
|
|
99
|
-
export function
|
|
99
|
+
export function useMujoco(): MujocoSimContextValue {
|
|
100
100
|
const ctx = useContext(MujocoSimContext);
|
|
101
101
|
if (!ctx)
|
|
102
|
-
throw new Error('
|
|
102
|
+
throw new Error('useMujoco must be used inside <MujocoSimProvider>');
|
|
103
103
|
return ctx;
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
export function useBeforePhysicsStep(callback: PhysicsStepCallback) {
|
|
107
|
-
const { beforeStepCallbacks } =
|
|
107
|
+
const { beforeStepCallbacks } = useMujoco();
|
|
108
108
|
const callbackRef = useRef(callback);
|
|
109
109
|
callbackRef.current = callback;
|
|
110
110
|
|
|
@@ -116,7 +116,7 @@ export function useBeforePhysicsStep(callback: PhysicsStepCallback) {
|
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
export function useAfterPhysicsStep(callback: PhysicsStepCallback) {
|
|
119
|
-
const { afterStepCallbacks } =
|
|
119
|
+
const { afterStepCallbacks } = useMujoco();
|
|
120
120
|
const callbackRef = useRef(callback);
|
|
121
121
|
callbackRef.current = callback;
|
|
122
122
|
|
package/src/core/SceneLoader.ts
CHANGED
|
@@ -156,7 +156,7 @@ export async function loadScene(
|
|
|
156
156
|
|
|
157
157
|
const baseUrl =
|
|
158
158
|
config.baseUrl ||
|
|
159
|
-
`https://raw.githubusercontent.com/google-deepmind/mujoco_menagerie/main/${config.
|
|
159
|
+
`https://raw.githubusercontent.com/google-deepmind/mujoco_menagerie/main/${config.modelId}/`;
|
|
160
160
|
|
|
161
161
|
const downloaded = new Set<string>();
|
|
162
162
|
const queue: string[] = [config.sceneFile];
|
|
@@ -37,7 +37,7 @@ export type ControllerComponent<TConfig> = React.FC<{
|
|
|
37
37
|
* Factory that produces a typed controller component.
|
|
38
38
|
*
|
|
39
39
|
* Controllers are React components that plug into the MuJoCo simulation tree.
|
|
40
|
-
* Inside `Impl`, use any hooks (`
|
|
40
|
+
* Inside `Impl`, use any hooks (`useMujoco`, `useBeforePhysicsStep`, etc.)
|
|
41
41
|
* to interact with the physics engine.
|
|
42
42
|
*
|
|
43
43
|
* @example
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { useMemo } from 'react';
|
|
7
|
-
import {
|
|
7
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
8
8
|
import { getName } from '../core/SceneLoader';
|
|
9
9
|
import type { ActuatorInfo } from '../types';
|
|
10
10
|
|
|
@@ -13,7 +13,7 @@ import type { ActuatorInfo } from '../types';
|
|
|
13
13
|
* Computed once when the model loads. Consumer reads/writes data.ctrl[id] directly.
|
|
14
14
|
*/
|
|
15
15
|
export function useActuators(): ActuatorInfo[] {
|
|
16
|
-
const { mjModelRef, status } =
|
|
16
|
+
const { mjModelRef, status } = useMujoco();
|
|
17
17
|
|
|
18
18
|
return useMemo(() => {
|
|
19
19
|
if (status !== 'ready') return [];
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import { useEffect, useRef } from 'react';
|
|
9
9
|
import * as THREE from 'three';
|
|
10
|
-
import {
|
|
10
|
+
import { useMujoco, useAfterPhysicsStep } from '../core/MujocoSimProvider';
|
|
11
11
|
import { findBodyByName } from '../core/SceneLoader';
|
|
12
12
|
import type { BodyStateResult } from '../types';
|
|
13
13
|
|
|
@@ -16,7 +16,7 @@ import type { BodyStateResult } from '../types';
|
|
|
16
16
|
* All values are ref-based — updated every physics frame without re-renders.
|
|
17
17
|
*/
|
|
18
18
|
export function useBodyState(name: string): BodyStateResult {
|
|
19
|
-
const { mjModelRef, status } =
|
|
19
|
+
const { mjModelRef, status } = useMujoco();
|
|
20
20
|
const bodyIdRef = useRef(-1);
|
|
21
21
|
const position = useRef(new THREE.Vector3());
|
|
22
22
|
const quaternion = useRef(new THREE.Quaternion());
|
package/src/hooks/useContacts.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { useCallback, useEffect, useRef } from 'react';
|
|
10
|
-
import {
|
|
10
|
+
import { useMujoco, useAfterPhysicsStep } from '../core/MujocoSimProvider';
|
|
11
11
|
import { findBodyByName, getName } from '../core/SceneLoader';
|
|
12
12
|
import { getContact } from '../types';
|
|
13
13
|
import type { ContactInfo, MujocoModel } from '../types';
|
|
@@ -39,7 +39,7 @@ export function useContacts(
|
|
|
39
39
|
bodyName?: string,
|
|
40
40
|
callback?: (contacts: ContactInfo[]) => void,
|
|
41
41
|
): React.RefObject<ContactInfo[]> {
|
|
42
|
-
const { mjModelRef, status } =
|
|
42
|
+
const { mjModelRef, status } = useMujoco();
|
|
43
43
|
const contactsRef = useRef<ContactInfo[]>([]);
|
|
44
44
|
const bodyIdRef = useRef(-1);
|
|
45
45
|
const bodyResolvedRef = useRef(false);
|
package/src/hooks/useCtrl.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { useCallback, useEffect, useRef } from 'react';
|
|
9
|
-
import {
|
|
9
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
10
10
|
import { findActuatorByName } from '../core/SceneLoader';
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -17,7 +17,7 @@ import { findActuatorByName } from '../core/SceneLoader';
|
|
|
17
17
|
* - `setValue` writes directly to `data.ctrl[actuatorId]`.
|
|
18
18
|
*/
|
|
19
19
|
export function useCtrl(name: string): [React.RefObject<number>, (value: number) => void] {
|
|
20
|
-
const { mjModelRef, mjDataRef, status } =
|
|
20
|
+
const { mjModelRef, mjDataRef, status } = useMujoco();
|
|
21
21
|
const actuatorIdRef = useRef(-1);
|
|
22
22
|
const valueRef = useRef(0);
|
|
23
23
|
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { useRef } from 'react';
|
|
9
|
-
import {
|
|
9
|
+
import { useMujoco, useBeforePhysicsStep } from '../core/MujocoSimProvider';
|
|
10
10
|
|
|
11
11
|
interface CtrlNoiseConfig {
|
|
12
12
|
/** Exponential filter rate (0-1). Higher = faster noise changes. Default: 0.01. */
|
|
@@ -25,7 +25,7 @@ interface CtrlNoiseConfig {
|
|
|
25
25
|
* data.ctrl[i] += noise[i]
|
|
26
26
|
*/
|
|
27
27
|
export function useCtrlNoise(config: CtrlNoiseConfig = {}) {
|
|
28
|
-
const { mjModelRef } =
|
|
28
|
+
const { mjModelRef } = useMujoco();
|
|
29
29
|
const configRef = useRef(config);
|
|
30
30
|
configRef.current = config;
|
|
31
31
|
const noiseRef = useRef<Float64Array | null>(null);
|
package/src/hooks/useGamepad.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { useEffect, useRef } from 'react';
|
|
9
|
-
import {
|
|
9
|
+
import { useMujoco, useBeforePhysicsStep } from '../core/MujocoSimProvider';
|
|
10
10
|
import { findActuatorByName } from '../core/SceneLoader';
|
|
11
11
|
|
|
12
12
|
interface GamepadConfig {
|
|
@@ -29,7 +29,7 @@ interface GamepadConfig {
|
|
|
29
29
|
* Buttons map their 0..1 pressed value to the actuator.
|
|
30
30
|
*/
|
|
31
31
|
export function useGamepad(config: GamepadConfig) {
|
|
32
|
-
const { mjModelRef, status } =
|
|
32
|
+
const { mjModelRef, status } = useMujoco();
|
|
33
33
|
const configRef = useRef(config);
|
|
34
34
|
configRef.current = config;
|
|
35
35
|
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { useEffect, useRef } from 'react';
|
|
9
|
-
import {
|
|
9
|
+
import { useMujoco, useAfterPhysicsStep } from '../core/MujocoSimProvider';
|
|
10
10
|
import { getName } from '../core/SceneLoader';
|
|
11
11
|
import type { JointStateResult } from '../types';
|
|
12
12
|
|
|
@@ -19,7 +19,7 @@ import type { JointStateResult } from '../types';
|
|
|
19
19
|
* For free joints, position is pos+quat (7), velocity is lin+ang vel (6).
|
|
20
20
|
*/
|
|
21
21
|
export function useJointState(name: string): JointStateResult {
|
|
22
|
-
const { mjModelRef, mjDataRef, status } =
|
|
22
|
+
const { mjModelRef, mjDataRef, status } = useMujoco();
|
|
23
23
|
const jointIdRef = useRef(-1);
|
|
24
24
|
const qposAdrRef = useRef(0);
|
|
25
25
|
const dofAdrRef = useRef(0);
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { useEffect, useRef } from 'react';
|
|
9
|
-
import {
|
|
9
|
+
import { useMujoco, useBeforePhysicsStep } from '../core/MujocoSimProvider';
|
|
10
10
|
import { findActuatorByName } from '../core/SceneLoader';
|
|
11
11
|
import type { KeyboardTeleopConfig } from '../types';
|
|
12
12
|
|
|
@@ -19,7 +19,7 @@ import type { KeyboardTeleopConfig } from '../types';
|
|
|
19
19
|
* - `set`: Set actuator to a fixed value while key is held
|
|
20
20
|
*/
|
|
21
21
|
export function useKeyboardTeleop(config: KeyboardTeleopConfig) {
|
|
22
|
-
const { mjModelRef, mjDataRef, status } =
|
|
22
|
+
const { mjModelRef, mjDataRef, status } = useMujoco();
|
|
23
23
|
const pressedRef = useRef(new Set<string>());
|
|
24
24
|
const toggleStateRef = useRef(new Map<string, boolean>());
|
|
25
25
|
const enabledRef = useRef(config.enabled ?? true);
|
package/src/hooks/usePolicy.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { useRef } from 'react';
|
|
9
|
-
import {
|
|
9
|
+
import { useMujoco, useBeforePhysicsStep } from '../core/MujocoSimProvider';
|
|
10
10
|
import type { PolicyConfig } from '../types';
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -20,7 +20,7 @@ import type { PolicyConfig } from '../types';
|
|
|
20
20
|
* @returns { step, isRunning } control handles
|
|
21
21
|
*/
|
|
22
22
|
export function usePolicy(config: PolicyConfig) {
|
|
23
|
-
const { mjModelRef } =
|
|
23
|
+
const { mjModelRef } = useMujoco();
|
|
24
24
|
const lastActionTimeRef = useRef(0);
|
|
25
25
|
const lastActionRef = useRef<Float32Array | Float64Array | number[] | null>(null);
|
|
26
26
|
const isRunningRef = useRef(true);
|
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
import { useEffect, useRef } from 'react';
|
|
11
11
|
import * as THREE from 'three';
|
|
12
12
|
import { useThree } from '@react-three/fiber';
|
|
13
|
-
import {
|
|
13
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
14
14
|
|
|
15
15
|
export function useSceneLights(intensity = 1.0) {
|
|
16
|
-
const { mjModelRef, status } =
|
|
16
|
+
const { mjModelRef, status } = useMujoco();
|
|
17
17
|
const { scene } = useThree();
|
|
18
18
|
const lightsRef = useRef<THREE.Light[]>([]);
|
|
19
19
|
const targetsRef = useRef<THREE.Object3D[]>([]);
|
package/src/hooks/useSensor.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { useEffect, useRef, useMemo } from 'react';
|
|
9
|
-
import {
|
|
9
|
+
import { useMujoco, useAfterPhysicsStep } from '../core/MujocoSimProvider';
|
|
10
10
|
import { getName } from '../core/SceneLoader';
|
|
11
11
|
import type { SensorInfo, SensorResult } from '../types';
|
|
12
12
|
|
|
@@ -15,7 +15,7 @@ import type { SensorInfo, SensorResult } from '../types';
|
|
|
15
15
|
* updated every physics frame without causing React re-renders.
|
|
16
16
|
*/
|
|
17
17
|
export function useSensor(name: string): SensorResult {
|
|
18
|
-
const { mjModelRef, mjDataRef, status } =
|
|
18
|
+
const { mjModelRef, mjDataRef, status } = useMujoco();
|
|
19
19
|
const sensorIdRef = useRef(-1);
|
|
20
20
|
const sensorAdrRef = useRef(0);
|
|
21
21
|
const sensorDimRef = useRef(0);
|
|
@@ -55,7 +55,7 @@ export function useSensor(name: string): SensorResult {
|
|
|
55
55
|
* Returns a stable array recomputed only when the model changes.
|
|
56
56
|
*/
|
|
57
57
|
export function useSensors(): SensorInfo[] {
|
|
58
|
-
const { mjModelRef, status } =
|
|
58
|
+
const { mjModelRef, status } = useMujoco();
|
|
59
59
|
|
|
60
60
|
return useMemo(() => {
|
|
61
61
|
const model = mjModelRef.current;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { useEffect, useRef } from 'react';
|
|
7
7
|
import { useFrame } from '@react-three/fiber';
|
|
8
8
|
import * as THREE from 'three';
|
|
9
|
-
import {
|
|
9
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
10
10
|
import { findSiteByName } from '../core/SceneLoader';
|
|
11
11
|
import type { SitePositionResult } from '../types';
|
|
12
12
|
|
|
@@ -18,7 +18,7 @@ const _mat4 = new THREE.Matrix4();
|
|
|
18
18
|
* Refs are updated every frame without triggering React re-renders.
|
|
19
19
|
*/
|
|
20
20
|
export function useSitePosition(siteName: string): SitePositionResult {
|
|
21
|
-
const { mjModelRef, mjDataRef, status } =
|
|
21
|
+
const { mjModelRef, mjDataRef, status } = useMujoco();
|
|
22
22
|
const siteIdRef = useRef(-1);
|
|
23
23
|
const positionRef = useRef(new THREE.Vector3());
|
|
24
24
|
const quaternionRef = useRef(new THREE.Quaternion());
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import { useCallback, useRef } from 'react';
|
|
9
9
|
import { useFrame } from '@react-three/fiber';
|
|
10
|
-
import {
|
|
10
|
+
import { useMujoco } from '../core/MujocoSimProvider';
|
|
11
11
|
|
|
12
12
|
interface TrajectoryPlayerOptions {
|
|
13
13
|
fps?: number;
|
|
@@ -24,7 +24,7 @@ export function useTrajectoryPlayer(
|
|
|
24
24
|
trajectory: number[][],
|
|
25
25
|
options: TrajectoryPlayerOptions = {},
|
|
26
26
|
) {
|
|
27
|
-
const { mjModelRef, mjDataRef, mujocoRef, pausedRef } =
|
|
27
|
+
const { mjModelRef, mjDataRef, mujocoRef, pausedRef } = useMujoco();
|
|
28
28
|
const fps = options.fps ?? 30;
|
|
29
29
|
const loop = options.loop ?? false;
|
|
30
30
|
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { useCallback, useRef } from 'react';
|
|
9
|
-
import {
|
|
9
|
+
import { useMujoco, useAfterPhysicsStep } from '../core/MujocoSimProvider';
|
|
10
10
|
import type { TrajectoryFrame } from '../types';
|
|
11
11
|
|
|
12
12
|
interface RecorderOptions {
|
|
@@ -17,7 +17,7 @@ interface RecorderOptions {
|
|
|
17
17
|
* Record simulation trajectories for analysis, replay, or training data.
|
|
18
18
|
*/
|
|
19
19
|
export function useTrajectoryRecorder(options: RecorderOptions = {}) {
|
|
20
|
-
const { mjModelRef } =
|
|
20
|
+
const { mjModelRef } = useMujoco();
|
|
21
21
|
const recordingRef = useRef(false);
|
|
22
22
|
const framesRef = useRef<TrajectoryFrame[]>([]);
|
|
23
23
|
const fields = options.fields ?? ['qpos'];
|
package/src/index.ts
CHANGED
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
// Core
|
|
7
|
-
export { MujocoProvider,
|
|
7
|
+
export { MujocoProvider, useMujocoWasm } from './core/MujocoProvider';
|
|
8
8
|
export { MujocoCanvas } from './core/MujocoCanvas';
|
|
9
9
|
export { MujocoPhysics } from './core/MujocoPhysics';
|
|
10
10
|
export type { MujocoPhysicsProps } from './core/MujocoPhysics';
|
|
11
|
-
export { MujocoSimProvider,
|
|
11
|
+
export { MujocoSimProvider, useMujoco, useBeforePhysicsStep, useAfterPhysicsStep } from './core/MujocoSimProvider';
|
|
12
12
|
export {
|
|
13
13
|
loadScene,
|
|
14
14
|
getName,
|