MathNode.js 5.1 KB

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