LightProbeHelper.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import {
  2. Mesh,
  3. ShaderMaterial,
  4. SphereGeometry
  5. } from 'three';
  6. class LightProbeHelper extends Mesh {
  7. constructor( lightProbe, size = 1 ) {
  8. const material = new ShaderMaterial( {
  9. type: 'LightProbeHelperMaterial',
  10. uniforms: {
  11. sh: { value: lightProbe.sh.coefficients }, // by reference
  12. intensity: { value: lightProbe.intensity }
  13. },
  14. vertexShader: /* glsl */`
  15. varying vec3 vNormal;
  16. void main() {
  17. vNormal = normalize( normalMatrix * normal );
  18. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  19. }
  20. `,
  21. fragmentShader: /* glsl */`
  22. #define RECIPROCAL_PI 0.318309886
  23. vec3 inverseTransformDirection( in vec3 normal, in mat4 matrix ) {
  24. // matrix is assumed to be orthogonal
  25. return normalize( ( vec4( normal, 0.0 ) * matrix ).xyz );
  26. }
  27. // source: https://graphics.stanford.edu/papers/envmap/envmap.pdf,
  28. vec3 shGetIrradianceAt( in vec3 normal, in vec3 shCoefficients[ 9 ] ) {
  29. // normal is assumed to have unit length,
  30. float x = normal.x, y = normal.y, z = normal.z;
  31. // band 0,
  32. vec3 result = shCoefficients[ 0 ] * 0.886227;
  33. // band 1,
  34. result += shCoefficients[ 1 ] * 2.0 * 0.511664 * y;
  35. result += shCoefficients[ 2 ] * 2.0 * 0.511664 * z;
  36. result += shCoefficients[ 3 ] * 2.0 * 0.511664 * x;
  37. // band 2,
  38. result += shCoefficients[ 4 ] * 2.0 * 0.429043 * x * y;
  39. result += shCoefficients[ 5 ] * 2.0 * 0.429043 * y * z;
  40. result += shCoefficients[ 6 ] * ( 0.743125 * z * z - 0.247708 );
  41. result += shCoefficients[ 7 ] * 2.0 * 0.429043 * x * z;
  42. result += shCoefficients[ 8 ] * 0.429043 * ( x * x - y * y );
  43. return result;
  44. }
  45. uniform vec3 sh[ 9 ]; // sh coefficients
  46. uniform float intensity; // light probe intensity
  47. varying vec3 vNormal;
  48. void main() {
  49. vec3 normal = normalize( vNormal );
  50. vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
  51. vec3 irradiance = shGetIrradianceAt( worldNormal, sh );
  52. vec3 outgoingLight = RECIPROCAL_PI * irradiance * intensity;
  53. gl_FragColor = linearToOutputTexel( vec4( outgoingLight, 1.0 ) );
  54. }
  55. `,
  56. } );
  57. const geometry = new SphereGeometry( 1, 32, 16 );
  58. super( geometry, material );
  59. this.lightProbe = lightProbe;
  60. this.size = size;
  61. this.type = 'LightProbeHelper';
  62. this.onBeforeRender();
  63. }
  64. dispose() {
  65. this.geometry.dispose();
  66. this.material.dispose();
  67. }
  68. onBeforeRender() {
  69. this.position.copy( this.lightProbe.position );
  70. this.scale.set( 1, 1, 1 ).multiplyScalar( this.size );
  71. this.material.uniforms.intensity.value = this.lightProbe.intensity;
  72. }
  73. }
  74. export { LightProbeHelper };