thi-ng / umbrella

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

[shader-ast-stdlib] adding easing functions #416

Closed Hantsouski closed 1 year ago

Hantsouski commented 1 year ago

Hi @postspectacular, I can add some easing functions to the shader-ast-stdlib, do you think that'd be a good idea?

I would just take those functions described on https://easings.net/ and put it under /src/easing-function folder. Those functions may be useful for something.

postspectacular commented 1 year ago

Hey @Hantsouski - Those would be indeed a great addition! If you can, please use the standard defn function forms (not inline functions) and try to re-use pre-defined constants where possible, e.g. like so:

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";
import { FLOAT0, FLOAT1, HALF_PI, TAU } from "@thi.ng/shader-ast/ast/lit";
import { div, gte, lte, madd, mul, sub } from "@thi.ng/shader-ast/ast/ops";
import { cos, exp2, sin } from "@thi.ng/shader-ast/builtin/math";

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

export const easeOutElastic = defn(F, null, [F], (x) => [
    ret(
        ternary(
            lte(x, FLOAT0),
            FLOAT0,
            ternary(
                gte(x, FLOAT1),
                FLOAT1,
                madd(
                    exp2(mul(-10, x)),
                    sin(mul(sub(mul(x, 10), 0.75), div(TAU, 3))),
                    FLOAT1
                )
            )
        )
    ),
]);

Also, I think since most of the functions are very short, it's best to put them all into a single file, e.g. /src/math/easing.ts...

Thank you again & pls ping me if you're unsure about anything! 👍