MathNode.js 5.3 KB

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