react-native-filament
Version:
A real-time physically based 3D rendering engine for React Native
61 lines (60 loc) • 3.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ModelInstance = ModelInstance;
var _react = _interopRequireWildcard(require("react"));
var _ParentInstancesContext = require("./ParentInstancesContext");
var _useApplyTransformations = require("../hooks/internal/useApplyTransformations");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
/**
* 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__*/_react.default.createElement(ModelInstanceImpl, _extends({
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__*/_react.default.createElement(_ParentInstancesContext.ParentInstancesContext.Provider, {
value: instances
}, children);
}
//# sourceMappingURL=ModelInstance.js.map