In this lesson, you will learn how to use the scene depth buffer to calculate soft intersections between particles and geometry, effectively eliminating harsh clipping lines.
The Problem: Harsh Clipping
When a particle with a semi-transparent texture overlaps with a solid mesh, the hard edge of the particle quad cuts abruptly through the geometry. This "pixel-perfect" collision creates a visual artifact known as clipping, which shatters the illusion of depth, especially with soft effects like smoke, fire, or fog.
The Solution: Depth Blending
To achieve a soft transition, we utilize the Scene Depth node in Shader Graph. The core idea is to compare the depth of the particle fragment with the depth of the geometry already rendered behind it.
- Calculate the Scene Depth: Use the
Scene Depthnode set toEyespace to retrieve the distance of existing geometry from the camera. - Calculate Particle Depth: Retrieve the current fragment’s depth using the
Screen Positionnode (set toRaworScenemode). - Find the Difference: Subtract the scene depth from the particle’s depth. This result represents the "thickness" of the particle at that specific pixel.
- Normalize: Pass this difference into a
Saturatenode and then aDividenode (or aSmoothstep) to control the "fade distance." A smaller distance results in a sharp transition, while a larger distance creates a soft, blurry fade. - Apply Alpha: Multiply this result by your texture’s alpha channel before connecting it to the
Alphaoutput of your master node.
Conceptual Logic
// Pseudo-HLSL logic for the depth calculation
float sceneDepth = tex2D(_CameraDepthTexture, screenPos);
float particleDepth = screenPos.z;
float distance = saturate((sceneDepth - particleDepth) / softFadeFactor);
outputAlpha = textureAlpha * distance;
When distance approaches zero (as the particle nears the mesh), the alpha drops, causing the particle to fade out gracefully instead of abruptly clipping. This creates a natural "soft" intersection, making your particles appear as if they are volumetrically integrated into the scene rather than just flat textures glued to the screen.
Key Takeaway: By comparing the current fragment depth against the _CameraDepthTexture, you can dynamically fade out particle alpha as they approach intersecting geometry, creating professional-grade, soft-blending effects.