Added box (very exciting)

This commit is contained in:
Conál 2024-02-09 08:35:11 -05:00
parent d945f49fca
commit d706da69e9
3 changed files with 23 additions and 7 deletions

View File

@ -10,12 +10,24 @@ layout (set = 0, binding = 0) uniform Matrices {
layout (location = 0) in vec2 pos;
layout (location = 0) out vec4 fragColor;
float sphere(vec3 p, vec3 center, float r) {
return length(p-center) - r;
float opUnion(float v1, float v2) {
return min(v1, v2);
}
float box(vec3 p, vec3 c, vec3 r) {
p -= c;
vec3 q = abs(p) - r;
return length(max(q,0.0)) + min(max(q.x, max(q.y,q.z)), 0.0);
}
float sphere(vec3 p, vec3 c, float r) {
return length(p-c) - r;
}
float sdf(vec3 pos) {
return sphere(pos, vec3(0.0, 0.0, 0.0), 3.0);
float d = sphere(pos, vec3(0.0, 0.0, 0.0), 3.0);
d = opUnion(d, box(pos, vec3(0.0, -10.0, 0.0), vec3(10.0, 1.0, 10.0)));
return d;
}
float raycast(vec3 dir) {
@ -23,13 +35,13 @@ float raycast(vec3 dir) {
for(int i = 0; i < 64; i++) {
float dt = sdf(cam_pos + dir * t);
if(dt < 0.0001*t)
return float(i) / 64.0;
return t;
else if(dt > 200.0)
return 1.0;
return -1.0;
t += dt;
}
return 1.0;
return -1.0;
}
vec3 raygen() {
@ -43,6 +55,10 @@ vec3 raygen() {
void main() {
vec3 dir = raygen();
float d = raycast(dir);
vec3 point = cam_pos + dir * d;
fragColor = vec4(d);
if(d < 0.0)
fragColor = vec4(0.0);
else
fragColor = vec4(mod(point.y, 1.0));
}

Binary file not shown.

0
assets/shaders/test Normal file
View File