thi-ng / umbrella

⛱ Broadly scoped ecosystem & mono-repository of 198 TypeScript projects (and ~175 examples) for general purpose, functional, data driven development
https://thi.ng
Apache License 2.0
3.31k stars 144 forks source link

feat(shader-ast-stdlib) add easing functions #422

Closed Hantsouski closed 9 months ago

Hantsouski commented 9 months ago

Adding easing functions based on the discussion in issues.

postspectacular commented 9 months ago

Hi @Hantsouski - thank you so much for this! Can only look through over the next couple of days, please also ignore the nagging from the Codeclimate bot... :)

postspectacular commented 9 months ago

@Hantsouski - It's only a small thing, but since all of these functions have the same signature, I think it'd be a great opportunity to use a higher-order function here to deduplicate some of the boilerplate, something like:

import type { FnU } from "@thi.ng/api";
import type { FloatSym } from "@thi.ng/shader-ast";

/**
 * Higher order function to wrap a given easing function body as proper
 * shader-ast function
 */
const defEasing = (fn: FnU<FloatSym>) => defn(F, null, [F], fn);

...we could then rewrite/simplify all of the fns like so:

export const easeInSine = defEasing((x) => [ret(sub(FLOAT1, cos(mul(x, HALF_PI))))]);

etc.

Hantsouski commented 9 months ago

good suggestion, I will update the PR

postspectacular commented 9 months ago

Thank you & much appreciated! Hope it's not too much effort...

Hantsouski commented 9 months ago

Made updates. But had to add explicit return type for a fn:

const defEasing = (fn: Fn<FloatSym, ScopeBody>) => defn(F, null, [F], fn);

Because defn has a problem of function overload. I'll show on screenshots:

image image

With explicit return type works fine:

image
postspectacular commented 9 months ago

Yeah, so sorry, that was my bad (forgot about the type!) - Fn<FloatSym,ScopeBody> works, but could also have used the alias FnBody1<"float">:

https://github.com/thi-ng/umbrella/blob/8d46d9326a9f9b81d65e7e274446f5964f9942ac/packages/shader-ast/src/api/function.ts#L67

Please don't worry about updating it, it's fine as is... You already did enough here (thank you again!) and I might update it quickly myself (just to help with any (hypothetical) future refactoring of shader-ast in general)

🎉