MathNode.js 5.3 KB

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