PMREM_CubeUVPacker.js 5.1 KB

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