2
0

MathNode.hx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package arm.node.brush;
  2. @:keep
  3. class MathNode extends LogicNode {
  4. public var operation: String;
  5. public var use_clamp: Bool;
  6. public function new(tree: LogicTree) {
  7. super(tree);
  8. }
  9. override function get(from: Int): Dynamic {
  10. var v1: Float = inputs[0].get();
  11. var v2: Float = inputs[1].get();
  12. var f = 0.0;
  13. switch (operation) {
  14. case "Add":
  15. f = v1 + v2;
  16. case "Multiply":
  17. f = v1 * v2;
  18. case "Sine":
  19. f = Math.sin(v1);
  20. case "Cosine":
  21. f = Math.cos(v1);
  22. case "Max":
  23. f = Math.max(v1, v2);
  24. case "Min":
  25. f = Math.min(v1, v2);
  26. case "Absolute":
  27. f = Math.abs(v1);
  28. case "Subtract":
  29. f = v1 - v2;
  30. case "Divide":
  31. f = v1 / (v2 == 0.0 ? 0.000001 : v2);
  32. case "Tangent":
  33. f = Math.tan(v1);
  34. case "Arcsine":
  35. f = Math.asin(v1);
  36. case "Arccosine":
  37. f = Math.acos(v1);
  38. case "Arctangent":
  39. f = Math.atan(v1);
  40. case "Arctan2":
  41. f = Math.atan2(v2, v1);
  42. case "Power":
  43. f = Math.pow(v1, v2);
  44. case "Logarithm":
  45. f = Math.log(v1);
  46. case "Round":
  47. f = Math.round(v1);
  48. case "Floor":
  49. f = Math.floor(v1);
  50. case "Ceil":
  51. f = Math.ceil(v1);
  52. case "Truncate":
  53. f = Math.ffloor(v1);
  54. case "Fraction":
  55. f = v1 - Math.floor(v1);
  56. case "Less Than":
  57. f = v1 < v2 ? 1.0 : 0.0;
  58. case "Greater Than":
  59. f = v1 > v2 ? 1.0 : 0.0;
  60. case "Modulo":
  61. f = v1 % v2;
  62. case "Snap":
  63. f = Math.floor(v1 / v2) * v2;
  64. case "Square Root":
  65. f = Math.sqrt(v1);
  66. case "Inverse Square Root":
  67. f = 1.0 / Math.sqrt(v1);
  68. case "Exponent":
  69. f = Math.exp(v1);
  70. case "Sign":
  71. f = v1 > 0 ? 1.0 : (v1 < 0 ? -1.0 : 0);
  72. case "Ping-Pong":
  73. f = (v2 != 0.0) ? v2 - Math.abs((Math.abs(v1) % (2 * v2)) - v2) : 0.0;
  74. case "Hyperbolic Sine":
  75. f = (Math.exp(v1) - Math.exp(-v1)) / 2.0;
  76. case "Hyperbolic Cosine":
  77. f = (Math.exp(v1) + Math.exp(-v1)) / 2.0;
  78. case "Hyperbolic Tangent":
  79. f = 1.0 - (2.0 / (Math.exp(2 * v1) + 1));
  80. case "To Radians":
  81. f = v1 / 180.0 * Math.PI;
  82. case "To Degrees":
  83. f = v1 / Math.PI * 180.0;
  84. }
  85. if (use_clamp) f = f < 0.0 ? 0.0 : (f > 1.0 ? 1.0 : f);
  86. return f;
  87. }
  88. }