Skip to content

Commit a229ffd

Browse files
authored
Merge pull request #15302 from jsantell/15301
PMREMGenerator flips cube textures from WebGLRenderTargetCube
2 parents 88f2493 + 56e29c8 commit a229ffd

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

examples/js/pmrem/PMREMGenerator.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ THREE.PMREMGenerator = function ( sourceTexture, samplesPerLevel, resolution ) {
1717
this.resolution = ( resolution !== undefined ) ? resolution : 256; // NODE: 256 is currently hard coded in the glsl code for performance reasons
1818
this.samplesPerLevel = ( samplesPerLevel !== undefined ) ? samplesPerLevel : 16;
1919

20-
var monotonicEncoding = ( sourceTexture.encoding === THREE.LinearEncoding ) ||
21-
( sourceTexture.encoding === THREE.GammaEncoding ) || ( sourceTexture.encoding === THREE.sRGBEncoding );
20+
var monotonicEncoding = ( this.sourceTexture.encoding === THREE.LinearEncoding ) ||
21+
( this.sourceTexture.encoding === THREE.GammaEncoding ) || ( this.sourceTexture.encoding === THREE.sRGBEncoding );
2222

2323
this.sourceTexture.minFilter = ( monotonicEncoding ) ? THREE.LinearFilter : THREE.NearestFilter;
2424
this.sourceTexture.magFilter = ( monotonicEncoding ) ? THREE.LinearFilter : THREE.NearestFilter;
@@ -82,6 +82,9 @@ THREE.PMREMGenerator.prototype = {
8282
* http://http.developer.nvidia.com/GPUGems3/gpugems3_ch20.html
8383
*/
8484
update: function ( renderer ) {
85+
// Texture should only be flipped for CubeTexture, not for
86+
// a Texture created via THREE.WebGLRenderTargetCube.
87+
var tFlip = ( this.sourceTexture.isCubeTexture ) ? - 1 : 1;
8588

8689
this.shader.uniforms[ 'envMap' ].value = this.sourceTexture;
8790
this.shader.envMap = this.sourceTexture;
@@ -101,7 +104,8 @@ THREE.PMREMGenerator.prototype = {
101104

102105
var r = i / ( this.numLods - 1 );
103106
this.shader.uniforms[ 'roughness' ].value = r * 0.9; // see comment above, pragmatic choice
104-
this.shader.uniforms[ 'queryScale' ].value.x = ( i == 0 ) ? - 1 : 1;
107+
// Only apply the tFlip for the first LOD
108+
this.shader.uniforms[ 'tFlip' ].value = ( i == 0 ) ? tFlip : 1;
105109
var size = this.cubeLods[ i ].width;
106110
this.shader.uniforms[ 'mapSize' ].value = size;
107111
this.renderToCubeMapTarget( renderer, this.cubeLods[ i ] );
@@ -149,8 +153,8 @@ THREE.PMREMGenerator.prototype = {
149153
"roughness": { value: 0.5 },
150154
"mapSize": { value: 0.5 },
151155
"envMap": { value: null },
152-
"queryScale": { value: new THREE.Vector3( 1, 1, 1 ) },
153156
"testColor": { value: new THREE.Vector3( 1, 1, 1 ) },
157+
"tFlip": { value: - 1 },
154158
},
155159

156160
vertexShader:
@@ -168,7 +172,7 @@ THREE.PMREMGenerator.prototype = {
168172
uniform samplerCube envMap;\n\
169173
uniform float mapSize;\n\
170174
uniform vec3 testColor;\n\
171-
uniform vec3 queryScale;\n\
175+
uniform float tFlip;\n\
172176
\n\
173177
float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\
174178
float a = ggxRoughness + 0.0001;\n\
@@ -239,7 +243,8 @@ THREE.PMREMGenerator.prototype = {
239243
} else {\n\
240244
sampleDirection = vec3(-uv.x, -uv.y, -1.0);\n\
241245
}\n\
242-
mat3 vecSpace = matrixFromVector(normalize(sampleDirection * queryScale));\n\
246+
vec3 correctedDirection = vec3( tFlip * sampleDirection.x, sampleDirection.yz );\n\
247+
mat3 vecSpace = matrixFromVector( normalize( correctedDirection ) );\n\
243248
vec3 rgbColor = vec3(0.0);\n\
244249
const int NumSamples = SAMPLES_PER_LEVEL;\n\
245250
vec3 vect;\n\
@@ -251,7 +256,7 @@ THREE.PMREMGenerator.prototype = {
251256
vect = ImportanceSampleGGX(vec2(float(i) / float(NumSamples), r), vecSpace, roughness);\n\
252257
float dotProd = dot(vect, normalize(sampleDirection));\n\
253258
weight += dotProd;\n\
254-
vec3 color = envMapTexelToLinear(textureCube(envMap,vect)).rgb;\n\
259+
vec3 color = envMapTexelToLinear(textureCube(envMap, vect)).rgb;\n\
255260
rgbColor.rgb += color;\n\
256261
}\n\
257262
rgbColor /= float(NumSamples);\n\

0 commit comments

Comments
 (0)