@react-three/rapier 0.5.0 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @react-three/rapier
2
2
 
3
+ ## 0.5.1
4
+
5
+ ### Patch Changes
6
+
7
+ - c36be39: Add MeshCollider, allowing more fine control over automatic collider creation
8
+
3
9
  ## 0.5.0
4
10
 
5
11
  ### Minor Changes
@@ -128,7 +128,7 @@ export interface WorldApi {
128
128
  getCollider(handle: number): Collider | undefined;
129
129
  getRigidBody(handle: number): RigidBody | undefined;
130
130
  createRigidBody(desc: RigidBodyDesc): RigidBody;
131
- createCollider(desc: ColliderDesc, rigidBody: RigidBody): Collider;
131
+ createCollider(desc: ColliderDesc, parent?: RigidBody): Collider;
132
132
  removeRigidBody(rigidBody: RigidBody): void;
133
133
  removeCollider(collider: Collider): void;
134
134
  createImpulseJoint(params: JointData, rigidBodyA: RigidBody, rigidBodyB: RigidBody): ImpulseJoint;
@@ -1,10 +1,15 @@
1
1
  import React from "react";
2
2
  import { ReactNode } from "react";
3
- import { BallArgs, CapsuleArgs, ConeArgs, ConvexHullArgs, CuboidArgs, CylinderArgs, HeightfieldArgs, RoundCuboidArgs, TrimeshArgs, UseColliderOptions, UseRigidBodyOptions } from "./types";
3
+ import { BallArgs, CapsuleArgs, ConeArgs, ConvexHullArgs, CuboidArgs, CylinderArgs, HeightfieldArgs, RigidBodyAutoCollider, RoundCuboidArgs, TrimeshArgs, UseColliderOptions, UseRigidBodyOptions } from "./types";
4
4
  interface RigidBodyProps extends UseRigidBodyOptions {
5
5
  children?: ReactNode;
6
6
  }
7
7
  export declare const RigidBody: React.ForwardRefExoticComponent<RigidBodyProps & React.RefAttributes<import("./api").RigidBodyApi>>;
8
+ interface MeshColliderProps {
9
+ children: ReactNode;
10
+ type: RigidBodyAutoCollider;
11
+ }
12
+ export declare const MeshCollider: ({ children, type }: MeshColliderProps) => JSX.Element;
8
13
  declare type UseColliderOptionsRequiredArgs<T> = Omit<UseColliderOptions<T>, "args"> & {
9
14
  args: T;
10
15
  children?: ReactNode;
@@ -1,6 +1,6 @@
1
1
  import { Collider, RigidBody } from "@dimforge/rapier3d-compat";
2
2
  import { Object3D, Quaternion, Vector3 } from "three";
3
- import { RigidBodyShape, RigidBodyTypeString, UseColliderOptions, UseRigidBodyOptions, Vector3Array, WorldApi } from "./types";
3
+ import { RigidBodyApi, RigidBodyShape, RigidBodyTypeString, UseColliderOptions, UseRigidBodyOptions, Vector3Array, WorldApi } from "./types";
4
4
  export declare const vectorArrayToObject: (arr: Vector3Array) => {
5
5
  x: number;
6
6
  y: number;
@@ -8,11 +8,11 @@ export declare const vectorArrayToObject: (arr: Vector3Array) => {
8
8
  };
9
9
  export declare const rigidBodyTypeFromString: (type: RigidBodyTypeString) => number;
10
10
  export declare const scaleColliderArgs: (shape: RigidBodyShape, args: (number | ArrayLike<number>)[], scale: Vector3) => (number | ArrayLike<number>)[];
11
- export declare const createColliderFromOptions: <A>(options: UseColliderOptions<A>, world: WorldApi, rigidBody: RigidBody, scale?: {
11
+ export declare const createColliderFromOptions: <A>(options: UseColliderOptions<A>, world: WorldApi, rigidBody?: RigidBody | undefined, scale?: {
12
12
  x: number;
13
13
  y: number;
14
14
  z: number;
15
15
  }, hasCollisionEvents?: boolean) => Collider;
16
- export declare const createCollidersFromChildren: (object: Object3D, rigidBody: RigidBody, options: UseRigidBodyOptions, world: WorldApi) => Collider[];
16
+ export declare const createCollidersFromChildren: (object: Object3D, rigidBody: RigidBodyApi, options: UseRigidBodyOptions, world: WorldApi, ignoreMeshColliders?: boolean) => Collider[];
17
17
  export declare const scaleVertices: (vertices: ArrayLike<number>, scale: Vector3) => number[];
18
18
  export declare const vector3ToQuaternion: (v: Vector3) => Quaternion;
@@ -113,13 +113,23 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
113
113
  const collider = world.createCollider(colliderDesc, rigidBody);
114
114
  return collider;
115
115
  };
116
- const createCollidersFromChildren = (object, rigidBody, options, world) => {
116
+
117
+ const isChildOfMeshCollider = child => {
118
+ let flag = false;
119
+ child.traverseAncestors(a => {
120
+ if (a.userData.r3RapierType === "MeshCollider") flag = true;
121
+ });
122
+ return flag;
123
+ };
124
+
125
+ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
117
126
  const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
118
127
  const colliders = [];
119
128
  let desc;
120
129
  let offset = new three.Vector3();
121
130
  object.traverse(child => {
122
131
  if ("isMesh" in child) {
132
+ if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
123
133
  const {
124
134
  geometry
125
135
  } = child;
@@ -190,7 +200,8 @@ const createCollidersFromChildren = (object, rigidBody, options, world) => {
190
200
  if (hasCollisionEvents) desc.setActiveEvents(rapier3dCompat.ActiveEvents.COLLISION_EVENTS);
191
201
  if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
192
202
  if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
193
- const collider = world.createCollider(desc, rigidBody);
203
+ const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
204
+ const collider = world.createCollider(desc, actualRigidBody);
194
205
  colliders.push(collider);
195
206
  }
196
207
  });
@@ -213,47 +224,6 @@ const vector3ToQuaternion = v => {
213
224
  return quaternion.setFromEuler(euler.setFromVector3(v));
214
225
  };
215
226
 
216
- function _defineProperty(obj, key, value) {
217
- if (key in obj) {
218
- Object.defineProperty(obj, key, {
219
- value: value,
220
- enumerable: true,
221
- configurable: true,
222
- writable: true
223
- });
224
- } else {
225
- obj[key] = value;
226
- }
227
-
228
- return obj;
229
- }
230
-
231
- function ownKeys(object, enumerableOnly) {
232
- var keys = Object.keys(object);
233
-
234
- if (Object.getOwnPropertySymbols) {
235
- var symbols = Object.getOwnPropertySymbols(object);
236
- enumerableOnly && (symbols = symbols.filter(function (sym) {
237
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
238
- })), keys.push.apply(keys, symbols);
239
- }
240
-
241
- return keys;
242
- }
243
-
244
- function _objectSpread2(target) {
245
- for (var i = 1; i < arguments.length; i++) {
246
- var source = null != arguments[i] ? arguments[i] : {};
247
- i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
248
- _defineProperty(target, key, source[key]);
249
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
250
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
251
- });
252
- }
253
-
254
- return target;
255
- }
256
-
257
227
  const createRigidBodyApi = ref => {
258
228
  return {
259
229
  raw: () => ref.current(),
@@ -298,9 +268,19 @@ const createRigidBodyApi = ref => {
298
268
  return new three.Quaternion(x, y, z, w);
299
269
  },
300
270
 
301
- setRotation: rotation => ref.current().setRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
302
- w: 1
303
- }), true),
271
+ setRotation: ({
272
+ x,
273
+ y,
274
+ z
275
+ }) => {
276
+ const q = vector3ToQuaternion(new three.Vector3(x, y, z));
277
+ ref.current().setRotation({
278
+ x: q.x,
279
+ y: q.y,
280
+ z: q.z,
281
+ w: q.w
282
+ }, true);
283
+ },
304
284
 
305
285
  linvel() {
306
286
  const {
@@ -566,6 +546,47 @@ const Physics = ({
566
546
  }, children);
567
547
  };
568
548
 
549
+ function _defineProperty(obj, key, value) {
550
+ if (key in obj) {
551
+ Object.defineProperty(obj, key, {
552
+ value: value,
553
+ enumerable: true,
554
+ configurable: true,
555
+ writable: true
556
+ });
557
+ } else {
558
+ obj[key] = value;
559
+ }
560
+
561
+ return obj;
562
+ }
563
+
564
+ function ownKeys(object, enumerableOnly) {
565
+ var keys = Object.keys(object);
566
+
567
+ if (Object.getOwnPropertySymbols) {
568
+ var symbols = Object.getOwnPropertySymbols(object);
569
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
570
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
571
+ })), keys.push.apply(keys, symbols);
572
+ }
573
+
574
+ return keys;
575
+ }
576
+
577
+ function _objectSpread2(target) {
578
+ for (var i = 1; i < arguments.length; i++) {
579
+ var source = null != arguments[i] ? arguments[i] : {};
580
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
581
+ _defineProperty(target, key, source[key]);
582
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
583
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
584
+ });
585
+ }
586
+
587
+ return target;
588
+ }
589
+
569
590
  const useRapier = () => {
570
591
  return React.useContext(RapierContext);
571
592
  };
