UNPKG

react-native-filament

Version:

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

115 lines (112 loc) 4.56 kB
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, { useCallback, useContext, useEffect, useMemo } from 'react'; import { useModel } from '../hooks/useModel'; import { getAssetFromModel } from '../utilities/getAssetFromModel'; import { useFilamentContext } from '../hooks/useFilamentContext'; import { Logger } from '../utilities/logger/Logger'; import { TouchHandlerContext } from './TouchHandlerContext'; import { useApplyTransformations } from '../hooks/internal/useApplyTransformations'; import { extractTransformationProps } from '../types/TransformProps'; import { ParentInstancesContext } from './ParentInstancesContext'; import { useConfigureAssetShadow } from '../hooks/useConfigureAssetShadow'; // Props we need for loading the model /** * Loads a model from the given source. * * * If you are passing in a `.glb` model or similar from your app's bundle using `require(..)`, make sure to add `glb` as an asset extension to `metro.config.js`! * If you are passing in a `{ uri: ... }`, make sure the URL points directly to a `.glb` model. This can either be a web URL (`http://..`/`https://..`), a local file (`file://..`), or an native asset path (`path/to/asset.glb`) */ export function Model({ source, ...restProps }) { const [_transformProps, modelProps] = extractTransformationProps(restProps); const model = useModel(source, modelProps); return /*#__PURE__*/React.createElement(ModelRenderer, _extends({ model: model }, restProps)); } /** * Renders a model that was loaded with `useModel`. * Always prefer the `Model` component, except when you explicitly need to access the model instance imperatively. */ export function ModelRenderer({ model, onPress, children, ...restProps }) { const [transformProps] = extractTransformationProps(restProps); const asset = getAssetFromModel(model); const boundingBox = model.state === 'loaded' ? model.boundingBox : undefined; const rootEntity = model.state === 'loaded' ? model.rootEntity : undefined; useApplyTransformations({ transformProps: transformProps, to: rootEntity, aabb: boundingBox }); const renderableEntities = useMemo(() => { // The entities are only needed for touch events, so only load them if a touch handler is provided if (asset == null || onPress == null) return []; return asset.getRenderableEntities(); }, [asset, onPress]); const { view, renderableManager } = useFilamentContext(); const onTouchStart = useCallback(async event => { if (renderableEntities == null || onPress == null) return; const { locationX, locationY } = event.nativeEvent; const entity = await view.pickEntity(locationX, locationY); Logger.debug('Picked entity', entity, 'at', locationX, locationY); if (entity == null) { Logger.debug('No entity was picked'); return; } // Check if the model was picked for (const renderableEntity of renderableEntities) { if ((entity === null || entity === void 0 ? void 0 : entity.id) === renderableEntity.id) { console.log('entity', entity.id, 'renderableEntity', renderableEntity.id); onPress(entity, renderableEntities); return; } } Logger.debug('No renderable entity of the model was picked:', renderableEntities); }, [onPress, renderableEntities, view]); const { addTouchHandler } = useContext(TouchHandlerContext); useEffect(() => { if (onPress == null) return; const id = Math.random().toString(36).substring(7); Logger.debug('Adding touch handler', id); const removeHandler = addTouchHandler(onTouchStart); return () => { Logger.debug('Removing touch handler', id); removeHandler(); }; }, [addTouchHandler, onPress, onTouchStart]); const instances = useMemo(() => { if (asset === undefined) { return null; } return asset.getAssetInstances(); }, [asset]); useConfigureAssetShadow({ renderableManager, asset, castShadow: restProps.castShadow, receiveShadow: restProps.receiveShadow }); if (asset == null) { return null; } return /*#__PURE__*/React.createElement(ParentInstancesContext.Provider, { value: instances }, children); } //# sourceMappingURL=Model.js.map