TextureCubeUVNode.js 7.3 KB

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