react-native-filament
Version:
A real-time physically based 3D rendering engine for React Native
54 lines • 2.01 kB
JavaScript
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); }
import React from 'react';
import { useMemo } from 'react';
import { ParentInstancesContext, useParentInstancesContext } from './ParentInstancesContext';
import { useApplyTransformations } from '../hooks/internal/useApplyTransformations';
/**
* 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>
* ```
*
*/
export function ModelInstance({
index,
...restProps
}) {
const instances = 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.createElement(ModelInstanceImpl, _extends({
instance: instance
}, restProps));
}
function ModelInstanceImpl({
instance,
children,
...transformProps
}) {
const rootEntity = useMemo(() => {
return instance.getRoot();
}, [instance]);
const boundingBox = useMemo(() => {
return instance.getBoundingBox();
}, [instance]);
const instances = useMemo(() => [instance], [instance]);
useApplyTransformations({
transformProps,
to: rootEntity,
aabb: boundingBox
});
return /*#__PURE__*/React.createElement(ParentInstancesContext.Provider, {
value: instances
}, children);
}
//# sourceMappingURL=ModelInstance.js.map