From 1831df147c24fe1bbbe3aeddf98da3535284ade8 Mon Sep 17 00:00:00 2001 From: Turanszki Janos Date: Thu, 21 May 2020 20:32:55 +0100 Subject: [PATCH] cascade shadow fix #115 --- WickedEngine/wiRenderer.cpp | 14 ++++++++++---- WickedEngine/wiVersion.cpp | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 63625f328..94be8d6ee 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -2916,23 +2916,29 @@ inline void CreateDirLightShadowCams(const LightComponent& light, CameraComponen } // Fit AABB onto bounding sphere: - XMVECTOR vMin = XMVectorAdd(center, XMVectorReplicate(-radius)); - XMVECTOR vMax = XMVectorAdd(center, XMVectorReplicate(radius)); + XMVECTOR vRadius = XMVectorReplicate(radius); + XMVECTOR vMin = XMVectorSubtract(center, vRadius); + XMVECTOR vMax = XMVectorAdd(center, vRadius); // Snap cascade to texel grid: const XMVECTOR extent = XMVectorSubtract(vMax, vMin); const XMVECTOR texelSize = extent / float(wiRenderer::GetShadowRes2D()); vMin = XMVectorFloor(vMin / texelSize) * texelSize; vMax = XMVectorFloor(vMax / texelSize) * texelSize; + center = (vMin + vMax) * 0.5f; + XMFLOAT3 _center; XMFLOAT3 _min; XMFLOAT3 _max; + XMStoreFloat3(&_center, center); XMStoreFloat3(&_min, vMin); XMStoreFloat3(&_max, vMax); // Extrude bounds to avoid early shadow clipping: - _min.z = std::min(_min.z, -farPlane * 0.5f); - _max.z = std::max(_max.z, farPlane * 0.5f); + float ext = abs(_center.z - _min.z); + ext = std::max(ext, farPlane * 0.5f); + _min.z = _center.z - ext; + _max.z = _center.z + ext; const XMMATRIX lightProjection = XMMatrixOrthographicOffCenterLH(_min.x, _max.x, _min.y, _max.y, _max.z, _min.z); // notice reversed Z! diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 9601767c7..83ee60430 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 42; // minor bug fixes, alterations, refactors, updates - const int revision = 2; + const int revision = 3; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);