UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

122 lines (114 loc) 3.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DirectionalLightHelper = void 0; var _Vector = require("../math/Vector3.js"); var _Object3D = require("../core/Object3D.js"); var _Line = require("../objects/Line.js"); var _BufferAttribute = require("../core/BufferAttribute.js"); var _BufferGeometry = require("../core/BufferGeometry.js"); var _LineBasicMaterial = require("../materials/LineBasicMaterial.js"); const _v1 = /*@__PURE__*/new _Vector.Vector3(); const _v2 = /*@__PURE__*/new _Vector.Vector3(); const _v3 = /*@__PURE__*/new _Vector.Vector3(); /** * Helper object to assist with visualizing a {@link DirectionalLight}'s * effect on the scene. This consists of plane and a line representing the * light's position and direction. * * ```js * const light = new THREE.DirectionalLight( 0xFFFFFF ); * scene.add( light ); * * const helper = new THREE.DirectionalLightHelper( light, 5 ); * scene.add( helper ); * ``` * * @augments Object3D */ class DirectionalLightHelper extends _Object3D.Object3D { /** * Constructs a new directional light helper. * * @param {DirectionalLight} light - The light to be visualized. * @param {number} [size=1] - The dimensions of the plane. * @param {number|Color|string} [color] - The helper's color. If not set, the helper will take * the color of the light. */ constructor(light, size, color) { super(); /** * The light being visualized. * * @type {DirectionalLight} */ this.light = light; this.matrix = light.matrixWorld; this.matrixAutoUpdate = false; /** * The color parameter passed in the constructor. * If not set, the helper will take the color of the light. * * @type {number|Color|string} */ this.color = color; this.type = 'DirectionalLightHelper'; if (size === undefined) size = 1; let geometry = new _BufferGeometry.BufferGeometry(); geometry.setAttribute('position', new _BufferAttribute.Float32BufferAttribute([-size, size, 0, size, size, 0, size, -size, 0, -size, -size, 0, -size, size, 0], 3)); const material = new _LineBasicMaterial.LineBasicMaterial({ fog: false, toneMapped: false }); /** * Contains the line showing the location of the directional light. * * @type {Line} */ this.lightPlane = new _Line.Line(geometry, material); this.add(this.lightPlane); geometry = new _BufferGeometry.BufferGeometry(); geometry.setAttribute('position', new _BufferAttribute.Float32BufferAttribute([0, 0, 0, 0, 0, 1], 3)); /** * Represents the target line of the directional light. * * @type {Line} */ this.targetLine = new _Line.Line(geometry, material); this.add(this.targetLine); this.update(); } /** * Frees the GPU-related resources allocated by this instance. Call this * method whenever this instance is no longer used in your app. */ dispose() { this.lightPlane.geometry.dispose(); this.lightPlane.material.dispose(); this.targetLine.geometry.dispose(); this.targetLine.material.dispose(); } /** * Updates the helper to match the position and direction of the * light being visualized. */ update() { this.light.updateWorldMatrix(true, false); this.light.target.updateWorldMatrix(true, false); _v1.setFromMatrixPosition(this.light.matrixWorld); _v2.setFromMatrixPosition(this.light.target.matrixWorld); _v3.subVectors(_v2, _v1); this.lightPlane.lookAt(_v2); if (this.color !== undefined) { this.lightPlane.material.color.set(this.color); this.targetLine.material.color.set(this.color); } else { this.lightPlane.material.color.copy(this.light.color); this.targetLine.material.color.copy(this.light.color); } this.targetLine.lookAt(_v2); this.targetLine.scale.z = _v3.length(); } } exports.DirectionalLightHelper = DirectionalLightHelper;