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

190 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-01-17 18:53:31 +01:00
class VectorMathNode extends LogicNode {
operation: string;
2024-02-05 20:57:20 +01:00
v = vec4_create();
2024-01-17 18:53:31 +01:00
constructor() {
super();
}
override get = (from: i32, done: (a: any)=>void) => {
2024-02-05 20:57:20 +01:00
this.inputs[0].get((v1: vec4_t) => {
this.inputs[1].get((v2: vec4_t) => {
vec4_set_from(this.v, v1);
2024-01-17 18:53:31 +01:00
let f = 0.0;
switch (this.operation) {
case "Add":
2024-02-05 20:57:20 +01:00
vec4_add(this.v, v2);
2024-01-17 18:53:31 +01:00
break;
case "Subtract":
2024-02-05 20:57:20 +01:00
vec4_sub(this.v, v2);
2024-01-17 18:53:31 +01:00
break;
case "Average":
2024-02-05 20:57:20 +01:00
vec4_add(this.v, v2);
2024-01-17 18:53:31 +01:00
this.v.x *= 0.5;
this.v.y *= 0.5;
this.v.z *= 0.5;
break;
case "Dot Product":
2024-02-05 20:57:20 +01:00
f = vec4_dot(this.v, v2);
vec4_set(this.v, f, f, f);
2024-01-17 18:53:31 +01:00
break;
case "Cross Product":
2024-02-05 20:57:20 +01:00
vec4_cross(this.v, v2);
2024-01-17 18:53:31 +01:00
break;
case "Normalize":
2024-02-05 20:57:20 +01:00
vec4_normalize(this.v, );
2024-01-17 18:53:31 +01:00
break;
case "Multiply":
this.v.x *= v2.x;
this.v.y *= v2.y;
this.v.z *= v2.z;
break;
case "Divide":
this.v.x /= v2.x == 0.0 ? 0.000001 : v2.x;
this.v.y /= v2.y == 0.0 ? 0.000001 : v2.y;
this.v.z /= v2.z == 0.0 ? 0.000001 : v2.z;
break;
case "Length":
2024-02-05 20:57:20 +01:00
f = vec4_len(this.v);
vec4_set(this.v, f, f, f);
2024-01-17 18:53:31 +01:00
break;
case "Distance":
2024-02-05 20:57:20 +01:00
f = vec4_dist_to(this.v, v2);
vec4_set(this.v, f, f, f);
2024-01-17 18:53:31 +01:00
break;
case "Project":
2024-02-05 20:57:20 +01:00
vec4_set_from(this.v, v2);
vec4_mult(this.v, vec4_dot(v1, v2) / vec4_dot(v2, v2));
2024-01-17 18:53:31 +01:00
break;
case "Reflect":
2024-02-05 20:57:20 +01:00
let tmp = vec4_create();
vec4_set_from(tmp, v2);
vec4_normalize(tmp);
vec4_reflect(this.v, tmp);
2024-01-17 18:53:31 +01:00
break;
case "Scale":
this.v.x *= v2.x;
this.v.y *= v2.x;
this.v.z *= v2.x;
break;
case "Absolute":
this.v.x = Math.abs(this.v.x);
this.v.y = Math.abs(this.v.y);
this.v.z = Math.abs(this.v.z);
break;
case "Minimum":
this.v.x = Math.min(v1.x, v2.x);
this.v.y = Math.min(v1.y, v2.y);
this.v.z = Math.min(v1.z, v2.z);
break;
case "Maximum":
this.v.x = Math.max(v1.x, v2.x);
this.v.y = Math.max(v1.y, v2.y);
this.v.z = Math.max(v1.z, v2.z);
break;
case "Floor":
this.v.x = Math.floor(v1.x);
this.v.y = Math.floor(v1.y);
this.v.z = Math.floor(v1.z);
break;
case "Ceil":
this.v.x = Math.ceil(v1.x);
this.v.y = Math.ceil(v1.y);
this.v.z = Math.ceil(v1.z);
break;
case "Fraction":
this.v.x = v1.x - Math.floor(v1.x);
this.v.y = v1.y - Math.floor(v1.y);
this.v.z = v1.z - Math.floor(v1.z);
break;
case "Modulo":
this.v.x = v1.x % v2.x;
this.v.y = v1.y % v2.y;
this.v.z = v1.z % v2.z;
break;
case "Snap":
this.v.x = Math.floor(v1.x / v2.x) * v2.x;
this.v.y = Math.floor(v1.y / v2.y) * v2.y;
this.v.z = Math.floor(v1.z / v2.z) * v2.z;
break;
case "Sine":
this.v.x = Math.sin(v1.x);
this.v.y = Math.sin(v1.y);
this.v.z = Math.sin(v1.z);
break;
case "Cosine":
this.v.x = Math.cos(v1.x);
this.v.y = Math.cos(v1.y);
this.v.z = Math.cos(v1.z);
break;
case "Tangent":
this.v.x = Math.tan(v1.x);
this.v.y = Math.tan(v1.y);
this.v.z = Math.tan(v1.z);
break;
}
if (from == 0) done(this.v);
else done(f);
});
});
}
2024-02-06 19:32:21 +01:00
static def: zui_node_t = {
2024-01-17 18:53:31 +01:00
id: 0,
name: _tr("Vector Math"),
type: "VectorMathNode",
x: 0,
y: 0,
color: 0xff4982a0,
inputs: [
{
id: 0,
node_id: 0,
name: _tr("Vector"),
type: "VECTOR",
color: 0xff6363c7,
2024-01-20 10:58:12 +01:00
default_value: new Float32Array([0.0, 0.0, 0.0])
2024-01-17 18:53:31 +01:00
},
{
id: 0,
node_id: 0,
name: _tr("Vector"),
type: "VECTOR",
color: 0xff6363c7,
2024-01-20 10:58:12 +01:00
default_value: new Float32Array([0.0, 0.0, 0.0])
2024-01-17 18:53:31 +01:00
}
],
outputs: [
{
id: 0,
node_id: 0,
name: _tr("Vector"),
type: "VECTOR",
color: 0xff6363c7,
2024-01-20 10:58:12 +01:00
default_value: new Float32Array([0.0, 0.0, 0.0])
2024-01-17 18:53:31 +01:00
},
{
id: 0,
node_id: 0,
name: _tr("Value"),
type: "VALUE",
color: 0xffa1a1a1,
default_value: 0.0
}
],
buttons: [
{
name: _tr("operation"),
type: "ENUM",
data: ["Add", "Subtract", "Multiply", "Divide", "Average", "Cross Product", "Project", "Reflect", "Dot Product", "Distance", "Length", "Scale", "Normalize", "Absolute", "Minimum", "Maximum", "Floor", "Ceil", "Fraction", "Modulo", "Snap", "Sine", "Cosine", "Tangent"],
default_value: 0,
output: 0
}
]
};
}