Discard
In the fragment shader, completely abandons (discards) rendering the current pixel based on a condition, equivalent to the `discard;` keyword in GLSL.
Core Advantages
It is the standard and most efficient way to achieve 'Alpha Testing' (cutout) effects. It fundamentally avoids the sorting issues of alpha blending by treating the material as opaque, and can optimize performance by exiting the shader early.
Common Uses
Creating cutout effects for foliage, fences, etc., using a texture's alpha channel.
Generating procedural patterns like stripes or checkerboards using math functions.
Implementing dynamic cross-section or slicing effects on models.
Culling pixels based on distance to the camera for a simplified form of LOD.
How to adjust
Adjusting the effect of `Discard` means adjusting its `conditional` parameter. For example, in a cutout effect, changing the comparison `threshold` will 'erode' or 'thicken' the visible edges of the object. In a dynamic slicing effect, changing the `slice_position` uniform will move the position of the 'cutting plane'.
Code Examples
1// Discard the pixel if its alpha value from the texture is below 0.5
2const alpha = texture( map, uv ).a;
3Discard( alpha.lessThan( 0.5 ) );