UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

113 lines (106 loc) 4.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.lightPosition = lightPosition; exports.lightProjectionUV = lightProjectionUV; exports.lightShadowMatrix = lightShadowMatrix; exports.lightTargetDirection = void 0; exports.lightTargetPosition = lightTargetPosition; exports.lightViewPosition = lightViewPosition; var _UniformNode = require("../core/UniformNode.js"); var _UniformGroupNode = require("../core/UniformGroupNode.js"); var _Vector = require("../../math/Vector3.js"); var _Camera = require("./Camera.js"); var _Position = require("./Position.js"); let uniformsLib; function getLightData(light) { uniformsLib = uniformsLib || new WeakMap(); let uniforms = uniformsLib.get(light); if (uniforms === undefined) uniformsLib.set(light, uniforms = {}); return uniforms; } /** * TSL function for getting a shadow matrix uniform node for the given light. * * @tsl * @function * @param {Light} light -The light source. * @returns {UniformNode<mat4>} The shadow matrix uniform node. */ function lightShadowMatrix(light) { const data = getLightData(light); return data.shadowMatrix || (data.shadowMatrix = (0, _UniformNode.uniform)('mat4').setGroup(_UniformGroupNode.renderGroup).onRenderUpdate(() => { if (light.castShadow !== true) { light.shadow.updateMatrices(light); } return light.shadow.matrix; })); } /** * TSL function for getting projected uv coordinates for the given light. * Relevant when using maps with spot lights. * * @tsl * @function * @param {Light} light -The light source. * @param {Node<vec3>} [position=positionWorld] -The position to project. * @returns {Node<vec3>} The projected uvs. */ function lightProjectionUV(light, position = _Position.positionWorld) { const spotLightCoord = lightShadowMatrix(light).mul(position); const projectionUV = spotLightCoord.xyz.div(spotLightCoord.w); return projectionUV; } /** * TSL function for getting the position in world space for the given light. * * @tsl * @function * @param {Light} light -The light source. * @returns {UniformNode<vec3>} The light's position in world space. */ function lightPosition(light) { const data = getLightData(light); return data.position || (data.position = (0, _UniformNode.uniform)(new _Vector.Vector3()).setGroup(_UniformGroupNode.renderGroup).onRenderUpdate((_, self) => self.value.setFromMatrixPosition(light.matrixWorld))); } /** * TSL function for getting the light target position in world space for the given light. * * @tsl * @function * @param {Light} light -The light source. * @returns {UniformNode<vec3>} The light target position in world space. */ function lightTargetPosition(light) { const data = getLightData(light); return data.targetPosition || (data.targetPosition = (0, _UniformNode.uniform)(new _Vector.Vector3()).setGroup(_UniformGroupNode.renderGroup).onRenderUpdate((_, self) => self.value.setFromMatrixPosition(light.target.matrixWorld))); } /** * TSL function for getting the position in view space for the given light. * * @tsl * @function * @param {Light} light - The light source. * @returns {UniformNode<vec3>} The light's position in view space. */ function lightViewPosition(light) { const data = getLightData(light); return data.viewPosition || (data.viewPosition = (0, _UniformNode.uniform)(new _Vector.Vector3()).setGroup(_UniformGroupNode.renderGroup).onRenderUpdate(({ camera }, self) => { self.value = self.value || new _Vector.Vector3(); self.value.setFromMatrixPosition(light.matrixWorld); self.value.applyMatrix4(camera.matrixWorldInverse); })); } /** * TSL function for getting the light target direction for the given light. * * @tsl * @function * @param {Light} light -The light source. * @returns {Node<vec3>} The light's target direction. */ const lightTargetDirection = light => _Camera.cameraViewMatrix.transformDirection(lightPosition(light).sub(lightTargetPosition(light))); exports.lightTargetDirection = lightTargetDirection;