Skip to content

Commit

Permalink
Math Node: add Ping-Pong property
Browse files Browse the repository at this point in the history
  • Loading branch information
t3du committed Feb 8, 2023
1 parent c70f376 commit 40a029a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 14 additions & 0 deletions Sources/armory/logicnode/MathNode.hx
Expand Up @@ -15,6 +15,18 @@ class MathNode extends LogicNode {
return Math.round(number) / Math.pow(10, precision);
}

public function fract(a: Float): Float {
return a - Math.floor(a);
}

public function pingpong(a: Float, b: Float): Float {
if (b == 0.0) {
return 0.0;
} else {
return Math.abs(fract((a - b) / (b * 2.0)) * b * 2.0 - b);
}
}

override function get(from: Int): Dynamic {
var r = 0.0;
switch (property0) {
Expand Down Expand Up @@ -84,6 +96,8 @@ class MathNode extends LogicNode {
r /= inputs[i].get();
i++;
}
case "Ping-Pong":
r = pingpong(inputs[0].get(), inputs[1].get());
}
// Clamp
if (property1) r = r < 0.0 ? 0.0 : (r > 1.0 ? 1.0 : r);
Expand Down
12 changes: 8 additions & 4 deletions blender/arm/logicnode/math/LN_math.py
Expand Up @@ -4,7 +4,7 @@ class MathNode(ArmLogicTreeNode):
"""Mathematical operations on values."""
bl_idname = 'LNMathNode'
bl_label = 'Math'
arm_version = 2
arm_version = 3

@staticmethod
def get_enum_id_value(obj, prop_name, value):
Expand Down Expand Up @@ -37,7 +37,8 @@ def get_count_in(operation_name):
'Arctan2': 2,
'Modulo': 2,
'Less Than': 2,
'Greater Than': 2
'Greater Than': 2,
'Ping-Pong': 2
}.get(operation_name, 0)

def get_enum(self):
Expand All @@ -52,7 +53,7 @@ def set_enum(self, value):
if (self.get_count_in(select_current) == 0):
while (len(self.inputs) < 2):
self.add_input('ArmFloatSocket', 'Value ' + str(len(self.inputs)))
# 2 arguments: Max, Min, Power, Arctan2, Modulo, Less Than, Greater Than
# 2 arguments: Max, Min, Power, Arctan2, Modulo, Less Than, Greater Than, Ping-Pong
if (self.get_count_in(select_current) == 2):
while (len(self.inputs) > 2):
self.inputs.remove(self.inputs.values()[-1])
Expand All @@ -65,6 +66,8 @@ def set_enum(self, value):
self['property0'] = value
if (self.property0 == 'Round'):
self.inputs[1].name = 'Precision'
elif (self.property0 == 'Ping-Pong'):
self.inputs[1].name = 'Scale'
elif (len(self.inputs) > 1): self.inputs[1].name = 'Value 1'

property0: HaxeEnumProperty(
Expand Down Expand Up @@ -93,7 +96,8 @@ def set_enum(self, value):
('Ceil', 'Ceil', 'Ceil'),
('Fract', 'Fract', 'Fract'),
('Square Root', 'Square Root', 'Square Root'),
('Exponent', 'Exponent', 'Exponent')],
('Exponent', 'Exponent', 'Exponent'),
('Ping-Pong', 'Ping-Pong', 'The output value is moved between 0.0 and the Scale based on the input value')],
name='', default='Add', set=set_enum, get=get_enum)

property1: HaxeBoolProperty('property1', name='Clamp', default=False)
Expand Down

0 comments on commit 40a029a

Please sign in to comment.