react-native-filament
Version:
A real-time physically based 3D rendering engine for React Native
151 lines (146 loc) • 5.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.EntitySelector = EntitySelector;
var _react = _interopRequireWildcard(require("react"));
var _ParentInstancesContext = require("./ParentInstancesContext");
var _types = require("../types");
var _useFilamentContext = require("../hooks/useFilamentContext");
var _useApplyTransformations = require("../hooks/internal/useApplyTransformations");
var _useWorkletEffect = require("../hooks/useWorkletEffect");
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); }
function EntitySelector(props) {
const parentInstances = (0, _react.useContext)(_ParentInstancesContext.ParentInstancesContext);
const parentInstance = parentInstances?.[0];
const byName = 'byName' in props ? props.byName : undefined;
const byIndex = 'byIndex' in props ? props.byIndex : undefined;
const {
nameComponentManager
} = (0, _useFilamentContext.useFilamentContext)();
const entity = (0, _react.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__*/(0, _jsxRuntime.jsx)(EntitySelectorImpl, {
entity: entity,
...props
});
}
/**
* @private
*/
function EntitySelectorImpl(props) {
const [transformProps, {
entity,
textureMap
}] = (0, _types.extractTransformationProps)(props);
(0, _useApplyTransformations.useApplyTransformations)({
to: entity,
transformProps
});
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [textureMap != null && /*#__PURE__*/(0, _jsxRuntime.jsx)(TextureModifier, {
entity: entity,
...textureMap
}), props.materialParameters != null && /*#__PURE__*/(0, _jsxRuntime.jsx)(MaterialModifier, {
entity: entity,
materialParameters: props.materialParameters
})]
});
}
/**
* @private
*/
function TextureModifier({
entity,
textureSource,
materialName,
textureFlags = 'sRGB'
}) {
const {
renderableManager
} = (0, _useFilamentContext.useFilamentContext)();
(0, _useWorkletEffect.useWorkletEffect)(() => {
'worklet';
renderableManager.changeMaterialTextureMap(entity, materialName, textureSource, textureFlags);
});
return null;
}
const applyMaterialParameters = (materialInstance, parameters) => {
'worklet';
if (parameters.baseColorFactor) {
materialInstance.setFloat4Parameter('baseColorFactor', parameters.baseColorFactor);
}
if (parameters.emissiveFactor) {
materialInstance.setFloat4Parameter('emissiveFactor', parameters.emissiveFactor);
}
if (parameters.roughnessFactor !== undefined) {
materialInstance.setFloatParameter('roughnessFactor', parameters.roughnessFactor);
}
if (parameters.metallicFactor !== undefined) {
materialInstance.setFloatParameter('metallicFactor', parameters.metallicFactor);
}
if (parameters.reflectance !== undefined) {
materialInstance.setFloatParameter('reflectance', parameters.reflectance);
}
if (parameters.clearCoatFactor !== undefined) {
materialInstance.setFloatParameter('clearCoatFactor', parameters.clearCoatFactor);
}
if (parameters.clearCoatRoughnessFactor !== undefined) {
materialInstance.setFloatParameter('clearCoatRoughnessFactor', parameters.clearCoatRoughnessFactor);
}
};
/**
* @private
*/
function MaterialModifier({
entity,
materialParameters
}) {
const {
renderableManager
} = (0, _useFilamentContext.useFilamentContext)();
(0, _useWorkletEffect.useWorkletEffect)(() => {
'worklet';
if (Array.isArray(materialParameters)) {
materialParameters.forEach(({
index,
parameters
}) => {
const materialInstance = renderableManager.getMaterialInstanceAt(entity, index);
applyMaterialParameters(materialInstance, parameters);
});
} else {
const {
index = 0,
parameters
} = materialParameters;
const materialInstance = renderableManager.getMaterialInstanceAt(entity, index);
applyMaterialParameters(materialInstance, parameters);
}
});
return null;
}
//# sourceMappingURL=EntitySelector.js.map