| 331 | } |
| 332 | |
| 333 | Bias ShadowBias(const Vec3& normal, const Vec3& lightDir, float texelWorldSize) |
| 334 | { |
| 335 | Vec3 N = Normalize(normal); |
| 336 | Vec3 L = Normalize(lightDir * -1.0f); // toward the light |
| 337 | float c = Dot(N, L); |
| 338 | c = std::max(1e-4f, std::min(1.0f, c)); |
| 339 | float sinT = std::sqrt(std::max(0.0f, 1.0f - c * c)); |
| 340 | float tanT = sinT / c; |
| 341 | |
| 342 | const float baseDepth = 0.0008f; |
| 343 | const float slopeScale = 0.0025f; |
| 344 | const float maxDepth = 0.01f; |
| 345 | float depthBias = std::min(maxDepth, baseDepth + slopeScale * tanT); |
| 346 | |
| 347 | const float offsetScale = 2.0f; |
| 348 | float normalOffset = std::min(texelWorldSize * offsetScale, texelWorldSize * offsetScale * sinT); |
| 349 | return {depthBias, normalOffset}; |
| 350 | } |
| 351 | |
| 352 | ShadowUV WorldToShadowUV(const Mat4& lightVP, const Vec3& p) |
| 353 | { |