1

I am trying to use a post processing shader to create bodies of water. The water level will be at y = 0 and anything underneath that boundary will be affected by the shader. I will cast rays out from the camera position in the shader and retrieve the distance from the ground of each ray. I will then use that information to calculate how much water you are seeing through in order to calculate the color of the surface.The problem is I have no idea how to detect and calculate the distance travelled by the ray before intersecting with y = 0. Is there any way for me to achieve this effect.

Failed Code

float rayGroundIntersect(float3 rayOrigin, float3 rayDir) {
    if (rayDir.y <= 0) {
        return maxFloat;
    }
    float a = rayOrigin.y / rayDir.y;
    float l = length(rayDir.xz * a);
    return l;
}

A similar successful project but on a sphere.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Ethan Ma
  • 69
  • 3
  • 7

1 Answers1

2

Based on this answer by Trillian

float rayGroundIntersect(float3 rayOrigin, float3 rayDir) {
    float denom = dot(float3(0,1,0), rayDir);
    if (abs(denom) > 0.0001f)
    {
        // negate normal instead of rayOrigin
        float t = dot(rayOrigin, float3(0,-1,0)) / denom; 
        return t >= 0 ? t : maxFloat;
    }
    return maxFloat;
}
Ruzihm
  • 19,749
  • 5
  • 36
  • 48