@@ -852,17 +873,52 @@ const RigidBody = /*#__PURE__*/React.forwardRef((_ref, ref) => {
852
873
  const [object, rigidBody] = useRigidBody(props);
853
874
  React.useImperativeHandle(ref, () => rigidBody);
854
875
  return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
855
- value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit)]
876
+ value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit), props]
856
877
  }, /*#__PURE__*/React__default["default"].createElement("object3D", {
857
878
  ref: object
858
879
  }, children));
859
- }); // Colliders
880
+ });
881
+ const MeshCollider = ({
882
+ children,
883
+ type
884
+ }) => {
885
+ const {
886
+ physicsOptions,
887
+ world
888
+ } = useRapier();
889
+ const object = React.useRef(null);
890
+ const [, rigidBody, hasCollisionEvents, rigidBodyOptions] = useRigidBodyContext();
891
+ React.useEffect(() => {
892
+ let autoColliders = [];
893
+
894
+ if (object.current) {
895
+ var _ref2;
896
+
897
+ const colliderSetting = (_ref2 = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref2 !== void 0 ? _ref2 : false;
898
+ autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, rigidBody, _objectSpread2(_objectSpread2({}, rigidBodyOptions), {}, {
899
+ colliders: colliderSetting
900
+ }), world, false) : [];
901
+ }
902
+
903
+ return () => {
904
+ autoColliders.forEach(collider => {
905
+ world.removeCollider(collider);
906
+ });
907
+ };
908
+ }, []);
909
+ return /*#__PURE__*/React__default["default"].createElement("object3D", {
910
+ ref: object,
911
+ userData: {
912
+ r3RapierType: 'MeshCollider'
913
+ }
914
+ }, children);
915
+ }; // Colliders
860
916
 
