Core Principle

The core principle of the Fragment shader, and the principles of detecting edges of translucent and transparent objects.

The Fragment Shader operates in the final step of the vertex shader process, specifically the multiplication by the MVP (Model-View-Projection) matrix, resulting in coordinates within the homogeneous clipping space. This essentially translates to 2D screen coordinates. The fragment shader iterates over these screen coordinates and calculates colors for them based on additional data. In a standard diffuse shader, for instance, the fragment shader would use the original model coordinates corresponding to the current screen position to obtain UV coordinates, then sample the texture at these UV coordinates to determine the color, enabling proper rendering.

Regarding water surface rendering, the roles of two nodes are crucial:

  1. The SceneDepth node retrieves the depth of the pixel at the current screen coordinate. This depth is the distance between the camera's clipping plane and the objects on the screen, including the object in question, at that screen coordinate. This value includes translucent objects.

  2. The ScreenPosition node yields four values (x, y, z, w), with 'w' similarly representing depth. This is the distance between the camera's clipping plane and the objects on the screen, including the object in question, at that screen coordinate, but does not include translucent objects.

Subtracting these values gives the edge of intersection because there is a translucent object at this point with an opaque object behind it. If the translucent and opaque objects coincide, the difference is zero. If they don't, the greater the difference, the farther the translucent object's surface is from the opaque object.

It's important to note that rendering follows a specific sequence. Therefore, the values obtained by this shader are all from the state before and during the execution of this shader. In terms of rendering order, objects rendered after this shader will not have their depths factored into the calculations of the current shader.

Scene depth (including transparent pixels) - w vector of the screen space position (excluding transparent pixels) = The edge where transparent and opaque objects intersect.

Set the edge detection depth to smooth the transition at the edges and define the range.

The X and Z coordinates remain unchanged, while the Y coordinate is controlled using noise.