2
0

TextureCubeUVNode.js 9.7 KB

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