PMREMCubeUVPacker.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, numLods ) {
  16. this.cubeLods = cubeTextureLods;
  17. this.numLods = numLods;
  18. var size = cubeTextureLods[ 0 ].width * 4;
  19. var sourceTexture = cubeTextureLods[ 0 ].texture;
  20. var params = {
  21. format: sourceTexture.format,
  22. magFilter: sourceTexture.magFilter,
  23. minFilter: sourceTexture.minFilter,
  24. type: sourceTexture.type,
  25. generateMipmaps: sourceTexture.generateMipmaps,
  26. anisotropy: sourceTexture.anisotropy,
  27. encoding: ( sourceTexture.encoding === THREE.RGBEEncoding ) ? THREE.RGBM16Encoding : sourceTexture.encoding
  28. };
  29. if ( params.encoding === THREE.RGBM16Encoding ) {
  30. params.magFilter = THREE.LinearFilter;
  31. params.minFilter = THREE.LinearFilter;
  32. }
  33. this.CubeUVRenderTarget = new THREE.WebGLRenderTarget( size, size, params );
  34. this.CubeUVRenderTarget.texture.name = "PMREMCubeUVPacker.cubeUv";
  35. this.CubeUVRenderTarget.texture.mapping = THREE.CubeUVReflectionMapping;
  36. this.camera = new THREE.OrthographicCamera( - size * 0.5, size * 0.5, - size * 0.5, size * 0.5, 0.0, 1000 );
  37. this.scene = new THREE.Scene();
  38. this.scene.add( this.camera );
  39. this.objects = [];
  40. var faceOffsets = [];
  41. faceOffsets.push( new THREE.Vector2( 0, 0 ) );
  42. faceOffsets.push( new THREE.Vector2( 1, 0 ) );
  43. faceOffsets.push( new THREE.Vector2( 2, 0 ) );
  44. faceOffsets.push( new THREE.Vector2( 0, 1 ) );
  45. faceOffsets.push( new THREE.Vector2( 1, 1 ) );
  46. faceOffsets.push( new THREE.Vector2( 2, 1 ) );
  47. var textureResolution = size;
  48. size = cubeTextureLods[ 0 ].width;
  49. var offset2 = 0;
  50. var c = 4.0;
  51. this.numLods = Math.log( cubeTextureLods[ 0 ].width ) / Math.log( 2 ) - 2; // IE11 doesn't support Math.log2
  52. for ( var i = 0; i < this.numLods; i ++ ) {
  53. var offset1 = ( textureResolution - textureResolution / c ) * 0.5;
  54. if ( size > 16 ) c *= 2;
  55. var nMips = size > 16 ? 6 : 1;
  56. var mipOffsetX = 0;
  57. var mipOffsetY = 0;
  58. var mipSize = size;
  59. for ( var j = 0; j < nMips; j ++ ) {
  60. // Mip Maps
  61. for ( var k = 0; k < 6; k ++ ) {
  62. // 6 Cube Faces
  63. var material = this.getShader();
  64. material.uniforms[ 'envMap' ].value = this.cubeLods[ i ].texture;
  65. material.envMap = this.cubeLods[ i ].texture;
  66. material.uniforms[ 'faceIndex' ].value = k;
  67. material.uniforms[ 'mapSize' ].value = mipSize;
  68. var planeMesh = new THREE.Mesh(
  69. new THREE.PlaneGeometry( mipSize, mipSize, 0 ),
  70. material );
  71. planeMesh.position.x = faceOffsets[ k ].x * mipSize - offset1 + mipOffsetX;
  72. planeMesh.position.y = faceOffsets[ k ].y * mipSize - offset1 + offset2 + mipOffsetY;
  73. planeMesh.material.side = THREE.DoubleSide;
  74. this.scene.add( planeMesh );
  75. this.objects.push( planeMesh );
  76. }
  77. mipOffsetY += 1.75 * mipSize;
  78. mipOffsetX += 1.25 * mipSize;
  79. mipSize /= 2;
  80. }
  81. offset2 += 2 * size;
  82. if ( size > 16 ) size /= 2;
  83. }
  84. };
  85. THREE.PMREMCubeUVPacker.prototype = {
  86. constructor: THREE.PMREMCubeUVPacker,
  87. update: function ( renderer ) {
  88. var gammaInput = renderer.gammaInput;
  89. var gammaOutput = renderer.gammaOutput;
  90. var toneMapping = renderer.toneMapping;
  91. var toneMappingExposure = renderer.toneMappingExposure;
  92. renderer.gammaInput = false;
  93. renderer.gammaOutput = false;
  94. renderer.toneMapping = THREE.LinearToneMapping;
  95. renderer.toneMappingExposure = 1.0;
  96. renderer.render( this.scene, this.camera, this.CubeUVRenderTarget, false );
  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.CustomBlending,
  147. premultipliedAlpha: false,
  148. blendSrc: THREE.OneFactor,
  149. blendDst: THREE.ZeroFactor,
  150. blendSrcAlpha: THREE.OneFactor,
  151. blendDstAlpha: THREE.ZeroFactor,
  152. blendEquation: THREE.AddEquation
  153. } );
  154. return shaderMaterial;
  155. }
  156. };