LightProbeHelper.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import {
  2. Mesh,
  3. ShaderMaterial,
  4. SphereGeometry
  5. } from 'three';
  6. /**
  7. * Renders a sphere to visualize a light probe in the scene.
  8. *
  9. * This helper can only be used with {@link WebGLRenderer}.
  10. * When using {@link WebGPURenderer}, import from `LightProbeHelperGPU.js`.
  11. *
  12. * ```js
  13. * const helper = new LightProbeHelper( lightProbe );
  14. * scene.add( helper );
  15. * ```
  16. *
  17. * @augments Mesh
  18. * @three_import import { LightProbeHelper } from 'three/addons/helpers/LightProbeHelper.js';
  19. */
  20. class LightProbeHelper extends Mesh {
  21. /**
  22. * Constructs a new light probe helper.
  23. *
  24. * @param {LightProbe} lightProbe - The light probe to visualize.
  25. * @param {number} [size=1] - The size of the helper.
  26. */
  27. constructor( lightProbe, size = 1 ) {
  28. const material = new ShaderMaterial( {
  29. type: 'LightProbeHelperMaterial',
  30. uniforms: {
  31. sh: { value: lightProbe.sh.coefficients }, // by reference
  32. intensity: { value: lightProbe.intensity }
  33. },
  34. vertexShader: /* glsl */`
  35. varying vec3 vNormal;
  36. void main() {
  37. vNormal = normalize( normalMatrix * normal );
  38. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  39. }
  40. `,
  41. fragmentShader: /* glsl */`
  42. #define RECIPROCAL_PI 0.318309886
  43. vec3 inverseTransformDirection( in vec3 normal, in mat4 matrix ) {
  44. // matrix is assumed to be orthogonal
  45. return normalize( ( vec4( normal, 0.0 ) * matrix ).xyz );
  46. }
  47. // source: https://graphics.stanford.edu/papers/envmap/envmap.pdf,
  48. vec3 shGetIrradianceAt( in vec3 normal, in vec3 shCoefficients[ 9 ] ) {
  49. // normal is assumed to have unit length,
  50. float x = normal.x, y = normal.y, z = normal.z;
  51. // band 0,
  52. vec3 result = shCoefficients[ 0 ] * 0.886227;
  53. // band 1,
  54. result += shCoefficients[ 1 ] * 2.0 * 0.511664 * y;
  55. result += shCoefficients[ 2 ] * 2.0 * 0.511664 * z;
  56. result += shCoefficients[ 3 ] * 2.0 * 0.511664 * x;
  57. // band 2,
  58. result += shCoefficients[ 4 ] * 2.0 * 0.429043 * x * y;
  59. result += shCoefficients[ 5 ] * 2.0 * 0.429043 * y * z;
  60. result += shCoefficients[ 6 ] * ( 0.743125 * z * z - 0.247708 );
  61. result += shCoefficients[ 7 ] * 2.0 * 0.429043 * x * z;
  62. result += shCoefficients[ 8 ] * 0.429043 * ( x * x - y * y );
  63. return result;
  64. }
  65. uniform vec3 sh[ 9 ]; // sh coefficients
  66. uniform float intensity; // light probe intensity
  67. varying vec3 vNormal;
  68. void main() {
  69. vec3 normal = normalize( vNormal );
  70. vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
  71. vec3 irradiance = shGetIrradianceAt( worldNormal, sh );
  72. vec3 outgoingLight = RECIPROCAL_PI * irradiance * intensity;
  73. gl_FragColor = linearToOutputTexel( vec4( outgoingLight, 1.0 ) );
  74. }
  75. `,
  76. } );
  77. const geometry = new SphereGeometry( 1, 32, 16 );
  78. super( geometry, material );
  79. /**
  80. * The light probe to visualize.
  81. *
  82. * @type {LightProbe}
  83. */
  84. this.lightProbe = lightProbe;
  85. /**
  86. * The size of the helper.
  87. *
  88. * @type {number}
  89. * @default 1
  90. */
  91. this.size = size;
  92. this.type = 'LightProbeHelper';
  93. this.onBeforeRender();
  94. }
  95. /**
  96. * Frees the GPU-related resources allocated by this instance. Call this
  97. * method whenever this instance is no longer used in your app.
  98. */
  99. dispose() {
  100. this.geometry.dispose();
  101. this.material.dispose();
  102. }
  103. onBeforeRender() {
  104. this.position.copy( this.lightProbe.position );
  105. this.scale.set( 1, 1, 1 ).multiplyScalar( this.size );
  106. this.material.uniforms.intensity.value = this.lightProbe.intensity;
  107. }
  108. }
  109. export { LightProbeHelper };