PMREMGenerator.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. * @author Prashant Sharma / spidersharma03
  3. * @author Ben Houston / bhouston, https://clara.io
  4. *
  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
  11. * by this class.
  12. */
  13. THREE.PMREMGenerator = function( sourceTexture ) {
  14. this.sourceTexture = sourceTexture;
  15. this.resolution = 256; // NODE: 256 is currently hard coded in the glsl code for performance reasons
  16. var monotonicEncoding = ( sourceTexture.encoding === THREE.LinearEncoding ) ||
  17. ( sourceTexture.encoding === THREE.GammaEncoding ) || ( sourceTexture.encoding === THREE.sRGBEncoding );
  18. this.sourceTexture.minFilter = ( monotonicEncoding ) ? THREE.LinearFilter : THREE.NearestFilter;
  19. this.sourceTexture.magFilter = ( monotonicEncoding ) ? THREE.LinearFilter : THREE.NearestFilter;
  20. this.sourceTexture.generateMipmaps = this.sourceTexture.generateMipmaps && monotonicEncoding;
  21. this.cubeLods = [];
  22. var size = this.resolution;
  23. var params = {
  24. format: this.sourceTexture.format,
  25. magFilter: this.sourceTexture.magFilter,
  26. minFilter: this.sourceTexture.minFilter,
  27. type: this.sourceTexture.type,
  28. generateMipmaps: this.sourceTexture.generateMipmaps,
  29. anisotropy: this.sourceTexture.anisotropy,
  30. encoding: this.sourceTexture.encoding
  31. };
  32. // how many LODs fit in the given CubeUV Texture.
  33. this.numLods = Math.log2( size ) - 2;
  34. for ( var i = 0; i < this.numLods; i ++ ) {
  35. var renderTarget = new THREE.WebGLRenderTargetCube( size, size, params );
  36. this.cubeLods.push( renderTarget );
  37. size = Math.max( 16, size / 2 );
  38. }
  39. this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0.0, 1000 );
  40. this.shader = this.getShader();
  41. this.planeMesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2, 0 ), this.shader );
  42. this.planeMesh.material.side = THREE.DoubleSide;
  43. this.scene = new THREE.Scene();
  44. this.scene.add( this.planeMesh );
  45. this.scene.add( this.camera );
  46. this.shader.uniforms[ 'envMap' ].value = this.sourceTexture;
  47. this.shader.envMap = this.sourceTexture;
  48. };
  49. THREE.PMREMGenerator.prototype = {
  50. constructor : THREE.PMREMGenerator,
  51. /*
  52. * Prashant Sharma / spidersharma03: More thought and work is needed here.
  53. * Right now it's a kind of a hack to use the previously convolved map to convolve the current one.
  54. * I tried to use the original map to convolve all the lods, but for many textures(specially the high frequency)
  55. * even a high number of samples(1024) dosen't lead to satisfactory results.
  56. * By using the previous convolved maps, a lower number of samples are generally sufficient(right now 32, which
  57. * gives okay results unless we see the reflection very carefully, or zoom in too much), however the math
  58. * goes wrong as the distribution function tries to sample a larger area than what it should be. So I simply scaled
  59. * the roughness by 0.9(totally empirical) to try to visually match the original result.
  60. * The condition "if(i <5)" is also an attemt to make the result match the original result.
  61. * This method requires the most amount of thinking I guess. Here is a paper which we could try to implement in future::
  62. * http://http.developer.nvidia.com/GPUGems3/gpugems3_ch20.html
  63. */
  64. update: function( renderer ) {
  65. this.shader.uniforms[ 'envMap' ].value = this.sourceTexture;
  66. this.shader.envMap = this.sourceTexture;
  67. var gammaInput = renderer.gammaInput;
  68. var gammaOutput = renderer.gammaOutput;
  69. var toneMapping = renderer.toneMapping;
  70. var toneMappingExposure = renderer.toneMappingExposure;
  71. renderer.toneMapping = THREE.LinearToneMapping;
  72. renderer.toneMappingExposure = 1.0;
  73. renderer.gammaInput = false;
  74. renderer.gammaOutput = false;
  75. for ( var i = 0; i < this.numLods; i ++ ) {
  76. var r = i / ( this.numLods - 1 );
  77. this.shader.uniforms[ 'roughness' ].value = r * 0.9; // see comment above, pragmatic choice
  78. var size = this.cubeLods[ i ].width;
  79. this.shader.uniforms[ 'mapSize' ].value = size;
  80. this.renderToCubeMapTarget( renderer, this.cubeLods[ i ] );
  81. if ( i < 5 ) this.shader.uniforms[ 'envMap' ].value = this.cubeLods[ i ].texture;
  82. }
  83. renderer.toneMapping = toneMapping;
  84. renderer.toneMappingExposure = toneMappingExposure;
  85. renderer.gammaInput = gammaInput;
  86. renderer.gammaOutput = gammaOutput;
  87. },
  88. renderToCubeMapTarget: function( renderer, renderTarget ) {
  89. for ( var i = 0; i < 6; i ++ ) {
  90. this.renderToCubeMapTargetFace( renderer, renderTarget, i )
  91. }
  92. },
  93. renderToCubeMapTargetFace: function( renderer, renderTarget, faceIndex ) {
  94. renderTarget.activeCubeFace = faceIndex;
  95. this.shader.uniforms[ 'faceIndex' ].value = faceIndex;
  96. renderer.render( this.scene, this.camera, renderTarget, true );
  97. },
  98. getShader: function() {
  99. return new THREE.ShaderMaterial( {
  100. uniforms: {
  101. "faceIndex": { value: 0 },
  102. "roughness": { value: 0.5 },
  103. "mapSize": { value: 0.5 },
  104. "envMap": { value: null },
  105. "testColor": { value: new THREE.Vector3( 1, 1, 1 ) }
  106. },
  107. vertexShader:
  108. "varying vec2 vUv;\n\
  109. void main() {\n\
  110. vUv = uv;\n\
  111. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  112. }",
  113. fragmentShader:
  114. "#include <common>\n\
  115. varying vec2 vUv;\n\
  116. uniform int faceIndex;\n\
  117. uniform float roughness;\n\
  118. uniform samplerCube envMap;\n\
  119. uniform float mapSize;\n\
  120. uniform vec3 testColor;\n\
  121. \n\
  122. float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\
  123. float a = ggxRoughness + 0.0001;\n\
  124. a *= a;\n\
  125. return ( 2.0 / a - 2.0 );\n\
  126. }\n\
  127. vec3 ImportanceSamplePhong(vec2 uv, mat3 vecSpace, float specPow) {\n\
  128. float phi = uv.y * 2.0 * PI;\n\
  129. float cosTheta = pow(1.0 - uv.x, 1.0 / (specPow + 1.0));\n\
  130. float sinTheta = sqrt(1.0 - cosTheta * cosTheta);\n\
  131. vec3 sampleDir = vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);\n\
  132. return vecSpace * sampleDir;\n\
  133. }\n\
  134. vec3 ImportanceSampleGGX( vec2 uv, mat3 vecSpace, float Roughness )\n\
  135. {\n\
  136. float a = Roughness * Roughness;\n\
  137. float Phi = 2.0 * PI * uv.x;\n\
  138. float CosTheta = sqrt( (1.0 - uv.y) / ( 1.0 + (a*a - 1.0) * uv.y ) );\n\
  139. float SinTheta = sqrt( 1.0 - CosTheta * CosTheta );\n\
  140. return vecSpace * vec3(SinTheta * cos( Phi ), SinTheta * sin( Phi ), CosTheta);\n\
  141. }\n\
  142. mat3 matrixFromVector(vec3 n) {\n\
  143. float a = 1.0 / (1.0 + n.z);\n\
  144. float b = -n.x * n.y * a;\n\
  145. vec3 b1 = vec3(1.0 - n.x * n.x * a, b, -n.x);\n\
  146. vec3 b2 = vec3(b, 1.0 - n.y * n.y * a, -n.y);\n\
  147. return mat3(b1, b2, n);\n\
  148. }\n\
  149. \n\
  150. vec4 testColorMap(float Roughness) {\n\
  151. vec4 color;\n\
  152. if(faceIndex == 0)\n\
  153. color = vec4(1.0,0.0,0.0,1.0);\n\
  154. else if(faceIndex == 1)\n\
  155. color = vec4(0.0,1.0,0.0,1.0);\n\
  156. else if(faceIndex == 2)\n\
  157. color = vec4(0.0,0.0,1.0,1.0);\n\
  158. else if(faceIndex == 3)\n\
  159. color = vec4(1.0,1.0,0.0,1.0);\n\
  160. else if(faceIndex == 4)\n\
  161. color = vec4(0.0,1.0,1.0,1.0);\n\
  162. else\n\
  163. color = vec4(1.0,0.0,1.0,1.0);\n\
  164. color *= ( 1.0 - Roughness );\n\
  165. return color;\n\
  166. }\n\
  167. void main() {\n\
  168. vec3 sampleDirection;\n\
  169. vec2 uv = vUv*2.0 - 1.0;\n\
  170. float offset = -1.0/mapSize;\n\
  171. const float a = -1.0;\n\
  172. const float b = 1.0;\n\
  173. float c = -1.0 + offset;\n\
  174. float d = 1.0 - offset;\n\
  175. float bminusa = b - a;\n\
  176. uv.x = (uv.x - a)/bminusa * d - (uv.x - b)/bminusa * c;\n\
  177. uv.y = (uv.y - a)/bminusa * d - (uv.y - b)/bminusa * c;\n\
  178. if (faceIndex==0) {\n\
  179. sampleDirection = vec3(1.0, -uv.y, -uv.x);\n\
  180. } else if (faceIndex==1) {\n\
  181. sampleDirection = vec3(-1.0, -uv.y, uv.x);\n\
  182. } else if (faceIndex==2) {\n\
  183. sampleDirection = vec3(uv.x, 1.0, uv.y);\n\
  184. } else if (faceIndex==3) {\n\
  185. sampleDirection = vec3(uv.x, -1.0, -uv.y);\n\
  186. } else if (faceIndex==4) {\n\
  187. sampleDirection = vec3(uv.x, -uv.y, 1.0);\n\
  188. } else {\n\
  189. sampleDirection = vec3(-uv.x, -uv.y, -1.0);\n\
  190. }\n\
  191. mat3 vecSpace = matrixFromVector(normalize(sampleDirection));\n\
  192. vec3 rgbColor = vec3(0.0);\n\
  193. const int NumSamples = 1024;\n\
  194. vec3 vect;\n\
  195. float weight = 0.0;\n\
  196. for(int i=0; i<NumSamples; i++) {\n\
  197. float sini = sin(float(i));\n\
  198. float cosi = cos(float(i));\n\
  199. float r = rand(vec2(sini, cosi));\n\
  200. vect = ImportanceSampleGGX(vec2(float(i) / float(NumSamples), r), vecSpace, roughness);\n\
  201. float dotProd = dot(vect, normalize(sampleDirection));\n\
  202. weight += dotProd;\n\
  203. vec3 color = envMapTexelToLinear(textureCube(envMap,vect)).rgb;\n\
  204. rgbColor.rgb += color;\n\
  205. }\n\
  206. rgbColor /= float(NumSamples);\n\
  207. //rgbColor = testColorMap( roughness ).rgb;\n\
  208. gl_FragColor = linearToOutputTexel( vec4( rgbColor, 1.0 ) );\n\
  209. }",
  210. blending: THREE.CustomBlending,
  211. blendSrc: THREE.OneFactor,
  212. blendDst: THREE.ZeroFactor,
  213. blendSrcAlpha: THREE.OneFactor,
  214. blendDstAlpha: THREE.ZeroFactor,
  215. blendEquation: THREE.AddEquation
  216. } );
  217. }
  218. };