LightProbeGenerator.js 2.6 KB

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