861
- const AnyCollider = _ref2 => {
917
+ const AnyCollider = _ref3 => {
862
918
  let {
863
919
  children
864
- } = _ref2,
865
- props = _objectWithoutProperties(_ref2, _excluded2);
920
+ } = _ref3,
921
+ props = _objectWithoutProperties(_ref3, _excluded2);
866
922
 
867
923
  const {
868
924
  world
@@ -1062,6 +1118,7 @@ exports.CuboidCollider = CuboidCollider;
1062
1118
  exports.CylinderCollider = CylinderCollider;
1063
1119
  exports.Debug = Debug;
1064
1120
  exports.HeightfieldCollider = HeightfieldCollider;
1121
+ exports.MeshCollider = MeshCollider;
1065
1122
  exports.Physics = Physics;
1066
1123
  exports.RigidBody = RigidBody;
1067
1124
  exports.RoundCuboidCollider = RoundCuboidCollider;
@@ -113,13 +113,23 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
113
113
  const collider = world.createCollider(colliderDesc, rigidBody);
114
114
  return collider;
115
115
  };
116
- const createCollidersFromChildren = (object, rigidBody, options, world) => {
116
+
117
+ const isChildOfMeshCollider = child => {
118
+ let flag = false;
119
+ child.traverseAncestors(a => {
120
+ if (a.userData.r3RapierType === "MeshCollider") flag = true;
121
+ });
122
+ return flag;
123
+ };
124
+
125
+ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
117
126
  const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
118
127
  const colliders = [];
119
128
  let desc;
120
129
  let offset = new three.Vector3();
121
130
  object.traverse(child => {
122
131
  if ("isMesh" in child) {
132
+ if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
123
133
  const {
124
134
  geometry
125
135
  } = child;
@@ -190,7 +200,8 @@ const createCollidersFromChildren = (object, rigidBody, options, world) => {
190
200
  if (hasCollisionEvents) desc.setActiveEvents(rapier3dCompat.ActiveEvents.COLLISION_EVENTS);
191
201
  if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
192
202
  if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
193
- const collider = world.createCollider(desc, rigidBody);
203
+ const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
204
+ const collider = world.createCollider(desc, actualRigidBody);
194
205
  colliders.push(collider);
195
206
  }
196
207
  });
@@ -213,47 +224,6 @@ const vector3ToQuaternion = v => {
213
224
  return quaternion.setFromEuler(euler.setFromVector3(v));
214
225
  };
215
226
 
216
- function _defineProperty(obj, key, value) {
217
- if (key in obj) {
218
- Object.defineProperty(obj, key, {
219
- value: value,
220
- enumerable: true,
221
- configurable: true,
222
- writable: true
223
- });
224
- } else {
225
- obj[key] = value;
226
- }
227
-
228
- return obj;
229
- }
230
-
231
- function ownKeys(object, enumerableOnly) {
232
- var keys = Object.keys(object);
233
-
234
- if (Object.getOwnPropertySymbols) {
235
- var symbols = Object.getOwnPropertySymbols(object);
236
- enumerableOnly && (symbols = symbols.filter(function (sym) {
237
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
238
- })), keys.push.apply(keys, symbols);
239
- }
240
-
241
- return keys;
242
- }
243
-
244
- function _objectSpread2(target) {
245
- for (var i = 1; i < arguments.length; i++) {
246
- var source = null != arguments[i] ? arguments[i] : {};
247
- i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
248
- _defineProperty(target, key, source[key]);
249
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
250
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
251
- });
252
- }
253
-
254
- return target;
255
- }
256
-
257
227
  const createRigidBodyApi = ref => {
258
228
  return {
259
229
  raw: () => ref.current(),
@@ -298,9 +268,19 @@ const createRigidBodyApi = ref => {
298
268
  return new three.Quaternion(x, y, z, w);
299
269
  },
300
270
 
301
- setRotation: rotation => ref.current().setRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
302
- w: 1
303
- }), true),
271
+ setRotation: ({
272
+ x,
273
+ y,
274
+ z
275
+ }) => {
276
+ const q = vector3ToQuaternion(new three.Vector3(x, y, z));
277
+ ref.current().setRotation({
278
+ x: q.x,
279
+ y: q.y,
280
+ z: q.z,
281
+ w: q.w
282
+ }, true);
283
+ },
304
284
 
