79130641

Date: 2024-10-27 13:10:23
Score: 1.5
Natty:
Report link

So the way i was doing it i was sending a list of all planets position to the fragment shader but instead i needed to send them individualy and the list of all the planets why? Simple because for each planet we need to check if there is an intersection within their ray direction so ultimately my theory was to check where the ray would intersect with the UV coordinates but later i find out that it was because of the computation t += dist looks susceptible to floating point inaccuracies.

vec3 computeShadow(in vec3 lightPos) {
    float t = 0.0;
    vec3 ro = lightPos;
    vec3 rd = normalize(sphereOrigin - lightPos);

    float maxDistToSphere = length(lightPos - sphereOrigin) - sphereRadius;

    for (int i = 0; i < 100 && t < maxDistToSphere; i++) {
        vec3 p = ro + t * rd; // Position along the ray
        float minDist = 1e9;

        // Check to each sphere (planet)
        for (int k = 0; k < planetCount; k++) {
            vec3 toPlanet = normalize(spheresOrigin[k] - lightPos); 

            // Check if the planet is roughly in the ray direction
            if (dot(rd, toPlanet) > 0.99) { // Allowing a slight tolerance
                float dist = SDF(p, spheresOrigin[k], spheresRadius[k]);
                minDist = min(minDist, dist);
            }
        }

        // If the ray is close enough to a surface, consider it in shadow
        if (minDist < 0.01) return vec3(t / float(80) * 4.0); // Soft shadow effect
        
        t += minDist; // Step forward along the ray
    }
    
    // If no shadow encountered, return light
    return vec3(1.0);
}

the maxDistToSphere is the length between the sun and a planet point in the sphere and in the if condition here if (dot(rd, toPlanet) > 0.99) we check the direction of those planets if they align with the planet that we are currently checking if they are perfectly align we calculate their distance and return soft shadow if t reaches a distance higher than maxDistToSphere so no planet were in the same direction

Reasons:
  • Blacklisted phrase (0.5): i need
  • Blacklisted phrase (0.5): why?
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Sengeki