UNPKG

react-native-filament

Version:

A real-time physically based 3D rendering engine for React Native

73 lines (71 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useRigidBody = useRigidBody; var _bulletApi = require("../bulletApi"); var _react = require("react"); var _FilamentProxy = require("../../native/FilamentProxy"); function useRigidBody(props) { const originVec = props != null && 'origin' in props ? props.origin : undefined; const transform = props != null && 'transform' in props ? props.transform : undefined; const { mass, shape, friction, activationState, damping, world, id, collisionCallback } = props ?? {}; const originX = originVec === null || originVec === void 0 ? void 0 : originVec[0]; const originY = originVec === null || originVec === void 0 ? void 0 : originVec[1]; const originZ = originVec === null || originVec === void 0 ? void 0 : originVec[2]; const [body, setBody] = (0, _react.useState)(); (0, _react.useEffect)(() => { if (mass == null || shape == null || id == null) { return; } const getBody = _FilamentProxy.FilamentWorkletContext.runAsync(() => { 'worklet'; if (originX != null && originY != null && originZ != null) { return _bulletApi.BulletAPI.createRigidBody(mass, originX, originY, originZ, shape, id, collisionCallback); } if (transform) { return _bulletApi.BulletAPI.createRigidBodyFromTransform(mass, transform, shape, id, collisionCallback); } throw new Error('Either origin or transform must be provided'); }); getBody.then(setBody); }, [id, mass, originX, originY, originZ, collisionCallback, shape, transform]); (0, _react.useEffect)(() => { if (friction == null || body == null) { return; } body.friction = friction; }, [body, friction]); (0, _react.useEffect)(() => { if (activationState == null || body == null) { return; } body.activationState = activationState; }, [body, activationState]); (0, _react.useEffect)(() => { if (damping == null || body == null) { return; } body.setDamping(damping[0], damping[1]); }, [body, damping]); (0, _react.useEffect)(() => { if (world == null || body == null) { return; } world.addRigidBody(body); return () => { world.removeRigidBody(body); }; }, [body, world]); return body; } //# sourceMappingURL=useRigidBody.js.map