305
285
  linvel() {
306
286
  const {
@@ -566,6 +546,47 @@ const Physics = ({
566
546
  }, children);
567
547
  };
568
548
 
549
+ function _defineProperty(obj, key, value) {
550
+ if (key in obj) {
551
+ Object.defineProperty(obj, key, {
552
+ value: value,
553
+ enumerable: true,
554
+ configurable: true,
555
+ writable: true
556
+ });
557
+ } else {
558
+ obj[key] = value;
559
+ }
560
+
561
+ return obj;
562
+ }
563
+
564
+ function ownKeys(object, enumerableOnly) {
565
+ var keys = Object.keys(object);
566
+
567
+ if (Object.getOwnPropertySymbols) {
568
+ var symbols = Object.getOwnPropertySymbols(object);
569
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
570
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
571
+ })), keys.push.apply(keys, symbols);
572
+ }
573
+
574
+ return keys;
575
+ }
576
+
577
+ function _objectSpread2(target) {
578
+ for (var i = 1; i < arguments.length; i++) {
579
+ var source = null != arguments[i] ? arguments[i] : {};
580
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
581
+ _defineProperty(target, key, source[key]);
582
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
583
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
584
+ });
585
+ }
586
+
587
+ return target;
588
+ }
589
+
569
590
  const useRapier = () => {
570
591
  return React.useContext(RapierContext);
571
592
  };
