PMREMGenerator.js 8.4 KB

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