MathNode.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import { TempNode } from '../core/TempNode.js';
  2. class MathNode extends TempNode {
  3. constructor( a, bOrMethod, cOrMethod, method ) {
  4. super();
  5. this.a = a;
  6. typeof bOrMethod !== 'string' ? this.b = bOrMethod : method = bOrMethod;
  7. typeof cOrMethod !== 'string' ? this.c = cOrMethod : method = cOrMethod;
  8. this.method = method;
  9. }
  10. getNumInputs( /*builder*/ ) {
  11. switch ( this.method ) {
  12. // variable
  13. case MathNode.ARCTAN:
  14. return this.b ? 2 : 1;
  15. // 3
  16. case MathNode.MIX:
  17. case MathNode.CLAMP:
  18. case MathNode.REFRACT:
  19. case MathNode.SMOOTHSTEP:
  20. case MathNode.FACEFORWARD:
  21. return 3;
  22. // 2
  23. case MathNode.MIN:
  24. case MathNode.MAX:
  25. case MathNode.MOD:
  26. case MathNode.STEP:
  27. case MathNode.REFLECT:
  28. case MathNode.DISTANCE:
  29. case MathNode.DOT:
  30. case MathNode.CROSS:
  31. case MathNode.POW:
  32. return 2;
  33. // 1
  34. default:
  35. return 1;
  36. }
  37. }
  38. getInputType( builder ) {
  39. const a = builder.getTypeLength( this.a.getType( builder ) );
  40. const b = this.b ? builder.getTypeLength( this.b.getType( builder ) ) : 0;
  41. const c = this.c ? builder.getTypeLength( this.c.getType( builder ) ) : 0;
  42. if ( a > b && a > c ) {
  43. return this.a.getType( builder );
  44. } else if ( b > c ) {
  45. return this.b.getType( builder );
  46. }
  47. return this.c.getType( builder );
  48. }
  49. getType( builder ) {
  50. switch ( this.method ) {
  51. case MathNode.LENGTH:
  52. case MathNode.DISTANCE:
  53. case MathNode.DOT:
  54. return 'f';
  55. case MathNode.CROSS:
  56. return 'v3';
  57. }
  58. return this.getInputType( builder );
  59. }
  60. generate( builder, output ) {
  61. let a, b, c;
  62. const al = this.a ? builder.getTypeLength( this.a.getType( builder ) ) : 0,
  63. bl = this.b ? builder.getTypeLength( this.b.getType( builder ) ) : 0,
  64. cl = this.c ? builder.getTypeLength( this.c.getType( builder ) ) : 0,
  65. inputType = this.getInputType( builder ),
  66. nodeType = this.getType( builder );
  67. switch ( this.method ) {
  68. // 1 input
  69. case MathNode.NEGATE:
  70. return builder.format( '( -' + this.a.build( builder, inputType ) + ' )', inputType, output );
  71. case MathNode.INVERT:
  72. return builder.format( '( 1.0 - ' + this.a.build( builder, inputType ) + ' )', inputType, output );
  73. // 2 inputs
  74. case MathNode.CROSS:
  75. a = this.a.build( builder, 'v3' );
  76. b = this.b.build( builder, 'v3' );
  77. break;
  78. case MathNode.STEP:
  79. a = this.a.build( builder, al === 1 ? 'f' : inputType );
  80. b = this.b.build( builder, inputType );
  81. break;
  82. case MathNode.MIN:
  83. case MathNode.MAX:
  84. case MathNode.MOD:
  85. a = this.a.build( builder, inputType );
  86. b = this.b.build( builder, bl === 1 ? 'f' : inputType );
  87. break;
  88. // 3 inputs
  89. case MathNode.REFRACT:
  90. a = this.a.build( builder, inputType );
  91. b = this.b.build( builder, inputType );
  92. c = this.c.build( builder, 'f' );
  93. break;
  94. case MathNode.MIX:
  95. a = this.a.build( builder, inputType );
  96. b = this.b.build( builder, inputType );
  97. c = this.c.build( builder, cl === 1 ? 'f' : inputType );
  98. break;
  99. // default
  100. default:
  101. a = this.a.build( builder, inputType );
  102. if ( this.b ) b = this.b.build( builder, inputType );
  103. if ( this.c ) c = this.c.build( builder, inputType );
  104. break;
  105. }
  106. // build function call
  107. const params = [];
  108. params.push( a );
  109. if ( b ) params.push( b );
  110. if ( c ) params.push( c );
  111. const numInputs = this.getNumInputs( builder );
  112. if ( params.length !== numInputs ) {
  113. throw Error( `Arguments not match used in "${this.method}". Require ${numInputs}, currently ${params.length}.` );
  114. }
  115. return builder.format( this.method + '( ' + params.join( ', ' ) + ' )', nodeType, output );
  116. }
  117. copy( source ) {
  118. super.copy( source );
  119. this.a = source.a;
  120. this.b = source.b;
  121. this.c = source.c;
  122. this.method = source.method;
  123. return this;
  124. }
  125. toJSON( meta ) {
  126. let data = this.getJSONNode( meta );
  127. if ( ! data ) {
  128. data = this.createJSONNode( meta );
  129. data.a = this.a.toJSON( meta ).uuid;
  130. if ( this.b ) data.b = this.b.toJSON( meta ).uuid;
  131. if ( this.c ) data.c = this.c.toJSON( meta ).uuid;
  132. data.method = this.method;
  133. }
  134. return data;
  135. }
  136. }
  137. // 1 input
  138. MathNode.RAD = 'radians';
  139. MathNode.DEG = 'degrees';
  140. MathNode.EXP = 'exp';
  141. MathNode.EXP2 = 'exp2';
  142. MathNode.LOG = 'log';
  143. MathNode.LOG2 = 'log2';
  144. MathNode.SQRT = 'sqrt';
  145. MathNode.INV_SQRT = 'inversesqrt';
  146. MathNode.FLOOR = 'floor';
  147. MathNode.CEIL = 'ceil';
  148. MathNode.NORMALIZE = 'normalize';
  149. MathNode.FRACT = 'fract';
  150. MathNode.SATURATE = 'saturate';
  151. MathNode.SIN = 'sin';
  152. MathNode.COS = 'cos';
  153. MathNode.TAN = 'tan';
  154. MathNode.ASIN = 'asin';
  155. MathNode.ACOS = 'acos';
  156. MathNode.ARCTAN = 'atan';
  157. MathNode.ABS = 'abs';
  158. MathNode.SIGN = 'sign';
  159. MathNode.LENGTH = 'length';
  160. MathNode.NEGATE = 'negate';
  161. MathNode.INVERT = 'invert';
  162. // 2 inputs
  163. MathNode.MIN = 'min';
  164. MathNode.MAX = 'max';
  165. MathNode.MOD = 'mod';
  166. MathNode.STEP = 'step';
  167. MathNode.REFLECT = 'reflect';
  168. MathNode.DISTANCE = 'distance';
  169. MathNode.DOT = 'dot';
  170. MathNode.CROSS = 'cross';
  171. MathNode.POW = 'pow';
  172. // 3 inputs
  173. MathNode.MIX = 'mix';
  174. MathNode.CLAMP = 'clamp';
  175. MathNode.REFRACT = 'refract';
  176. MathNode.SMOOTHSTEP = 'smoothstep';
  177. MathNode.FACEFORWARD = 'faceforward';
  178. MathNode.prototype.nodeType = 'Math';
  179. MathNode.prototype.hashProperties = [ 'method' ];
  180. export { MathNode };