MathNode.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import TempNode from '../core/TempNode.js';
  2. import ExpressionNode from '../core/ExpressionNode.js';
  3. import SplitNode from '../utils/SplitNode.js';
  4. import OperatorNode from './OperatorNode.js';
  5. class MathNode extends TempNode {
  6. // 1 input
  7. static RADIANS = 'radians';
  8. static DEGREES = 'degrees';
  9. static EXP = 'exp';
  10. static EXP2 = 'exp2';
  11. static LOG = 'log';
  12. static LOG2 = 'log2';
  13. static SQRT = 'sqrt';
  14. static INVERSE_SQRT = 'inversesqrt';
  15. static FLOOR = 'floor';
  16. static CEIL = 'ceil';
  17. static NORMALIZE = 'normalize';
  18. static FRACT = 'fract';
  19. static SIN = 'sin';
  20. static COS = 'cos';
  21. static TAN = 'tan';
  22. static ASIN = 'asin';
  23. static ACOS = 'acos';
  24. static ATAN = 'atan';
  25. static ABS = 'abs';
  26. static SIGN = 'sign';
  27. static LENGTH = 'length';
  28. static NEGATE = 'negate';
  29. static INVERT = 'invert';
  30. static DFDX = 'dFdx';
  31. static DFDY = 'dFdy';
  32. static ROUND = 'round';
  33. // 2 inputs
  34. static ATAN2 = 'atan2';
  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.isMatrix( aType ) ? 0 : builder.getTypeLength( aType );
  63. const bLen = builder.isMatrix( bType ) ? 0 : builder.getTypeLength( bType );
  64. const cLen = builder.isMatrix( cType ) ? 0 : 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 ( method === MathNode.TRANSFORM_DIRECTION ) {
  93. // dir can be either a direction vector or a normal vector
  94. // upper-left 3x3 of matrix is assumed to be orthogonal
  95. let tA = a;
  96. let tB = b;
  97. if ( builder.isMatrix( tA.getNodeType( builder ) ) ) {
  98. tB = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tB.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  99. } else {
  100. tA = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tA.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  101. }
  102. const mulNode = new SplitNode( new OperatorNode( '*', tA, tB ), 'xyz' );
  103. return new MathNode( MathNode.NORMALIZE, mulNode ).build( builder );
  104. } else if ( method === MathNode.NEGATE ) {
  105. return builder.format( '( -' + a.build( builder, inputType ) + ' )', type, output );
  106. } else if ( method === MathNode.INVERT ) {
  107. return builder.format( '( 1.0 - ' + a.build( builder, inputType ) + ' )', type, output );
  108. } else {
  109. const params = [];
  110. if ( method === MathNode.CROSS ) {
  111. params.push(
  112. a.build( builder, type ),
  113. b.build( builder, type )
  114. );
  115. } else if ( method === MathNode.STEP ) {
  116. params.push(
  117. a.build( builder, builder.getTypeLength( a.getNodeType( builder ) ) === 1 ? 'float' : inputType ),
  118. b.build( builder, inputType )
  119. );
  120. } else if ( ( isWebGL && ( method === MathNode.MIN || method === MathNode.MAX ) ) || method === MathNode.MOD ) {
  121. params.push(
  122. a.build( builder, inputType ),
  123. b.build( builder, builder.getTypeLength( b.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  124. );
  125. } else if ( method === MathNode.REFRACT ) {
  126. params.push(
  127. a.build( builder, inputType ),
  128. b.build( builder, inputType ),
  129. c.build( builder, 'float' )
  130. );
  131. } else if ( method === MathNode.MIX ) {
  132. params.push(
  133. a.build( builder, inputType ),
  134. b.build( builder, inputType ),
  135. c.build( builder, builder.getTypeLength( c.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  136. );
  137. } else {
  138. params.push( a.build( builder, inputType ) );
  139. if ( c !== null ) {
  140. params.push( b.build( builder, inputType ), c.build( builder, inputType ) );
  141. } else if ( b !== null ) {
  142. params.push( b.build( builder, inputType ) );
  143. }
  144. }
  145. return builder.format( `${ builder.getMethod( method ) }( ${params.join( ', ' )} )`, type, output );
  146. }
  147. }
  148. serialize( data ) {
  149. super.serialize( data );
  150. data.method = this.method;
  151. }
  152. deserialize( data ) {
  153. super.deserialize( data );
  154. this.method = data.method;
  155. }
  156. }
  157. export default MathNode;