PMREMGenerator.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @author Prashant Sharma / spidersharma03
  3. * @author Ben Houston / bhouston, https://clara.io
  4. */
  5. THREE.PMREMGenerator = function( sourceTexture ) {
  6. if ( sourceTexture instanceof THREE.CubeTexture ) {
  7. if ( sourceTexture.images[ 0 ] === undefined )
  8. console.error( "sourceTexture Not Initialized" );
  9. if(sourceTexture.images[ 0 ] instanceof THREE.DataTexture) {
  10. this.resolution = sourceTexture.images[ 0 ].image.width;
  11. }
  12. else {
  13. this.resolution = sourceTexture.images[ 0 ].width;
  14. }
  15. }
  16. else if ( sourceTexture instanceof THREE.WebGLRenderTargetCube ) {
  17. if ( sourceTexture === undefined ) console.error( "Render Target Not Initialized" );
  18. this.resolution = sourceTexture.width;
  19. }
  20. else {
  21. console.error( "Wrong Input to PMREMGenerator" );
  22. }
  23. this.sourceTexture = sourceTexture;
  24. var monotonicEncoding = ( sourceTexture.encoding === THREE.LinearEncoding ) ||
  25. ( sourceTexture.encoding === THREE.GammaEncoding ) || ( sourceTexture.encoding === THREE.sRGBEncoding );
  26. this.sourceTexture.minFilter = ( monotonicEncoding ) ? THREE.LinearFilter : THREE.NearestFilter;
  27. this.sourceTexture.magFilter = ( monotonicEncoding ) ? THREE.LinearFilter : THREE.NearestFilter;
  28. this.sourceTexture.generateMipmaps = this.sourceTexture.generateMipmaps && monotonicEncoding;
  29. this.cubeLods = [];
  30. var size = this.resolution;
  31. var params = { format: this.sourceTexture.format, magFilter: this.sourceTexture.magFilter, minFilter: this.sourceTexture.minFilter, type: this.sourceTexture.type };
  32. this.numLods = Math.log2( size ) - 2;
  33. for ( var i = 0; i < this.numLods; i ++ ) {
  34. var renderTarget = new THREE.WebGLRenderTargetCube( size, size, params );
  35. renderTarget.texture.generateMipmaps = this.sourceTexture.generateMipmaps;
  36. renderTarget.texture.anisotropy = this.sourceTexture.anisotropy;
  37. renderTarget.texture.encoding = this.sourceTexture.encoding;
  38. renderTarget.texture.minFilter = this.sourceTexture.minFilter;
  39. renderTarget.texture.magFilter = this.sourceTexture.magFilter;
  40. this.cubeLods.push( renderTarget );
  41. size = Math.max( 16, size / 2 );
  42. }
  43. this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0.0, 1000 );
  44. this.shader = this.getShader();
  45. this.planeMesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2, 0 ), this.shader );
  46. this.planeMesh.material.side = THREE.DoubleSide;
  47. this.scene = new THREE.Scene();
  48. this.scene.add( this.planeMesh );
  49. this.scene.add( this.camera );
  50. this.shader.uniforms[ "envMap" ].value = this.sourceTexture;
  51. this.shader.envMap = this.sourceTexture;
  52. };
  53. THREE.PMREMGenerator.prototype = {
  54. constructor : THREE.PMREMGenerator,
  55. update: function( renderer ) {
  56. this.shader.uniforms[ "envMap" ].value = this.sourceTexture;
  57. this.shader.envMap = this.sourceTexture;
  58. var gammaInput = renderer.gammaInput;
  59. var gammaOutput = renderer.gammaOutput;
  60. renderer.gammaInput = false;
  61. renderer.gammaOutput = false;
  62. for ( var i = 0; i < this.numLods; i ++ ) {
  63. var r = i / ( this.numLods - 1 );
  64. this.shader.uniforms[ "roughness" ].value = r * 0.9;
  65. var size = this.cubeLods[ i ].width;
  66. this.shader.uniforms[ "mapSize" ].value = size;
  67. this.renderToCubeMapTarget( renderer, this.cubeLods[ i ] );
  68. if ( i < 5 )
  69. this.shader.uniforms[ "envMap" ].value = this.cubeLods[ i ];
  70. }
  71. renderer.gammaInput = renderer.gammaInput;
  72. renderer.gammaOutput = renderer.gammaOutput;
  73. },
  74. renderToCubeMapTarget: function( renderer, renderTarget ) {
  75. for ( var i = 0; i < 6; i ++ ) {
  76. this.renderToCubeMapTargetFace( renderer, renderTarget, i )
  77. }
  78. },
  79. renderToCubeMapTargetFace: function( renderer, renderTarget, faceIndex ) {
  80. renderTarget.activeCubeFace = faceIndex;
  81. this.shader.uniforms[ "faceIndex" ].value = faceIndex;
  82. renderer.render( this.scene, this.camera, renderTarget, true );
  83. },
  84. getShader: function() {
  85. return new THREE.ShaderMaterial( {
  86. uniforms: {
  87. "faceIndex": { type: 'i', value: 0 },
  88. "roughness": { type: 'f', value: 0.5 },
  89. "mapSize": { type: 'f', value: 0.5 },
  90. "envMap": { type: 't', value: null },
  91. "testColor": { type: 'v3', value: new THREE.Vector3( 1, 1, 1 ) }
  92. },
  93. vertexShader:
  94. "varying vec2 vUv;\n\
  95. void main() {\n\
  96. vUv = uv;\n\
  97. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  98. }",
  99. fragmentShader:
  100. "varying vec2 vUv;\n\
  101. uniform int faceIndex;\n\
  102. uniform float roughness;\n\
  103. uniform samplerCube envMap;\n\
  104. uniform float mapSize;\n\
  105. uniform vec3 testColor;\n\
  106. \n\
  107. float rnd(vec2 uv) {\n\
  108. return fract(sin(dot(uv, vec2(12.9898, 78.233) * 2.0)) * 43758.5453);\n\
  109. }\n\
  110. float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\
  111. float a = ggxRoughness + 0.0001;\n\
  112. a *= a;\n\
  113. return ( 2.0 / a - 2.0 );\n\
  114. }\n\
  115. const float PI = 3.14159265358979;\n\
  116. vec3 ImportanceSamplePhong(vec2 uv, mat3 vecSpace, float specPow) {\n\
  117. float phi = uv.y * 2.0 * PI;\n\
  118. float cosTheta = pow(1.0 - uv.x, 1.0 / (specPow + 1.0));\n\
  119. float sinTheta = sqrt(1.0 - cosTheta * cosTheta);\n\
  120. vec3 sampleDir = vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);\n\
  121. return vecSpace * sampleDir;\n\
  122. }\n\
  123. vec3 ImportanceSampleGGX( vec2 uv, mat3 vecSpace, float Roughness )\n\
  124. {\n\
  125. float a = Roughness * Roughness;\n\
  126. float Phi = 2.0 * PI * uv.x;\n\
  127. float CosTheta = sqrt( (1.0 - uv.y) / ( 1.0 + (a*a - 1.0) * uv.y ) );\n\
  128. float SinTheta = sqrt( 1.0 - CosTheta * CosTheta );\n\
  129. return vecSpace * vec3(SinTheta * cos( Phi ), SinTheta * sin( Phi ), CosTheta);\n\
  130. }\n\
  131. mat3 matrixFromVector(vec3 n) {\n\
  132. float a = 1.0 / (1.0 + n.z);\n\
  133. float b = -n.x * n.y * a;\n\
  134. vec3 b1 = vec3(1.0 - n.x * n.x * a, b, -n.x);\n\
  135. vec3 b2 = vec3(b, 1.0 - n.y * n.y * a, -n.y);\n\
  136. return mat3(b1, b2, n);\n\
  137. }\n\
  138. \n\
  139. vec4 testColorMap(float Roughness) {\n\
  140. vec4 color;\n\
  141. if(faceIndex == 0)\n\
  142. color = vec4(1.0,0.0,0.0,1.0);\n\
  143. else if(faceIndex == 1)\n\
  144. color = vec4(0.0,1.0,0.0,1.0);\n\
  145. else if(faceIndex == 2)\n\
  146. color = vec4(0.0,0.0,1.0,1.0);\n\
  147. else if(faceIndex == 3)\n\
  148. color = vec4(1.0,1.0,0.0,1.0);\n\
  149. else if(faceIndex == 4)\n\
  150. color = vec4(0.0,1.0,1.0,1.0);\n\
  151. else\n\
  152. color = vec4(1.0,0.0,1.0,1.0);\n\
  153. color *= ( 1.0 - Roughness );\n\
  154. return color;\n\
  155. }\n\
  156. void main() {\n\
  157. vec3 sampleDirection;\n\
  158. vec2 uv = vUv*2.0 - 1.0;\n\
  159. float offset = -1.0/mapSize;\n\
  160. const float a = -1.0;\n\
  161. const float b = 1.0;\n\
  162. float c = -1.0 + offset;\n\
  163. float d = 1.0 - offset;\n\
  164. float bminusa = b - a;\n\
  165. uv.x = (uv.x - a)/bminusa * d - (uv.x - b)/bminusa * c;\n\
  166. uv.y = (uv.y - a)/bminusa * d - (uv.y - b)/bminusa * c;\n\
  167. if (faceIndex==0) {\n\
  168. sampleDirection = vec3(1.0, -uv.y, -uv.x);\n\
  169. }\n\
  170. else if (faceIndex==1) {\n\
  171. sampleDirection = vec3(-1.0, -uv.y, uv.x);\n\
  172. } else if (faceIndex==2) {\n\
  173. sampleDirection = vec3(uv.x, 1.0, uv.y);\n\
  174. } else if (faceIndex==3) {\n\
  175. sampleDirection = vec3(uv.x, -1.0, -uv.y);\n\
  176. } else if (faceIndex==4) {\n\
  177. sampleDirection = vec3(uv.x, -uv.y, 1.0);\n\
  178. } else {\n\
  179. sampleDirection = vec3(-uv.x, -uv.y, -1.0);\n\
  180. }\n\
  181. mat3 vecSpace = matrixFromVector(normalize(sampleDirection));\n\
  182. vec3 rgbColor = vec3(0.0);\n\
  183. const int NumSamples = 1024;\n\
  184. vec3 vect;\n\
  185. float weight = 0.0;\n\
  186. for(int i=0; i<NumSamples; i++) {\n\
  187. float sini = sin(float(i));\n\
  188. float cosi = cos(float(i));\n\
  189. float rand = rnd(vec2(sini, cosi));\n\
  190. vect = ImportanceSampleGGX(vec2(float(i) / float(NumSamples), rand), vecSpace, roughness);\n\
  191. float dotProd = dot(vect, normalize(sampleDirection));\n\
  192. weight += dotProd;\n\
  193. vec3 color = envMapTexelToLinear(textureCube(envMap,vect)).rgb;\n\
  194. rgbColor.rgb += color;\n\
  195. }\n\
  196. rgbColor /= float(NumSamples);\n\
  197. //rgbColor = testColorMap( roughness ).rgb;\n\
  198. gl_FragColor = linearToOutputTexel( vec4( rgbColor, 1.0 ) );\n\
  199. }",
  200. blending: THREE.CustomBlending,
  201. blendSrc: THREE.OneFactor,
  202. blendDst: THREE.ZeroFactor,
  203. blendSrcAlpha: THREE.OneFactor,
  204. blendDstAlpha: THREE.ZeroFactor,
  205. blendEquation: THREE.AddEquation
  206. }
  207. );
  208. }
  209. };