2 * @author Prashant Sharma / spidersharma03
3 * @author Ben Houston / bhouston, https://clara.io
5 * To avoid cube map seams, I create an extra pixel around each face. This way when the cube map is
6 * sampled by an application later(with a little care by sampling the centre of the texel), the extra 1 border
7 * of pixels makes sure that there is no seams artifacts present. This works perfectly for cubeUV format as
8 * well where the 6 faces can be arranged in any manner whatsoever.
9 * Code in the beginning of fragment shader's main function does this job for a given resolution.
10 * Run Scene_PMREM_Test.html in the examples directory to see the sampling from the cube lods generated
14 THREE.PMREMGenerator = function( sourceTexture, samplesPerLevel, resolution ) {
16 this.sourceTexture = sourceTexture;
17 this.resolution = ( resolution !== undefined ) ? resolution : 256; // NODE: 256 is currently hard coded in the glsl code for performance reasons
18 this.samplesPerLevel = ( samplesPerLevel !== undefined ) ? samplesPerLevel : 16;
20 var monotonicEncoding = ( sourceTexture.encoding === THREE.LinearEncoding ) ||
21 ( sourceTexture.encoding === THREE.GammaEncoding ) || ( sourceTexture.encoding === THREE.sRGBEncoding );
23 this.sourceTexture.minFilter = ( monotonicEncoding ) ? THREE.LinearFilter : THREE.NearestFilter;
24 this.sourceTexture.magFilter = ( monotonicEncoding ) ? THREE.LinearFilter : THREE.NearestFilter;
25 this.sourceTexture.generateMipmaps = this.sourceTexture.generateMipmaps && monotonicEncoding;
29 var size = this.resolution;
31 format: this.sourceTexture.format,
32 magFilter: this.sourceTexture.magFilter,
33 minFilter: this.sourceTexture.minFilter,
34 type: this.sourceTexture.type,
35 generateMipmaps: this.sourceTexture.generateMipmaps,
36 anisotropy: this.sourceTexture.anisotropy,
37 encoding: this.sourceTexture.encoding
40 // how many LODs fit in the given CubeUV Texture.
41 this.numLods = Math.log( size ) / Math.log( 2 ) - 2; // IE11 doesn't support Math.log2
43 for ( var i = 0; i < this.numLods; i ++ ) {
45 var renderTarget = new THREE.WebGLRenderTargetCube( size, size, params );
46 renderTarget.texture.name = "PMREMGenerator.cube" + i;
47 this.cubeLods.push( renderTarget );
48 size = Math.max( 16, size / 2 );
52 this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0.0, 1000 );
54 this.shader = this.getShader();
55 this.shader.defines['SAMPLES_PER_LEVEL'] = this.samplesPerLevel;
56 this.planeMesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2, 0 ), this.shader );
57 this.planeMesh.material.side = THREE.DoubleSide;
58 this.scene = new THREE.Scene();
59 this.scene.add( this.planeMesh );
60 this.scene.add( this.camera );
62 this.shader.uniforms[ 'envMap' ].value = this.sourceTexture;
63 this.shader.envMap = this.sourceTexture;
67 THREE.PMREMGenerator.prototype = {
69 constructor : THREE.PMREMGenerator,
72 * Prashant Sharma / spidersharma03: More thought and work is needed here.
73 * Right now it's a kind of a hack to use the previously convolved map to convolve the current one.
74 * I tried to use the original map to convolve all the lods, but for many textures(specially the high frequency)
75 * even a high number of samples(1024) dosen't lead to satisfactory results.
76 * By using the previous convolved maps, a lower number of samples are generally sufficient(right now 32, which
77 * gives okay results unless we see the reflection very carefully, or zoom in too much), however the math
78 * goes wrong as the distribution function tries to sample a larger area than what it should be. So I simply scaled
79 * the roughness by 0.9(totally empirical) to try to visually match the original result.
80 * The condition "if(i <5)" is also an attemt to make the result match the original result.
81 * This method requires the most amount of thinking I guess. Here is a paper which we could try to implement in future::
82 * http://http.developer.nvidia.com/GPUGems3/gpugems3_ch20.html
84 update: function( renderer ) {
86 this.shader.uniforms[ 'envMap' ].value = this.sourceTexture;
87 this.shader.envMap = this.sourceTexture;
89 var gammaInput = renderer.gammaInput;
90 var gammaOutput = renderer.gammaOutput;
91 var toneMapping = renderer.toneMapping;
92 var toneMappingExposure = renderer.toneMappingExposure;
94 renderer.toneMapping = THREE.LinearToneMapping;
95 renderer.toneMappingExposure = 1.0;
96 renderer.gammaInput = false;
97 renderer.gammaOutput = false;
99 for ( var i = 0; i < this.numLods; i ++ ) {
101 var r = i / ( this.numLods - 1 );
102 this.shader.uniforms[ 'roughness' ].value = r * 0.9; // see comment above, pragmatic choice
103 this.shader.uniforms[ 'queryScale' ].value.x = ( i == 0 ) ? -1 : 1;
104 var size = this.cubeLods[ i ].width;
105 this.shader.uniforms[ 'mapSize' ].value = size;
106 this.renderToCubeMapTarget( renderer, this.cubeLods[ i ] );
108 if ( i < 5 ) this.shader.uniforms[ 'envMap' ].value = this.cubeLods[ i ].texture;
112 renderer.toneMapping = toneMapping;
113 renderer.toneMappingExposure = toneMappingExposure;
114 renderer.gammaInput = gammaInput;
115 renderer.gammaOutput = gammaOutput;
119 renderToCubeMapTarget: function( renderer, renderTarget ) {
121 for ( var i = 0; i < 6; i ++ ) {
123 this.renderToCubeMapTargetFace( renderer, renderTarget, i )
129 renderToCubeMapTargetFace: function( renderer, renderTarget, faceIndex ) {
131 renderTarget.activeCubeFace = faceIndex;
132 this.shader.uniforms[ 'faceIndex' ].value = faceIndex;
133 renderer.render( this.scene, this.camera, renderTarget, true );
137 getShader: function() {
139 return new THREE.ShaderMaterial( {
142 "SAMPLES_PER_LEVEL": 20,
146 "faceIndex": { value: 0 },
147 "roughness": { value: 0.5 },
148 "mapSize": { value: 0.5 },
149 "envMap": { value: null },
150 "queryScale": { value: new THREE.Vector3( 1, 1, 1 ) },
151 "testColor": { value: new THREE.Vector3( 1, 1, 1 ) },
155 "varying vec2 vUv;\n\
158 gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
162 "#include <common>\n\
164 uniform int faceIndex;\n\
165 uniform float roughness;\n\
166 uniform samplerCube envMap;\n\
167 uniform float mapSize;\n\
168 uniform vec3 testColor;\n\
169 uniform vec3 queryScale;\n\
171 float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\
172 float a = ggxRoughness + 0.0001;\n\
174 return ( 2.0 / a - 2.0 );\n\
176 vec3 ImportanceSamplePhong(vec2 uv, mat3 vecSpace, float specPow) {\n\
177 float phi = uv.y * 2.0 * PI;\n\
178 float cosTheta = pow(1.0 - uv.x, 1.0 / (specPow + 1.0));\n\
179 float sinTheta = sqrt(1.0 - cosTheta * cosTheta);\n\
180 vec3 sampleDir = vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);\n\
181 return vecSpace * sampleDir;\n\
183 vec3 ImportanceSampleGGX( vec2 uv, mat3 vecSpace, float Roughness )\n\
185 float a = Roughness * Roughness;\n\
186 float Phi = 2.0 * PI * uv.x;\n\
187 float CosTheta = sqrt( (1.0 - uv.y) / ( 1.0 + (a*a - 1.0) * uv.y ) );\n\
188 float SinTheta = sqrt( 1.0 - CosTheta * CosTheta );\n\
189 return vecSpace * vec3(SinTheta * cos( Phi ), SinTheta * sin( Phi ), CosTheta);\n\
191 mat3 matrixFromVector(vec3 n) {\n\
192 float a = 1.0 / (1.0 + n.z);\n\
193 float b = -n.x * n.y * a;\n\
194 vec3 b1 = vec3(1.0 - n.x * n.x * a, b, -n.x);\n\
195 vec3 b2 = vec3(b, 1.0 - n.y * n.y * a, -n.y);\n\
196 return mat3(b1, b2, n);\n\
199 vec4 testColorMap(float Roughness) {\n\
201 if(faceIndex == 0)\n\
202 color = vec4(1.0,0.0,0.0,1.0);\n\
203 else if(faceIndex == 1)\n\
204 color = vec4(0.0,1.0,0.0,1.0);\n\
205 else if(faceIndex == 2)\n\
206 color = vec4(0.0,0.0,1.0,1.0);\n\
207 else if(faceIndex == 3)\n\
208 color = vec4(1.0,1.0,0.0,1.0);\n\
209 else if(faceIndex == 4)\n\
210 color = vec4(0.0,1.0,1.0,1.0);\n\
212 color = vec4(1.0,0.0,1.0,1.0);\n\
213 color *= ( 1.0 - Roughness );\n\
217 vec3 sampleDirection;\n\
218 vec2 uv = vUv*2.0 - 1.0;\n\
219 float offset = -1.0/mapSize;\n\
220 const float a = -1.0;\n\
221 const float b = 1.0;\n\
222 float c = -1.0 + offset;\n\
223 float d = 1.0 - offset;\n\
224 float bminusa = b - a;\n\
225 uv.x = (uv.x - a)/bminusa * d - (uv.x - b)/bminusa * c;\n\
226 uv.y = (uv.y - a)/bminusa * d - (uv.y - b)/bminusa * c;\n\
227 if (faceIndex==0) {\n\
228 sampleDirection = vec3(1.0, -uv.y, -uv.x);\n\
229 } else if (faceIndex==1) {\n\
230 sampleDirection = vec3(-1.0, -uv.y, uv.x);\n\
231 } else if (faceIndex==2) {\n\
232 sampleDirection = vec3(uv.x, 1.0, uv.y);\n\
233 } else if (faceIndex==3) {\n\
234 sampleDirection = vec3(uv.x, -1.0, -uv.y);\n\
235 } else if (faceIndex==4) {\n\
236 sampleDirection = vec3(uv.x, -uv.y, 1.0);\n\
238 sampleDirection = vec3(-uv.x, -uv.y, -1.0);\n\
240 mat3 vecSpace = matrixFromVector(normalize(sampleDirection * queryScale));\n\
241 vec3 rgbColor = vec3(0.0);\n\
242 const int NumSamples = SAMPLES_PER_LEVEL;\n\
244 float weight = 0.0;\n\
245 for( int i = 0; i < NumSamples; i ++ ) {\n\
246 float sini = sin(float(i));\n\
247 float cosi = cos(float(i));\n\
248 float r = rand(vec2(sini, cosi));\n\
249 vect = ImportanceSampleGGX(vec2(float(i) / float(NumSamples), r), vecSpace, roughness);\n\
250 float dotProd = dot(vect, normalize(sampleDirection));\n\
251 weight += dotProd;\n\
252 vec3 color = envMapTexelToLinear(textureCube(envMap,vect)).rgb;\n\
253 rgbColor.rgb += color;\n\
255 rgbColor /= float(NumSamples);\n\
256 //rgbColor = testColorMap( roughness ).rgb;\n\
257 gl_FragColor = linearToOutputTexel( vec4( rgbColor, 1.0 ) );\n\
259 blending: THREE.CustomBlending,
260 blendSrc: THREE.OneFactor,
261 blendDst: THREE.ZeroFactor,
262 blendSrcAlpha: THREE.OneFactor,
263 blendDstAlpha: THREE.ZeroFactor,
264 blendEquation: THREE.AddEquation