DirectionalLightHelper.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { Vector3 } from '../math/Vector3.js';
  2. import { Object3D } from '../core/Object3D.js';
  3. import { Line } from '../objects/Line.js';
  4. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  5. import { BufferGeometry } from '../core/BufferGeometry.js';
  6. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  7. const _v1 = /*@__PURE__*/ new Vector3();
  8. const _v2 = /*@__PURE__*/ new Vector3();
  9. const _v3 = /*@__PURE__*/ new Vector3();
  10. /**
  11. * Helper object to assist with visualizing a {@link DirectionalLight}'s
  12. * effect on the scene. This consists of plane and a line representing the
  13. * light's position and direction.
  14. *
  15. * ```js
  16. * const light = new THREE.DirectionalLight( 0xFFFFFF );
  17. * scene.add( light );
  18. *
  19. * const helper = new THREE.DirectionalLightHelper( light, 5 );
  20. * scene.add( helper );
  21. * ```
  22. *
  23. * @augments Object3D
  24. */
  25. class DirectionalLightHelper extends Object3D {
  26. /**
  27. * Constructs a new directional light helper.
  28. *
  29. * @param {DirectionalLight} light - The light to be visualized.
  30. * @param {number} [size=1] - The dimensions of the plane.
  31. * @param {number|Color|string} [color] - The helper's color. If not set, the helper will take
  32. * the color of the light.
  33. */
  34. constructor( light, size, color ) {
  35. super();
  36. /**
  37. * The light being visualized.
  38. *
  39. * @type {DirectionalLight}
  40. */
  41. this.light = light;
  42. this.matrix = light.matrixWorld;
  43. this.matrixAutoUpdate = false;
  44. /**
  45. * The color parameter passed in the constructor.
  46. * If not set, the helper will take the color of the light.
  47. *
  48. * @type {number|Color|string}
  49. */
  50. this.color = color;
  51. this.type = 'DirectionalLightHelper';
  52. if ( size === undefined ) size = 1;
  53. let geometry = new BufferGeometry();
  54. geometry.setAttribute( 'position', new Float32BufferAttribute( [
  55. - size, size, 0,
  56. size, size, 0,
  57. size, - size, 0,
  58. - size, - size, 0,
  59. - size, size, 0
  60. ], 3 ) );
  61. const material = new LineBasicMaterial( { fog: false, toneMapped: false } );
  62. /**
  63. * Contains the line showing the location of the directional light.
  64. *
  65. * @type {Line}
  66. */
  67. this.lightPlane = new Line( geometry, material );
  68. this.add( this.lightPlane );
  69. geometry = new BufferGeometry();
  70. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  71. /**
  72. * Represents the target line of the directional light.
  73. *
  74. * @type {Line}
  75. */
  76. this.targetLine = new Line( geometry, material );
  77. this.add( this.targetLine );
  78. this.update();
  79. }
  80. /**
  81. * Frees the GPU-related resources allocated by this instance. Call this
  82. * method whenever this instance is no longer used in your app.
  83. */
  84. dispose() {
  85. this.lightPlane.geometry.dispose();
  86. this.lightPlane.material.dispose();
  87. this.targetLine.geometry.dispose();
  88. this.targetLine.material.dispose();
  89. }
  90. /**
  91. * Updates the helper to match the position and direction of the
  92. * light being visualized.
  93. */
  94. update() {
  95. this.light.updateWorldMatrix( true, false );
  96. this.light.target.updateWorldMatrix( true, false );
  97. _v1.setFromMatrixPosition( this.light.matrixWorld );
  98. _v2.setFromMatrixPosition( this.light.target.matrixWorld );
  99. _v3.subVectors( _v2, _v1 );
  100. this.lightPlane.lookAt( _v2 );
  101. if ( this.color !== undefined ) {
  102. this.lightPlane.material.color.set( this.color );
  103. this.targetLine.material.color.set( this.color );
  104. } else {
  105. this.lightPlane.material.color.copy( this.light.color );
  106. this.targetLine.material.color.copy( this.light.color );
  107. }
  108. this.targetLine.lookAt( _v2 );
  109. this.targetLine.scale.z = _v3.length();
  110. }
  111. }
  112. export { DirectionalLightHelper };
粤ICP备19079148号