LightProbeHelper.js 3.2 KB

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