PointLightHelper.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { Mesh } from '../objects/Mesh.js';
  2. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
  3. import { SphereGeometry } from '../geometries/SphereGeometry.js';
  4. /**
  5. * This displays a helper object consisting of a spherical mesh for
  6. * visualizing an instance of {@link PointLight}.
  7. *
  8. * ```js
  9. * const pointLight = new THREE.PointLight( 0xff0000, 1, 100 );
  10. * pointLight.position.set( 10, 10, 10 );
  11. * scene.add( pointLight );
  12. *
  13. * const sphereSize = 1;
  14. * const pointLightHelper = new THREE.PointLightHelper( pointLight, sphereSize );
  15. * scene.add( pointLightHelper );
  16. * ```
  17. *
  18. * @augments Mesh
  19. */
  20. class PointLightHelper extends Mesh {
  21. /**
  22. * Constructs a new point light helper.
  23. *
  24. * @param {PointLight} light - The light to be visualized.
  25. * @param {number} [sphereSize=1] - The size of the sphere helper.
  26. * @param {number|Color|string} [color] - The helper's color. If not set, the helper will take
  27. * the color of the light.
  28. */
  29. constructor( light, sphereSize, color ) {
  30. const geometry = new SphereGeometry( sphereSize, 4, 2 );
  31. const material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
  32. super( geometry, material );
  33. /**
  34. * The light being visualized.
  35. *
  36. * @type {PointLight}
  37. */
  38. this.light = light;
  39. /**
  40. * The color parameter passed in the constructor.
  41. * If not set, the helper will take the color of the light.
  42. *
  43. * @type {number|Color|string}
  44. */
  45. this.color = color;
  46. this.type = 'PointLightHelper';
  47. this.matrix = this.light.matrixWorld;
  48. this.matrixAutoUpdate = false;
  49. this.update();
  50. }
  51. /**
  52. * Frees the GPU-related resources allocated by this instance. Call this
  53. * method whenever this instance is no longer used in your app.
  54. */
  55. dispose() {
  56. this.geometry.dispose();
  57. this.material.dispose();
  58. }
  59. /**
  60. * Updates the helper to match the position of the
  61. * light being visualized.
  62. */
  63. update() {
  64. this.light.updateWorldMatrix( true, false );
  65. if ( this.color !== undefined ) {
  66. this.material.color.set( this.color );
  67. } else {
  68. this.material.color.copy( this.light.color );
  69. }
  70. /*
  71. const d = this.light.distance;
  72. if ( d === 0.0 ) {
  73. this.lightDistance.visible = false;
  74. } else {
  75. this.lightDistance.visible = true;
  76. this.lightDistance.scale.set( d, d, d );
  77. }
  78. */
  79. }
  80. }
  81. export { PointLightHelper };
粤ICP备19079148号