-
-
Notifications
You must be signed in to change notification settings - Fork 151
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 snoise2, distance fns
- Loading branch information
1 parent
7f7f1f6
commit 0849f8b
Showing
6 changed files
with
175 additions
and
1 deletion.
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,25 @@ | ||
import { | ||
$w, | ||
$x, | ||
$y, | ||
$z, | ||
abs, | ||
max, | ||
sub, | ||
Vec2Term, | ||
Vec3Term, | ||
Vec4Term | ||
} from "@thi.ng/shader-ast"; | ||
|
||
export const distChebyshev2 = ( | ||
a: Vec2Term | Vec3Term | Vec4Term, | ||
b: Vec2Term | Vec3Term | Vec4Term | ||
) => max(abs(sub($x(a), $x(b))), abs(sub($y(a), $y(b)))); | ||
|
||
export const distChebyshev3 = ( | ||
a: Vec3Term | Vec4Term, | ||
b: Vec3Term | Vec4Term | ||
) => max(distChebyshev2(a, b), abs(sub($z(a), $z(b)))); | ||
|
||
export const distChebyshev4 = (a: Vec4Term, b: Vec4Term) => | ||
max(distChebyshev3(a, b), abs(sub($w(a), $w(b)))); |
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 { | ||
$w, | ||
$x, | ||
$y, | ||
$z, | ||
abs, | ||
add, | ||
sub, | ||
Vec2Term, | ||
Vec3Term, | ||
Vec4Term | ||
} from "@thi.ng/shader-ast"; | ||
|
||
export const distManhattan2 = ( | ||
a: Vec2Term | Vec3Term | Vec4Term, | ||
b: Vec2Term | Vec3Term | Vec4Term | ||
) => add(abs(sub($x(a), $x(b))), abs(sub($y(a), $y(b)))); | ||
|
||
export const distManhattan3 = ( | ||
a: Vec3Term | Vec4Term, | ||
b: Vec3Term | Vec4Term | ||
) => add(distManhattan2(a, b), abs(sub($z(a), $z(b)))); | ||
|
||
export const distManhattan4 = (a: Vec4Term, b: Vec4Term) => | ||
add(distManhattan3(a, b), abs(sub($w(a), $w(b)))); |
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,13 @@ | ||
import { defn, dot, ret } from "@thi.ng/shader-ast"; | ||
|
||
export const magSq2 = defn("float", "magSq2", [["vec2"]], (v) => [ | ||
ret(dot(v, v)) | ||
]); | ||
|
||
export const magSq3 = defn("float", "magSq3", [["vec3"]], (v) => [ | ||
ret(dot(v, v)) | ||
]); | ||
|
||
export const magSq4 = defn("float", "magSq4", [["vec4"]], (v) => [ | ||
ret(dot(v, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { | ||
$, | ||
$x, | ||
$y, | ||
abs, | ||
add, | ||
assign, | ||
defn, | ||
dot, | ||
float, | ||
FLOAT05, | ||
FLOAT1, | ||
floor, | ||
fract, | ||
gt, | ||
max, | ||
mod, | ||
mul, | ||
ret, | ||
sub, | ||
sym, | ||
ternary, | ||
vec2, | ||
Vec2Sym, | ||
vec3, | ||
Vec3Sym, | ||
vec4, | ||
Vec4Sym | ||
} from "@thi.ng/shader-ast"; | ||
import { magSq2 } from "../math/magsq"; | ||
import { permute3 } from "./permute"; | ||
|
||
/** | ||
* Array and textureless GLSL 2D simplex noise function. Ported from | ||
* original GLSL implementation by Ian McEwan & Ashima Arts. | ||
* | ||
* https://github.com/ashima/webgl-noise | ||
*/ | ||
export const snoise2 = defn("float", "snoise2", [["vec2"]], (v) => { | ||
let C: Vec4Sym; | ||
let i: Vec2Sym; | ||
let i1: Vec2Sym; | ||
let x0: Vec2Sym; | ||
let x12: Vec4Sym; | ||
let p: Vec3Sym; | ||
let m: Vec3Sym; | ||
let x: Vec3Sym; | ||
let h: Vec3Sym; | ||
let ox: Vec3Sym; | ||
let a0: Vec3Sym; | ||
let g: Vec3Sym; | ||
return [ | ||
(C = sym( | ||
vec4( | ||
0.211324865405187, | ||
0.366025403784439, | ||
-0.577350269189626, | ||
0.024390243902439 | ||
) | ||
)), | ||
(i = sym(floor(add(v, dot(v, $(C, "yy")))))), | ||
(x0 = sym(add(sub(v, i), dot(i, $(C, "xx"))))), | ||
(i1 = sym(ternary(gt($x(x0), $y(x0)), vec2(1, 0), vec2(0, 1)))), | ||
(x12 = sym(sub(add($(x0, "xyxy"), $(C, "xxzz")), vec4(i1, 0, 0)))), | ||
assign(i, mod(i, float(289))), | ||
(p = sym( | ||
permute3( | ||
add( | ||
permute3(add(vec3(0, $y(i1), 1), $y(i))), | ||
add(vec3(0, $x(i1), 1), $x(i)) | ||
) | ||
) | ||
)), | ||
(m = sym( | ||
max( | ||
sub( | ||
FLOAT05, | ||
vec3(magSq2(x0), magSq2($(x12, "xy")), magSq2($(x12, "zw"))) | ||
), | ||
vec3() | ||
) | ||
)), | ||
assign(m, mul(m, m)), | ||
assign(m, mul(m, m)), | ||
(x = sym(sub(mul(2, fract(mul(p, $(C, "www")))), FLOAT1))), | ||
(h = sym(sub(abs(x), FLOAT05))), | ||
(ox = sym(floor(add(x, FLOAT05)))), | ||
(a0 = sym(sub(x, ox))), | ||
assign( | ||
m, | ||
mul( | ||
m, | ||
sub( | ||
1.79284291400159, | ||
mul(0.85373472095314, add(mul(a0, a0), mul(h, h))) | ||
) | ||
) | ||
), | ||
(g = sym(vec3(add(mul($x(a0), $x(x0)), mul($x(h), $y(x0)))))), | ||
assign( | ||
$(g, "yz"), | ||
add(mul($(a0, "yz"), $(x12, "xz")), mul($(h, "yz"), $(x12, "yw"))) | ||
), | ||
ret(mul(130, dot(m, g))) | ||
]; | ||
}); |
File renamed without changes.