Last active
July 11, 2019 17:48
-
-
Save paniq/7e580feb40c331dec95a07a095a771a1 to your computer and use it in GitHub Desktop.
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
#------------------------------------------------------------------------------- | |
implicit cube-to-simplex subdivision | |
fn cube-simplex (pred) | |
""""ivec3 ivec3 ivec3 ivec3 <- ivec3 | |
for three coordinate predicates | |
r: y <= z, g: z < x, b: x < y | |
return the two defining vertices of the simplex that the coordinate is in | |
and the four planes required for computing barycentric coordinates | |
the resulting simplex has the corners (0 0 0) v1 v2 (1 1 1) | |
the planes are ordered so that each plane is opposite each defining vertex | |
e1 := pred.gbr | |
e2 := 1 - pred.brg | |
p1 := e1 & e2 | |
p2 := e1 | e2 | |
plane0 := -p1 | |
plane1 := p1 * 2 - p2 | |
plane2 := 2 * p2 - p1 - 1 | |
plane3 := 1 - p2 | |
return p1 p2 plane0 plane1 plane2 plane3 | |
fn cube-simplex-bary (p) | |
""""vec4 ivec3 ivec3 <- vec3 | |
for a cube coordinate in the range (0..1 0..1 0..1) | |
return the barycentric coordinate within the sub-simplex of the cube | |
as well as the two defining vertices of the simplex with | |
corners (0 0 0) v1 v2 (1 1 1) | |
pred := | |
ivec3 | |
? (p.y <= p.z) 1 0 | |
? (p.z < p.x) 1 0 | |
? (p.x < p.y) 1 0 | |
let p1 p2 n0 n1 n2 n3 = (cube-simplex pred) | |
let w0 w1 w3 = | |
(dot p (vec3 n0)) + 1.0 | |
dot p (vec3 n1) | |
dot p (vec3 n3) | |
w2 := 1.0 - w0 - w1 - w3 | |
return (vec4 w0 w1 w2 w3) p1 p2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment