DirectionalLight.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { Light } from './Light.js';
  2. import { DirectionalLightShadow } from './DirectionalLightShadow.js';
  3. import { Object3D } from '../core/Object3D.js';
  4. /**
  5. * A light that gets emitted in a specific direction. This light will behave
  6. * as though it is infinitely far away and the rays produced from it are all
  7. * parallel. The common use case for this is to simulate daylight; the sun is
  8. * far enough away that its position can be considered to be infinite, and
  9. * all light rays coming from it are parallel.
  10. *
  11. * A common point of confusion for directional lights is that setting the
  12. * rotation has no effect. This is because three.js's DirectionalLight is the
  13. * equivalent to what is often called a 'Target Direct Light' in other
  14. * applications.
  15. *
  16. * This means that its direction is calculated as pointing from the light's
  17. * {@link Object3D#position} to the {@link DirectionalLight#target} position
  18. * (as opposed to a 'Free Direct Light' that just has a rotation
  19. * component).
  20. *
  21. * This light can cast shadows - see the {@link DirectionalLightShadow} for details.
  22. *
  23. * ```js
  24. * // White directional light at half intensity shining from the top.
  25. * const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
  26. * scene.add( directionalLight );
  27. * ```
  28. *
  29. * @augments Light
  30. */
  31. class DirectionalLight extends Light {
  32. /**
  33. * Constructs a new directional light.
  34. *
  35. * @param {(number|Color|string)} [color=0xffffff] - The light's color.
  36. * @param {number} [intensity=1] - The light's strength/intensity.
  37. */
  38. constructor( color, intensity ) {
  39. super( color, intensity );
  40. /**
  41. * This flag can be used for type testing.
  42. *
  43. * @type {boolean}
  44. * @readonly
  45. * @default true
  46. */
  47. this.isDirectionalLight = true;
  48. this.type = 'DirectionalLight';
  49. this.position.copy( Object3D.DEFAULT_UP );
  50. this.updateMatrix();
  51. /**
  52. * The directional light points from its position to the
  53. * target's position.
  54. *
  55. * For the target's position to be changed to anything other
  56. * than the default, it must be added to the scene.
  57. *
  58. * It is also possible to set the target to be another 3D object
  59. * in the scene. The light will now track the target object.
  60. *
  61. * @type {Object3D}
  62. */
  63. this.target = new Object3D();
  64. /**
  65. * This property holds the light's shadow configuration.
  66. *
  67. * @type {DirectionalLightShadow}
  68. */
  69. this.shadow = new DirectionalLightShadow();
  70. }
  71. dispose() {
  72. this.shadow.dispose();
  73. }
  74. copy( source ) {
  75. super.copy( source );
  76. this.target = source.target.clone();
  77. this.shadow = source.shadow.clone();
  78. return this;
  79. }
  80. toJSON( meta ) {
  81. const data = super.toJSON( meta );
  82. data.object.shadow = this.shadow.toJSON();
  83. data.object.target = this.target.uuid;
  84. return data;
  85. }
  86. }
  87. export { DirectionalLight };
粤ICP备19079148号