UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

105 lines (100 loc) 3.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _BasicLightingModel = _interopRequireDefault(require("./BasicLightingModel.js")); var _F_Schlick = _interopRequireDefault(require("./BSDF/F_Schlick.js")); var _BRDF_Lambert = _interopRequireDefault(require("./BSDF/BRDF_Lambert.js")); var _PropertyNode = require("../core/PropertyNode.js"); var _Normal = require("../accessors/Normal.js"); var _MaterialNode = require("../accessors/MaterialNode.js"); var _Position = require("../accessors/Position.js"); var _TSLBase = require("../tsl/TSLBase.js"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const G_BlinnPhong_Implicit = () => (0, _TSLBase.float)(0.25); const D_BlinnPhong = /*@__PURE__*/(0, _TSLBase.Fn)(({ dotNH }) => { return _PropertyNode.shininess.mul((0, _TSLBase.float)(0.5)).add(1.0).mul((0, _TSLBase.float)(1 / Math.PI)).mul(dotNH.pow(_PropertyNode.shininess)); }); const BRDF_BlinnPhong = /*@__PURE__*/(0, _TSLBase.Fn)(({ lightDirection }) => { const halfDir = lightDirection.add(_Position.positionViewDirection).normalize(); const dotNH = _Normal.transformedNormalView.dot(halfDir).clamp(); const dotVH = _Position.positionViewDirection.dot(halfDir).clamp(); const F = (0, _F_Schlick.default)({ f0: _PropertyNode.specularColor, f90: 1.0, dotVH }); const G = G_BlinnPhong_Implicit(); const D = D_BlinnPhong({ dotNH }); return F.mul(G).mul(D); }); /** * Represents the lighting model for a phong material. Used in {@link MeshPhongNodeMaterial}. * * @augments BasicLightingModel */ class PhongLightingModel extends _BasicLightingModel.default { /** * Constructs a new phong lighting model. * * @param {boolean} [specular=true] - Whether specular is supported or not. */ constructor(specular = true) { super(); /** * Whether specular is supported or not. Set this to `false` if you are * looking for a Lambert-like material meaning a material for non-shiny * surfaces, without specular highlights. * * @type {boolean} * @default true */ this.specular = specular; } /** * Implements the direct lighting. The specular portion is optional an can be controlled * with the {@link PhongLightingModel#specular} flag. * * @param {Object} lightData - The light data. */ direct({ lightDirection, lightColor, reflectedLight }) { const dotNL = _Normal.transformedNormalView.dot(lightDirection).clamp(); const irradiance = dotNL.mul(lightColor); reflectedLight.directDiffuse.addAssign(irradiance.mul((0, _BRDF_Lambert.default)({ diffuseColor: _PropertyNode.diffuseColor.rgb }))); if (this.specular === true) { reflectedLight.directSpecular.addAssign(irradiance.mul(BRDF_BlinnPhong({ lightDirection })).mul(_MaterialNode.materialSpecularStrength)); } } /** * Implements the indirect lighting. * * @param {NodeBuilder} builder - The current node builder. */ indirect(builder) { const { ambientOcclusion, irradiance, reflectedLight } = builder.context; reflectedLight.indirectDiffuse.addAssign(irradiance.mul((0, _BRDF_Lambert.default)({ diffuseColor: _PropertyNode.diffuseColor }))); reflectedLight.indirectDiffuse.mulAssign(ambientOcclusion); } } var _default = exports.default = PhongLightingModel;