PMREMCubeUVPacker.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * @author Prashant Sharma / spidersharma03
  3. * @author Ben Houston / bhouston, https://clara.io
  4. *
  5. * This class takes the cube lods(corresponding to different roughness values), and creates a single cubeUV
  6. * Texture. The format for a given roughness set of faces is simply::
  7. * +X+Y+Z
  8. * -X-Y-Z
  9. * For every roughness a mip map chain is also saved, which is essential to remove the texture artifacts due to
  10. * minification.
  11. * Right now for every face a PlaneMesh is drawn, which leads to a lot of geometry draw calls, but can be replaced
  12. * later by drawing a single buffer and by sending the appropriate faceIndex via vertex attributes.
  13. * The arrangement of the faces is fixed, as assuming this arrangement, the sampling function has been written.
  14. */
  15. THREE.PMREMCubeUVPacker = function ( cubeTextureLods ) {
  16. this.cubeLods = cubeTextureLods;
  17. var size = cubeTextureLods[ 0 ].width * 4;
  18. var sourceTexture = cubeTextureLods[ 0 ].texture;
  19. var params = {
  20. format: sourceTexture.format,
  21. magFilter: sourceTexture.magFilter,
  22. minFilter: sourceTexture.minFilter,
  23. type: sourceTexture.type,
  24. generateMipmaps: sourceTexture.generateMipmaps,
  25. anisotropy: sourceTexture.anisotropy,
  26. encoding: ( sourceTexture.encoding === THREE.RGBEEncoding ) ? THREE.RGBM16Encoding : sourceTexture.encoding
  27. };
  28. if ( params.encoding === THREE.RGBM16Encoding ) {
  29. params.magFilter = THREE.LinearFilter;
  30. params.minFilter = THREE.LinearFilter;
  31. }
  32. this.CubeUVRenderTarget = new THREE.WebGLRenderTarget( size, size, params );
  33. this.CubeUVRenderTarget.texture.name = "PMREMCubeUVPacker.cubeUv";
  34. this.CubeUVRenderTarget.texture.mapping = THREE.CubeUVReflectionMapping;
  35. this.camera = new THREE.OrthographicCamera( - size * 0.5, size * 0.5, - size * 0.5, size * 0.5, 0, 1 ); // top and bottom are swapped for some reason?
  36. this.scene = new THREE.Scene();
  37. this.objects = [];
  38. var geometry = new THREE.PlaneBufferGeometry( 1, 1 );
  39. var faceOffsets = [];
  40. faceOffsets.push( new THREE.Vector2( 0, 0 ) );
  41. faceOffsets.push( new THREE.Vector2( 1, 0 ) );
  42. faceOffsets.push( new THREE.Vector2( 2, 0 ) );
  43. faceOffsets.push( new THREE.Vector2( 0, 1 ) );
  44. faceOffsets.push( new THREE.Vector2( 1, 1 ) );
  45. faceOffsets.push( new THREE.Vector2( 2, 1 ) );
  46. var textureResolution = size;
  47. size = cubeTextureLods[ 0 ].width;
  48. var offset2 = 0;
  49. var c = 4.0;
  50. this.numLods = Math.log( cubeTextureLods[ 0 ].width ) / Math.log( 2 ) - 2; // IE11 doesn't support Math.log2
  51. for ( var i = 0; i < this.numLods; i ++ ) {
  52. var offset1 = ( textureResolution - textureResolution / c ) * 0.5;
  53. if ( size > 16 ) c *= 2;
  54. var nMips = size > 16 ? 6 : 1;
  55. var mipOffsetX = 0;
  56. var mipOffsetY = 0;
  57. var mipSize = size;
  58. for ( var j = 0; j < nMips; j ++ ) {
  59. // Mip Maps
  60. for ( var k = 0; k < 6; k ++ ) {
  61. // 6 Cube Faces
  62. var material = this.getShader();
  63. material.uniforms[ 'envMap' ].value = this.cubeLods[ i ].texture;
  64. material.envMap = this.cubeLods[ i ].texture;
  65. material.uniforms[ 'faceIndex' ].value = k;
  66. material.uniforms[ 'mapSize' ].value = mipSize;
  67. var planeMesh = new THREE.Mesh( geometry, material );
  68. planeMesh.position.x = faceOffsets[ k ].x * mipSize - offset1 + mipOffsetX;
  69. planeMesh.position.y = faceOffsets[ k ].y * mipSize - offset1 + offset2 + mipOffsetY;
  70. planeMesh.material.side = THREE.BackSide;
  71. planeMesh.scale.setScalar( mipSize );
  72. this.scene.add( planeMesh );
  73. this.objects.push( planeMesh );
  74. }
  75. mipOffsetY += 1.75 * mipSize;
  76. mipOffsetX += 1.25 * mipSize;
  77. mipSize /= 2;
  78. }
  79. offset2 += 2 * size;
  80. if ( size > 16 ) size /= 2;
  81. }
  82. };
  83. THREE.PMREMCubeUVPacker.prototype = {
  84. constructor: THREE.PMREMCubeUVPacker,
  85. update: function ( renderer ) {
  86. var gammaInput = renderer.gammaInput;
  87. var gammaOutput = renderer.gammaOutput;
  88. var toneMapping = renderer.toneMapping;
  89. var toneMappingExposure = renderer.toneMappingExposure;
  90. var currentRenderTarget = renderer.getRenderTarget();
  91. renderer.gammaInput = false;
  92. renderer.gammaOutput = false;
  93. renderer.toneMapping = THREE.LinearToneMapping;
  94. renderer.toneMappingExposure = 1.0;
  95. renderer.render( this.scene, this.camera, this.CubeUVRenderTarget, false );
  96. renderer.setRenderTarget( currentRenderTarget );
  97. renderer.toneMapping = toneMapping;
  98. renderer.toneMappingExposure = toneMappingExposure;
  99. renderer.gammaInput = gammaInput;
  100. renderer.gammaOutput = gammaOutput;
  101. },
  102. getShader: function () {
  103. var shaderMaterial = new THREE.ShaderMaterial( {
  104. uniforms: {
  105. "faceIndex": { value: 0 },
  106. "mapSize": { value: 0 },
  107. "envMap": { value: null },
  108. "testColor": { value: new THREE.Vector3( 1, 1, 1 ) }
  109. },
  110. vertexShader:
  111. "precision highp float;\
  112. varying vec2 vUv;\
  113. void main() {\
  114. vUv = uv;\
  115. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\
  116. }",
  117. fragmentShader:
  118. "precision highp float;\
  119. varying vec2 vUv;\
  120. uniform samplerCube envMap;\
  121. uniform float mapSize;\
  122. uniform vec3 testColor;\
  123. uniform int faceIndex;\
  124. \
  125. void main() {\
  126. vec3 sampleDirection;\
  127. vec2 uv = vUv;\
  128. uv = uv * 2.0 - 1.0;\
  129. uv.y *= -1.0;\
  130. if(faceIndex == 0) {\
  131. sampleDirection = normalize(vec3(1.0, uv.y, -uv.x));\
  132. } else if(faceIndex == 1) {\
  133. sampleDirection = normalize(vec3(uv.x, 1.0, uv.y));\
  134. } else if(faceIndex == 2) {\
  135. sampleDirection = normalize(vec3(uv.x, uv.y, 1.0));\
  136. } else if(faceIndex == 3) {\
  137. sampleDirection = normalize(vec3(-1.0, uv.y, uv.x));\
  138. } else if(faceIndex == 4) {\
  139. sampleDirection = normalize(vec3(uv.x, -1.0, -uv.y));\
  140. } else {\
  141. sampleDirection = normalize(vec3(-uv.x, uv.y, -1.0));\
  142. }\
  143. vec4 color = envMapTexelToLinear( textureCube( envMap, sampleDirection ) );\
  144. gl_FragColor = linearToOutputTexel( color );\
  145. }",
  146. blending: THREE.NoBlending
  147. } );
  148. shaderMaterial.type = 'PMREMCubeUVPacker';
  149. return shaderMaterial;
  150. },
  151. dispose: function () {
  152. for ( var i = 0, l = this.objects.length; i < l; i ++ ) {
  153. this.objects[ i ].material.dispose();
  154. }
  155. this.objects[ 0 ].geometry.dispose();
  156. }
  157. };