TextureCubeUVNode.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import { TempNode } from '../core/TempNode.js';
  2. import { ConstNode } from '../core/ConstNode.js';
  3. import { StructNode } from '../core/StructNode.js';
  4. import { FunctionNode } from '../core/FunctionNode.js';
  5. import { FunctionCallNode } from '../core/FunctionCallNode.js';
  6. import { ExpressionNode } from '../core/ExpressionNode.js';
  7. import { FloatNode } from '../inputs/FloatNode.js';
  8. import { OperatorNode } from '../math/OperatorNode.js';
  9. import { MathNode } from '../math/MathNode.js';
  10. import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
  11. class TextureCubeUVNode extends TempNode {
  12. constructor( value, uv, bias ) {
  13. super( 'v4' );
  14. this.value = value,
  15. this.uv = uv;
  16. this.bias = bias;
  17. }
  18. bilinearCubeUV( builder, texture, uv, mipInt ) {
  19. const bilinearCubeUV = new FunctionCallNode( TextureCubeUVNode.Nodes.bilinearCubeUV, [ texture, uv, mipInt ] );
  20. this.colorSpaceTL = this.colorSpaceTL || new ColorSpaceNode( new ExpressionNode( '', 'v4' ) );
  21. this.colorSpaceTL.fromDecoding( builder.getTextureEncodingFromMap( this.value.value ) );
  22. this.colorSpaceTL.input.parse( bilinearCubeUV.build( builder ) + '.tl' );
  23. this.colorSpaceTR = this.colorSpaceTR || new ColorSpaceNode( new ExpressionNode( '', 'v4' ) );
  24. this.colorSpaceTR.fromDecoding( builder.getTextureEncodingFromMap( this.value.value ) );
  25. this.colorSpaceTR.input.parse( bilinearCubeUV.build( builder ) + '.tr' );
  26. this.colorSpaceBL = this.colorSpaceBL || new ColorSpaceNode( new ExpressionNode( '', 'v4' ) );
  27. this.colorSpaceBL.fromDecoding( builder.getTextureEncodingFromMap( this.value.value ) );
  28. this.colorSpaceBL.input.parse( bilinearCubeUV.build( builder ) + '.bl' );
  29. this.colorSpaceBR = this.colorSpaceBR || new ColorSpaceNode( new ExpressionNode( '', 'v4' ) );
  30. this.colorSpaceBR.fromDecoding( builder.getTextureEncodingFromMap( this.value.value ) );
  31. this.colorSpaceBR.input.parse( bilinearCubeUV.build( builder ) + '.br' );
  32. // add a custom context for fix incompatibility with the core
  33. // include ColorSpace function only for vertex shader (in fragment shader color space functions is added automatically by core)
  34. // this should be removed in the future
  35. // context.include =: is used to include or not functions if used FunctionNode
  36. // context.ignoreCache =: not create temp variables nodeT0..9 to optimize the code
  37. const context = { include: builder.isShader( 'vertex' ), ignoreCache: true };
  38. builder.addContext( context );
  39. this.colorSpaceTLExp = new ExpressionNode( this.colorSpaceTL.build( builder, 'v4' ), 'v4' );
  40. this.colorSpaceTRExp = new ExpressionNode( this.colorSpaceTR.build( builder, 'v4' ), 'v4' );
  41. this.colorSpaceBLExp = new ExpressionNode( this.colorSpaceBL.build( builder, 'v4' ), 'v4' );
  42. this.colorSpaceBRExp = new ExpressionNode( this.colorSpaceBR.build( builder, 'v4' ), 'v4' );
  43. // end custom context
  44. builder.removeContext();
  45. // --
  46. const output = new ExpressionNode( 'mix( mix( cubeUV_TL, cubeUV_TR, cubeUV.f.x ), mix( cubeUV_BL, cubeUV_BR, cubeUV.f.x ), cubeUV.f.y )', 'v4' );
  47. output.keywords[ 'cubeUV_TL' ] = this.colorSpaceTLExp;
  48. output.keywords[ 'cubeUV_TR' ] = this.colorSpaceTRExp;
  49. output.keywords[ 'cubeUV_BL' ] = this.colorSpaceBLExp;
  50. output.keywords[ 'cubeUV_BR' ] = this.colorSpaceBRExp;
  51. output.keywords[ 'cubeUV' ] = bilinearCubeUV;
  52. return output;
  53. }
  54. generate( builder, output ) {
  55. if ( builder.isShader( 'fragment' ) ) {
  56. const uv = this.uv;
  57. const bias = this.bias || builder.context.roughness;
  58. const mipV = new FunctionCallNode( TextureCubeUVNode.Nodes.roughnessToMip, [ bias ] );
  59. const mip = new MathNode( mipV, TextureCubeUVNode.Nodes.m0, TextureCubeUVNode.Nodes.cubeUV_maxMipLevel, MathNode.CLAMP );
  60. const mipInt = new MathNode( mip, MathNode.FLOOR );
  61. const mipF = new MathNode( mip, MathNode.FRACT );
  62. const color0 = this.bilinearCubeUV( builder, this.value, uv, mipInt );
  63. const color1 = this.bilinearCubeUV( builder, this.value, uv, new OperatorNode(
  64. mipInt,
  65. new FloatNode( 1 ).setReadonly( true ),
  66. OperatorNode.ADD
  67. ) );
  68. const color1Mix = new MathNode( color0, color1, mipF, MathNode.MIX );
  69. /*
  70. // TODO: Optimize this in the future
  71. let cond = new CondNode(
  72. mipF,
  73. new FloatNode( 0 ).setReadonly( true ),
  74. CondNode.EQUAL,
  75. color0, // if
  76. color1Mix // else
  77. );
  78. */
  79. return builder.format( color1Mix.build( builder ), 'v4', output );
  80. } else {
  81. console.warn( 'THREE.TextureCubeUVNode is not compatible with ' + builder.shader + ' shader.' );
  82. return builder.format( 'vec4( 0.0 )', this.getType( builder ), output );
  83. }
  84. }
  85. toJSON( meta ) {
  86. let data = this.getJSONNode( meta );
  87. if ( ! data ) {
  88. data = this.createJSONNode( meta );
  89. data.value = this.value.toJSON( meta ).uuid;
  90. data.uv = this.uv.toJSON( meta ).uuid;
  91. data.bias = this.bias.toJSON( meta ).uuid;
  92. }
  93. return data;
  94. }
  95. }
  96. TextureCubeUVNode.Nodes = ( function () {
  97. const TextureCubeUVData = new StructNode(
  98. `struct TextureCubeUVData {
  99. vec4 tl;
  100. vec4 tr;
  101. vec4 br;
  102. vec4 bl;
  103. vec2 f;
  104. }` );
  105. const cubeUV_maxMipLevel = new ConstNode( 'float cubeUV_maxMipLevel 8.0', true );
  106. const cubeUV_minMipLevel = new ConstNode( 'float cubeUV_minMipLevel 4.0', true );
  107. const cubeUV_maxTileSize = new ConstNode( 'float cubeUV_maxTileSize 256.0', true );
  108. const cubeUV_minTileSize = new ConstNode( 'float cubeUV_minTileSize 16.0', true );
  109. // These shader functions convert between the UV coordinates of a single face of
  110. // a cubemap, the 0-5 integer index of a cube face, and the direction vector for
  111. // sampling a textureCube (not generally normalized).
  112. const getFace = new FunctionNode(
  113. `float getFace(vec3 direction) {
  114. vec3 absDirection = abs(direction);
  115. float face = -1.0;
  116. if (absDirection.x > absDirection.z) {
  117. if (absDirection.x > absDirection.y)
  118. face = direction.x > 0.0 ? 0.0 : 3.0;
  119. else
  120. face = direction.y > 0.0 ? 1.0 : 4.0;
  121. } else {
  122. if (absDirection.z > absDirection.y)
  123. face = direction.z > 0.0 ? 2.0 : 5.0;
  124. else
  125. face = direction.y > 0.0 ? 1.0 : 4.0;
  126. }
  127. return face;
  128. }` );
  129. getFace.useKeywords = false;
  130. const getUV = new FunctionNode(
  131. `vec2 getUV(vec3 direction, float face) {
  132. vec2 uv;
  133. if (face == 0.0) {
  134. uv = vec2(direction.z, direction.y) / abs(direction.x); // pos x
  135. } else if (face == 1.0) {
  136. uv = vec2(-direction.x, -direction.z) / abs(direction.y); // pos y
  137. } else if (face == 2.0) {
  138. uv = vec2(-direction.x, direction.y) / abs(direction.z); // pos z
  139. } else if (face == 3.0) {
  140. uv = vec2(-direction.z, direction.y) / abs(direction.x); // neg x
  141. } else if (face == 4.0) {
  142. uv = vec2(-direction.x, direction.z) / abs(direction.y); // neg y
  143. } else {
  144. uv = vec2(direction.x, direction.y) / abs(direction.z); // neg z
  145. }
  146. return 0.5 * (uv + 1.0);
  147. }` );
  148. getUV.useKeywords = false;
  149. const bilinearCubeUV = new FunctionNode(
  150. `TextureCubeUVData bilinearCubeUV(sampler2D envMap, vec3 direction, float mipInt) {
  151. float face = getFace(direction);
  152. float filterInt = max(cubeUV_minMipLevel - mipInt, 0.0);
  153. mipInt = max(mipInt, cubeUV_minMipLevel);
  154. float faceSize = exp2(mipInt);
  155. float texelSize = 1.0 / (3.0 * cubeUV_maxTileSize);
  156. vec2 uv = getUV(direction, face) * (faceSize - 1.0);
  157. vec2 f = fract(uv);
  158. uv += 0.5 - f;
  159. if (face > 2.0) {
  160. uv.y += faceSize;
  161. face -= 3.0;
  162. }
  163. uv.x += face * faceSize;
  164. if(mipInt < cubeUV_maxMipLevel){
  165. uv.y += 2.0 * cubeUV_maxTileSize;
  166. }
  167. uv.y += filterInt * 2.0 * cubeUV_minTileSize;
  168. uv.x += 3.0 * max(0.0, cubeUV_maxTileSize - 2.0 * faceSize);
  169. uv *= texelSize;
  170. vec4 tl = texture2D(envMap, uv);
  171. uv.x += texelSize;
  172. vec4 tr = texture2D(envMap, uv);
  173. uv.y += texelSize;
  174. vec4 br = texture2D(envMap, uv);
  175. uv.x -= texelSize;
  176. vec4 bl = texture2D(envMap, uv);
  177. return TextureCubeUVData( tl, tr, br, bl, f );
  178. }`, [ TextureCubeUVData, getFace, getUV, cubeUV_maxMipLevel, cubeUV_minMipLevel, cubeUV_maxTileSize, cubeUV_minTileSize ] );
  179. bilinearCubeUV.useKeywords = false;
  180. // These defines must match with PMREMGenerator
  181. const r0 = new ConstNode( 'float r0 1.0', true );
  182. const v0 = new ConstNode( 'float v0 0.339', true );
  183. const m0 = new ConstNode( 'float m0 -2.0', true );
  184. const r1 = new ConstNode( 'float r1 0.8', true );
  185. const v1 = new ConstNode( 'float v1 0.276', true );
  186. const m1 = new ConstNode( 'float m1 -1.0', true );
  187. const r4 = new ConstNode( 'float r4 0.4', true );
  188. const v4 = new ConstNode( 'float v4 0.046', true );
  189. const m4 = new ConstNode( 'float m4 2.0', true );
  190. const r5 = new ConstNode( 'float r5 0.305', true );
  191. const v5 = new ConstNode( 'float v5 0.016', true );
  192. const m5 = new ConstNode( 'float m5 3.0', true );
  193. const r6 = new ConstNode( 'float r6 0.21', true );
  194. const v6 = new ConstNode( 'float v6 0.0038', true );
  195. const m6 = new ConstNode( 'float m6 4.0', true );
  196. const defines = [ r0, v0, m0, r1, v1, m1, r4, v4, m4, r5, v5, m5, r6, v6, m6 ];
  197. const roughnessToMip = new FunctionNode(
  198. `float roughnessToMip(float roughness) {
  199. float mip = 0.0;
  200. if (roughness >= r1) {
  201. mip = (r0 - roughness) * (m1 - m0) / (r0 - r1) + m0;
  202. } else if (roughness >= r4) {
  203. mip = (r1 - roughness) * (m4 - m1) / (r1 - r4) + m1;
  204. } else if (roughness >= r5) {
  205. mip = (r4 - roughness) * (m5 - m4) / (r4 - r5) + m4;
  206. } else if (roughness >= r6) {
  207. mip = (r5 - roughness) * (m6 - m5) / (r5 - r6) + m5;
  208. } else {
  209. mip = -2.0 * log2(1.16 * roughness);// 1.16 = 1.79^0.25
  210. }
  211. return mip;
  212. }`, defines );
  213. return {
  214. bilinearCubeUV: bilinearCubeUV,
  215. roughnessToMip: roughnessToMip,
  216. m0: m0,
  217. cubeUV_maxMipLevel: cubeUV_maxMipLevel
  218. };
  219. } )();
  220. TextureCubeUVNode.prototype.nodeType = 'TextureCubeUV';
  221. export { TextureCubeUVNode };