DirectionalLightHelper.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 { Line } from '../objects/Line.js';
  9. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  10. import { BufferGeometry } from '../core/BufferGeometry.js';
  11. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  12. var _v1 = new Vector3();
  13. var _v2 = new Vector3();
  14. var _v3 = new Vector3();
  15. function DirectionalLightHelper( light, size, color ) {
  16. Object3D.call( this );
  17. this.light = light;
  18. this.light.updateMatrixWorld();
  19. this.matrix = light.matrixWorld;
  20. this.matrixAutoUpdate = false;
  21. this.color = color;
  22. if ( size === undefined ) size = 1;
  23. var geometry = new BufferGeometry();
  24. geometry.addAttribute( 'position', new Float32BufferAttribute( [
  25. - size, size, 0,
  26. size, size, 0,
  27. size, - size, 0,
  28. - size, - size, 0,
  29. - size, size, 0
  30. ], 3 ) );
  31. var material = new LineBasicMaterial( { fog: false } );
  32. this.lightPlane = new Line( geometry, material );
  33. this.add( this.lightPlane );
  34. geometry = new BufferGeometry();
  35. geometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  36. this.targetLine = new Line( geometry, material );
  37. this.add( this.targetLine );
  38. this.update();
  39. }
  40. DirectionalLightHelper.prototype = Object.create( Object3D.prototype );
  41. DirectionalLightHelper.prototype.constructor = DirectionalLightHelper;
  42. DirectionalLightHelper.prototype.dispose = function () {
  43. this.lightPlane.geometry.dispose();
  44. this.lightPlane.material.dispose();
  45. this.targetLine.geometry.dispose();
  46. this.targetLine.material.dispose();
  47. };
  48. DirectionalLightHelper.prototype.update = function () {
  49. _v1.setFromMatrixPosition( this.light.matrixWorld );
  50. _v2.setFromMatrixPosition( this.light.target.matrixWorld );
  51. _v3.subVectors( _v2, _v1 );
  52. this.lightPlane.lookAt( _v2 );
  53. if ( this.color !== undefined ) {
  54. this.lightPlane.material.color.set( this.color );
  55. this.targetLine.material.color.set( this.color );
  56. } else {
  57. this.lightPlane.material.color.copy( this.light.color );
  58. this.targetLine.material.color.copy( this.light.color );
  59. }
  60. this.targetLine.lookAt( _v2 );
  61. this.targetLine.scale.z = _v3.length();
  62. };
  63. export { DirectionalLightHelper };