@@ -852,17 +873,52 @@ const RigidBody = /*#__PURE__*/React.forwardRef((_ref, ref) => {
852
873
  const [object, rigidBody] = useRigidBody(props);
853
874
  React.useImperativeHandle(ref, () => rigidBody);
854
875
  return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
855
- value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit)]
876
+ value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit), props]
856
877
  }, /*#__PURE__*/React__default["default"].createElement("object3D", {
857
878
  ref: object
858
879
  }, children));
859
- }); // Colliders
880
+ });
881
+ const MeshCollider = ({
882
+ children,
883
+ type
884
+ }) => {
885
+ const {
886
+ physicsOptions,
887
+ world
888
+ } = useRapier();
889
+ const object = React.useRef(null);
890
+ const [, rigidBody, hasCollisionEvents, rigidBodyOptions] = useRigidBodyContext();
891
+ React.useEffect(() => {
892
+ let autoColliders = [];
893
+
894
+ if (object.current) {
895
+ var _ref2;
896
+
897
+ const colliderSetting = (_ref2 = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref2 !== void 0 ? _ref2 : false;
898
+ autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, rigidBody, _objectSpread2(_objectSpread2({}, rigidBodyOptions), {}, {
899
+ colliders: colliderSetting
900
+ }), world, false) : [];
901
+ }
902
+
903
+ return () => {
904
+ autoColliders.forEach(collider => {
905
+ world.removeCollider(collider);
906
+ });
907
+ };
908
+ }, []);
909
+ return /*#__PURE__*/React__default["default"].createElement("object3D", {
910
+ ref: object,
911
+ userData: {
912
+ r3RapierType: 'MeshCollider'
913
+ }
914
+ }, children);
915
+ }; // Colliders
860
916
 
861
- const AnyCollider = _ref2 => {
917
+ const AnyCollider = _ref3 => {
862
918
  let {
863
919
  children
864
- } = _ref2,
865
- props = _objectWithoutProperties(_ref2, _excluded2);
920
+ } = _ref3,
921
+ props = _objectWithoutProperties(_ref3, _excluded2);
866
922
 
867
923
  const {
868
924
  world
@@ -1062,6 +1118,7 @@ exports.CuboidCollider = CuboidCollider;
1062
1118
  exports.CylinderCollider = CylinderCollider;
1063
1119
  exports.Debug = Debug;
1064
1120
  exports.HeightfieldCollider = HeightfieldCollider;
1121
+ exports.MeshCollider = MeshCollider;
1065
1122
  exports.Physics = Physics;
1066
1123
  exports.RigidBody = RigidBody;
1067
1124
  exports.RoundCuboidCollider = RoundCuboidCollider;
@@ -88,13 +88,23 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
88
88
  const collider = world.createCollider(colliderDesc, rigidBody);
89
89
  return collider;
90
90
  };
91
- const createCollidersFromChildren = (object, rigidBody, options, world) => {
91
+
92
+ const isChildOfMeshCollider = child => {
93
+ let flag = false;
94
+ child.traverseAncestors(a => {
95
+ if (a.userData.r3RapierType === "MeshCollider") flag = true;
96
+ });
97
+ return flag;
98
+ };
99
+
100
+ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
92
101
  const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
93
102
  const colliders = [];
94
103
  let desc;
95
104
  let offset = new Vector3();
96
105
  object.traverse(child => {
97
106
  if ("isMesh" in child) {
107
+ if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
98
108
  const {
99
109
  geometry
100
110
  } = child;
@@ -165,7 +175,8 @@ const createCollidersFromChildren = (object, rigidBody, options, world) => {
165
175
  if (hasCollisionEvents) desc.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
166
176
  if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
167
177
  if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
168
- const collider = world.createCollider(desc, rigidBody);
178
+ const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
179
+ const collider = world.createCollider(desc, actualRigidBody);
169
180
  colliders.push(collider);
170
181
  }
171
182
  });
@@ -188,47 +199,6 @@ const vector3ToQuaternion = v => {
188
199
  return quaternion.setFromEuler(euler.setFromVector3(v));
189
200
  };
190
201
 
191
- function _defineProperty(obj, key, value) {
192
- if (key in obj) {
193
- Object.defineProperty(obj, key, {
194
- value: value,
195
- enumerable: true,
196
- configurable: true,
197
- writable: true
198
- });
199
- } else {
200
- obj[key] = value;
201
- }
202
-
203
- return obj;
204
- }
205
-
206
- function ownKeys(object, enumerableOnly) {
207
- var keys = Object.keys(object);
208
-
209
- if (Object.getOwnPropertySymbols) {
210
- var symbols = Object.getOwnPropertySymbols(object);
211
- enumerableOnly && (symbols = symbols.filter(function (sym) {
212
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
213
- })), keys.push.apply(keys, symbols);
214
- }
215
-
216
- return keys;
217
- }
218
-
219
- function _objectSpread2(target) {
220
- for (var i = 1; i < arguments.length; i++) {
221
- var source = null != arguments[i] ? arguments[i] : {};
222
- i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
223
- _defineProperty(target, key, source[key]);
224
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
225
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
226
- });
227
- }
228
-
229
- return target;
230
- }
231
-
232
202
  const createRigidBodyApi = ref => {
233
203
  return {
234
204
  raw: () => ref.current(),
@@ -273,9 +243,19 @@ const createRigidBodyApi = ref => {
273
243
  return new Quaternion(x, y, z, w);
274
244
  },
275
245
 
276
- setRotation: rotation => ref.current().setRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
277
- w: 1
278
- }), true),
246
+ setRotation: ({
247
+ x,
248
+ y,
249
+ z
250
+ }) => {
251
+ const q = vector3ToQuaternion(new Vector3(x, y, z));
252
+ ref.current().setRotation({
253
+ x: q.x,
254
+ y: q.y,
255
+ z: q.z,
256
+ w: q.w
257
+ }, true);
258
+ },
279
259
 
