SpotLightHelper.js 2.3 KB

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