Skip to content

Commit

Permalink
refactor(shader-ast): more re-use vec/mat ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 19, 2019
1 parent c8ef0cf commit bcd5829
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions packages/shader-ast/src/ast/lit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,17 @@ export const TAU: FloatTerm = float(Math.PI * 2);
export const HALF_PI: FloatTerm = float(Math.PI / 2);
export const SQRT2: FloatTerm = float(Math.SQRT2);

const $vec = (xs: any[], init = FLOAT0) => [
xs[0] === undefined ? init : wrapFloat(xs[0]),
...xs.slice(1).map(wrapFloat)
];

const $ivec = (xs: any[], init = INT0) => [
xs[0] === undefined ? init : wrapInt(xs[0]),
...xs.slice(1).map(wrapInt)
];

const $uvec = (xs: any[], init = UINT0) => [
xs[0] === undefined ? init : wrapUint(xs[0]),
...xs.slice(1).map(wrapUint)
];

const $bvec = (xs: any[], init = FALSE) => [
xs[0] === undefined ? init : wrapBool(xs[0]),
...xs.slice(1).map(wrapBool)
];

const $mat = (xs: any[], init = FLOAT0) => [
xs[0] === undefined ? init : wrapFloat(xs[0]),
...xs.slice(1).map(wrapInt)
];
const $gvec = (wrap: Fn<any, Term<any> | undefined>, init: Term<any>) => (
xs: any[]
) => [xs[0] === undefined ? init : wrap(xs[0]), ...xs.slice(1).map(wrap)];

const $vec = $gvec(wrapFloat, FLOAT0);

const $ivec = $gvec(wrapInt, INT0);

const $uvec = $gvec(wrapUint, UINT0);

const $bvec = $gvec(wrapBool, FALSE);

const $gvec2 = <T extends Type>(
type: T,
Expand Down Expand Up @@ -152,6 +139,12 @@ const $gvec4 = <T extends Type>(
: ["n", "n", , "vnn"][xs.length]
);

const $gmat = <T extends Type>(
type: T,
info: (string | undefined)[],
xs: any[]
) => lit(type, (xs = $vec(xs)), info[xs.length]);

export function vec2(): Lit<"vec2">;
export function vec2(x: NumericF): Lit<"vec2">;
// export function vec2(x: Term<Vec | IVec | BVec>): Lit<"vec2">;
Expand Down Expand Up @@ -279,7 +272,7 @@ export function mat2(x: Vec2Term, y: Vec2Term): Lit<"mat2">;
// prettier-ignore
export function mat2(a: NumericF, b: NumericF, c: NumericF, d: NumericF): Lit<"mat2">;
export function mat2(...xs: any[]): Lit<"mat2"> {
return lit("mat2", (xs = $mat(xs)), ["n", "n", "vv"][xs.length]);
return $gmat("mat2", ["n", "n", "vv"], xs);
}

export function mat3(): Lit<"mat3">;
Expand All @@ -289,7 +282,7 @@ export function mat3(x: Vec3Term, y: Vec3Term, z: Vec3Term): Lit<"mat3">;
// prettier-ignore
export function mat3(a: NumericF, b: NumericF, c: NumericF, d: NumericF, e: NumericF, f: NumericF, g: NumericF, h: NumericF, i: NumericF): Lit<"mat3">;
export function mat3(...xs: any[]): Lit<"mat3"> {
return lit("mat3", (xs = $mat(xs)), ["n", "n", , "vvv"][xs.length]);
return $gmat("mat3", ["n", "n", , "vvv"], xs);
}

export function mat4(): Lit<"mat4">;
Expand All @@ -299,5 +292,5 @@ export function mat4(x: Vec4Term, y: Vec4Term, z: Vec4Term, w: Vec4Term): Lit<"m
// prettier-ignore
export function mat4(a: NumericF, b: NumericF, c: NumericF, d: NumericF, e: NumericF, f: NumericF, g: NumericF, h: NumericF, i: NumericF, j: NumericF, k: NumericF, l: NumericF, m: NumericF, n: NumericF, o: NumericF, p: NumericF): Lit<"mat4">;
export function mat4(...xs: any[]): Lit<"mat4"> {
return lit("mat4", (xs = $mat(xs)), ["n", "n", , , "vvvv"][xs.length]);
return $gmat("mat4", ["n", "n", , , "vvvv"], xs);
}

0 comments on commit bcd5829

Please sign in to comment.