280
260
  linvel() {
281
261
  const {
@@ -541,6 +521,47 @@ const Physics = ({
541
521
  }, children);
542
522
  };
543
523
 
524
+ function _defineProperty(obj, key, value) {
525
+ if (key in obj) {
526
+ Object.defineProperty(obj, key, {
527
+ value: value,
528
+ enumerable: true,
529
+ configurable: true,
530
+ writable: true
531
+ });
532
+ } else {
533
+ obj[key] = value;
534
+ }
535
+
536
+ return obj;
537
+ }
538
+
539
+ function ownKeys(object, enumerableOnly) {
540
+ var keys = Object.keys(object);
541
+
542
+ if (Object.getOwnPropertySymbols) {
543
+ var symbols = Object.getOwnPropertySymbols(object);
544
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
545
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
546
+ })), keys.push.apply(keys, symbols);
547
+ }
548
+
549
+ return keys;
550
+ }
551
+
552
+ function _objectSpread2(target) {
553
+ for (var i = 1; i < arguments.length; i++) {
554
+ var source = null != arguments[i] ? arguments[i] : {};
555
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
556
+ _defineProperty(target, key, source[key]);
557
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
558
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
559
+ });
560
+ }
561
+
562
+ return target;
563
+ }
564
+
544
565
  const useRapier = () => {
545
566
  return useContext(RapierContext);
546
567
  };
@@ -827,17 +848,52 @@ const RigidBody = /*#__PURE__*/forwardRef((_ref, ref) => {
827
848
  const [object, rigidBody] = useRigidBody(props);
828
849
  useImperativeHandle(ref, () => rigidBody);
829
850
  return /*#__PURE__*/React.createElement(RigidBodyContext.Provider, {
830
- value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit)]
851
+ value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit), props]
831
852
  }, /*#__PURE__*/React.createElement("object3D", {
832
853
  ref: object
833
854
  }, children));
