Skip to content

Commit

Permalink
refactor(shader-ast-stdlib): update & fix porterDuff() HOF
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 19, 2019
1 parent 002e3d1 commit 5f83ce1
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions packages/shader-ast-stdlib/src/color/porter-duff.ts
Expand Up @@ -8,10 +8,18 @@ import {
FloatTerm,
mul,
ret,
sub
sub,
vec4,
Vec4Sym
} from "@thi.ng/shader-ast";
import { clamp01 } from "../math/clamp";

const coeff = (
f: Fn2<FloatTerm, FloatTerm, FloatTerm>,
a: Vec4Sym,
b: Vec4Sym
) => (f === ZERO ? FLOAT0 : f === ONE ? a : mul(a, f($w(a), $w(b))));

/**
* Higher-order Porter-Duff alpha compositing operator. See
* thi.ng/porter-duff for reference. Returns an optimized AST function
Expand All @@ -23,6 +31,9 @@ import { clamp01 } from "../math/clamp";
* for src/dest colors and are called with the alpha components of both
* colors.
*
* Optimization only happens for cases where either `fa` and/or `fb` are
* `ZERO`.
*
* @param name function name
* @param fa src coeff fn
* @param fb dest coeff fn
Expand All @@ -33,19 +44,15 @@ export const porterDuff = (
fb: Fn2<FloatTerm, FloatTerm, FloatTerm>
) =>
defn("vec4", name, ["vec4", "vec4"], (a, b) => {
const src =
fa === ZERO ? FLOAT0 : fa === ONE ? a : mul(a, fa($w(a), $w(b)));
const dest =
fb === ZERO ? FLOAT0 : fb === ONE ? b : mul(b, fb($w(a), $w(b)));
const src = coeff(fa, a, b);
const dest = coeff(fb, a, b);
const srcZero = src === FLOAT0;
const destZero = dest === FLOAT0;
return [
ret(
clamp01(
src === FLOAT0
? dest
: dest === FLOAT0
? src
: add(src, dest)
)
srcZero && destZero
? vec4()
: clamp01(srcZero ? dest : destZero ? src : add(src, dest))
)
];
});
Expand Down

0 comments on commit 5f83ce1

Please sign in to comment.