UNPKG

react-native-filament

Version:

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

48 lines (44 loc) 3.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useLightEntity = useLightEntity; var _react = require("react"); var _useFilamentContext = require("./useFilamentContext"); /** * Creates a new memoized light entity based on the given configuration. */ function useLightEntity(lightManager, config) { var _config$direction, _config$direction2, _config$direction3, _config$position, _config$position2, _config$position3; const falloffRadius = 'falloffRadius' in config ? config.falloffRadius : undefined; const spotLightCone = 'spotLightCone' in config ? config.spotLightCone : undefined; // Unwrap all array-ish values so the user doesn't have to memo them! const directionX = ((_config$direction = config.direction) === null || _config$direction === void 0 ? void 0 : _config$direction[0]) ?? undefined; const directionY = ((_config$direction2 = config.direction) === null || _config$direction2 === void 0 ? void 0 : _config$direction2[1]) ?? undefined; const directionZ = ((_config$direction3 = config.direction) === null || _config$direction3 === void 0 ? void 0 : _config$direction3[2]) ?? undefined; const positionX = ((_config$position = config.position) === null || _config$position === void 0 ? void 0 : _config$position[0]) ?? undefined; const positionY = ((_config$position2 = config.position) === null || _config$position2 === void 0 ? void 0 : _config$position2[1]) ?? undefined; const positionZ = ((_config$position3 = config.position) === null || _config$position3 === void 0 ? void 0 : _config$position3[2]) ?? undefined; const innerSpotLightCone = (spotLightCone === null || spotLightCone === void 0 ? void 0 : spotLightCone[0]) ?? undefined; const outerSpotLightCone = (spotLightCone === null || spotLightCone === void 0 ? void 0 : spotLightCone[1]) ?? undefined; const entity = (0, _react.useMemo)(() => { var _config$intensity; return lightManager.createLightEntity(config.type, config.colorKelvin, typeof config.intensity === 'number' ? config.intensity : (_config$intensity = config.intensity) === null || _config$intensity === void 0 ? void 0 : _config$intensity.value, directionX != null && directionY != null && directionZ != null ? [directionX, directionY, directionZ] : undefined, positionX != null && positionY != null && positionZ != null ? [positionX, positionY, positionZ] : undefined, config.castShadows, falloffRadius, innerSpotLightCone != null && outerSpotLightCone != null ? [innerSpotLightCone, outerSpotLightCone] : undefined); }, [config.castShadows, config.colorKelvin, config.intensity, config.type, directionX, directionY, directionZ, falloffRadius, innerSpotLightCone, lightManager, outerSpotLightCone, positionX, positionY, positionZ]); // Eventually subscribe to the intensity shared value const { workletContext } = (0, _useFilamentContext.useFilamentContext)(); (0, _react.useEffect)(() => { const intensity = config.intensity; if (intensity == null) return; if (typeof intensity === 'number') return; const setIntensity = lightManager.setIntensity; return intensity.addListener(workletContext.createRunAsync(() => { 'worklet'; setIntensity(entity, intensity.value); })); }, [config.intensity, entity, lightManager, workletContext]); return entity; } //# sourceMappingURL=useLightEntity.js.map