LightProbeHelper.js 2.9 KB

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