Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shader-ast): add trilight lighting model
- Loading branch information
1 parent
4017284
commit 0705e9d
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
) | ||
) | ||
]; | ||
} | ||
); |