2
0

PMREMCubeUVPacker.js 5.5 KB

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