TextureCubeUVNode.js 7.4 KB

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