feat: added soft shadows by multi sampling the shadow

This commit is contained in:
2025-08-28 16:47:22 +02:00
parent e3df77e2ec
commit c5b3c45d27

28
sdf.fs
View File

@@ -100,17 +100,33 @@ void main()
vec3 normal = calcNormal(ray); vec3 normal = calcNormal(ray);
vec3 lighDir = normalize(vec3(.5,-1.0,0.5)); vec3 lighDir = normalize(vec3(.5,-1.0,0.5));
float light = (- min(dot(normal,lighDir),0.0))*0.85+0.3; float light = (- min(dot(normal,lighDir),0.0))*0.85;
Ray shRay = Ray(lighDir * (-1.0), ray.pos + 0.5*normal , .0,.0, vec3(.0)); float shLeve = 1.0;
shRay = march(shRay);
if( (dot(lighDir,normal) < 0.0) && (shRay.len <20)){ vec2 offsets[8] = vec2[](
light *= min(shRay.len*0.1,1.0); vec2( -0.94201624, -0.39906216 ),
vec2( 0.94558609, -0.76890725 ),
vec2( -0.094184101, -0.92938870 ),
vec2( 0.34495938, 0.29387760 ),
vec2( 0.94201624, 0.39906216 ),
vec2( -0.94558609, 0.76890725 ),
vec2( 0.094184101, 0.92938870 ),
vec2( -0.34495938, -0.29387760 )
);
for(int i = 0; i < 8; i++){
Ray shRay = Ray(lighDir * (-1.0) + vec3(offsets[i].x,.0,offsets[i].y)*0.05, ray.pos + 0.5*normal, .0,.0, vec3(.0));
shRay = march(shRay);
if( (dot(lighDir,normal) < 0.0) && (shRay.len <20)){
shLeve -= 0.125*max(1.0-shRay.len*0.07 , 0.0);
}
} }
light *= shLeve * .8;
if(ray.min <= 0.2){ if(ray.min <= 0.2){
FragColor = vec4(ray.color * light, 1.0); FragColor = vec4(ray.color * (light+0.3), 1.0);
} else { } else {
FragColor = vec4(.0,0.0,0.0,1.0); FragColor = vec4(.0,0.0,0.0,1.0);
} }