**Intial Problem:** I have a procedural mesh representing a planet. I want to add water to that planet using a ray cast to alter the color of a pixel if the ocean sphere is intersected. Unfortunately, if the water is outside the bounds of the planet (i.e, the camera is looking at the horizon), there's no frag called for that pixel and **the ocean isn't rendered.**
----------
The solution I came up with was to Blit a post processing shader inside the camera's OnRenderImage method and use the depth buffer to determine where the the ocean sphere should be occluded. This works well enough at a distance, but something is wrong when the camera is close with a wide FOV:
![alt text][1]
[1]: /storage/temp/166861-depthbufferissue.png
I thought there might be an issue with how I'm generating rays from the UVs:
// In frag
Ray ray = CreateCameraRay(i.uv * -2 + 1);
[...]
Ray CreateRay(float3 origin, float3 direction)
{
Ray ray;
ray.origin = origin;
ray.direction = direction;
return ray;
}
Ray CreateCameraRay(float2 uv)
{
// Set the origin of the ray to the camera origin in world space.
float3 origin = mul(unity_CameraToWorld, float4(0.0f, 0.0f, 0.0f, 1.0f)).xyz;
// Set the direction of the ray by mapping the uv input to the inverse projection matrix, rotating to match world space, and then normalizing it.
float3 direction = mul(_CameraInverseProjection, float4(uv, 0.0f, 1.0f)).xyz;
direction = mul(unity_CameraToWorld, float4(direction, 0.0f)).xyz;
direction = normalize(direction);
direction = float3(direction.x, direction.y, direction.z);
return CreateRay(origin, direction);
}
I'm trying to debug this now, but I'm wondering if I'm going about this the wrong way in the first place. Is there a simpler way to draw outside the boundaries of the mesh using a script/shader on the planet itself rather than having to involve a post processing effect on the Camera [and effectively recalculate the world position from the depth buffer]?
↧