SpotLightHelper.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author WestLangley / http://github.com/WestLangley
  5. */
  6. import { Vector3 } from '../math/Vector3.js';
  7. import { Object3D } from '../core/Object3D.js';
  8. import { LineSegments } from '../objects/LineSegments.js';
  9. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  10. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  11. import { BufferGeometry } from '../core/BufferGeometry.js';
  12. var _vector = new Vector3();
  13. function SpotLightHelper( light, color ) {
  14. Object3D.call( this );
  15. this.light = light;
  16. this.light.updateMatrixWorld();
  17. this.matrix = light.matrixWorld;
  18. this.matrixAutoUpdate = false;
  19. this.color = color;
  20. var geometry = new BufferGeometry();
  21. var positions = [
  22. 0, 0, 0, 0, 0, 1,
  23. 0, 0, 0, 1, 0, 1,
  24. 0, 0, 0, - 1, 0, 1,
  25. 0, 0, 0, 0, 1, 1,
  26. 0, 0, 0, 0, - 1, 1
  27. ];
  28. for ( var i = 0, j = 1, l = 32; i < l; i ++, j ++ ) {
  29. var p1 = ( i / l ) * Math.PI * 2;
  30. var p2 = ( j / l ) * Math.PI * 2;
  31. positions.push(
  32. Math.cos( p1 ), Math.sin( p1 ), 1,
  33. Math.cos( p2 ), Math.sin( p2 ), 1
  34. );
  35. }
  36. geometry.addAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  37. var material = new LineBasicMaterial( { fog: false } );
  38. this.cone = new LineSegments( geometry, material );
  39. this.add( this.cone );
  40. this.update();
  41. }
  42. SpotLightHelper.prototype = Object.create( Object3D.prototype );
  43. SpotLightHelper.prototype.constructor = SpotLightHelper;
  44. SpotLightHelper.prototype.dispose = function () {
  45. this.cone.geometry.dispose();
  46. this.cone.material.dispose();
  47. };
  48. SpotLightHelper.prototype.update = function () {
  49. this.light.updateMatrixWorld();
  50. var coneLength = this.light.distance ? this.light.distance : 1000;
  51. var coneWidth = coneLength * Math.tan( this.light.angle );
  52. this.cone.scale.set( coneWidth, coneWidth, coneLength );
  53. _vector.setFromMatrixPosition( this.light.target.matrixWorld );
  54. this.cone.lookAt( _vector );
  55. if ( this.color !== undefined ) {
  56. this.cone.material.color.set( this.color );
  57. } else {
  58. this.cone.material.color.copy( this.light.color );
  59. }
  60. };
  61. export { SpotLightHelper };
粤ICP备19079148号