-
-
Notifications
You must be signed in to change notification settings - Fork 143
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-stdlib): add new fns, various refactoring, add docs
- add clamp11(), orthogonal(), rayPointAt() - split lambert() / halfLambert() - switch to more inline fns
- Loading branch information
1 parent
fb5141e
commit b215055
Showing
17 changed files
with
179 additions
and
78 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
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 |
---|---|---|
@@ -1,46 +1,47 @@ | ||
import { | ||
add, | ||
defn, | ||
dot, | ||
FloatSym, | ||
FLOAT0, | ||
FloatTerm, | ||
madd, | ||
max, | ||
mul, | ||
ret, | ||
sym, | ||
ternary | ||
Vec3Term | ||
} from "@thi.ng/shader-ast"; | ||
import { fit1101 } from "../math/fit"; | ||
|
||
/** | ||
* Computes Lambert term, optionally using Half-Lambertian, | ||
* if `half` is true. | ||
* | ||
* https://developer.valvesoftware.com/wiki/Half_Lambert | ||
* Inline function. Computes Lambert term, i.e. max(dot(n,l), 0). Both | ||
* vectors must be pre-normalized. | ||
* | ||
* @param surfNormal vec3 | ||
* @param lightDir vec3 | ||
* @param half bool | ||
*/ | ||
export const lambert = defn( | ||
"float", | ||
"lambert", | ||
[["vec3"], ["vec3"], ["bool"]], | ||
(n, ldir, bidir) => { | ||
let d: FloatSym; | ||
return [(d = sym(dot(n, ldir))), ret(ternary(bidir, fit1101(d), d))]; | ||
} | ||
); | ||
export const lambert = (n: Vec3Term, ldir: Vec3Term) => | ||
max(dot(n, ldir), FLOAT0); | ||
|
||
/** | ||
* Inline function. Computes Half-Lambertian term. Both vectors must be | ||
* pre-normalized. | ||
* | ||
* https://developer.valvesoftware.com/wiki/Half_Lambert | ||
* | ||
* @param n | ||
* @param ldir | ||
*/ | ||
export const halfLambert = (n: Vec3Term, ldir: Vec3Term) => | ||
fit1101(dot(n, ldir)); | ||
|
||
/** | ||
* Inline function. Computes: col = lambert * light * diffuse + ambient | ||
* | ||
* @param lambertian float | ||
* @param diffuseCol vec3 | ||
* @param lightCol vec3 | ||
* @param ambientCol vec3 | ||
*/ | ||
export const diffuseLighting = defn( | ||
"vec3", | ||
"diffuseLighting", | ||
[["float"], ["vec3"], ["vec3"], ["vec3"]], | ||
(lambertian, diffuse, light, ambient) => [ | ||
ret(mul(diffuse, add(mul(light, lambertian), ambient))) | ||
] | ||
); | ||
export const diffuseLighting = ( | ||
lambertian: FloatTerm, | ||
diffuse: Vec3Term, | ||
light: Vec3Term, | ||
ambient: Vec3Term | ||
) => madd(mul(light, lambertian), diffuse, ambient); |
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
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
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,27 @@ | ||
import { | ||
$x, | ||
$y, | ||
$z, | ||
abs, | ||
defn, | ||
gt, | ||
neg, | ||
ret, | ||
ternary, | ||
vec3 | ||
} from "@thi.ng/shader-ast"; | ||
|
||
/** | ||
* Returns an orthogonal vector to `v`. | ||
* | ||
* http://lolengine.net/blog/2013/09/21/picking-orthogonal-vector-combing-coconuts | ||
*/ | ||
export const orthogonal = defn("vec3", "orthogonal", [["vec3"]], (v) => [ | ||
ret( | ||
ternary( | ||
gt(abs($x(v)), abs($z(v))), | ||
vec3(neg($y(v)), $x(v), 0), | ||
vec3(0, neg($z(v)), $y(v)) | ||
) | ||
) | ||
]); |
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
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,25 @@ | ||
import { | ||
add, | ||
FloatTerm, | ||
mul, | ||
normalize, | ||
Term, | ||
Vec | ||
} from "@thi.ng/shader-ast"; | ||
|
||
/** | ||
* Inline function. Returns point on ray (`p`, `dir`) at distance `t`. | ||
* Unless `norm` is true (default false), `dir` must be already | ||
* normalized. | ||
* | ||
* @param p | ||
* @param dir | ||
* @param t | ||
* @param normalize | ||
*/ | ||
export const rayPointAt = <A extends Vec, B extends A>( | ||
p: Term<A>, | ||
dir: Term<B>, | ||
t: FloatTerm, | ||
norm = false | ||
) => add(p, mul(norm ? normalize(dir) : dir, t)); |
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 |
---|---|---|
@@ -1,12 +1,9 @@ | ||
import { defn, max, ret } from "@thi.ng/shader-ast"; | ||
import { FloatTerm, max } from "@thi.ng/shader-ast"; | ||
|
||
/** | ||
* @param d1 float | ||
* @param d2 float | ||
* Inline function. SDF shape intersection (a & b). | ||
* | ||
* @param a float | ||
* @param b float | ||
*/ | ||
export const sdfIntersect = defn( | ||
"float", | ||
"sdOpIntersect", | ||
[["float"], ["float"]], | ||
(d1, d2) => [ret(max(d2, d1))] | ||
); | ||
export const sdfIntersect = (a: FloatTerm, b: FloatTerm) => max(b, a); |
Oops, something went wrong.