DirectionalLightHelper.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Vector3 } from '../math/Vector3';
  2. import { Object3D } from '../core/Object3D';
  3. import { Line } from '../objects/Line';
  4. import { Float32BufferAttribute } from '../core/BufferAttribute';
  5. import { BufferGeometry } from '../core/BufferGeometry';
  6. import { LineBasicMaterial } from '../materials/LineBasicMaterial';
  7. /**
  8. * @author alteredq / http://alteredqualia.com/
  9. * @author mrdoob / http://mrdoob.com/
  10. * @author WestLangley / http://github.com/WestLangley
  11. */
  12. function DirectionalLightHelper( light, size ) {
  13. Object3D.call( this );
  14. this.light = light;
  15. this.light.updateMatrixWorld();
  16. this.matrix = light.matrixWorld;
  17. this.matrixAutoUpdate = false;
  18. if ( size === undefined ) size = 1;
  19. var geometry = new BufferGeometry();
  20. geometry.addAttribute( 'position', new Float32BufferAttribute( [
  21. - size, size, 0,
  22. size, size, 0,
  23. size, - size, 0,
  24. - size, - size, 0,
  25. - size, size, 0
  26. ], 3 ) );
  27. var material = new LineBasicMaterial( { fog: false } );
  28. this.add( new Line( geometry, material ) );
  29. geometry = new BufferGeometry();
  30. geometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  31. this.add( new Line( geometry, material ));
  32. this.update();
  33. }
  34. DirectionalLightHelper.prototype = Object.create( Object3D.prototype );
  35. DirectionalLightHelper.prototype.constructor = DirectionalLightHelper;
  36. DirectionalLightHelper.prototype.dispose = function () {
  37. var lightPlane = this.children[ 0 ];
  38. var targetLine = this.children[ 1 ];
  39. lightPlane.geometry.dispose();
  40. lightPlane.material.dispose();
  41. targetLine.geometry.dispose();
  42. targetLine.material.dispose();
  43. };
  44. DirectionalLightHelper.prototype.update = function () {
  45. var v1 = new Vector3();
  46. var v2 = new Vector3();
  47. var v3 = new Vector3();
  48. return function update() {
  49. v1.setFromMatrixPosition( this.light.matrixWorld );
  50. v2.setFromMatrixPosition( this.light.target.matrixWorld );
  51. v3.subVectors( v2, v1 );
  52. var lightPlane = this.children[ 0 ];
  53. var targetLine = this.children[ 1 ];
  54. lightPlane.lookAt( v3 );
  55. lightPlane.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  56. targetLine.lookAt( v3 );
  57. targetLine.scale.z = v3.length();
  58. };
  59. }();
  60. export { DirectionalLightHelper };