EPSILON
A very small positive constant (typically 1e-6) used in shader calculations to circumvent floating-point precision issues, ensuring numerical stability and robustness.
Core Advantages
By providing a unified, clearly-named 'fault tolerance threshold,' it fundamentally enhances code robustness and readability, preventing rendering artifacts from issues like division-by-zero or imprecise comparisons, and centralizing control over this precision standard.
Common Uses
Safe Division: In lighting calculations, using it as a minimum for a potentially zero denominator to prevent division by zero.
Fuzzy Comparison: Determining if a value is 'close enough' to zero, e.g., `length(v) < EPSILON`, instead of the risky `v == 0.0`.
Preventing Self-Intersection: In ray tracing, offsetting a reflected ray's origin along the normal by an EPSILON distance to avoid 'shadow acne'.
Protecting Function Domains: Ensuring arguments passed to functions like `log()` or `sqrt()` are positive, e.g., `log(x + EPSILON)`.
How to adjust
EPSILON is a compile-time constant and cannot be adjusted at runtime. Choosing its value is a trade-off: a value too large (e.g., 0.01) sacrifices visual precision, causing artifacts like dimmed highlights or floating shadows. A value too small (e.g., 1e-10) may be ineffective on low-precision hardware and fail to provide protection. The default value of 1e-6 is a widely accepted good balance between safety and accuracy.
Code Examples
1// Ensure the denominator is a small positive number at minimum to prevent division by zero
2const denominator = TSL.max( dot( N, L ), TSL.EPSILON );
3const result = 1.0 / denominator;