Skip to content

Commit

Permalink
added math/mix
Browse files Browse the repository at this point in the history
  • Loading branch information
pfcDorn committed Dec 11, 2024
1 parent 346cbf1 commit 7ae11a4
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/BasicBehaveEngine/BasicBehaveEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {Divide} from "./nodes/math/arithmetic/Divide";
import {Remainder} from "./nodes/math/arithmetic/Remainder";
import {Min} from "./nodes/math/arithmetic/Min";
import {Max} from "./nodes/math/arithmetic/Max";
import {Mix} from "./nodes/math/arithmetic/Mix";
import {Clamp} from "./nodes/math/arithmetic/Clamp";
import {DegreeToRadians} from "./nodes/math/trigonometry/DegreeToRadians";
import {RadiansToDegrees} from "./nodes/math/trigonometry/RadiansToDegrees";
Expand Down Expand Up @@ -344,6 +345,7 @@ export class BasicBehaveEngine implements IBehaveEngine {
this.registerBehaveEngineNode("math/rem", Remainder);
this.registerBehaveEngineNode("math/min", Min);
this.registerBehaveEngineNode("math/max", Max);
this.registerBehaveEngineNode("math/mix", Mix);
this.registerBehaveEngineNode("math/clamp", Clamp);
this.registerBehaveEngineNode("math/rad", DegreeToRadians);
this.registerBehaveEngineNode("math/deg", RadiansToDegrees);
Expand Down
60 changes: 60 additions & 0 deletions src/BasicBehaveEngine/nodes/math/arithmetic/Mix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {BehaveEngineNode, IBehaviourNodeProps} from "../../../BehaveEngineNode";

export class Mix extends BehaveEngineNode {
REQUIRED_VALUES = [{ id: "a" }, { id: "b" }, { id: "c"}]

constructor(props: IBehaviourNodeProps) {
super(props);
this.name = "MixNode";
this.validateValues(this.values);
}

private mix = (a: number, b: number, t: number): number => {
return a + t * (b - a);
}

override processNode(flowSocket?: string) {
const {a, b, c} = this.evaluateAllValues(this.REQUIRED_VALUES.map(val => val.id));
this.graphEngine.processNodeStarted(this);
const typeIndexA = this.values['a'].type!
const typeA: string = this.getType(typeIndexA);
const typeIndexB = this.values['b'].type!
const typeB: string = this.getType(typeIndexB);

if (typeA !== typeB) {
throw Error("input types not equivalent")
}
let val: any;

switch (typeA) {
case "float":
val = [this.mix(a,b,c)];
break;
case "float2":
val = [
this.mix(a[0], b[0], c),
this.mix(a[1], b[1], c)
]
break;
case "float3":
val = [
this.mix(a[0], b[0], c),
this.mix(a[1], b[1], c),
this.mix(a[2], b[2], c),
]
break;
case "float4":
val = [
this.mix(a[0], b[0], c),
this.mix(a[1], b[1], c),
this.mix(a[2], b[2], c),
this.mix(a[3], b[3], c),
]
break
default:
throw Error("Invalid type")
}

return {'value': {id: "value", value: val, type: typeIndexA}}
}
}
52 changes: 52 additions & 0 deletions src/authoring/AuthoringNodeSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,58 @@ export const arithmeticNodes = [
]
}
},
{
type: "math/mix",
description: "Linear interpolation operation",
configuration: [],
input: {
flows: [],
values: [
{
id: "a",
description: "First Arg",
types: [
"float",
"float2",
"float3",
"float4"
]
},
{
id: "b",
description: "Second Arg",
types: [
"float",
"float2",
"float3",
"float4"
]
},
{
id: "c",
description: "Unclamped interpolation coefficient",
types: [
"float"
]
}
]
},
output: {
flows: [],
values: [
{
id: "value",
description: "mix of a and b",
types: [
"float",
"float2",
"float3",
"float4"
]
}
]
}
},
{
type: "math/clamp",
description: "Clamp operation",
Expand Down
29 changes: 29 additions & 0 deletions tst/nodes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {Divide} from "../src/BasicBehaveEngine/nodes/math/arithmetic/Divide";
import {Remainder} from "../src/BasicBehaveEngine/nodes/math/arithmetic/Remainder";
import {Max} from "../src/BasicBehaveEngine/nodes/math/arithmetic/Max";
import {Min} from "../src/BasicBehaveEngine/nodes/math/arithmetic/Min";
import {Mix} from "../src/BasicBehaveEngine/nodes/math/arithmetic/Mix";
import {DegreeToRadians} from "../src/BasicBehaveEngine/nodes/math/trigonometry/DegreeToRadians";
import {RadiansToDegrees} from "../src/BasicBehaveEngine/nodes/math/trigonometry/RadiansToDegrees";
import {Sine} from "../src/BasicBehaveEngine/nodes/math/trigonometry/Sine";
Expand Down Expand Up @@ -1167,6 +1168,34 @@ describe('nodes', () => {
expect(val['val'].value[2]).toBe(-10);
});

it("math/mix", () => {
let mix: Mix = new Mix({
...defaultProps,
values: [
{ id: 'a', value: [1], type: 2 },
{ id: 'b', value: [2], type: 2 },
{ id: 'c', value: [0.5], type: 2 }
]
});

let val = mix.processNode();
expect(val['val'].value[0]).toBe(1.5);

mix = new Mix({
...defaultProps,
values: [
{ id: 'a', value: [0, 1, 3], type: 4 },
{ id: 'b', value: [1, 2, 4], type: 4 },
{ id: 'c', value: [0.5], type: 2 }
]
});

val = mix.processNode();
expect(val['val'].value[0]).toBe(0.5);
expect(val['val'].value[1]).toBe(1.5);
expect(val['val'].value[2]).toBe(3.5);
});

it("math/rad", () => {
let rad: DegreeToRadians = new DegreeToRadians({
...defaultProps,
Expand Down

0 comments on commit 7ae11a4

Please sign in to comment.