feat: ray marching now works with ray structs, allowing for color

This commit is contained in:
2025-08-27 13:52:51 +02:00
parent 4044ad9b36
commit ae44554bf4

69
sdf.fs
View File

@@ -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));
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;
while( (min < 100.0f) && (min > 0.1f) ) {
ray = normalize(ray);
ray = ray * min;
pos = pos + ray;
min = sphereMin(pos);
}
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);
}
}