smuud shadows

This commit is contained in:
2026-01-30 16:52:17 +01:00
parent e4e223637a
commit fd7ff7288c

View File

@@ -36,6 +36,9 @@ const float la = 0.1, lb = 0.01, lc = 0.001;
const float ballRadius = 1.0;
const float hardRadius = ballRadius;
const float softRadius = ballRadius * 2.0;
void main()
{
vec3 norm = normalize(normal);
@@ -45,12 +48,10 @@ void main()
vec3 result = ambient;
for (int i = 0; i < 3; i++) {
vec3 lightVector = lights[i].position - worldPositions;
vec3 lightDir = normalize(lightVector);
vec3 fragToLight = lights[i].position - worldPositions;
vec3 lightDir = normalize(fragToLight);
vec3 fragToBall = lights[i].position - ballPosition;
float d = length(lightVector);
float d = length(fragToLight);
float attenuation = 1.0 / (la + d * lb + d * d * lc);
float diff = max(dot(norm, lightDir), 0.0);
@@ -60,15 +61,23 @@ void main()
float spec = pow(max(dot(norm, halfwayDir), 0.0), shininess);
vec3 specular = specularStrength * spec * lights[i].color;
// the ball's light is put inside it, thus it would cast a shadow all over the scene.
// to counter-act this, we can simply exclude it. i should instead move the light.
// either way, it makes for a cool light (shadow?) show.
bool shadow = false;
if (i != 2) {
shadow = length(lightVector) > length(fragToBall) && dot(lightVector, fragToBall) < 0.0;
// shadow stuff
vec3 fragToBall = ballPosition - worldPositions;
float projDist = dot(fragToBall, lightDir);
float r = length(fragToBall - projDist * lightDir);
bool inShadowRegion = (projDist > 0.0) && (projDist < d);
float shadowFactor = 1.0;
// exclude ball light to avoid casting shadows everywhere
if (i != 2 && inShadowRegion) {
if (r <= hardRadius) {
shadowFactor = 0.0;
} else if (r < softRadius) {
shadowFactor = (r - hardRadius) / (softRadius - hardRadius);
}
}
if (!shadow)
result += attenuation * (diffuse + specular) * objectColor;
result += shadowFactor * attenuation * (diffuse + specular) * objectColor;
}
color = vec4(result + dither(textureCoordinates), 1.0);