UNPKG

react-native-filament-buildcores

Version:

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

80 lines (79 loc) 3.16 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 { useContext, useMemo } from 'react'; import { ParentInstancesContext } from './ParentInstancesContext'; import { extractTransformationProps } from '../types'; import { useFilamentContext } from '../hooks/useFilamentContext'; import React from 'react'; import { useApplyTransformations } from '../hooks/internal/useApplyTransformations'; import { useWorkletEffect } from '../hooks/useWorkletEffect'; export function EntitySelector(props) { const parentInstances = useContext(ParentInstancesContext); const parentInstance = parentInstances === null || parentInstances === void 0 ? void 0 : parentInstances[0]; const byName = 'byName' in props ? props.byName : undefined; const byIndex = 'byIndex' in props ? props.byIndex : undefined; const { nameComponentManager } = useFilamentContext(); const entity = useMemo(() => { if (parentInstance == null) return; const entities = parentInstance.getEntities(); if (byName != null) { // TODO: can be optimized by using it from asset, or https://github.com/google/filament/discussions/7980 return entities.find(e => nameComponentManager.getEntityName(e) === byName); } else if (byIndex != null) { if (byIndex >= entities.length) { throw new Error('<EntitySelector>: byIndex is out of bounds. Instance only has ' + entities.length + ' entities'); } return entities[byIndex]; } else { return null; } }, [byIndex, byName, nameComponentManager, parentInstance]); if (parentInstance == null) { throw new Error('<EntitySelector> must be used inside a <Model> or <ModelInstance> component'); } if (byName != null && byIndex != null) { throw new Error('<EntitySelector>: Only one of byName or byIndex can be provided'); } if (entity == null) { throw new Error(`<EntitySelector>: Could not find entity by ${byName != null ? 'name' : 'index'} "${byName ?? byIndex}"`); } return /*#__PURE__*/React.createElement(EntitySelectorImpl, _extends({ entity: entity }, props)); } /** * @private */ function EntitySelectorImpl(props) { const [transformProps, { entity, textureMap }] = extractTransformationProps(props); useApplyTransformations({ to: entity, transformProps }); return /*#__PURE__*/React.createElement(React.Fragment, null, textureMap != null && /*#__PURE__*/React.createElement(TextureModifier, _extends({ entity: entity }, textureMap))); } /** * @private */ function TextureModifier({ entity, textureSource, materialName, textureFlags = 'sRGB' }) { const { renderableManager } = useFilamentContext(); useWorkletEffect(() => { 'worklet'; renderableManager.changeMaterialTextureMap(entity, materialName, textureSource, textureFlags); }); return null; } //# sourceMappingURL=EntitySelector.js.map