LightProbeGenerator.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @author WestLangley / http://github.com/WestLangley
  3. */
  4. import {
  5. Color,
  6. LightProbe,
  7. SphericalHarmonics3,
  8. Vector3
  9. } from "../../../build/three.module.js";
  10. var LightProbeGenerator = {
  11. // https://www.ppsloan.org/publications/StupidSH36.pdf
  12. fromCubeTexture: function ( cubeTexture ) {
  13. var norm, lengthSq, weight, totalWeight = 0;
  14. var coord = new Vector3();
  15. var dir = new Vector3();
  16. var color = new Color();
  17. var shBasis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  18. var sh = new SphericalHarmonics3();
  19. var shCoefficients = sh.coefficients;
  20. for ( var faceIndex = 0; faceIndex < 6; faceIndex ++ ) {
  21. var image = cubeTexture.image[ faceIndex ];
  22. var width = image.width;
  23. var height = image.height;
  24. var canvas = document.createElement( 'canvas' );
  25. canvas.width = width;
  26. canvas.height = height;
  27. var context = canvas.getContext( '2d' );
  28. context.drawImage( image, 0, 0, width, height );
  29. var imageData = context.getImageData( 0, 0, width, height );
  30. var data = imageData.data;
  31. var imageWidth = imageData.width; // assumed to be square
  32. var pixelSize = 2 / imageWidth;
  33. for ( var i = 0, il = data.length; i < il; i += 4 ) { // RGBA assumed
  34. // pixel color
  35. color.setRGB( data[ i ] / 255, data[ i + 1 ] / 255, data[ i + 2 ] / 255 );
  36. // convert to linear color space
  37. color.copySRGBToLinear( color );
  38. // pixel coordinate on unit cube
  39. var pixelIndex = i / 4;
  40. var col = - 1 + ( pixelIndex % imageWidth + 0.5 ) * pixelSize;
  41. var row = 1 - ( Math.floor( pixelIndex / imageWidth ) + 0.5 ) * pixelSize;
  42. switch ( faceIndex ) {
  43. case 0: coord.set( - 1, row, - col ); break;
  44. case 1: coord.set( 1, row, col ); break;
  45. case 2: coord.set( - col, 1, - row ); break;
  46. case 3: coord.set( - col, - 1, row ); break;
  47. case 4: coord.set( - col, row, 1 ); break;
  48. case 5: coord.set( col, row, - 1 ); break;
  49. }
  50. // weight assigned to this pixel
  51. lengthSq = coord.lengthSq();
  52. weight = 4 / ( Math.sqrt( lengthSq ) * lengthSq );
  53. totalWeight += weight;
  54. // direction vector to this pixel
  55. dir.copy( coord ).normalize();
  56. // evaluate SH basis functions in direction dir
  57. SphericalHarmonics3.getBasisAt( dir, shBasis );
  58. // accummuulate
  59. for ( var j = 0; j < 9; j ++ ) {
  60. shCoefficients[ j ].x += shBasis[ j ] * color.r * weight;
  61. shCoefficients[ j ].y += shBasis[ j ] * color.g * weight;
  62. shCoefficients[ j ].z += shBasis[ j ] * color.b * weight;
  63. }
  64. }
  65. }
  66. // normalize
  67. norm = ( 4 * Math.PI ) / totalWeight;
  68. for ( var j = 0; j < 9; j ++ ) {
  69. shCoefficients[ j ].x *= norm;
  70. shCoefficients[ j ].y *= norm;
  71. shCoefficients[ j ].z *= norm;
  72. }
  73. return new LightProbe( sh );
  74. }
  75. };
  76. export { LightProbeGenerator };