Files
armorpaint/base/Sources/nodes/vector_math_node.ts
T

217 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-03-14 23:58:24 +01:00
type vector_math_node_t = {
base?: logic_node_t;
operation?: string;
v?: vec4_t;
};
2024-05-03 21:29:39 +02:00
function vector_math_node_create(args: f32_array_t): vector_math_node_t {
2024-03-14 23:58:24 +01:00
let n: vector_math_node_t = {};
n.base = logic_node_create();
n.base.get = vector_math_node_get;
n.v = vec4_create();
return n;
}
2024-04-08 17:40:32 +02:00
function vector_math_node_get(self: vector_math_node_t, from: i32): logic_node_value_t {
let v1: vec4_t = logic_node_input_get(self.base.inputs[0])._any;
let v2: vec4_t = logic_node_input_get(self.base.inputs[1])._any;
2024-04-02 15:45:49 +02:00
vec4_set_from(self.v, v1);
let f: f32 = 0.0;
let op: string = self.operation;
if (op == "Add") {
vec4_add(self.v, v2);
}
else if (op == "Subtract") {
vec4_sub(self.v, v2);
}
else if (op == "Average") {
vec4_add(self.v, v2);
self.v.x *= 0.5;
self.v.y *= 0.5;
self.v.z *= 0.5;
}
else if (op == "Dot Product") {
f = vec4_dot(self.v, v2);
vec4_set(self.v, f, f, f);
}
else if (op == "Cross Product") {
vec4_cross(self.v, v2);
}
else if (op == "Normalize") {
2024-05-03 21:29:39 +02:00
vec4_normalize(self.v);
2024-04-02 15:45:49 +02:00
}
else if (op == "Multiply") {
self.v.x *= v2.x;
self.v.y *= v2.y;
self.v.z *= v2.z;
}
else if (op == "Divide") {
self.v.x /= v2.x == 0.0 ? 0.000001 : v2.x;
self.v.y /= v2.y == 0.0 ? 0.000001 : v2.y;
self.v.z /= v2.z == 0.0 ? 0.000001 : v2.z;
}
else if (op == "Length") {
f = vec4_len(self.v);
vec4_set(self.v, f, f, f);
}
else if (op == "Distance") {
f = vec4_dist_to(self.v, v2);
vec4_set(self.v, f, f, f);
}
else if (op == "Project") {
vec4_set_from(self.v, v2);
vec4_mult(self.v, vec4_dot(v1, v2) / vec4_dot(v2, v2));
}
else if (op == "Reflect") {
let tmp: vec4_t = vec4_create();
vec4_set_from(tmp, v2);
vec4_normalize(tmp);
vec4_reflect(self.v, tmp);
}
else if (op == "Scale") {
self.v.x *= v2.x;
self.v.y *= v2.x;
self.v.z *= v2.x;
}
else if (op == "Absolute") {
self.v.x = math_abs(self.v.x);
self.v.y = math_abs(self.v.y);
self.v.z = math_abs(self.v.z);
}
else if (op == "Minimum") {
self.v.x = math_min(v1.x, v2.x);
self.v.y = math_min(v1.y, v2.y);
self.v.z = math_min(v1.z, v2.z);
}
else if (op == "Maximum") {
self.v.x = math_max(v1.x, v2.x);
self.v.y = math_max(v1.y, v2.y);
self.v.z = math_max(v1.z, v2.z);
}
else if (op == "Floor") {
self.v.x = math_floor(v1.x);
self.v.y = math_floor(v1.y);
self.v.z = math_floor(v1.z);
}
else if (op == "Ceil") {
self.v.x = math_ceil(v1.x);
self.v.y = math_ceil(v1.y);
self.v.z = math_ceil(v1.z);
}
else if (op == "Fraction") {
self.v.x = v1.x - math_floor(v1.x);
self.v.y = v1.y - math_floor(v1.y);
self.v.z = v1.z - math_floor(v1.z);
}
else if (op == "Modulo") {
2024-05-03 21:29:39 +02:00
self.v.x = math_fmod(v1.x, v2.x);
self.v.y = math_fmod(v1.y, v2.y);
self.v.z = math_fmod(v1.z, v2.z);
2024-04-02 15:45:49 +02:00
}
else if (op == "Snap") {
self.v.x = math_floor(v1.x / v2.x) * v2.x;
self.v.y = math_floor(v1.y / v2.y) * v2.y;
self.v.z = math_floor(v1.z / v2.z) * v2.z;
}
else if (op == "Sine") {
self.v.x = math_sin(v1.x);
self.v.y = math_sin(v1.y);
self.v.z = math_sin(v1.z);
}
else if (op == "Cosine") {
self.v.x = math_cos(v1.x);
self.v.y = math_cos(v1.y);
self.v.z = math_cos(v1.z);
}
else if (op == "Tangent") {
self.v.x = math_tan(v1.x);
self.v.y = math_tan(v1.y);
self.v.z = math_tan(v1.z);
}
2024-03-14 23:58:24 +01:00
2024-04-02 15:45:49 +02:00
if (from == 0) {
2024-04-08 17:40:32 +02:00
let v: logic_node_value_t = { _any: self.v };
return v;
2024-04-02 15:45:49 +02:00
}
else {
2024-04-08 17:40:32 +02:00
let v: logic_node_value_t = { _f32: f };
return v;
2024-04-02 15:45:49 +02:00
}
2024-03-14 23:58:24 +01:00
}
let vector_math_node_def: zui_node_t = {
id: 0,
name: _tr("Vector Math"),
type: "vector_math_node",
x: 0,
y: 0,
color: 0xff4982a0,
inputs: [
{
id: 0,
node_id: 0,
name: _tr("Vector"),
type: "VECTOR",
color: 0xff6363c7,
2024-04-10 19:39:17 +02:00
default_value: f32_array_create_xyz(0.0, 0.0, 0.0),
min: 0.0,
max: 1.0,
precision: 100,
display: 0
2024-03-14 23:58:24 +01:00
},
{
id: 0,
node_id: 0,
name: _tr("Vector"),
type: "VECTOR",
color: 0xff6363c7,
2024-04-10 19:39:17 +02:00
default_value: f32_array_create_xyz(0.0, 0.0, 0.0),
min: 0.0,
max: 1.0,
precision: 100,
display: 0
2024-03-14 23:58:24 +01:00
}
],
outputs: [
{
id: 0,
node_id: 0,
name: _tr("Vector"),
type: "VECTOR",
color: 0xff6363c7,
2024-04-10 19:39:17 +02:00
default_value: f32_array_create_xyz(0.0, 0.0, 0.0),
min: 0.0,
max: 1.0,
precision: 100,
display: 0
2024-03-14 23:58:24 +01:00
},
{
id: 0,
node_id: 0,
name: _tr("Value"),
type: "VALUE",
color: 0xffa1a1a1,
2024-04-24 12:14:47 +02:00
default_value: f32_array_create_x(0.0),
2024-04-10 19:39:17 +02:00
min: 0.0,
max: 1.0,
precision: 100,
display: 0
2024-03-14 23:58:24 +01:00
}
],
buttons: [
{
name: _tr("operation"),
type: "ENUM",
2024-04-10 19:39:17 +02:00
output: 0,
2024-04-24 12:14:47 +02:00
default_value: f32_array_create_x(0),
data: u8_array_create_from_string("Add\0Subtract\0Multiply\0Divide\0Average\0Cross Product\0Project\0Reflect\0Dot Product\0Distance\0Length\0Scale\0Normalize\0Absolute\0Minimum\0Maximum\0Floor\0Ceil\0Fraction\0Modulo\0Snap\0Sine\0Cosine\0Tangent"),
2024-04-10 19:39:17 +02:00
min: 0.0,
max: 1.0,
precision: 100,
height: 0
2024-03-14 23:58:24 +01:00
}
2024-04-10 19:39:17 +02:00
],
width: 0
2024-03-14 23:58:24 +01:00
};