From ae44554bf40bcc4bf6cd11c954661d33cdafde11 Mon Sep 17 00:00:00 2001 From: Amy Retzerau Date: Wed, 27 Aug 2025 13:52:51 +0200 Subject: [PATCH] feat: ray marching now works with ray structs, allowing for color --- sdf.fs | 69 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 14 deletions(-) diff --git a/sdf.fs b/sdf.fs index bd53af9..402d79b 100644 --- a/sdf.fs +++ b/sdf.fs @@ -7,29 +7,70 @@ in vec2 texCoords; vec3 center = vec3(0.0f,0.0f,20.0f); float radius = 5.0f; -float sphereMin(vec3 pos){ - return length(pos - center) - radius; +float planeHeight = -7.0f; + +struct Ray { + vec3 dir; + vec3 pos; + float min; + float len; + vec3 color; +}; + +Ray applyMin(Ray ray) { + vec3 off = ray.dir * ray.min; + ray.pos = ray.pos + off; + ray.len = ray.len + ray.min; + return ray; +} + + +Ray planeMin(Ray ray){ + ray.min = ray.pos.y - planeHeight; + ray.color = vec3(1.0,1.0,1.0); + return applyMin(ray); +} + +Ray sphereMin(Ray ray){ + ray.min = length(ray.pos - center) - radius; + ray.color = vec3(1.0,0.0,0.0); + return applyMin(ray); +} + +Ray minFn(Ray ray){ + Ray sp = sphereMin(ray); + Ray pl = planeMin(ray); + + if (sp.min < pl.min){ + return sp; + } else { + return pl; + } } void main() { - vec3 ray = vec3(texCoords.x, texCoords.y,2.0f); - ray = (ray - 0.5f) * 2; - vec3 pos = ray; - float min = sphereMin(ray); + vec3 dir = vec3(texCoords.x, texCoords.y,2.0f); + dir = (dir - 0.5f) * 2; + vec3 pos = dir; + Ray ray = Ray(pos,normalize(dir),.0,.0,vec3(.0)); - while( (min < 100.0f) && (min > 0.1f) ) { - ray = normalize(ray); - ray = ray * min; - pos = pos + ray; - min = sphereMin(pos); + + ray = minFn(ray); + + while( (ray.min < 100.0f) && (ray.min > 0.1f) ) { + //ray.dir = normalize(ray.dir); + ray = minFn(ray); + //vec3 off = ray.dir * min; + //ray.pos = ray.pos + off; + } - if(min <= 0.2f){ - FragColor = vec4(texCoords.x, texCoords.y, 0.2f, 1.0f); + if(ray.min <= 0.2f){ + FragColor = vec4(ray.color, 1.0f); } else { - FragColor = vec4(0.0f,0.0f,0.0f,1.0f); + FragColor = vec4(.0,0.0f,0.0f,1.0f); } }