PMREMCubeUVPacker.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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
  28. };
  29. this.CubeUVRenderTarget = new THREE.WebGLRenderTarget( size, size, params );
  30. this.CubeUVRenderTarget.texture.mapping = THREE.CubeUVReflectionMapping;
  31. this.camera = new THREE.OrthographicCamera( - size * 0.5, size * 0.5, - size * 0.5, size * 0.5, 0.0, 1000 );
  32. this.scene = new THREE.Scene();
  33. this.scene.add( this.camera );
  34. this.objects = [];
  35. var xOffset = 0;
  36. var faceOffsets = [];
  37. faceOffsets.push( new THREE.Vector2( 0, 0 ) );
  38. faceOffsets.push( new THREE.Vector2( 1, 0 ) );
  39. faceOffsets.push( new THREE.Vector2( 2, 0 ) );
  40. faceOffsets.push( new THREE.Vector2( 0, 1 ) );
  41. faceOffsets.push( new THREE.Vector2( 1, 1 ) );
  42. faceOffsets.push( new THREE.Vector2( 2, 1 ) );
  43. var yOffset = 0;
  44. var textureResolution = size;
  45. size = cubeTextureLods[ 0 ].width;
  46. var offset2 = 0;
  47. var c = 4.0;
  48. this.numLods = Math.log2( cubeTextureLods[ 0 ].width ) - 2;
  49. for ( var i = 0; i < this.numLods; i ++ ) {
  50. var offset1 = ( textureResolution - textureResolution / c ) * 0.5;
  51. if ( size > 16 )
  52. c *= 2;
  53. var nMips = size > 16 ? 6 : 1;
  54. var mipOffsetX = 0;
  55. var mipOffsetY = 0;
  56. var mipSize = size;
  57. for ( var j = 0; j < nMips; j ++ ) {
  58. // Mip Maps
  59. for ( var k = 0; k < 6; k ++ ) {
  60. // 6 Cube Faces
  61. var material = this.getShader();
  62. material.uniforms[ 'envMap' ].value = this.cubeLods[ i ].texture;
  63. material.envMap = this.cubeLods[ i ].texture;
  64. material.uniforms[ 'faceIndex' ].value = k;
  65. material.uniforms[ 'mapSize' ].value = mipSize;
  66. var color = material.uniforms[ 'testColor' ].value;
  67. //color.copy(testColor[j]);
  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 )
  83. size /= 2;
  84. }
  85. };
  86. THREE.PMREMCubeUVPacker.prototype = {
  87. constructor : THREE.PMREMCubeUVPacker,
  88. update: function( renderer ) {
  89. var gammaInput = renderer.gammaInput;
  90. var gammaOutput = renderer.gammaOutput;
  91. var toneMapping = renderer.toneMapping;
  92. var toneMappingExposure = renderer.toneMappingExposure;
  93. renderer.gammaInput = false;
  94. renderer.gammaOutput = false;
  95. renderer.toneMapping = THREE.LinearToneMapping;
  96. renderer.toneMappingExposure = 1.0;
  97. renderer.render( this.scene, this.camera, this.CubeUVRenderTarget, false );
  98. renderer.toneMapping = toneMapping;
  99. renderer.toneMappingExposure = toneMappingExposure;
  100. renderer.gammaInput = gammaInput;
  101. renderer.gammaOutput = gammaOutput;
  102. },
  103. getShader: function() {
  104. var shaderMaterial = new THREE.ShaderMaterial( {
  105. uniforms: {
  106. "faceIndex": { value: 0 },
  107. "mapSize": { value: 0 },
  108. "envMap": { value: null },
  109. "testColor": { value: new THREE.Vector3( 1, 1, 1 ) }
  110. },
  111. vertexShader:
  112. "precision highp float;\
  113. varying vec2 vUv;\
  114. void main() {\
  115. vUv = uv;\
  116. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\
  117. }",
  118. fragmentShader:
  119. "precision highp float;\
  120. varying vec2 vUv;\
  121. uniform samplerCube envMap;\
  122. uniform float mapSize;\
  123. uniform vec3 testColor;\
  124. uniform int faceIndex;\
  125. \
  126. void main() {\
  127. vec3 sampleDirection;\
  128. vec2 uv = vUv;\
  129. uv = uv * 2.0 - 1.0;\
  130. uv.y *= -1.0;\
  131. if(faceIndex == 0) {\
  132. sampleDirection = normalize(vec3(1.0, uv.y, -uv.x));\
  133. } else if(faceIndex == 1) {\
  134. sampleDirection = normalize(vec3(uv.x, 1.0, uv.y));\
  135. } else if(faceIndex == 2) {\
  136. sampleDirection = normalize(vec3(uv.x, uv.y, 1.0));\
  137. } else if(faceIndex == 3) {\
  138. sampleDirection = normalize(vec3(-1.0, uv.y, uv.x));\
  139. } else if(faceIndex == 4) {\
  140. sampleDirection = normalize(vec3(uv.x, -1.0, -uv.y));\
  141. } else {\
  142. sampleDirection = normalize(vec3(-uv.x, uv.y, -1.0));\
  143. }\
  144. vec4 color = envMapTexelToLinear( textureCube( envMap, sampleDirection ) );\
  145. gl_FragColor = linearToOutputTexel( color );\
  146. }",
  147. blending: THREE.CustomBlending,
  148. premultipliedAlpha: false,
  149. blendSrc: THREE.OneFactor,
  150. blendDst: THREE.ZeroFactor,
  151. blendSrcAlpha: THREE.OneFactor,
  152. blendDstAlpha: THREE.ZeroFactor,
  153. blendEquation: THREE.AddEquation
  154. } );
  155. return shaderMaterial;
  156. }
  157. };