MathNode.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.hashProperties = [ "method" ];
  57. MathNode.prototype.getNumInputs = function ( /*builder*/ ) {
  58. switch ( this.method ) {
  59. case MathNode.MIX:
  60. case MathNode.CLAMP:
  61. case MathNode.REFRACT:
  62. case MathNode.SMOOTHSTEP:
  63. case MathNode.FACEFORWARD:
  64. return 3;
  65. case MathNode.MIN:
  66. case MathNode.MAX:
  67. case MathNode.MOD:
  68. case MathNode.STEP:
  69. case MathNode.REFLECT:
  70. case MathNode.DISTANCE:
  71. case MathNode.DOT:
  72. case MathNode.CROSS:
  73. case MathNode.POW:
  74. return 2;
  75. default:
  76. return 1;
  77. }
  78. };
  79. MathNode.prototype.getInputType = function ( builder ) {
  80. var a = builder.getTypeLength( this.a.getType( builder ) );
  81. var b = this.b ? builder.getTypeLength( this.b.getType( builder ) ) : 0;
  82. var c = this.c ? builder.getTypeLength( this.c.getType( builder ) ) : 0;
  83. if ( a > b && a > c ) {
  84. return this.a.getType( builder );
  85. } else if ( b > c ) {
  86. return this.b.getType( builder );
  87. }
  88. return this.c.getType( builder );
  89. };
  90. MathNode.prototype.getType = function ( builder ) {
  91. switch ( this.method ) {
  92. case MathNode.LENGTH:
  93. case MathNode.DISTANCE:
  94. case MathNode.DOT:
  95. return 'f';
  96. case MathNode.CROSS:
  97. return 'v3';
  98. }
  99. return this.getInputType( builder );
  100. };
  101. MathNode.prototype.generate = function ( builder, output ) {
  102. var a, b, c,
  103. al = this.a ? builder.getTypeLength( this.a.getType( builder ) ) : 0,
  104. bl = this.b ? builder.getTypeLength( this.b.getType( builder ) ) : 0,
  105. cl = this.c ? builder.getTypeLength( this.c.getType( builder ) ) : 0,
  106. inputType = this.getInputType( builder ),
  107. nodeType = this.getType( builder );
  108. switch ( this.method ) {
  109. // 1 input
  110. case MathNode.NEGATE:
  111. return builder.format( '( -' + this.a.build( builder, inputType ) + ' )', inputType, output );
  112. case MathNode.INVERT:
  113. return builder.format( '( 1.0 - ' + this.a.build( builder, inputType ) + ' )', inputType, output );
  114. // 2 inputs
  115. case MathNode.CROSS:
  116. a = this.a.build( builder, 'v3' );
  117. b = this.b.build( builder, 'v3' );
  118. break;
  119. case MathNode.STEP:
  120. a = this.a.build( builder, al === 1 ? 'f' : inputType );
  121. b = this.b.build( builder, inputType );
  122. break;
  123. case MathNode.MIN:
  124. case MathNode.MAX:
  125. case MathNode.MOD:
  126. a = this.a.build( builder, inputType );
  127. b = this.b.build( builder, bl === 1 ? 'f' : inputType );
  128. break;
  129. // 3 inputs
  130. case MathNode.REFRACT:
  131. a = this.a.build( builder, inputType );
  132. b = this.b.build( builder, inputType );
  133. c = this.c.build( builder, 'f' );
  134. break;
  135. case MathNode.MIX:
  136. a = this.a.build( builder, inputType );
  137. b = this.b.build( builder, inputType );
  138. c = this.c.build( builder, cl === 1 ? 'f' : inputType );
  139. break;
  140. // default
  141. default:
  142. a = this.a.build( builder, inputType );
  143. if ( this.b ) b = this.b.build( builder, inputType );
  144. if ( this.c ) c = this.c.build( builder, inputType );
  145. break;
  146. }
  147. // build function call
  148. var params = [];
  149. params.push( a );
  150. if ( b ) params.push( b );
  151. if ( c ) params.push( c );
  152. var numInputs = this.getNumInputs( builder );
  153. if ( params.length !== numInputs ) {
  154. throw Error( `Arguments not match used in "${this.method}". Require ${numInputs}, currently ${params.length}.` );
  155. }
  156. return builder.format( this.method + '( ' + params.join( ', ' ) + ' )', nodeType, output );
  157. };
  158. MathNode.prototype.copy = function ( source ) {
  159. TempNode.prototype.copy.call( this, source );
  160. this.a = source.a;
  161. this.b = source.b;
  162. this.c = source.c;
  163. this.method = source.method;
  164. return this;
  165. };
  166. MathNode.prototype.toJSON = function ( meta ) {
  167. var data = this.getJSONNode( meta );
  168. if ( ! data ) {
  169. data = this.createJSONNode( meta );
  170. data.a = this.a.toJSON( meta ).uuid;
  171. if ( this.b ) data.b = this.b.toJSON( meta ).uuid;
  172. if ( this.c ) data.c = this.c.toJSON( meta ).uuid;
  173. data.method = this.method;
  174. }
  175. return data;
  176. };
  177. export { MathNode };