MathNode.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import TempNode from '../core/TempNode.js';
  2. import ExpressionNode from '../core/ExpressionNode.js';
  3. import JoinNode from '../utils/JoinNode.js';
  4. import SplitNode from '../utils/SplitNode.js';
  5. import OperatorNode from './OperatorNode.js';
  6. class MathNode extends TempNode {
  7. // 1 input
  8. static RAD = 'radians';
  9. static DEG = 'degrees';
  10. static EXP = 'exp';
  11. static EXP2 = 'exp2';
  12. static LOG = 'log';
  13. static LOG2 = 'log2';
  14. static SQRT = 'sqrt';
  15. static INV_SQRT = 'inversesqrt';
  16. static FLOOR = 'floor';
  17. static CEIL = 'ceil';
  18. static NORMALIZE = 'normalize';
  19. static FRACT = 'fract';
  20. static SIN = 'sin';
  21. static COS = 'cos';
  22. static TAN = 'tan';
  23. static ASIN = 'asin';
  24. static ACOS = 'acos';
  25. static ATAN = 'atan';
  26. static ABS = 'abs';
  27. static SIGN = 'sign';
  28. static LENGTH = 'length';
  29. static NEGATE = 'negate';
  30. static INVERT = 'invert';
  31. static DFDX = 'dFdx';
  32. static DFDY = 'dFdy';
  33. static SATURATE = 'saturate'
  34. // 2 inputs
  35. static MIN = 'min';
  36. static MAX = 'max';
  37. static MOD = 'mod';
  38. static STEP = 'step';
  39. static REFLECT = 'reflect';
  40. static DISTANCE = 'distance';
  41. static DOT = 'dot';
  42. static CROSS = 'cross';
  43. static POW = 'pow';
  44. static TRANSFORM_DIRECTION = 'transformDirection';
  45. // 3 inputs
  46. static MIX = 'mix';
  47. static CLAMP = 'clamp';
  48. static REFRACT = 'refract';
  49. static SMOOTHSTEP = 'smoothstep';
  50. static FACEFORWARD = 'faceforward';
  51. constructor( method, aNode, bNode = null, cNode = null ) {
  52. super();
  53. this.method = method;
  54. this.aNode = aNode;
  55. this.bNode = bNode;
  56. this.cNode = cNode;
  57. }
  58. getInputType( builder ) {
  59. const aType = this.aNode.getNodeType( builder );
  60. const bType = this.bNode ? this.bNode.getNodeType( builder ) : null;
  61. const cType = this.cNode ? this.cNode.getNodeType( builder ) : null;
  62. const aLen = builder.getTypeLength( aType );
  63. const bLen = builder.getTypeLength( bType );
  64. const cLen = builder.getTypeLength( cType );
  65. if ( aLen > bLen && aLen > cLen ) {
  66. return aType;
  67. } else if ( bLen > cLen ) {
  68. return bType;
  69. } else if ( cLen > aLen ) {
  70. return cType;
  71. }
  72. return aType;
  73. }
  74. getNodeType( builder ) {
  75. const method = this.method;
  76. if ( method === MathNode.LENGTH || method === MathNode.DISTANCE || method === MathNode.DOT ) {
  77. return 'float';
  78. } else if ( method === MathNode.CROSS ) {
  79. return 'vec3';
  80. } else {
  81. return this.getInputType( builder );
  82. }
  83. }
  84. generate( builder, output ) {
  85. const method = this.method;
  86. const type = this.getNodeType( builder );
  87. const inputType = this.getInputType( builder );
  88. const a = this.aNode;
  89. const b = this.bNode;
  90. const c = this.cNode;
  91. const isWebGL = builder.renderer.isWebGLRenderer === true;
  92. if ( isWebGL && ( method === MathNode.DFDX || method === MathNode.DFDY ) && output === 'vec3' ) {
  93. // Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
  94. return new JoinNode( [
  95. new MathNode( method, new SplitNode( a, 'x' ) ),
  96. new MathNode( method, new SplitNode( a, 'y' ) ),
  97. new MathNode( method, new SplitNode( a, 'z' ) )
  98. ] ).build( builder );
  99. } else if ( method === MathNode.TRANSFORM_DIRECTION ) {
  100. // dir can be either a direction vector or a normal vector
  101. // upper-left 3x3 of matrix is assumed to be orthogonal
  102. let tA = a;
  103. let tB = b;
  104. if ( builder.isMatrix( tA.getNodeType( builder ) ) ) {
  105. tB = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tB.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  106. } else {
  107. tA = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tA.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  108. }
  109. const mulNode = new SplitNode( new OperatorNode( '*', tA, tB ), 'xyz' );
  110. return new MathNode( MathNode.NORMALIZE, mulNode ).build( builder );
  111. } else if ( method === MathNode.SATURATE ) {
  112. return `clamp( ${ a.build( builder, inputType ) }, 0.0, 1.0 )`;
  113. } else if ( method === MathNode.NEGATE ) {
  114. return '( -' + a.build( builder, inputType ) + ' )';
  115. } else if ( method === MathNode.INVERT ) {
  116. return '( 1.0 - ' + a.build( builder, inputType ) + ' )';
  117. } else {
  118. const params = [];
  119. if ( method === MathNode.CROSS ) {
  120. params.push(
  121. a.build( builder, type ),
  122. b.build( builder, type )
  123. );
  124. } else if ( method === MathNode.STEP ) {
  125. params.push(
  126. b.build( builder, builder.getTypeLength( a.getNodeType( builder ) ) === 1 ? 'float' : inputType ),
  127. b.build( builder, inputType )
  128. );
  129. } else if ( ( isWebGL && ( method === MathNode.MIN || method === MathNode.MAX ) ) || method === MathNode.MOD ) {
  130. params.push(
  131. a.build( builder, inputType ),
  132. b.build( builder, builder.getTypeLength( b.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  133. );
  134. } else if ( method === MathNode.REFRACT ) {
  135. params.push(
  136. a.build( builder, inputType ),
  137. b.build( builder, inputType ),
  138. c.build( builder, 'float' )
  139. );
  140. } else if ( method === MathNode.MIX ) {
  141. params.push(
  142. a.build( builder, inputType ),
  143. b.build( builder, inputType ),
  144. c.build( builder, builder.getTypeLength( c.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  145. );
  146. } else {
  147. params.push( a.build( builder, inputType ) );
  148. if ( c !== null ) {
  149. params.push( b.build( builder, inputType ), c.build( builder, inputType ) );
  150. } else if ( b !== null ) {
  151. params.push( b.build( builder, inputType ) );
  152. }
  153. }
  154. return `${ builder.getMethod( method ) }( ${params.join( ', ' )} )`;
  155. }
  156. }
  157. }
  158. export default MathNode;