MathNode.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. if ( builder.renderer.isWebGLRenderer === true && ( method === MathNode.DFDX || method === MathNode.DFDY ) && output === 'vec3' ) {
  92. // Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
  93. return new JoinNode( [
  94. new MathNode( method, new SplitNode( a, 'x' ) ),
  95. new MathNode( method, new SplitNode( a, 'y' ) ),
  96. new MathNode( method, new SplitNode( a, 'z' ) )
  97. ] ).build( builder );
  98. } else if ( method === MathNode.TRANSFORM_DIRECTION ) {
  99. // dir can be either a direction vector or a normal vector
  100. // upper-left 3x3 of matrix is assumed to be orthogonal
  101. let tA = a;
  102. let tB = b;
  103. if ( builder.isMatrix( tA.getNodeType( builder ) ) ) {
  104. tB = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tB.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  105. } else {
  106. tA = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tA.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  107. }
  108. const mulNode = new SplitNode( new OperatorNode( '*', tA, tB ), 'xyz' );
  109. return new MathNode( MathNode.NORMALIZE, mulNode ).build( builder );
  110. } else if ( method === MathNode.SATURATE ) {
  111. return `clamp( ${ a.build( builder, inputType ) }, 0.0, 1.0 )`;
  112. } else if ( method === MathNode.NEGATE ) {
  113. return '( -' + a.build( builder, inputType ) + ' )';
  114. } else if ( method === MathNode.INVERT ) {
  115. return '( 1.0 - ' + a.build( builder, inputType ) + ' )';
  116. } else {
  117. const params = [];
  118. if ( method === MathNode.CROSS ) {
  119. params.push(
  120. a.build( builder, type ),
  121. b.build( builder, type )
  122. );
  123. } else if ( method === MathNode.STEP ) {
  124. params.push(
  125. b.build( builder, builder.getTypeLength( a.getNodeType( builder ) ) === 1 ? 'float' : inputType ),
  126. b.build( builder, inputType )
  127. );
  128. } else if ( method === MathNode.MIN || method === MathNode.MAX || method === MathNode.MOD ) {
  129. params.push(
  130. a.build( builder, inputType ),
  131. b.build( builder, builder.getTypeLength( b.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  132. );
  133. } else if ( method === MathNode.REFRACT ) {
  134. params.push(
  135. a.build( builder, inputType ),
  136. b.build( builder, inputType ),
  137. c.build( builder, 'float' )
  138. );
  139. } else if ( method === MathNode.MIX ) {
  140. params.push(
  141. a.build( builder, inputType ),
  142. b.build( builder, inputType ),
  143. c.build( builder, builder.getTypeLength( c.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  144. );
  145. } else {
  146. params.push( a.build( builder, inputType ) );
  147. if ( c !== null ) {
  148. params.push( b.build( builder, inputType ), c.build( builder, inputType ) );
  149. } else if ( b !== null ) {
  150. params.push( b.build( builder, inputType ) );
  151. }
  152. }
  153. return `${ builder.getMethod( method ) }( ${params.join( ', ' )} )`;
  154. }
  155. }
  156. }
  157. export default MathNode;