@@ -28,7 +28,7 @@ THREE.PMREMGenerator = ( function () {
2828 this . samplesPerLevel = ( samplesPerLevel !== undefined ) ? samplesPerLevel : 32 ;
2929
3030 var monotonicEncoding = ( this . sourceTexture . encoding === THREE . LinearEncoding ) ||
31- ( this . sourceTexture . encoding === THREE . GammaEncoding ) || ( this . sourceTexture . encoding === THREE . sRGBEncoding ) ;
31+ ( this . sourceTexture . encoding === THREE . GammaEncoding ) || ( this . sourceTexture . encoding === THREE . sRGBEncoding ) ;
3232
3333 this . sourceTexture . minFilter = ( monotonicEncoding ) ? THREE . LinearFilter : THREE . NearestFilter ;
3434 this . sourceTexture . magFilter = ( monotonicEncoding ) ? THREE . LinearFilter : THREE . NearestFilter ;
@@ -66,18 +66,18 @@ THREE.PMREMGenerator = ( function () {
6666 constructor : PMREMGenerator ,
6767
6868 /*
69- * Prashant Sharma / spidersharma03: More thought and work is needed here.
70- * Right now it's a kind of a hack to use the previously convolved map to convolve the current one.
71- * I tried to use the original map to convolve all the lods, but for many textures(specially the high frequency)
72- * even a high number of samples(1024) dosen't lead to satisfactory results.
73- * By using the previous convolved maps, a lower number of samples are generally sufficient(right now 32, which
74- * gives okay results unless we see the reflection very carefully, or zoom in too much), however the math
75- * goes wrong as the distribution function tries to sample a larger area than what it should be. So I simply scaled
76- * the roughness by 0.9(totally empirical) to try to visually match the original result.
77- * The condition "if(i <5)" is also an attemt to make the result match the original result.
78- * This method requires the most amount of thinking I guess. Here is a paper which we could try to implement in future::
79- * https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch20.html
80- */
69+ * Prashant Sharma / spidersharma03: More thought and work is needed here.
70+ * Right now it's a kind of a hack to use the previously convolved map to convolve the current one.
71+ * I tried to use the original map to convolve all the lods, but for many textures(specially the high frequency)
72+ * even a high number of samples(1024) dosen't lead to satisfactory results.
73+ * By using the previous convolved maps, a lower number of samples are generally sufficient(right now 32, which
74+ * gives okay results unless we see the reflection very carefully, or zoom in too much), however the math
75+ * goes wrong as the distribution function tries to sample a larger area than what it should be. So I simply scaled
76+ * the roughness by 0.9(totally empirical) to try to visually match the original result.
77+ * The condition "if(i <5)" is also an attemt to make the result match the original result.
78+ * This method requires the most amount of thinking I guess. Here is a paper which we could try to implement in future::
79+ * https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch20.html
80+ */
8181 update : function ( renderer ) {
8282
8383 // Texture should only be flipped for CubeTexture, not for
@@ -170,110 +170,110 @@ THREE.PMREMGenerator = ( function () {
170170 } ,
171171
172172 vertexShader :
173- "varying vec2 vUv;\n\
174- void main() {\n\
175- vUv = uv;\n\
176- gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
177- }",
173+ "varying vec2 vUv;\n\
174+ void main() {\n\
175+ vUv = uv;\n\
176+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
177+ }",
178178
179179 fragmentShader :
180- "#include <common>\n\
181- varying vec2 vUv;\n\
182- uniform int faceIndex;\n\
183- uniform float roughness;\n\
184- uniform samplerCube envMap;\n\
185- uniform float mapSize;\n\
186- uniform float tFlip;\n\
187- \n\
188- float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\
189- float a = ggxRoughness + 0.0001;\n\
190- a *= a;\n\
191- return ( 2.0 / a - 2.0 );\n\
192- }\n\
193- vec3 ImportanceSamplePhong(vec2 uv, mat3 vecSpace, float specPow) {\n\
194- float phi = uv.y * 2.0 * PI;\n\
195- float cosTheta = pow(1.0 - uv.x, 1.0 / (specPow + 1.0));\n\
196- float sinTheta = sqrt(1.0 - cosTheta * cosTheta);\n\
197- vec3 sampleDir = vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);\n\
198- return vecSpace * sampleDir;\n\
199- }\n\
200- vec3 ImportanceSampleGGX( vec2 uv, mat3 vecSpace, float Roughness )\n\
201- {\n\
202- float a = Roughness * Roughness;\n\
203- float Phi = 2.0 * PI * uv.x;\n\
204- float CosTheta = sqrt( (1.0 - uv.y) / ( 1.0 + (a*a - 1.0) * uv.y ) );\n\
205- float SinTheta = sqrt( 1.0 - CosTheta * CosTheta );\n\
206- return vecSpace * vec3(SinTheta * cos( Phi ), SinTheta * sin( Phi ), CosTheta);\n\
207- }\n\
208- mat3 matrixFromVector(vec3 n) {\n\
209- float a = 1.0 / (1.0 + n.z);\n\
210- float b = -n.x * n.y * a;\n\
211- vec3 b1 = vec3(1.0 - n.x * n.x * a, b, -n.x);\n\
212- vec3 b2 = vec3(b, 1.0 - n.y * n.y * a, -n.y);\n\
213- return mat3(b1, b2, n);\n\
214- }\n\
215- \n\
216- vec4 testColorMap(float Roughness) {\n\
217- vec4 color;\n\
218- if(faceIndex == 0)\n\
219- color = vec4(1.0,0.0,0.0,1.0);\n\
220- else if(faceIndex == 1)\n\
221- color = vec4(0.0,1.0,0.0,1.0);\n\
222- else if(faceIndex == 2)\n\
223- color = vec4(0.0,0.0,1.0,1.0);\n\
224- else if(faceIndex == 3)\n\
225- color = vec4(1.0,1.0,0.0,1.0);\n\
226- else if(faceIndex == 4)\n\
227- color = vec4(0.0,1.0,1.0,1.0);\n\
228- else\n\
229- color = vec4(1.0,0.0,1.0,1.0);\n\
230- color *= ( 1.0 - Roughness );\n\
231- return color;\n\
232- }\n\
233- void main() {\n\
234- vec3 sampleDirection;\n\
235- vec2 uv = vUv*2.0 - 1.0;\n\
236- float offset = -1.0/mapSize;\n\
237- const float a = -1.0;\n\
238- const float b = 1.0;\n\
239- float c = -1.0 + offset;\n\
240- float d = 1.0 - offset;\n\
241- float bminusa = b - a;\n\
242- uv.x = (uv.x - a)/bminusa * d - (uv.x - b)/bminusa * c;\n\
243- uv.y = (uv.y - a)/bminusa * d - (uv.y - b)/bminusa * c;\n\
244- if (faceIndex==0) {\n\
245- sampleDirection = vec3(1.0, -uv.y, -uv.x);\n\
246- } else if (faceIndex==1) {\n\
247- sampleDirection = vec3(-1.0, -uv.y, uv.x);\n\
248- } else if (faceIndex==2) {\n\
249- sampleDirection = vec3(uv.x, 1.0, uv.y);\n\
250- } else if (faceIndex==3) {\n\
251- sampleDirection = vec3(uv.x, -1.0, -uv.y);\n\
252- } else if (faceIndex==4) {\n\
253- sampleDirection = vec3(uv.x, -uv.y, 1.0);\n\
254- } else {\n\
255- sampleDirection = vec3(-uv.x, -uv.y, -1.0);\n\
256- }\n\
257- vec3 correctedDirection = vec3( tFlip * sampleDirection.x, sampleDirection.yz );\n\
258- mat3 vecSpace = matrixFromVector( normalize( correctedDirection ) );\n\
259- vec3 rgbColor = vec3(0.0);\n\
260- const int NumSamples = SAMPLES_PER_LEVEL;\n\
261- vec3 vect;\n\
262- float weight = 0.0;\n\
263- for( int i = 0; i < NumSamples; i ++ ) {\n\
264- float sini = sin(float(i));\n\
265- float cosi = cos(float(i));\n\
266- float r = rand(vec2(sini, cosi));\n\
267- vect = ImportanceSampleGGX(vec2(float(i) / float(NumSamples), r), vecSpace, roughness);\n\
268- float dotProd = dot(vect, normalize(sampleDirection));\n\
269- weight += dotProd;\n\
270- vec3 color = envMapTexelToLinear(textureCube(envMap, vect)).rgb;\n\
271- rgbColor.rgb += color;\n\
272- }\n\
273- rgbColor /= float(NumSamples);\n\
274- //rgbColor = testColorMap( roughness ).rgb;\n\
275- gl_FragColor = linearToOutputTexel( vec4( rgbColor, 1.0 ) );\n\
276- }",
180+ "#include <common>\n\
181+ varying vec2 vUv;\n\
182+ uniform int faceIndex;\n\
183+ uniform float roughness;\n\
184+ uniform samplerCube envMap;\n\
185+ uniform float mapSize;\n\
186+ uniform float tFlip;\n\
187+ \n\
188+ float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\
189+ float a = ggxRoughness + 0.0001;\n\
190+ a *= a;\n\
191+ return ( 2.0 / a - 2.0 );\n\
192+ }\n\
193+ vec3 ImportanceSamplePhong(vec2 uv, mat3 vecSpace, float specPow) {\n\
194+ float phi = uv.y * 2.0 * PI;\n\
195+ float cosTheta = pow(1.0 - uv.x, 1.0 / (specPow + 1.0));\n\
196+ float sinTheta = sqrt(1.0 - cosTheta * cosTheta);\n\
197+ vec3 sampleDir = vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);\n\
198+ return vecSpace * sampleDir;\n\
199+ }\n\
200+ vec3 ImportanceSampleGGX( vec2 uv, mat3 vecSpace, float Roughness )\n\
201+ {\n\
202+ float a = Roughness * Roughness;\n\
203+ float Phi = 2.0 * PI * uv.x;\n\
204+ float CosTheta = sqrt( (1.0 - uv.y) / ( 1.0 + (a*a - 1.0) * uv.y ) );\n\
205+ float SinTheta = sqrt( 1.0 - CosTheta * CosTheta );\n\
206+ return vecSpace * vec3(SinTheta * cos( Phi ), SinTheta * sin( Phi ), CosTheta);\n\
207+ }\n\
208+ mat3 matrixFromVector(vec3 n) {\n\
209+ float a = 1.0 / (1.0 + n.z);\n\
210+ float b = -n.x * n.y * a;\n\
211+ vec3 b1 = vec3(1.0 - n.x * n.x * a, b, -n.x);\n\
212+ vec3 b2 = vec3(b, 1.0 - n.y * n.y * a, -n.y);\n\
213+ return mat3(b1, b2, n);\n\
214+ }\n\
215+ \n\
216+ vec4 testColorMap(float Roughness) {\n\
217+ vec4 color;\n\
218+ if(faceIndex == 0)\n\
219+ color = vec4(1.0,0.0,0.0,1.0);\n\
220+ else if(faceIndex == 1)\n\
221+ color = vec4(0.0,1.0,0.0,1.0);\n\
222+ else if(faceIndex == 2)\n\
223+ color = vec4(0.0,0.0,1.0,1.0);\n\
224+ else if(faceIndex == 3)\n\
225+ color = vec4(1.0,1.0,0.0,1.0);\n\
226+ else if(faceIndex == 4)\n\
227+ color = vec4(0.0,1.0,1.0,1.0);\n\
228+ else\n\
229+ color = vec4(1.0,0.0,1.0,1.0);\n\
230+ color *= ( 1.0 - Roughness );\n\
231+ return color;\n\
232+ }\n\
233+ void main() {\n\
234+ vec3 sampleDirection;\n\
235+ vec2 uv = vUv*2.0 - 1.0;\n\
236+ float offset = -1.0/mapSize;\n\
237+ const float a = -1.0;\n\
238+ const float b = 1.0;\n\
239+ float c = -1.0 + offset;\n\
240+ float d = 1.0 - offset;\n\
241+ float bminusa = b - a;\n\
242+ uv.x = (uv.x - a)/bminusa * d - (uv.x - b)/bminusa * c;\n\
243+ uv.y = (uv.y - a)/bminusa * d - (uv.y - b)/bminusa * c;\n\
244+ if (faceIndex==0) {\n\
245+ sampleDirection = vec3(1.0, -uv.y, -uv.x);\n\
246+ } else if (faceIndex==1) {\n\
247+ sampleDirection = vec3(-1.0, -uv.y, uv.x);\n\
248+ } else if (faceIndex==2) {\n\
249+ sampleDirection = vec3(uv.x, 1.0, uv.y);\n\
250+ } else if (faceIndex==3) {\n\
251+ sampleDirection = vec3(uv.x, -1.0, -uv.y);\n\
252+ } else if (faceIndex==4) {\n\
253+ sampleDirection = vec3(uv.x, -uv.y, 1.0);\n\
254+ } else {\n\
255+ sampleDirection = vec3(-uv.x, -uv.y, -1.0);\n\
256+ }\n\
257+ vec3 correctedDirection = vec3( tFlip * sampleDirection.x, sampleDirection.yz );\n\
258+ mat3 vecSpace = matrixFromVector( normalize( correctedDirection ) );\n\
259+ vec3 rgbColor = vec3(0.0);\n\
260+ const int NumSamples = SAMPLES_PER_LEVEL;\n\
261+ vec3 vect;\n\
262+ float weight = 0.0;\n\
263+ for( int i = 0; i < NumSamples; i ++ ) {\n\
264+ float sini = sin(float(i));\n\
265+ float cosi = cos(float(i));\n\
266+ float r = rand(vec2(sini, cosi));\n\
267+ vect = ImportanceSampleGGX(vec2(float(i) / float(NumSamples), r), vecSpace, roughness);\n\
268+ float dotProd = dot(vect, normalize(sampleDirection));\n\
269+ weight += dotProd;\n\
270+ vec3 color = envMapTexelToLinear(textureCube(envMap, vect)).rgb;\n\
271+ rgbColor.rgb += color;\n\
272+ }\n\
273+ rgbColor /= float(NumSamples);\n\
274+ //rgbColor = testColorMap( roughness ).rgb;\n\
275+ gl_FragColor = linearToOutputTexel( vec4( rgbColor, 1.0 ) );\n\
276+ }",
277277
278278 blending : THREE . NoBlending
279279
0 commit comments