834
- }); // Colliders
855
+ });
856
+ const MeshCollider = ({
857
+ children,
858
+ type
859
+ }) => {
860
+ const {
861
+ physicsOptions,
862
+ world
863
+ } = useRapier();
864
+ const object = useRef(null);
865
+ const [, rigidBody, hasCollisionEvents, rigidBodyOptions] = useRigidBodyContext();
866
+ useEffect(() => {
867
+ let autoColliders = [];
868
+
869
+ if (object.current) {
870
+ var _ref2;
871
+
872
+ const colliderSetting = (_ref2 = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref2 !== void 0 ? _ref2 : false;
873
+ autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, rigidBody, _objectSpread2(_objectSpread2({}, rigidBodyOptions), {}, {
874
+ colliders: colliderSetting
875
+ }), world, false) : [];
876
+ }
877
+
878
+ return () => {
879
+ autoColliders.forEach(collider => {
880
+ world.removeCollider(collider);
881
+ });
882
+ };
883
+ }, []);
884
+ return /*#__PURE__*/React.createElement("object3D", {
885
+ ref: object,
886
+ userData: {
887
+ r3RapierType: 'MeshCollider'
888
+ }
889
+ }, children);
890
+ }; // Colliders
835
891
 
836
- const AnyCollider = _ref2 => {
892
+ const AnyCollider = _ref3 => {
837
893
  let {
838
894
  children
839
- } = _ref2,
840
- props = _objectWithoutProperties(_ref2, _excluded2);
895
+ } = _ref3,
896
+ props = _objectWithoutProperties(_ref3, _excluded2);
841
897
 
842
898
  const {
843
899
  world
@@ -1017,4 +1073,4 @@ const Debug = () => {
1017
1073
  })));
1018
1074
  };
1019
1075
 
1020
- export { BallCollider, CapsuleCollider, ConeCollider, ConvexHullCollider, CuboidCollider, CylinderCollider, Debug, HeightfieldCollider, Physics, RigidBody, RoundCuboidCollider, TrimeshCollider, useCollider, useFixedJoint, useImpulseJoint, usePrismaticJoint, useRapier, useRevoluteJoint, useRigidBody, useSphericalJoint };
1076
+ export { BallCollider, CapsuleCollider, ConeCollider, ConvexHullCollider, CuboidCollider, CylinderCollider, Debug, HeightfieldCollider, MeshCollider, Physics, RigidBody, RoundCuboidCollider, TrimeshCollider, useCollider, useFixedJoint, useImpulseJoint, usePrismaticJoint, useRapier, useRevoluteJoint, useRigidBody, useSphericalJoint };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/rapier",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "source": "src/index.ts",
5
5
  "main": "dist/react-three-rapier.cjs.js",
6
6
  "module": "dist/react-three-rapier.esm.js",
package/readme.md CHANGED
@@ -60,19 +60,39 @@ Turn off automatic collider generation globally, but apply auto generation local
60
60
  ```tsx
61
61
  const Scene = () => (
62
62
  <Physics colliders={false}>
63
+ {/* Use an automatic CuboidCollider for all meshes inside this RigidBody */}
63
64
  <RigidBody colliders="cuboid">
64
65
  <Box />
65
66
  </RigidBody>
66
67
 
68
+ {/* Use an automatic BallCollider for all meshes inside this RigidBody */}
67
69
  <RigidBody position={[0, 10, 0]} colliders="ball">
68
70
  <Sphere />
69
71
  </RigidBody>
70
72
 
73
+ {/* Make a compound shape with two custom BallColliders */}
71
74
  <RigidBody position={[0, 10, 0]}>
72
75
  <Sphere />
73
76
  <BallCollider args={0.5} />
74
77
  <BallCollider args={0.5} position={[1, 0, 0]} />
75
78
  </RigidBody>
79
+
80
+ {/* Make a compound shape with two custom BallColliders, an automatic BallCollider,
81
+ Two automatic MeshColliders, based on two different shape strategies */}
82
+ <RigidBody position={[0, 10, 0]} colliders='ball'>
83
+ <MeshCollider type="trimesh">
84
+ <mesh ... />
85
+ </MeshCollider>
86
+
87
+ <MeshCollider type="hull">
88
+ <mesh ... />
89
+ </MeshCollider>
90
+
91
+ <Sphere />
92
+
93
+ <BallCollider args={0.5} />
94
+ <BallCollider args={0.5} position={[1, 0, 0]} />
95
+ </RigidBody>
76
96
  </Physics>
77
97
  );
78
98
  ```