PMREMGenerator.js 10 KB

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