LightProbeHelper.js 2.9 KB

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