SpotLight.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { Light } from './Light.js';
  2. import { SpotLightShadow } from './SpotLightShadow.js';
  3. import { Object3D } from '../core/Object3D.js';
  4. /**
  5. * This light gets emitted from a single point in one direction, along a cone
  6. * that increases in size the further from the light it gets.
  7. *
  8. * This light can cast shadows - see the {@link SpotLightShadow} for details.
  9. *
  10. * ```js
  11. * // white spotlight shining from the side, modulated by a texture
  12. * const spotLight = new THREE.SpotLight( 0xffffff );
  13. * spotLight.position.set( 100, 1000, 100 );
  14. * spotLight.map = new THREE.TextureLoader().load( url );
  15. *
  16. * spotLight.castShadow = true;
  17. * spotLight.shadow.mapSize.width = 1024;
  18. * spotLight.shadow.mapSize.height = 1024;
  19. * spotLight.shadow.camera.near = 500;
  20. * spotLight.shadow.camera.far = 4000;
  21. * spotLight.shadow.camera.fov = 30;s
  22. * ```
  23. *
  24. * @augments Light
  25. */
  26. class SpotLight extends Light {
  27. /**
  28. * Constructs a new spot light.
  29. *
  30. * @param {(number|Color|string)} [color=0xffffff] - The light's color.
  31. * @param {number} [intensity=1] - The light's strength/intensity measured in candela (cd).
  32. * @param {number} [distance=0] - Maximum range of the light. `0` means no limit.
  33. * @param {number} [angle=Math.PI/3] - Maximum angle of light dispersion from its direction whose upper bound is `Math.PI/2`.
  34. * @param {number} [penumbra=0] - Percent of the spotlight cone that is attenuated due to penumbra. Value range is `[0,1]`.
  35. * @param {number} [decay=2] - The amount the light dims along the distance of the light.
  36. */
  37. constructor( color, intensity, distance = 0, angle = Math.PI / 3, penumbra = 0, decay = 2 ) {
  38. super( color, intensity );
  39. /**
  40. * This flag can be used for type testing.
  41. *
  42. * @type {boolean}
  43. * @readonly
  44. * @default true
  45. */
  46. this.isSpotLight = true;
  47. this.type = 'SpotLight';
  48. this.position.copy( Object3D.DEFAULT_UP );
  49. this.updateMatrix();
  50. /**
  51. * The spot light points from its position to the
  52. * target's position.
  53. *
  54. * For the target's position to be changed to anything other
  55. * than the default, it must be added to the scene.
  56. *
  57. * It is also possible to set the target to be another 3D object
  58. * in the scene. The light will now track the target object.
  59. *
  60. * @type {Object3D}
  61. */
  62. this.target = new Object3D();
  63. /**
  64. * Maximum range of the light. `0` means no limit.
  65. *
  66. * @type {number}
  67. * @default 0
  68. */
  69. this.distance = distance;
  70. /**
  71. * Maximum angle of light dispersion from its direction whose upper bound is `Math.PI/2`.
  72. *
  73. * @type {number}
  74. * @default Math.PI/3
  75. */
  76. this.angle = angle;
  77. /**
  78. * Percent of the spotlight cone that is attenuated due to penumbra.
  79. * Value range is `[0,1]`.
  80. *
  81. * @type {number}
  82. * @default 0
  83. */
  84. this.penumbra = penumbra;
  85. /**
  86. * The amount the light dims along the distance of the light. In context of
  87. * physically-correct rendering the default value should not be changed.
  88. *
  89. * @type {number}
  90. * @default 2
  91. */
  92. this.decay = decay;
  93. /**
  94. * A texture used to modulate the color of the light. The spot light
  95. * color is mixed with the RGB value of this texture, with a ratio
  96. * corresponding to its alpha value. The cookie-like masking effect is
  97. * reproduced using pixel values (0, 0, 0, 1-cookie_value).
  98. *
  99. * *Warning*: This property is disabled if {@link Object3D#castShadow} is set to `false`.
  100. *
  101. * @type {?Texture}
  102. * @default null
  103. */
  104. this.map = null;
  105. /**
  106. * This property holds the light's shadow configuration.
  107. *
  108. * @type {SpotLightShadow}
  109. */
  110. this.shadow = new SpotLightShadow();
  111. }
  112. /**
  113. * The light's power. Power is the luminous power of the light measured in lumens (lm).
  114. * Changing the power will also change the light's intensity.
  115. *
  116. * @type {number}
  117. */
  118. get power() {
  119. // compute the light's luminous power (in lumens) from its intensity (in candela)
  120. // by convention for a spotlight, luminous power (lm) = π * luminous intensity (cd)
  121. return this.intensity * Math.PI;
  122. }
  123. set power( power ) {
  124. // set the light's intensity (in candela) from the desired luminous power (in lumens)
  125. this.intensity = power / Math.PI;
  126. }
  127. dispose() {
  128. this.shadow.dispose();
  129. }
  130. copy( source, recursive ) {
  131. super.copy( source, recursive );
  132. this.distance = source.distance;
  133. this.angle = source.angle;
  134. this.penumbra = source.penumbra;
  135. this.decay = source.decay;
  136. this.target = source.target.clone();
  137. this.shadow = source.shadow.clone();
  138. return this;
  139. }
  140. }
  141. export { SpotLight };
粤ICP备19079148号