|
@@ -5,8 +5,10 @@
|
|
import {
|
|
import {
|
|
Color,
|
|
Color,
|
|
LightProbe,
|
|
LightProbe,
|
|
|
|
+ LinearEncoding,
|
|
SphericalHarmonics3,
|
|
SphericalHarmonics3,
|
|
- Vector3
|
|
|
|
|
|
+ Vector3,
|
|
|
|
+ sRGBEncoding
|
|
} from "../../../build/three.module.js";
|
|
} from "../../../build/three.module.js";
|
|
|
|
|
|
var LightProbeGenerator = {
|
|
var LightProbeGenerator = {
|
|
@@ -57,7 +59,7 @@ var LightProbeGenerator = {
|
|
color.setRGB( data[ i ] / 255, data[ i + 1 ] / 255, data[ i + 2 ] / 255 );
|
|
color.setRGB( data[ i ] / 255, data[ i + 1 ] / 255, data[ i + 2 ] / 255 );
|
|
|
|
|
|
// convert to linear color space
|
|
// convert to linear color space
|
|
- color.copySRGBToLinear( color );
|
|
|
|
|
|
+ convertColorToLinear( color, cubeTexture.encoding );
|
|
|
|
|
|
// pixel coordinate on unit cube
|
|
// pixel coordinate on unit cube
|
|
|
|
|
|
@@ -123,8 +125,130 @@ var LightProbeGenerator = {
|
|
|
|
|
|
return new LightProbe( sh );
|
|
return new LightProbe( sh );
|
|
|
|
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ fromRenderTargetCube: function ( renderer, renderTargetCube ) {
|
|
|
|
+
|
|
|
|
+ // The renderTarget must be set to RGBA in order to make readRenderTargetPixels works
|
|
|
|
+ var norm, lengthSq, weight, totalWeight = 0;
|
|
|
|
+
|
|
|
|
+ var coord = new Vector3();
|
|
|
|
+
|
|
|
|
+ var dir = new Vector3();
|
|
|
|
+
|
|
|
|
+ var color = new Color();
|
|
|
|
+
|
|
|
|
+ var shBasis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
|
|
|
|
+
|
|
|
|
+ var sh = new SphericalHarmonics3();
|
|
|
|
+ var shCoefficients = sh.coefficients;
|
|
|
|
+
|
|
|
|
+ for ( var faceIndex = 0; faceIndex < 6; faceIndex ++ ) {
|
|
|
|
+
|
|
|
|
+ var imageWidth = renderTargetCube.width; // assumed to be square
|
|
|
|
+ var data = new Uint8Array( imageWidth * imageWidth * 4 );
|
|
|
|
+ renderer.readRenderTargetPixels( renderTargetCube, 0, 0, imageWidth, imageWidth, data, faceIndex );
|
|
|
|
+
|
|
|
|
+ var pixelSize = 2 / imageWidth;
|
|
|
|
+
|
|
|
|
+ for ( var i = 0, il = data.length; i < il; i += 4 ) { // RGBA assumed
|
|
|
|
+
|
|
|
|
+ // pixel color
|
|
|
|
+ color.setRGB( data[ i ] / 255, data[ i + 1 ] / 255, data[ i + 2 ] / 255 );
|
|
|
|
+
|
|
|
|
+ // convert to linear color space
|
|
|
|
+ convertColorToLinear( color, renderTargetCube.texture.encoding );
|
|
|
|
+
|
|
|
|
+ // pixel coordinate on unit cube
|
|
|
|
+
|
|
|
|
+ var pixelIndex = i / 4;
|
|
|
|
+
|
|
|
|
+ var col = - 1 + ( pixelIndex % imageWidth + 0.5 ) * pixelSize;
|
|
|
|
+
|
|
|
|
+ var row = 1 - ( Math.floor( pixelIndex / imageWidth ) + 0.5 ) * pixelSize;
|
|
|
|
+
|
|
|
|
+ switch ( faceIndex ) {
|
|
|
|
+
|
|
|
|
+ case 0: coord.set( 1, row, - col ); break;
|
|
|
|
+
|
|
|
|
+ case 1: coord.set( - 1, row, col ); break;
|
|
|
|
+
|
|
|
|
+ case 2: coord.set( col, 1, - row ); break;
|
|
|
|
+
|
|
|
|
+ case 3: coord.set( col, - 1, row ); break;
|
|
|
|
+
|
|
|
|
+ case 4: coord.set( col, row, 1 ); break;
|
|
|
|
+
|
|
|
|
+ case 5: coord.set( - col, row, - 1 ); break;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // weight assigned to this pixel
|
|
|
|
+
|
|
|
|
+ lengthSq = coord.lengthSq();
|
|
|
|
+
|
|
|
|
+ weight = 4 / ( Math.sqrt( lengthSq ) * lengthSq );
|
|
|
|
+
|
|
|
|
+ totalWeight += weight;
|
|
|
|
+
|
|
|
|
+ // direction vector to this pixel
|
|
|
|
+ dir.copy( coord ).normalize();
|
|
|
|
+
|
|
|
|
+ // evaluate SH basis functions in direction dir
|
|
|
|
+ SphericalHarmonics3.getBasisAt( dir, shBasis );
|
|
|
|
+
|
|
|
|
+ // accummuulate
|
|
|
|
+ for ( var j = 0; j < 9; j ++ ) {
|
|
|
|
+
|
|
|
|
+ shCoefficients[ j ].x += shBasis[ j ] * color.r * weight;
|
|
|
|
+ shCoefficients[ j ].y += shBasis[ j ] * color.g * weight;
|
|
|
|
+ shCoefficients[ j ].z += shBasis[ j ] * color.b * weight;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // normalize
|
|
|
|
+ norm = ( 4 * Math.PI ) / totalWeight;
|
|
|
|
+
|
|
|
|
+ for ( var j = 0; j < 9; j ++ ) {
|
|
|
|
+
|
|
|
|
+ shCoefficients[ j ].x *= norm;
|
|
|
|
+ shCoefficients[ j ].y *= norm;
|
|
|
|
+ shCoefficients[ j ].z *= norm;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return new LightProbe( sh );
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+var convertColorToLinear = function ( color, encoding ) {
|
|
|
|
+
|
|
|
|
+ switch ( encoding ) {
|
|
|
|
+
|
|
|
|
+ case sRGBEncoding:
|
|
|
|
+
|
|
|
|
+ color.convertSRGBToLinear();
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case LinearEncoding:
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+
|
|
|
|
+ console.warn( 'WARNING: LightProbeGenerator convertColorToLinear() encountered an unsupported encoding.' );
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return color;
|
|
|
|
+
|
|
|
|
+};
|
|
|
|
+
|
|
export { LightProbeGenerator };
|
|
export { LightProbeGenerator };
|