mujoco-react 10.2.0 → 10.3.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.
@@ -37,6 +37,7 @@ import {
37
37
  LoadFromFilesOptions,
38
38
  LocalMujocoFile,
39
39
  ModelOptions,
40
+ MujocoRenderOptions,
40
41
  MujocoSimAPI,
41
42
  PhysicsStepCallback,
42
43
  RayHit,
@@ -241,6 +242,143 @@ function applyMountedCameraPoseOffsets(
241
242
  };
242
243
  }
243
244
 
245
+ function resolveMujocoCameraCompatibilityOptions(
246
+ options: CameraFrameCaptureOptions
247
+ ) {
248
+ const compatibility = options.mujocoCameraCompatibility;
249
+ if (!compatibility) return null;
250
+ if (compatibility === true) {
251
+ return {
252
+ useResolution: true,
253
+ useIntrinsics: true,
254
+ useClipping: true,
255
+ preserveAspect: true,
256
+ preferResolution: false,
257
+ };
258
+ }
259
+ return {
260
+ useResolution: compatibility.useResolution ?? true,
261
+ useIntrinsics: compatibility.useIntrinsics ?? true,
262
+ useClipping: compatibility.useClipping ?? true,
263
+ preserveAspect: compatibility.preserveAspect ?? true,
264
+ preferResolution: compatibility.preferResolution ?? false,
265
+ };
266
+ }
267
+
268
+ function mujocoVisualClip(model: MujocoModel) {
269
+ const map = (model as unknown as {
270
+ vis?: { map?: { znear?: number; zfar?: number } };
271
+ }).vis?.map;
272
+ const near = typeof map?.znear === 'number' && map.znear > 0
273
+ ? map.znear
274
+ : undefined;
275
+ const far = typeof map?.zfar === 'number' && map.zfar > 0
276
+ ? map.zfar
277
+ : undefined;
278
+ return { near, far };
279
+ }
280
+
281
+ function mujocoCameraResolution(
282
+ model: MujocoModel,
283
+ cameraId: number
284
+ ): { width?: number; height?: number } {
285
+ const resolution = model.cam_resolution;
286
+ if (!resolution) return {};
287
+ const width = Number(resolution[cameraId * 2]);
288
+ const height = Number(resolution[cameraId * 2 + 1]);
289
+ return {
290
+ width: Number.isFinite(width) && width > 0 ? width : undefined,
291
+ height: Number.isFinite(height) && height > 0 ? height : undefined,
292
+ };
293
+ }
294
+
295
+ function mujocoCameraProjectionMatrix(
296
+ model: MujocoModel,
297
+ cameraId: number,
298
+ width: number | undefined,
299
+ height: number | undefined,
300
+ near: number | undefined,
301
+ far: number | undefined
302
+ ): THREE.Matrix4 | undefined {
303
+ const intrinsic = model.cam_intrinsic;
304
+ const sensorSize = model.cam_sensorsize;
305
+ if (!intrinsic || !sensorSize || !width || !height) return undefined;
306
+
307
+ const intrinsicOffset = cameraId * 4;
308
+ const sensorOffset = cameraId * 2;
309
+ const focalX = Number(intrinsic[intrinsicOffset]);
310
+ const focalY = Number(intrinsic[intrinsicOffset + 1]);
311
+ const principalX = Number(intrinsic[intrinsicOffset + 2]);
312
+ const principalY = Number(intrinsic[intrinsicOffset + 3]);
313
+ const sensorWidth = Number(sensorSize[sensorOffset]);
314
+ const sensorHeight = Number(sensorSize[sensorOffset + 1]);
315
+ if (
316
+ !Number.isFinite(focalX) ||
317
+ !Number.isFinite(focalY) ||
318
+ !Number.isFinite(principalX) ||
319
+ !Number.isFinite(principalY) ||
320
+ !Number.isFinite(sensorWidth) ||
321
+ !Number.isFinite(sensorHeight) ||
322
+ focalX <= 0 ||
323
+ focalY <= 0 ||
324
+ sensorWidth <= 0 ||
325
+ sensorHeight <= 0
326
+ ) {
327
+ return undefined;
328
+ }
329
+
330
+ const fx = focalX / sensorWidth * width;
331
+ const fy = focalY / sensorHeight * height;
332
+ const cx = width * (0.5 + principalX / sensorWidth);
333
+ const cy = height * (0.5 + principalY / sensorHeight);
334
+ const znear = near ?? 0.01;
335
+ const zfar = far ?? 100;
336
+
337
+ return new THREE.Matrix4().set(
338
+ 2 * fx / width, 0, 1 - 2 * cx / width, 0,
339
+ 0, 2 * fy / height, 2 * cy / height - 1, 0,
340
+ 0, 0, -(zfar + znear) / (zfar - znear), -2 * zfar * znear / (zfar - znear),
341
+ 0, 0, -1, 0
342
+ );
343
+ }
344
+
345
+ function resolveMujocoCameraCaptureDimensions(
346
+ requested: CameraFrameCaptureOptions,
347
+ cameraResolution: { width?: number; height?: number },
348
+ compatibility: NonNullable<ReturnType<typeof resolveMujocoCameraCompatibilityOptions>>
349
+ ) {
350
+ if (!compatibility.useResolution) {
351
+ return {
352
+ width: requested.width,
353
+ height: requested.height,
354
+ };
355
+ }
356
+
357
+ if (compatibility.preferResolution) {
358
+ return {
359
+ width: cameraResolution.width ?? requested.width,
360
+ height: cameraResolution.height ?? requested.height,
361
+ };
362
+ }
363
+
364
+ let width = requested.width ?? cameraResolution.width;
365
+ let height = requested.height ?? cameraResolution.height;
366
+
367
+ if (
368
+ compatibility.preserveAspect &&
369
+ cameraResolution.width &&
370
+ cameraResolution.height
371
+ ) {
372
+ if (requested.width !== undefined && requested.height === undefined) {
373
+ height = requested.width * cameraResolution.height / cameraResolution.width;
374
+ } else if (requested.height !== undefined && requested.width === undefined) {
375
+ width = requested.height * cameraResolution.width / cameraResolution.height;
376
+ }
377
+ }
378
+
379
+ return { width, height };
380
+ }
381
+
244
382
  function countMountedCameraSelectors(options: CameraFrameCaptureOptions) {
245
383
  return Number(Boolean(options.cameraName)) +
246
384
  Number(Boolean(options.siteName)) +
@@ -406,6 +544,7 @@ interface MujocoSimProviderProps {
406
544
  paused?: boolean;
407
545
  speed?: number;
408
546
  interpolate?: boolean;
547
+ renderOptions?: MujocoRenderOptions;
409
548
  children: React.ReactNode;
410
549
  }
411
550
 
@@ -423,6 +562,7 @@ export function MujocoSimProvider({
423
562
  paused,
424
563
  speed,
425
564
  interpolate,
565
+ renderOptions,
426
566
  children,
427
567
  }: MujocoSimProviderProps) {
428
568
  const { gl, camera, scene } = useThree();
@@ -462,14 +602,12 @@ export function MujocoSimProvider({
462
602
  const hiddenBodiesRef = useRef(new Set<string>());
463
603
  const bodyReloadTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
464
604
 
465
- useEffect(() => { configRef.current = config; }, [config]);
466
- useEffect(() => { mujocoRef.current = mujoco; }, [mujoco]);
467
-
468
- // Sync declarative props to refs
469
- useEffect(() => { pausedRef.current = paused ?? false; }, [paused]);
470
- useEffect(() => { speedRef.current = speed ?? 1; }, [speed]);
471
- useEffect(() => { substepsRef.current = substeps ?? 1; }, [substeps]);
472
- useEffect(() => { interpolateRef.current = interpolate ?? false; }, [interpolate]);
605
+ configRef.current = config;
606
+ mujocoRef.current = mujoco;
607
+ pausedRef.current = paused ?? false;
608
+ speedRef.current = speed ?? 1;
609
+ substepsRef.current = substeps ?? 1;
610
+ interpolateRef.current = interpolate ?? false;
473
611
 
474
612
  // Sync gravity prop
475
613
  useEffect(() => {
@@ -1037,11 +1175,33 @@ export function MujocoSimProvider({
1037
1175
  for (let i = 0; i < ncam; i += 1) {
1038
1176
  const posOffset = i * 3;
1039
1177
  const quatOffset = i * 4;
1178
+ const intrinsicOffset = i * 4;
1179
+ const resolutionOffset = i * 2;
1040
1180
  result.push({
1041
1181
  id: i,
1042
1182
  name: getName(model, nameAddresses[i]),
1043
1183
  bodyId: model.cam_bodyid?.[i] ?? -1,
1044
1184
  fov: model.cam_fovy?.[i] ?? null,
1185
+ resolution: model.cam_resolution
1186
+ ? [
1187
+ model.cam_resolution[resolutionOffset],
1188
+ model.cam_resolution[resolutionOffset + 1],
1189
+ ]
1190
+ : null,
1191
+ sensorSize: model.cam_sensorsize
1192
+ ? [
1193
+ model.cam_sensorsize[resolutionOffset],
1194
+ model.cam_sensorsize[resolutionOffset + 1],
1195
+ ]
1196
+ : null,
1197
+ intrinsic: model.cam_intrinsic
1198
+ ? [
1199
+ model.cam_intrinsic[intrinsicOffset],
1200
+ model.cam_intrinsic[intrinsicOffset + 1],
1201
+ model.cam_intrinsic[intrinsicOffset + 2],
1202
+ model.cam_intrinsic[intrinsicOffset + 3],
1203
+ ]
1204
+ : null,
1045
1205
  position: model.cam_pos
1046
1206
  ? vector3FromArray(model.cam_pos, posOffset)
1047
1207
  : null,
@@ -1087,11 +1247,31 @@ export function MujocoSimProvider({
1087
1247
  }
1088
1248
 
1089
1249
  const pose = applyMountedCameraPoseOffsets(options, position, quaternion);
1250
+ const compatibility = resolveMujocoCameraCompatibilityOptions(options);
1251
+ const cameraResolution = compatibility?.useResolution
1252
+ ? mujocoCameraResolution(model, cameraId)
1253
+ : { width: undefined, height: undefined };
1254
+ const clip = compatibility?.useClipping
1255
+ ? mujocoVisualClip(model)
1256
+ : { near: undefined, far: undefined };
1257
+ const { width, height } = compatibility
1258
+ ? resolveMujocoCameraCaptureDimensions(options, cameraResolution, compatibility)
1259
+ : { width: options.width, height: options.height };
1260
+ const near = options.near ?? clip.near;
1261
+ const far = options.far ?? clip.far;
1262
+ const projectionMatrix = compatibility?.useIntrinsics
1263
+ ? mujocoCameraProjectionMatrix(model, cameraId, width, height, near, far)
1264
+ : undefined;
1090
1265
 
1091
1266
  return {
1092
1267
  ...baseOptions,
1268
+ width,
1269
+ height,
1093
1270
  ...pose,
1094
1271
  fov: options.fov ?? model.cam_fovy?.[cameraId],
1272
+ near,
1273
+ far,
1274
+ projectionMatrix: options.projectionMatrix ?? projectionMatrix,
1095
1275
  source: { kind: 'mujoco-camera', cameraName: options.cameraName },
1096
1276
  };
1097
1277
  }
@@ -1759,7 +1939,7 @@ export function MujocoSimProvider({
1759
1939
 
1760
1940
  return (
1761
1941
  <MujocoSimContext.Provider value={contextValue}>
1762
- <SceneRenderer />
1942
+ <SceneRenderer renderOptions={renderOptions} />
1763
1943
  {children}
1764
1944
  </MujocoSimContext.Provider>
1765
1945
  );
@@ -111,7 +111,10 @@ export function usePolicy(config: PolicyConfig): PolicyAPI {
111
111
 
112
112
  // Build observation
113
113
  const observation = cfg.onObservation({ model, data });
114
- const result = cfg.infer ? cfg.infer({ observation, model, data }) : observation;
114
+ const queuedActions = actionQueueRef.current.length;
115
+ const result = cfg.infer
116
+ ? cfg.infer({ observation, model, data, queuedActions })
117
+ : observation;
115
118
 
116
119
  if (isPromiseLike(result)) {
117
120
  const epoch = epochRef.current;
@@ -163,7 +163,7 @@ export function useRemotePolicy(config: RemotePolicyConfig): RemotePolicyAPI {
163
163
 
164
164
  const policy = usePolicy({
165
165
  ...config,
166
- infer: async ({ observation, model, data }) => {
166
+ infer: async ({ observation, model, data, queuedActions }) => {
167
167
  const cfg = configRef.current;
168
168
  abortController(abortControllerRef.current, createAbortError('Remote policy request was superseded.'));
169
169
  const controller = new AbortController();
@@ -175,6 +175,7 @@ export function useRemotePolicy(config: RemotePolicyConfig): RemotePolicyAPI {
175
175
  observation,
176
176
  model,
177
177
  data,
178
+ queuedActions,
178
179
  reset: requestIndex === 0,
179
180
  requestIndex,
180
181
  signal,
@@ -5,9 +5,12 @@
5
5
 
6
6
 
7
7
  import * as THREE from 'three';
8
+ import { mergeVertices } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
8
9
  import { CapsuleGeometry } from './CapsuleGeometry';
9
10
  import { getName } from '../core/SceneLoader';
10
- import { MujocoModel, MujocoModule } from '../types';
11
+ import { MujocoModel, MujocoModule, MujocoRenderOptions } from '../types';
12
+
13
+ const DEFAULT_MESH_NORMAL_SMOOTHING_TOLERANCE = 1e-4;
11
14
 
12
15
  /**
13
16
  * GeomBuilder
@@ -20,9 +23,18 @@ import { MujocoModel, MujocoModule } from '../types';
20
23
  export class GeomBuilder {
21
24
  private mujoco: MujocoModule;
22
25
  private textureCache = new Map<number, THREE.Texture>();
26
+ private renderOptions?: MujocoRenderOptions;
23
27
 
24
- constructor(mujoco: MujocoModule) {
28
+ constructor(mujoco: MujocoModule, renderOptions?: MujocoRenderOptions) {
25
29
  this.mujoco = mujoco;
30
+ this.renderOptions = renderOptions;
31
+ }
32
+
33
+ private getMeshNormalSmoothingTolerance(): number | null {
34
+ const smoothing = this.renderOptions?.meshNormalSmoothing;
35
+ if (!smoothing) return null;
36
+ if (smoothing === true) return DEFAULT_MESH_NORMAL_SMOOTHING_TOLERANCE;
37
+ return smoothing.tolerance ?? DEFAULT_MESH_NORMAL_SMOOTHING_TOLERANCE;
26
38
  }
27
39
 
28
40
  private getMaterialTexture(mjModel: MujocoModel, matId: number): THREE.Texture | null {
@@ -148,6 +160,10 @@ export class GeomBuilder {
148
160
  geo.setAttribute('position', new THREE.Float32BufferAttribute(mjModel.mesh_vert.subarray(vAdr * 3, (vAdr + vNum) * 3), 3));
149
161
  // 'index' = faces (triangles connecting vertices)
150
162
  geo.setIndex(Array.from(mjModel.mesh_face.subarray(fAdr * 3, (fAdr + fNum) * 3)));
163
+ const smoothingTolerance = this.getMeshNormalSmoothingTolerance();
164
+ if (smoothingTolerance !== null) {
165
+ geo = mergeVertices(geo, smoothingTolerance);
166
+ }
151
167
  geo.computeVertexNormals(); // Auto-calculate smooth lighting normals
152
168
  }
153
169