PMREMGenerator.js 8.1 KB

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