PMREMGenerator.js 7.7 KB

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