Skip to content

Commit

Permalink
Fix (again) the shadow transform
Browse files Browse the repository at this point in the history
- we were not using the correct field in ShadowMapManager
- we were not computing the transform correctly, it should applied
  after the local transform, not before.

FIXES=[299310624]
  • Loading branch information
pixelflinger authored and poweifeng committed Nov 14, 2023
1 parent cfc133f commit 57aa99e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion filament/src/ShadowMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ ShadowMap::ShaderParameters ShadowMap::updateDirectional(FEngine& engine,
FLightManager::Instance const li = lightData.elementAt<FScene::LIGHT_INSTANCE>(index);
FLightManager::ShadowParams const params = lcm.getShadowParams(li);

// We can't use LISPSM in stable mode
const auto direction = lightData.elementAt<FScene::SHADOW_DIRECTION>(index);

auto [Mv, znear, zfar, lsClippedShadowVolume, vertexCount, visibleShadows] =
Expand All @@ -140,6 +139,7 @@ ShadowMap::ShaderParameters ShadowMap::updateDirectional(FEngine& engine,
*/

mat4f W, Wp, L;
// We can't use LISPSM in stable mode
const bool useLispsm = params.options.lispsm && !params.options.stable;
if (useLispsm) {
// Orient the shadow map in the direction of the view vector by constructing a
Expand Down
2 changes: 1 addition & 1 deletion filament/src/ShadowMapManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ ShadowMapManager::ShadowTechnique ShadowMapManager::updateCascadeShadowMaps(FEng
// entire camera frustum, as if we only had a single cascade.
ShadowMap& shadowMap = cascadedShadowMaps[0];

const auto direction = options.transform * lightData.elementAt<FScene::DIRECTION>(0);
const auto direction = lightData.elementAt<FScene::SHADOW_DIRECTION>(0);

// We compute the directional light's model matrix using the origin's as the light position.
// The choice of the light's origin initially doesn't matter for a directional light.
Expand Down
14 changes: 6 additions & 8 deletions filament/src/details/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,16 @@ void FScene::prepare(utils::JobSystem& js,
// in the code below, we only transform directions, so the translation of the
// world transform is irrelevant, and we don't need to use getWorldTransformAccurate()

mat3 const worldDirectionTransform =
mat3::getTransformForNormals(tcm.getWorldTransformAccurate(ti).upperLeft());
FLightManager::ShadowParams const params = lcm.getShadowParams(li);
float3 const localDirection = lcm.getLocalDirection(li);
float3 const shadowLocalDirection = params.options.transform * localDirection;
mat3 const worldDirectionTransform = tcm.getWorldTransformAccurate(ti).upperLeft();
mat3 const shaderWorldTransform = worldTransform.upperLeft() * worldDirectionTransform;
float3 const localDirection = worldDirectionTransform * lcm.getLocalDirection(li);
double3 const shadowLocalDirection = params.options.transform * localDirection;

// using mat3::getTransformForNormals handles non-uniform scaling
// note: in the common case of the rigid-body transform, getTransformForNormals() returns
// identity.
mat3 const worlTransformNormals = mat3::getTransformForNormals(shaderWorldTransform);
mat3 const worlTransformNormals = mat3::getTransformForNormals(worldTransform.upperLeft());
double3 const d = worlTransformNormals * localDirection;
double3 const s = worlTransformNormals * shadowLocalDirection;

Expand All @@ -290,10 +290,8 @@ void FScene::prepare(utils::JobSystem& js,
// is pointing down, which is a common case for lights. See ShadowMap.cpp.
return transpose(mat3::lookTo(direction, double3{ 1, 0, 0 }));
};
double3 const worldDirection =
mat3::getTransformForNormals(worldDirectionTransform) * shadowLocalDirection;
double3 const worldOrigin = transpose(worldTransform.upperLeft()) * worldTransform[3].xyz;
mat3 const Mv = getMv(worldDirection);
mat3 const Mv = getMv(shadowLocalDirection);
double2 const lsReferencePoint = (Mv * worldOrigin).xy;

constexpr float inf = std::numeric_limits<float>::infinity();
Expand Down

0 comments on commit 57aa99e

Please sign in to comment.