From 41d2882857e56d4bda35fc2d6942f6d623d8fdb9 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Tue, 5 Mar 2024 20:18:32 +0100 Subject: [PATCH] fix(shader-ast-stdlib): update generics for clamp01(), clamp11() and various fitXX() fns --- packages/shader-ast-stdlib/src/math/clamp.ts | 14 +++--- packages/shader-ast-stdlib/src/math/fit.ts | 48 +++++++++----------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/packages/shader-ast-stdlib/src/math/clamp.ts b/packages/shader-ast-stdlib/src/math/clamp.ts index 3550028737..1f9ca0fefb 100644 --- a/packages/shader-ast-stdlib/src/math/clamp.ts +++ b/packages/shader-ast-stdlib/src/math/clamp.ts @@ -1,8 +1,7 @@ import type { FloatTerm, - PrimTerm, + Prim, Term, - TermType, Vec2Term, Vec3Term, Vec4Term, @@ -13,14 +12,15 @@ import { clamp } from "@thi.ng/shader-ast/builtin/math"; const __clamp = (min: number, max: number) => - (x: T): Term> => + (x: Term): Term => x.type === F - ? clamp(x, float(min), float(max)) + ? clamp(x, float(min), float(max)) : x.type === V2 - ? clamp(x, vec2(min), vec2(max)) + ? clamp(x, vec2(min), vec2(max)) : x.type === V3 - ? clamp(x, vec3(min), vec3(max)) - : clamp(x, vec4(min), vec4(max)); + ? clamp(x, vec3(min), vec3(max)) + : clamp(x, vec4(min), vec4(max)); + /** * Inline function, expands to equivalent of `clamp(x, 0, 1)`. * diff --git a/packages/shader-ast-stdlib/src/math/fit.ts b/packages/shader-ast-stdlib/src/math/fit.ts index 891ad454d6..47b77e45a4 100644 --- a/packages/shader-ast-stdlib/src/math/fit.ts +++ b/packages/shader-ast-stdlib/src/math/fit.ts @@ -1,4 +1,4 @@ -import type { PrimTerm, Term, TermType } from "@thi.ng/shader-ast"; +import type { Prim, PrimTerm, Term, TermType } from "@thi.ng/shader-ast"; import { F } from "@thi.ng/shader-ast/api/types"; import { ternary } from "@thi.ng/shader-ast/ast/controlflow"; import { defn, ret } from "@thi.ng/shader-ast/ast/function"; @@ -27,13 +27,9 @@ export const fitNorm1 = defn(F, "fitNorm1", [F, F, F], (x, a, b) => [ * @param x * @param a * @param b - * @returns */ -export const fitNorm = ( - x: T, - a: T, - b: T -): Term> => div(sub(x, a), sub(b, a)); +export const fitNorm = (x: Term, a: Term, b: Term) => + div(sub(x, a), sub(b, a)); /** * Fits value `x` from closed interval [a,b] to closed interval [c,d]. No @@ -45,13 +41,13 @@ export const fitNorm = ( * @param c - * @param d - */ -export const fit = ( - x: T, - a: T, - b: T, - c: T, - d: T -): Term> => mix(c, d, fitNorm(x, a, b)); +export const fit = ( + x: Term, + a: Term, + b: Term, + c: Term, + d: Term +) => mix(c, d, fitNorm(x, a, b)); /** * Same as {@link fit}, but first clamps `x` to closed [a,b] interval. @@ -62,13 +58,13 @@ export const fit = ( * @param c - * @param d - */ -export const fitClamped = ( - x: T, - a: T, - b: T, - c: T, - d: T -): Term> => mix(c, d, clamp01(div(sub(x, a), sub(b, a)))); +export const fitClamped = ( + x: Term, + a: Term, + b: Term, + c: Term, + d: Term +) => mix(c, d, clamp01(div(sub(x, a), sub(b, a)))); /** * Inline function. Fits value `a` in [0..1] interval to new interval @@ -78,11 +74,11 @@ export const fitClamped = ( * @param b - * @param c - */ -export const fit01 = ( - a: T, - b: T, - c: T -): Term> => mix(b, c, a); +export const fit01 = ( + a: Term, + b: Term, + c: Term +): Term => mix(b, c, a); /** * Inline function. Fits value `x` in [-1..+1] interval to [0..1]