PMREM_CubeUVPacker.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @author Prashant Sharma / spidersharma03
  3. * @author Ben Houston / bhouston, https://clara.io
  4. */
  5. THREE.PMREM_CubeUVPacker = function( cubeTextureLods, numLods ) {
  6. this.cubeLods = cubeTextureLods;
  7. this.numLods = numLods;
  8. var size = cubeTextureLods[ 0 ].width * 4;
  9. this.CubeUVRenderTarget = new THREE.WebGLRenderTarget( size, size,
  10. { format: THREE.RGBAFormat, magFilter: THREE.LinearFilter, minFilter: THREE.LinearFilter } );
  11. this.CubeUVRenderTarget.texture.generateMipmaps = false;
  12. this.CubeUVRenderTarget.mapping = THREE.CubeUVReflectionMapping;
  13. this.camera = new THREE.OrthographicCamera( - size * 0.5, size * 0.5, - size * 0.5, size * 0.5, 0.0, 1000 );
  14. this.CubeUVRenderTarget.encoding = this.cubeLods[0].encoding;
  15. this.scene = new THREE.Scene();
  16. this.scene.add( this.camera );
  17. this.objects = [];
  18. var xOffset = 0;
  19. var faceOffsets = [];
  20. faceOffsets.push( new THREE.Vector2( 0, 0 ) );
  21. faceOffsets.push( new THREE.Vector2( 1, 0 ) );
  22. faceOffsets.push( new THREE.Vector2( 2, 0 ) );
  23. faceOffsets.push( new THREE.Vector2( 0, 1 ) );
  24. faceOffsets.push( new THREE.Vector2( 1, 1 ) );
  25. faceOffsets.push( new THREE.Vector2( 2, 1 ) );
  26. var yOffset = 0;
  27. var textureResolution = size;
  28. size = cubeTextureLods[ 0 ].width;
  29. var offset2 = 0;
  30. var c = 4.0;
  31. this.numLods = Math.log2( cubeTextureLods[ 0 ].width ) - 2;
  32. for ( var i = 0; i < this.numLods; i ++ ) {
  33. var offset1 = ( textureResolution - textureResolution / c ) * 0.5;
  34. if ( size > 16 )
  35. c *= 2;
  36. var nMips = size > 16 ? 6 : 1;
  37. var mipOffsetX = 0;
  38. var mipOffsetY = 0;
  39. var mipSize = size;
  40. /*
  41. var testColor = [];
  42. testColor.push( new THREE.Vector3( 1, 0, 0 ) );
  43. testColor.push( new THREE.Vector3( 0, 1, 0 ) );
  44. testColor.push( new THREE.Vector3( 0, 0, 1 ) );
  45. testColor.push( new THREE.Vector3( 1, 1, 0 ) );
  46. testColor.push( new THREE.Vector3( 0, 1, 1 ) );
  47. testColor.push( new THREE.Vector3( 1, 0, 1 ) );
  48. testColor.push( new THREE.Vector3( 1, 1, 1 ) );
  49. testColor.push( new THREE.Vector3( 0.5, 1, 0.5 ) );*/
  50. for ( var j = 0; j < nMips; j ++ ) {
  51. // Mip Maps
  52. for ( var k = 0; k < 6; k ++ ) {
  53. // 6 Cube Faces
  54. var material = this.getShader();
  55. material.uniforms[ "cubeTexture" ].value = 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.PMREM_CubeUVPacker.prototype = {
  79. constructor : THREE.PMREM_CubeUVPacker,
  80. update: function( renderer ) {
  81. renderer.render( this.scene, this.camera, this.CubeUVRenderTarget, true );
  82. },
  83. getShader: function() {
  84. return new THREE.ShaderMaterial( {
  85. uniforms: {
  86. "faceIndex": { type: 'i', value: 0 },
  87. "mapSize": { type: 'f', value: 0 },
  88. "cubeTexture": { type: 't', value: null },
  89. "testColor": { type: 'v3', value: new THREE.Vector3( 1, 1, 1 ) }
  90. },
  91. vertexShader:
  92. "precision highp float;\
  93. varying vec2 vUv;\
  94. void main() {\
  95. vUv = uv;\
  96. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\
  97. }",
  98. fragmentShader:
  99. "precision highp float;\
  100. varying vec2 vUv;\
  101. uniform samplerCube cubeTexture;\
  102. uniform float mapSize;\
  103. uniform vec3 testColor;\
  104. uniform int faceIndex;\
  105. \
  106. const float PI = 3.14159265358979;\
  107. vec4 sampleCubeMap(float phi, float theta) {\
  108. float sinTheta = sin(theta);\
  109. float cosTheta = cos(theta);\
  110. vec3 sampleDir = vec3(cos(phi) * sinTheta, cosTheta, sin(phi) * sinTheta);\
  111. vec4 color = textureCube(cubeTexture, sampleDir);\
  112. return color * vec4(testColor, 1.0);\
  113. }\
  114. void main() {\
  115. vec3 sampleDirection;\
  116. vec2 uv = vUv;\
  117. uv = uv * 2.0 - 1.0;\
  118. uv.y *= -1.0;\
  119. if(faceIndex == 0) {\
  120. sampleDirection = normalize(vec3(1.0, uv.y, -uv.x));\
  121. }\
  122. else if(faceIndex == 1) {\
  123. sampleDirection = normalize(vec3(uv.x, 1.0, uv.y));\
  124. }\
  125. else if(faceIndex == 2) {\
  126. sampleDirection = normalize(vec3(uv.x, uv.y, 1.0));\
  127. }\
  128. else if(faceIndex == 3) {\
  129. sampleDirection = normalize(vec3(-1.0, uv.y, uv.x));\
  130. }\
  131. else if(faceIndex == 4) {\
  132. sampleDirection = normalize(vec3(uv.x, -1.0, -uv.y));\
  133. }\
  134. else {\
  135. sampleDirection = normalize(vec3(-uv.x, uv.y, -1.0));\
  136. }\
  137. vec4 color = textureCube(cubeTexture, (sampleDirection));\
  138. gl_FragColor = color * vec4(testColor, 1.0);\
  139. }"
  140. });
  141. }
  142. };