UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

137 lines (123 loc) 5.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.transformedNormalWorld = exports.transformedNormalView = exports.transformedClearcoatNormalView = exports.transformNormalToView = exports.transformNormal = exports.normalWorld = exports.normalView = exports.normalLocal = exports.normalGeometry = exports.normalFlat = void 0; var _AttributeNode = require("../core/AttributeNode.js"); var _Camera = require("./Camera.js"); var _ModelNode = require("./ModelNode.js"); var _TSLBase = require("../tsl/TSLBase.js"); var _Position = require("./Position.js"); var _FrontFacingNode = require("../display/FrontFacingNode.js"); /** * TSL object that represents the normal attribute of the current rendered object. * * @tsl * @type {Node<vec3>} */ const normalGeometry = exports.normalGeometry = /*@__PURE__*/(0, _AttributeNode.attribute)('normal', 'vec3'); /** * TSL object that represents the vertex normal in local space of the current rendered object. * * @tsl * @type {Node<vec3>} */ const normalLocal = exports.normalLocal = /*@__PURE__*/(0, _TSLBase.Fn)(builder => { if (builder.geometry.hasAttribute('normal') === false) { console.warn('THREE.TSL: Vertex attribute "normal" not found on geometry.'); return (0, _TSLBase.vec3)(0, 1, 0); } return normalGeometry; }, 'vec3').once()().toVar('normalLocal'); /** * TSL object that represents the flat vertex normal in view space of the current rendered object. * * @tsl * @type {Node<vec3>} */ const normalFlat = exports.normalFlat = /*@__PURE__*/_Position.positionView.dFdx().cross(_Position.positionView.dFdy()).normalize().toVar('normalFlat'); /** * TSL object that represents the vertex normal in view space of the current rendered object. * * @tsl * @type {Node<vec3>} */ const normalView = exports.normalView = /*@__PURE__*/(0, _TSLBase.Fn)(builder => { let node; if (builder.material.flatShading === true) { node = normalFlat; } else { node = (0, _TSLBase.varying)(transformNormalToView(normalLocal), 'v_normalView').normalize(); } return node; }, 'vec3').once()().toVar('normalView'); /** * TSL object that represents the vertex normal in world space of the current rendered object. * * @tsl * @type {Node<vec3>} */ const normalWorld = exports.normalWorld = /*@__PURE__*/(0, _TSLBase.varying)(normalView.transformDirection(_Camera.cameraViewMatrix), 'v_normalWorld').normalize().toVar('normalWorld'); /** * TSL object that represents the transformed vertex normal in view space of the current rendered object. * * @tsl * @type {Node<vec3>} */ const transformedNormalView = exports.transformedNormalView = /*@__PURE__*/(0, _TSLBase.Fn)(builder => { // Use getUV context to avoid side effects from nodes overwriting getUV in the context (e.g. EnvironmentNode) return builder.context.setupNormal().context({ getUV: null }); }, 'vec3').once()().mul(_FrontFacingNode.faceDirection).toVar('transformedNormalView'); /** * TSL object that represents the transformed vertex normal in world space of the current rendered object. * * @tsl * @type {Node<vec3>} */ const transformedNormalWorld = exports.transformedNormalWorld = /*@__PURE__*/transformedNormalView.transformDirection(_Camera.cameraViewMatrix).toVar('transformedNormalWorld'); /** * TSL object that represents the transformed clearcoat vertex normal in view space of the current rendered object. * * @tsl * @type {Node<vec3>} */ const transformedClearcoatNormalView = exports.transformedClearcoatNormalView = /*@__PURE__*/(0, _TSLBase.Fn)(builder => { // Use getUV context to avoid side effects from nodes overwriting getUV in the context (e.g. EnvironmentNode) return builder.context.setupClearcoatNormal().context({ getUV: null }); }, 'vec3').once()().mul(_FrontFacingNode.faceDirection).toVar('transformedClearcoatNormalView'); /** * Transforms the normal with the given matrix. * * @tsl * @function * @param {Node<vec3>} normal - The normal. * @param {Node<mat3>} [matrix=modelWorldMatrix] - The matrix. * @return {Node<vec3>} The transformed normal. */ const transformNormal = exports.transformNormal = /*@__PURE__*/(0, _TSLBase.Fn)(([normal, matrix = _ModelNode.modelWorldMatrix]) => { const m = (0, _TSLBase.mat3)(matrix); const transformedNormal = normal.div((0, _TSLBase.vec3)(m[0].dot(m[0]), m[1].dot(m[1]), m[2].dot(m[2]))); return m.mul(transformedNormal).xyz; }); /** * Transforms the given normal from local to view space. * * @tsl * @function * @param {Node<vec3>} normal - The normal. * @param {NodeBuilder} builder - The current node builder. * @return {Node<vec3>} The transformed normal. */ const transformNormalToView = exports.transformNormalToView = /*@__PURE__*/(0, _TSLBase.Fn)(([normal], builder) => { const modelNormalViewMatrix = builder.renderer.nodes.modelNormalViewMatrix; if (modelNormalViewMatrix !== null) { return modelNormalViewMatrix.transformDirection(normal); } // const transformedNormal = _ModelNode.modelNormalMatrix.mul(normal); return _Camera.cameraViewMatrix.transformDirection(transformedNormal); });