UNPKG

react-native-filament

Version:

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

62 lines (61 loc) 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModelInstance = ModelInstance; var _react = _interopRequireWildcard(require("react")); var _ParentInstancesContext = require("./ParentInstancesContext"); var _useApplyTransformations = require("../hooks/internal/useApplyTransformations"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } /** * You can render multiple instances of the same model. If you use a model multiple times this is recommended, * as it will only be loaded once into memory. * This component helps with applying configs to a specific instance of a model. * * @example * ```tsx * <Model source={require('model.glb')} instanceCount={2}> * <ModelInstance index={0} scale={[1, 1, 1]} /> * <ModelInstance index={1} scale={[2, 2, 2]} /> * </Model> * ``` * */ function ModelInstance({ index, ...restProps }) { const instances = (0, _ParentInstancesContext.useParentInstancesContext)(); const instance = instances[index]; if (instance == null) { throw new Error(`The parent Model component has ${instances.length} instances. You tried to use index ${index}`); } return /*#__PURE__*/(0, _jsxRuntime.jsx)(ModelInstanceImpl, { instance: instance, ...restProps }); } function ModelInstanceImpl({ instance, children, ...transformProps }) { const rootEntity = (0, _react.useMemo)(() => { return instance.getRoot(); }, [instance]); const boundingBox = (0, _react.useMemo)(() => { return instance.getBoundingBox(); }, [instance]); const instances = (0, _react.useMemo)(() => [instance], [instance]); (0, _useApplyTransformations.useApplyTransformations)({ transformProps, to: rootEntity, aabb: boundingBox }); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ParentInstancesContext.ParentInstancesContext.Provider, { value: instances, children: children }); } //# sourceMappingURL=ModelInstance.js.map