UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

55 lines (54 loc) 2.15 kB
"use strict"; import { BufferGeometry, Float32BufferAttribute, LineBasicMaterial, LineSegments, Mesh, Vector3 } from "three"; const _CoreSpotLightHelper = class { constructor(container) { this.container = container; this.object = new Mesh(); this._cone = new LineSegments(); this._lineMaterial = new LineBasicMaterial(); this.object.name = `CoreSpotLightHelper_${this.container.nodeName}`; this.object.matrixAutoUpdate = false; this.createAndBuildObject({ helperSize: 1 }); } createAndBuildObject(params) { this.buildHelper(); this.update(params); } buildHelper() { this._cone.geometry = _CoreSpotLightHelper._buildConeGeometry(); this._cone.material = this._lineMaterial; this._cone.matrixAutoUpdate = false; this._cone.name = `CoreSpotLightHelperCone_${this.container.nodeName}`; this.object.add(this._cone); } update(params) { const light = this.container.light(); _CoreSpotLightHelper.transformObject(this._cone, { sizeMult: params.helperSize, distance: light.distance, angle: light.angle }); this._lineMaterial.color.copy(light.color); } static transformObject(object, options) { const coneLength = (options.distance ? options.distance : 1e3) * options.sizeMult; const coneWidth = coneLength * Math.tan(options.angle); this._matrixScale.set(coneWidth, coneWidth, coneLength); object.matrix.identity(); object.matrix.makeRotationX(Math.PI * 1); object.matrix.scale(this._matrixScale); } static _buildConeGeometry() { const geometry = new BufferGeometry(); const positions = [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, -1, 1]; for (let i = 0, j = 1, l = 32; i < l; i++, j++) { const p1 = i / l * Math.PI * 2; const p2 = j / l * Math.PI * 2; positions.push(Math.cos(p1), Math.sin(p1), 1, Math.cos(p2), Math.sin(p2), 1); } geometry.setAttribute("position", new Float32BufferAttribute(positions, 3)); return geometry; } }; export let CoreSpotLightHelper = _CoreSpotLightHelper; CoreSpotLightHelper._matrixScale = new Vector3();