LightProbeHelper.js 3.4 KB

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