1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

Adjust shadowmap distortion to use entire SM texture (#12166)

This commit is contained in:
x2048 2022-04-07 22:13:50 +02:00 committed by GitHub
parent 0b5b2b2633
commit 48f7c5603e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 159 additions and 115 deletions

View file

@ -29,7 +29,6 @@ using m4f = core::matrix4;
void DirectionalLight::createSplitMatrices(const Camera *cam)
{
float radius;
v3f newCenter;
v3f look = cam->getDirection();
@ -42,17 +41,16 @@ void DirectionalLight::createSplitMatrices(const Camera *cam)
float sfFar = adjustDist(future_frustum.zFar, cam->getFovY());
// adjusted camera positions
v3f camPos2 = cam->getPosition();
v3f camPos = v3f(camPos2.X - cam->getOffset().X * BS,
camPos2.Y - cam->getOffset().Y * BS,
camPos2.Z - cam->getOffset().Z * BS);
camPos += look * sfNear;
camPos2 += look * sfNear;
v3f cam_pos_world = cam->getPosition();
v3f cam_pos_scene = v3f(cam_pos_world.X - cam->getOffset().X * BS,
cam_pos_world.Y - cam->getOffset().Y * BS,
cam_pos_world.Z - cam->getOffset().Z * BS);
cam_pos_scene += look * sfNear;
cam_pos_world += look * sfNear;
// center point of light frustum
float end = sfNear + sfFar;
newCenter = camPos + look * (sfNear + 0.05f * end);
v3f world_center = camPos2 + look * (sfNear + 0.05f * end);
v3f center_scene = cam_pos_scene + look * 0.35 * (sfFar - sfNear);
v3f center_world = cam_pos_world + look * 0.35 * (sfFar - sfNear);
// Create a vector to the frustum far corner
const v3f &viewUp = cam->getCameraNode()->getUpVector();
@ -60,22 +58,21 @@ void DirectionalLight::createSplitMatrices(const Camera *cam)
v3f farCorner = (look + viewRight * tanFovX + viewUp * tanFovY).normalize();
// Compute the frustumBoundingSphere radius
v3f boundVec = (camPos + farCorner * sfFar) - newCenter;
radius = boundVec.getLength();
// boundVec.getLength();
float vvolume = radius;
v3f frustumCenter = newCenter;
v3f eye_displacement = direction * vvolume;
v3f boundVec = (cam_pos_scene + farCorner * sfFar) - center_scene;
float radius = boundVec.getLength();
float length = radius * 3.0f;
v3f eye_displacement = direction * length;
// we must compute the viewmat with the position - the camera offset
// but the future_frustum position must be the actual world position
v3f eye = frustumCenter - eye_displacement;
future_frustum.position = world_center - eye_displacement;
future_frustum.length = vvolume;
future_frustum.ViewMat.buildCameraLookAtMatrixLH(eye, frustumCenter, v3f(0.0f, 1.0f, 0.0f));
future_frustum.ProjOrthMat.buildProjectionMatrixOrthoLH(future_frustum.length,
future_frustum.length, -future_frustum.length,
future_frustum.length,false);
v3f eye = center_scene - eye_displacement;
future_frustum.player = cam_pos_scene;
future_frustum.position = center_world - eye_displacement;
future_frustum.length = length;
future_frustum.radius = radius;
future_frustum.ViewMat.buildCameraLookAtMatrixLH(eye, center_scene, v3f(0.0f, 1.0f, 0.0f));
future_frustum.ProjOrthMat.buildProjectionMatrixOrthoLH(radius, radius,
0.0f, length, false);
future_frustum.camera_offset = cam->getOffset();
}
@ -94,7 +91,7 @@ void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool fo
float zNear = cam->getCameraNode()->getNearValue();
float zFar = getMaxFarValue();
if (!client->getEnv().getClientMap().getControl().range_all)
zFar = MYMIN(zFar, client->getEnv().getClientMap().getControl().wanted_range * BS * 1.5);
zFar = MYMIN(zFar, client->getEnv().getClientMap().getControl().wanted_range * BS);
///////////////////////////////////
// update splits near and fars
@ -105,7 +102,7 @@ void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool fo
createSplitMatrices(cam);
// get the draw list for shadows
client->getEnv().getClientMap().updateDrawListShadow(
getPosition(), getDirection(), future_frustum.length);
getPosition(), getDirection(), future_frustum.radius, future_frustum.length);
should_update_map_shadow = true;
dirty = true;
@ -115,6 +112,7 @@ void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool fo
v3f rotated_offset;
shadow_frustum.ViewMat.rotateVect(rotated_offset, intToFloat(cam_offset - shadow_frustum.camera_offset, BS));
shadow_frustum.ViewMat.setTranslation(shadow_frustum.ViewMat.getTranslation() + rotated_offset);
shadow_frustum.player += intToFloat(shadow_frustum.camera_offset - cam->getOffset(), BS);
shadow_frustum.camera_offset = cam_offset;
}
}
@ -139,6 +137,16 @@ v3f DirectionalLight::getPosition() const
return shadow_frustum.position;
}
v3f DirectionalLight::getPlayerPos() const
{
return shadow_frustum.player;
}
v3f DirectionalLight::getFuturePlayerPos() const
{
return future_frustum.player;
}
const m4f &DirectionalLight::getViewMatrix() const
{
return shadow_frustum.ViewMat;