MathNode.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package arm.logic;
  2. import zui.Nodes;
  3. import arm.logic.LogicNode;
  4. import arm.Translator._tr;
  5. @:keep
  6. class MathNode extends LogicNode {
  7. public var operation: String;
  8. public var use_clamp: Bool;
  9. public function new(tree: LogicTree) {
  10. super(tree);
  11. }
  12. override function get(from: Int, done: Dynamic->Void) {
  13. inputs[0].get(function(v1: Float) {
  14. inputs[1].get(function(v2: Float) {
  15. var f = 0.0;
  16. switch (operation) {
  17. case "Add":
  18. f = v1 + v2;
  19. case "Multiply":
  20. f = v1 * v2;
  21. case "Sine":
  22. f = Math.sin(v1);
  23. case "Cosine":
  24. f = Math.cos(v1);
  25. case "Max":
  26. f = Math.max(v1, v2);
  27. case "Min":
  28. f = Math.min(v1, v2);
  29. case "Absolute":
  30. f = Math.abs(v1);
  31. case "Subtract":
  32. f = v1 - v2;
  33. case "Divide":
  34. f = v1 / (v2 == 0.0 ? 0.000001 : v2);
  35. case "Tangent":
  36. f = Math.tan(v1);
  37. case "Arcsine":
  38. f = Math.asin(v1);
  39. case "Arccosine":
  40. f = Math.acos(v1);
  41. case "Arctangent":
  42. f = Math.atan(v1);
  43. case "Arctan2":
  44. f = Math.atan2(v2, v1);
  45. case "Power":
  46. f = Math.pow(v1, v2);
  47. case "Logarithm":
  48. f = Math.log(v1);
  49. case "Round":
  50. f = Math.round(v1);
  51. case "Floor":
  52. f = Math.floor(v1);
  53. case "Ceil":
  54. f = Math.ceil(v1);
  55. case "Truncate":
  56. f = Math.ffloor(v1);
  57. case "Fraction":
  58. f = v1 - Math.floor(v1);
  59. case "Less Than":
  60. f = v1 < v2 ? 1.0 : 0.0;
  61. case "Greater Than":
  62. f = v1 > v2 ? 1.0 : 0.0;
  63. case "Modulo":
  64. f = v1 % v2;
  65. case "Snap":
  66. f = Math.floor(v1 / v2) * v2;
  67. case "Square Root":
  68. f = Math.sqrt(v1);
  69. case "Inverse Square Root":
  70. f = 1.0 / Math.sqrt(v1);
  71. case "Exponent":
  72. f = Math.exp(v1);
  73. case "Sign":
  74. f = v1 > 0 ? 1.0 : (v1 < 0 ? -1.0 : 0);
  75. case "Ping-Pong":
  76. f = (v2 != 0.0) ? v2 - Math.abs((Math.abs(v1) % (2 * v2)) - v2) : 0.0;
  77. case "Hyperbolic Sine":
  78. f = (Math.exp(v1) - Math.exp(-v1)) / 2.0;
  79. case "Hyperbolic Cosine":
  80. f = (Math.exp(v1) + Math.exp(-v1)) / 2.0;
  81. case "Hyperbolic Tangent":
  82. f = 1.0 - (2.0 / (Math.exp(2 * v1) + 1));
  83. case "To Radians":
  84. f = v1 / 180.0 * Math.PI;
  85. case "To Degrees":
  86. f = v1 / Math.PI * 180.0;
  87. }
  88. if (use_clamp) f = f < 0.0 ? 0.0 : (f > 1.0 ? 1.0 : f);
  89. done(f);
  90. });
  91. });
  92. }
  93. public static var def: TNode = {
  94. id: 0,
  95. name: _tr("Math"),
  96. type: "MathNode",
  97. x: 0,
  98. y: 0,
  99. color: 0xff4982a0,
  100. inputs: [
  101. {
  102. id: 0,
  103. node_id: 0,
  104. name: _tr("Value"),
  105. type: "VALUE",
  106. color: 0xffa1a1a1,
  107. default_value: 0.5
  108. },
  109. {
  110. id: 0,
  111. node_id: 0,
  112. name: _tr("Value"),
  113. type: "VALUE",
  114. color: 0xffa1a1a1,
  115. default_value: 0.5
  116. }
  117. ],
  118. outputs: [
  119. {
  120. id: 0,
  121. node_id: 0,
  122. name: _tr("Value"),
  123. type: "VALUE",
  124. color: 0xffa1a1a1,
  125. default_value: 0.0
  126. }
  127. ],
  128. buttons: [
  129. {
  130. name: _tr("operation"),
  131. type: "ENUM",
  132. data: ["Add", "Subtract", "Multiply", "Divide", "Power", "Logarithm", "Square Root", "Inverse Square Root", "Absolute", "Exponent", "Minimum", "Maximum", "Less Than", "Greater Than", "Sign", "Round", "Floor", "Ceil", "Truncate", "Fraction", "Modulo", "Snap", "Ping-Pong", "Sine", "Cosine", "Tangent", "Arcsine", "Arccosine", "Arctangent", "Arctan2", "Hyperbolic Sine", "Hyperbolic Cosine", "Hyperbolic Tangent", "To Radians", "To Degrees"],
  133. default_value: 0,
  134. output: 0
  135. },
  136. {
  137. name: _tr("use_clamp"),
  138. type: "BOOL",
  139. default_value: false,
  140. output: 0
  141. }
  142. ]
  143. };
  144. }