Skip to content

Commit

Permalink
feat(shader-ast): add trilight lighting model
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 18, 2019
1 parent 4017284 commit 0705e9d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/shader-ast/src/index.ts
Expand Up @@ -13,3 +13,4 @@ export * from "./std/fog";
export * from "./std/lambert";
export * from "./std/raymarch";
export * from "./std/sdf";
export * from "./std/trilight";
42 changes: 42 additions & 0 deletions packages/shader-ast/src/std/trilight.ts
@@ -0,0 +1,42 @@
import { Sym } from "../api";
import {
add,
defn,
F32_0,
F32_1,
mul,
neg,
ret,
sub,
sym
} from "../ast";
import { abs, dot, max } from "../builtins";

/**
* Tom Forsyth's Trilight lighting model.
*
* https://tomforsyth1000.github.io/papers/trilight/trilight.html
*
* @param surfNormal vec3
* @param lightDir vec3
* @param col1 vec3
* @param col2 vec3
* @param col3 vec3
*/
export const trilight = defn(
"vec3",
"trilight",
[["vec3"], ["vec3"], ["vec3"], ["vec3"], ["vec3"]],
(n, l, c1, c2, c3) => {
let d: Sym<"f32">;
return [
(d = sym(dot(n, l))),
ret(
add(
add(mul(c1, max(d, F32_0)), mul(c2, sub(F32_1, abs(d)))),
mul(c3, max(dot(neg(n), l), F32_0))
)
)
];
}
);

0 comments on commit 0705e9d

Please sign in to comment.