2
0

PMREMGenerator.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // encoded formats do not interpolate well, thus turn off interpolation for them
  25. var textureFilter = ( sourceTexture.encoding === THREE.Linear ) ? THREE.LinearFilter : THREE.NearestFilter;
  26. this.sourceTexture.minFilter = this.sourceTexture.magFilter = textureFilter;
  27. this.cubeLods = [];
  28. var size = this.resolution;
  29. var params = { format: THREE.RGBAFormat, magFilter: textureFilter, minFilter: textureFilter };
  30. this.numLods = Math.log2( size ) - 2;
  31. for ( var i = 0; i < this.numLods; i ++ ) {
  32. var renderTarget = new THREE.WebGLRenderTargetCube( size, size, params );
  33. renderTarget.texture.generateMipmaps = false;
  34. renderTarget.encoding = this.sourceTexture.encoding;
  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[ "sourceTexture" ].value = this.sourceTexture;
  46. this.shader.uniforms[ "encoding" ].value = this.sourceTexture.encoding;
  47. };
  48. THREE.PMREMGenerator.prototype = {
  49. constructor : PMREMGenerator,
  50. update: function( renderer ) {
  51. this.shader.uniforms[ "sourceTexture" ].value = this.sourceTexture;
  52. for ( var i = 0; i < this.numLods; i ++ ) {
  53. var r = i / ( this.numLods - 1 );
  54. this.shader.uniforms[ "roughness" ].value = r * 0.9;
  55. var size = this.cubeLods[ i ].width;
  56. this.shader.uniforms[ "mapSize" ].value = size;
  57. this.renderToCubeMapTarget( renderer, this.cubeLods[ i ] );
  58. if ( i < 5 )
  59. this.shader.uniforms[ "sourceTexture" ].value = this.cubeLods[ i ];
  60. }
  61. },
  62. renderToCubeMapTarget: function( renderer, renderTarget ) {
  63. for ( var i = 0; i < 6; i ++ ) {
  64. this.renderToCubeMapTargetFace( renderer, renderTarget, i )
  65. }
  66. },
  67. renderToCubeMapTargetFace: function( renderer, renderTarget, faceIndex ) {
  68. renderTarget.texture.generateMipmaps = false;
  69. renderTarget.activeCubeFace = faceIndex;
  70. this.shader.uniforms[ "faceIndex" ].value = faceIndex;
  71. renderer.render( this.scene, this.camera, renderTarget, true );
  72. },
  73. getShader: function() {
  74. return new THREE.ShaderMaterial( {
  75. uniforms: {
  76. "faceIndex": { type: 'i', value: 0 },
  77. "roughness": { type: 'f', value: 0.5 },
  78. "mapSize": { type: 'f', value: 0.5 },
  79. "sourceTexture": { type: 't', value: null },
  80. "encoding": { type: 'i', value: 3002},
  81. "testColor": { type: 'v3', value: new THREE.Vector3( 1, 1, 1 ) }
  82. },
  83. vertexShader:
  84. "varying vec2 vUv;\n\
  85. void main() {\n\
  86. vUv = uv;\n\
  87. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  88. }",
  89. fragmentShader:
  90. "varying vec2 vUv;\n\
  91. uniform int faceIndex;\n\
  92. uniform float roughness;\n\
  93. uniform samplerCube sourceTexture;\n\
  94. uniform float mapSize;\n\
  95. uniform vec3 testColor;\n\
  96. uniform int encoding;\n\
  97. \n\
  98. float rnd(vec2 uv) {\n\
  99. return fract(sin(dot(uv, vec2(12.9898, 78.233) * 2.0)) * 43758.5453);\n\
  100. }\n\
  101. float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\
  102. float a = ggxRoughness + 0.0001;\n\
  103. a *= a;\n\
  104. return ( 2.0 / a - 2.0 );\n\
  105. }\n\
  106. const float PI = 3.14159265358979;\n\
  107. vec3 ImportanceSamplePhong(vec2 uv, mat3 vecSpace, float specPow) {\n\
  108. float phi = uv.y * 2.0 * PI;\n\
  109. float cosTheta = pow(1.0 - uv.x, 1.0 / (specPow + 1.0));\n\
  110. float sinTheta = sqrt(1.0 - cosTheta * cosTheta);\n\
  111. vec3 sampleDir = vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);\n\
  112. return vecSpace * sampleDir;\n\
  113. }\n\
  114. vec3 ImportanceSampleGGX( vec2 uv, mat3 vecSpace, float Roughness )\n\
  115. {\n\
  116. float a = Roughness * Roughness;\n\
  117. float Phi = 2.0 * PI * uv.x;\n\
  118. float CosTheta = sqrt( (1.0 - uv.y) / ( 1.0 + (a*a - 1.0) * uv.y ) );\n\
  119. float SinTheta = sqrt( 1.0 - CosTheta * CosTheta );\n\
  120. return vecSpace * vec3(SinTheta * cos( Phi ), SinTheta * sin( Phi ), CosTheta);\n\
  121. }\n\
  122. mat3 matrixFromVector(vec3 n) {\n\
  123. float a = 1.0 / (1.0 + n.z);\n\
  124. float b = -n.x * n.y * a;\n\
  125. vec3 b1 = vec3(1.0 - n.x * n.x * a, b, -n.x);\n\
  126. vec3 b2 = vec3(b, 1.0 - n.y * n.y * a, -n.y);\n\
  127. return mat3(b1, b2, n);\n\
  128. }\n\
  129. \n\
  130. vec4 testColorMap() {\n\
  131. vec4 color;\n\
  132. if(faceIndex == 0)\n\
  133. color = vec4(1.0,0.0,0.0,1.0);\n\
  134. else if(faceIndex == 1)\n\
  135. color = vec4(0.0,1.0,0.0,1.0);\n\
  136. else if(faceIndex == 2)\n\
  137. color = vec4(0.0,0.0,1.0,1.0);\n\
  138. else if(faceIndex == 3)\n\
  139. color = vec4(1.0,1.0,0.0,1.0);\n\
  140. else if(faceIndex == 4)\n\
  141. color = vec4(0.0,1.0,1.0,1.0);\n\
  142. else\n\
  143. color = vec4(1.0,0.0,1.0,1.0);\n\
  144. return color;\n\
  145. }",
  146. THREE.ShaderChunk[ "encodings" ],
  147. "void main() {\n\
  148. vec3 sampleDirection;\n\
  149. vec2 uv = vUv*2.0 - 1.0;\n\
  150. float offset = -1.0/mapSize;\n\
  151. const float a = -1.0;\n\
  152. const float b = 1.0;\n\
  153. float c = -1.0 + offset;\n\
  154. float d = 1.0 - offset;\n\
  155. float bminusa = b - a;\n\
  156. uv.x = (uv.x - a)/bminusa * d - (uv.x - b)/bminusa * c;\n\
  157. uv.y = (uv.y - a)/bminusa * d - (uv.y - b)/bminusa * c;\n\
  158. if (faceIndex==0) {\n\
  159. sampleDirection = vec3(1.0, -uv.y, -uv.x);\n\
  160. }\n\
  161. else if (faceIndex==1) {\n\
  162. sampleDirection = vec3(-1.0, -uv.y, uv.x);\n\
  163. } else if (faceIndex==2) {\n\
  164. sampleDirection = vec3(uv.x, 1.0, uv.y);\n\
  165. } else if (faceIndex==3) {\n\
  166. sampleDirection = vec3(uv.x, -1.0, -uv.y);\n\
  167. } else if (faceIndex==4) {\n\
  168. sampleDirection = vec3(uv.x, -uv.y, 1.0);\n\
  169. } else {\n\
  170. sampleDirection = vec3(-uv.x, -uv.y, -1.0);\n\
  171. }\n\
  172. mat3 vecSpace = matrixFromVector(normalize(sampleDirection));\n\
  173. vec3 rgbColor = vec3(0.0);\n\
  174. const int NumSamples = 1024;\n\
  175. vec3 vect;\n\
  176. float weight = 0.0;\n\
  177. for(int i=0; i<NumSamples; i++) {\n\
  178. float sini = sin(float(i));\n\
  179. float cosi = cos(float(i));\n\
  180. float rand = rnd(vec2(sini, cosi));\n\
  181. vect = ImportanceSampleGGX(vec2(float(i) / float(NumSamples), rand), vecSpace, roughness);\n\
  182. float dotProd = dot(vect, normalize(sampleDirection));\n\
  183. weight += dotProd;\n\
  184. vec3 color = texelDecode(textureCube(sourceTexture, vect), encoding).rgb;\n\
  185. rgbColor.rgb += color;\n\
  186. }\n\
  187. rgbColor /= float(NumSamples);\n\
  188. gl_FragColor = texelEncode( rgbColor, encoding );\n\
  189. }"
  190. }
  191. );
  192. }
  193. };