Skip to content

Commit

Permalink
feat(shader-ast-stdlib): add mixCubic()/mixQuadratic()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 27, 2020
1 parent 64ba64c commit 4dfc020
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/shader-ast-stdlib/src/math/mix-cubic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
add,
defn,
FloatSym,
mul,
PrimTypeMap,
ret,
sub,
sym,
} from "@thi.ng/shader-ast";

const $ = <N extends 1 | 2 | 3 | 4, T extends PrimTypeMap[N]>(n: N, type: T) =>
defn(
type,
`mixCubic${n > 1 ? n : ""}`,
[type, type, type, type, "float"],
(a, b, c, d, t) => {
let s: FloatSym;
let s2: FloatSym;
let t2: FloatSym;
return [
(t2 = sym(mul(t, t))),
(s = sym(sub(1, t))),
(s2 = sym(mul(s, s))),
ret(
add(
add(
add(
mul(<any>a, mul(s, s2)),
mul(<any>b, mul(3, mul(s2, t)))
),
mul(<any>c, mul(3, mul(t2, s)))
),
mul(<any>d, mul(t, t2))
)
),
];
}
);

export const mixCubic = $(1, "float");
export const mixCubic2 = $(2, "vec2");
export const mixCubic3 = $(3, "vec3");
export const mixCubic4 = $(4, "vec4");
37 changes: 37 additions & 0 deletions packages/shader-ast-stdlib/src/math/mix-quadratic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
add,
defn,
FloatSym,
mul,
PrimTypeMap,
ret,
sub,
sym,
} from "@thi.ng/shader-ast";

const $ = <N extends 1 | 2 | 3 | 4, T extends PrimTypeMap[N]>(n: N, type: T) =>
defn(
type,
`mixQuadratic${n > 1 ? n : ""}`,
[type, type, type, "float"],
(a, b, c, t) => {
let s: FloatSym;
return [
(s = sym(sub(1, t))),
ret(
add(
add(
mul(<any>a, mul(s, s)),
mul(<any>b, mul(2, mul(s, t)))
),
mul(<any>c, mul(t, t))
)
),
];
}
);

export const mixQuadratic = $(1, "float");
export const mixQuadratic2 = $(2, "vec2");
export const mixQuadratic3 = $(3, "vec3");
export const mixQuadratic4 = $(4, "vec4");

0 comments on commit 4dfc020

Please sign in to comment.