TextureCubeUVNode.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.TextureCubeUVNode = function ( coord, textureSize, blinnExponentToRoughness ) {
  5. THREE.TempNode.call( this, THREE.TextureCubeUVNode.fTextureCubeUVOutput.type );
  6. this.coord = coord || new THREE.ReflectNode( THREE.ReflectNode.VECTOR );
  7. this.textureSize = textureSize || new THREE.FloatNode( 1024 );
  8. this.blinnExponentToRoughness = this.blinnExponentToRoughness || new THREE.BlinnExponentToRoughnessNode();
  9. };
  10. THREE.TextureCubeUVNode.fTextureCubeUVOutput = new THREE.StructNode([
  11. "struct TextureCubeUVData {",
  12. " vec2 uv_10;",
  13. " vec2 uv_20;",
  14. " float t;",
  15. "}"
  16. ].join( "\n" ));
  17. THREE.TextureCubeUVNode.fTextureCubeUV = ( function () {
  18. var getFaceFromDirection = new THREE.FunctionNode( [
  19. "int getFaceFromDirection(vec3 direction) {",
  20. " vec3 absDirection = abs(direction);",
  21. " int face = -1;",
  22. " if( absDirection.x > absDirection.z ) {",
  23. " if(absDirection.x > absDirection.y )",
  24. " face = direction.x > 0.0 ? 0 : 3;",
  25. " else",
  26. " face = direction.y > 0.0 ? 1 : 4;",
  27. " }",
  28. " else {",
  29. " if(absDirection.z > absDirection.y )",
  30. " face = direction.z > 0.0 ? 2 : 5;",
  31. " else",
  32. " face = direction.y > 0.0 ? 1 : 4;",
  33. " }",
  34. " return face;",
  35. "}" ].join( "\n" ) );
  36. var cubeUV_maxLods1 = new THREE.ConstNode( "#define cubeUV_maxLods1 ( log2( cubeUV_textureSize * 0.25 ) - 1.0 )" );
  37. var cubeUV_rangeClamp = new THREE.ConstNode( "#define cubeUV_rangeClamp ( exp2( ( 6.0 - 1.0 ) * 2.0 ) )" );
  38. var MipLevelInfo = new THREE.FunctionNode( [
  39. "vec2 MipLevelInfo( vec3 vec, float roughnessLevel, float roughness, in float cubeUV_textureSize ) {",
  40. " float scale = exp2(cubeUV_maxLods1 - roughnessLevel);",
  41. " float dxRoughness = dFdx(roughness);",
  42. " float dyRoughness = dFdy(roughness);",
  43. " vec3 dx = dFdx( vec * scale * dxRoughness );",
  44. " vec3 dy = dFdy( vec * scale * dyRoughness );",
  45. " float d = max( dot( dx, dx ), dot( dy, dy ) );",
  46. // Clamp the value to the max mip level counts. hard coded to 6 mips"
  47. " d = clamp(d, 1.0, cubeUV_rangeClamp);",
  48. " float mipLevel = 0.5 * log2(d);",
  49. " return vec2(floor(mipLevel), fract(mipLevel));",
  50. "}" ].join( "\n" ), [ cubeUV_maxLods1, cubeUV_rangeClamp ], { derivatives: true } );
  51. var cubeUV_maxLods2 = new THREE.ConstNode( "#define cubeUV_maxLods2 ( log2( cubeUV_textureSize * 0.25 ) - 2.0 )" );
  52. var cubeUV_rcpTextureSize = new THREE.ConstNode( "#define cubeUV_rcpTextureSize ( 1.0 / cubeUV_textureSize )" );
  53. var getCubeUV = new THREE.FunctionNode( [
  54. "vec2 getCubeUV( vec3 direction, float roughnessLevel, float mipLevel, in float cubeUV_textureSize ) {",
  55. " mipLevel = roughnessLevel > cubeUV_maxLods2 - 3.0 ? 0.0 : mipLevel;",
  56. " float a = 16.0 * cubeUV_rcpTextureSize;",
  57. "",
  58. " vec2 exp2_packed = exp2( vec2( roughnessLevel, mipLevel ) );",
  59. " vec2 rcp_exp2_packed = vec2( 1.0 ) / exp2_packed;",
  60. // float powScale = exp2(roughnessLevel + mipLevel);"
  61. " float powScale = exp2_packed.x * exp2_packed.y;",
  62. // float scale = 1.0 / exp2(roughnessLevel + 2.0 + mipLevel);"
  63. " float scale = rcp_exp2_packed.x * rcp_exp2_packed.y * 0.25;",
  64. // float mipOffset = 0.75*(1.0 - 1.0/exp2(mipLevel))/exp2(roughnessLevel);"
  65. " float mipOffset = 0.75*(1.0 - rcp_exp2_packed.y) * rcp_exp2_packed.x;",
  66. "",
  67. " bool bRes = mipLevel == 0.0;",
  68. " scale = bRes && (scale < a) ? a : scale;",
  69. "",
  70. " vec3 r;",
  71. " vec2 offset;",
  72. " int face = getFaceFromDirection(direction);",
  73. "",
  74. " float rcpPowScale = 1.0 / powScale;",
  75. "",
  76. " if( face == 0) {",
  77. " r = vec3(direction.x, -direction.z, direction.y);",
  78. " offset = vec2(0.0+mipOffset,0.75 * rcpPowScale);",
  79. " offset.y = bRes && (offset.y < 2.0*a) ? a : offset.y;",
  80. " }",
  81. " else if( face == 1) {",
  82. " r = vec3(direction.y, direction.x, direction.z);",
  83. " offset = vec2(scale+mipOffset, 0.75 * rcpPowScale);",
  84. " offset.y = bRes && (offset.y < 2.0*a) ? a : offset.y;",
  85. " }",
  86. " else if( face == 2) {",
  87. " r = vec3(direction.z, direction.x, direction.y);",
  88. " offset = vec2(2.0*scale+mipOffset, 0.75 * rcpPowScale);",
  89. " offset.y = bRes && (offset.y < 2.0*a) ? a : offset.y;",
  90. " }",
  91. " else if( face == 3) {",
  92. " r = vec3(direction.x, direction.z, direction.y);",
  93. " offset = vec2(0.0+mipOffset,0.5 * rcpPowScale);",
  94. " offset.y = bRes && (offset.y < 2.0*a) ? 0.0 : offset.y;",
  95. " }",
  96. " else if( face == 4) {",
  97. " r = vec3(direction.y, direction.x, -direction.z);",
  98. " offset = vec2(scale+mipOffset, 0.5 * rcpPowScale);",
  99. " offset.y = bRes && (offset.y < 2.0*a) ? 0.0 : offset.y;",
  100. " }",
  101. " else {",
  102. " r = vec3(direction.z, -direction.x, direction.y);",
  103. " offset = vec2(2.0*scale+mipOffset, 0.5 * rcpPowScale);",
  104. " offset.y = bRes && (offset.y < 2.0*a) ? 0.0 : offset.y;",
  105. " }",
  106. " r = normalize(r);",
  107. " float texelOffset = 0.5 * cubeUV_rcpTextureSize;",
  108. " vec2 s = ( r.yz / abs( r.x ) + vec2( 1.0 ) ) * 0.5;",
  109. " vec2 base = offset + vec2( texelOffset );",
  110. " return base + s * ( scale - 2.0 * texelOffset );",
  111. "}" ].join( "\n" ), [ cubeUV_maxLods2, cubeUV_rcpTextureSize, getFaceFromDirection ] );
  112. var cubeUV_maxLods3 = new THREE.ConstNode( "#define cubeUV_maxLods3 ( log2( cubeUV_textureSize * 0.25 ) - 3.0 )" );
  113. return new THREE.FunctionNode( [
  114. "TextureCubeUVData textureCubeUV( vec3 reflectedDirection, float roughness, in float cubeUV_textureSize ) {",
  115. " float roughnessVal = roughness * cubeUV_maxLods3;",
  116. " float r1 = floor(roughnessVal);",
  117. " float r2 = r1 + 1.0;",
  118. " float t = fract(roughnessVal);",
  119. " vec2 mipInfo = MipLevelInfo(reflectedDirection, r1, roughness, cubeUV_textureSize);",
  120. " float s = mipInfo.y;",
  121. " float level0 = mipInfo.x;",
  122. " float level1 = level0 + 1.0;",
  123. " level1 = level1 > 5.0 ? 5.0 : level1;",
  124. "",
  125. // round to nearest mipmap if we are not interpolating."
  126. " level0 += min( floor( s + 0.5 ), 5.0 );",
  127. "",
  128. // Tri linear interpolation."
  129. " vec2 uv_10 = getCubeUV(reflectedDirection, r1, level0, cubeUV_textureSize);",
  130. " vec2 uv_20 = getCubeUV(reflectedDirection, r2, level0, cubeUV_textureSize);",
  131. "",
  132. " return TextureCubeUVData(uv_10, uv_20, t);",
  133. "}" ].join( "\n" ), [ THREE.TextureCubeUVNode.fTextureCubeUVOutput, cubeUV_maxLods3, MipLevelInfo, getCubeUV ] );
  134. } )();
  135. THREE.TextureCubeUVNode.prototype = Object.create( THREE.TempNode.prototype );
  136. THREE.TextureCubeUVNode.prototype.constructor = THREE.TextureCubeUVNode;
  137. THREE.TextureCubeUVNode.prototype.nodeType = "TextureCubeUV";
  138. THREE.TextureCubeUVNode.prototype.generate = function ( builder, output ) {
  139. var material = builder.material, func = THREE.TextureCubeUVNode.fTextureCubeUV;
  140. builder.include( func );
  141. if ( builder.isShader( 'fragment' ) ) {
  142. return builder.format( func.name + '( ' + this.coord.build( builder, 'v3' ) + ', ' +
  143. this.blinnExponentToRoughness.build( builder, 'fv1' ) + ', ' +
  144. this.textureSize.build( builder, 'fv1' ) + ' )', this.getType( builder ), output );
  145. } else {
  146. console.warn( "THREE.TextureCubeUVNode is not compatible with " + builder.shader + " shader." );
  147. return builder.format( 'vec4( 0.0 )', this.getType( builder ), output );
  148. }
  149. };
  150. THREE.TextureCubeUVNode.prototype.toJSON = function ( meta ) {
  151. var data = this.getJSONNode( meta );
  152. if ( ! data ) {
  153. data = this.createJSONNode( meta );
  154. data.coord = this.coord.toJSON( meta ).uuid;
  155. data.textureSize = this.textureSize.toJSON( meta ).uuid;
  156. data.blinnExponentToRoughness = this.blinnExponentToRoughness.toJSON( meta ).uuid;
  157. }
  158. return data;
  159. };