6 changes: 6 additions & 0 deletions src/client/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class MainShaderConstantSetter : public IShaderConstantSetter
CachedPixelShaderSetting<f32> m_shadow_strength;
CachedPixelShaderSetting<f32> m_time_of_day;
CachedPixelShaderSetting<f32> m_shadowfar;
CachedPixelShaderSetting<f32, 4> m_camera_pos;
CachedPixelShaderSetting<s32> m_shadow_texture;
CachedVertexShaderSetting<f32> m_perspective_bias0_vertex;
CachedPixelShaderSetting<f32> m_perspective_bias0_pixel;
Expand Down Expand Up @@ -252,6 +253,7 @@ class MainShaderConstantSetter : public IShaderConstantSetter
, m_shadow_strength("f_shadow_strength")
, m_time_of_day("f_timeofday")
, m_shadowfar("f_shadowfar")
, m_camera_pos("CameraPos")
, m_shadow_texture("ShadowMapSampler")
, m_perspective_bias0_vertex("xyPerspectiveBias0")
, m_perspective_bias0_pixel("xyPerspectiveBias0")
Expand Down Expand Up @@ -321,6 +323,10 @@ class MainShaderConstantSetter : public IShaderConstantSetter
f32 shadowFar = shadow->getMaxShadowFar();
m_shadowfar.set(&shadowFar, services);

f32 cam_pos[4];
shadowViewProj.transformVect(cam_pos, light.getPlayerPos());
m_camera_pos.set(cam_pos, services);

// I dont like using this hardcoded value. maybe something like
// MAX_TEXTURE - 1 or somthing like that??
s32 TextureLayerID = 3;
Expand Down
58 changes: 33 additions & 25 deletions src/client/shadows/dynamicshadows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ using m4f = core::matrix4;

void DirectionalLight::createSplitMatrices(const Camera *cam)
{
float radius;
v3f newCenter;
v3f look = cam->getDirection();

Expand All @@ -42,40 +41,38 @@ 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();
v3f viewRight = look.crossProduct(viewUp);

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();
}

Expand All @@ -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
Expand All @@ -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;

Expand All @@ -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;
}
}
Expand All @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions src/client/shadows/dynamicshadows.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ class Client;

struct shadowFrustum
{
float zNear{0.0f};
float zFar{0.0f};
float length{0.0f};
f32 zNear{0.0f};
f32 zFar{0.0f};
f32 length{0.0f};
f32 radius{0.0f};
core::matrix4 ProjOrthMat;
core::matrix4 ViewMat;
v3f position;
v3f player;
v3s16 camera_offset;
};

Expand All @@ -57,6 +59,8 @@ class DirectionalLight
return direction;
};
v3f getPosition() const;
v3f getPlayerPos() const;
v3f getFuturePlayerPos() const;

/// Gets the light's matrices.
const core::matrix4 &getViewMatrix() const;
Expand Down
11 changes: 6 additions & 5 deletions src/client/shadows/dynamicshadowsrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ void ShadowRenderer::setShadowIntensity(float shadow_intensity)
disable();
}


void ShadowRenderer::addNodeToShadowList(
scene::ISceneNode *node, E_SHADOW_MODE shadowMode)
{
Expand Down Expand Up @@ -261,8 +260,9 @@ void ShadowRenderer::updateSMTextures()
cb->MaxFar = (f32)m_shadow_map_max_distance * BS;
cb->PerspectiveBiasXY = getPerspectiveBiasXY();
cb->PerspectiveBiasZ = getPerspectiveBiasZ();
cb->CameraPos = light.getFuturePlayerPos();
}

// set the Render Target
// right now we can only render in usual RTT, not
// Depth texture is available in irrlicth maybe we
Expand Down Expand Up @@ -322,9 +322,10 @@ void ShadowRenderer::update(video::ITexture *outputTarget)
if (!m_shadow_node_array.empty() && !m_light_list.empty()) {

for (DirectionalLight &light : m_light_list) {
// Static shader values.
m_shadow_depth_cb->MapRes = (f32)m_shadow_map_texture_size;
m_shadow_depth_cb->MaxFar = (f32)m_shadow_map_max_distance * BS;
// Static shader values for entities are set in updateSMTextures
// SM texture for entities is not updated incrementally and
// must by updated using current player position.
m_shadow_depth_entity_cb->CameraPos = light.getPlayerPos();

// render shadows for the n0n-map objects.
m_driver->setRenderTarget(shadowMapTextureDynamicObjects, true,
Expand Down
6 changes: 6 additions & 0 deletions src/client/shadows/shadowsshadercallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ void ShadowDepthShaderCB::OnSetConstants(

core::matrix4 lightMVP = driver->getTransform(video::ETS_PROJECTION);
lightMVP *= driver->getTransform(video::ETS_VIEW);

f32 cam_pos[4];
lightMVP.transformVect(cam_pos, CameraPos);

lightMVP *= driver->getTransform(video::ETS_WORLD);

m_light_mvp_setting.set(lightMVP.pointer(), services);
Expand All @@ -39,4 +43,6 @@ void ShadowDepthShaderCB::OnSetConstants(
m_perspective_bias1.set(&bias1, services);
f32 zbias = PerspectiveBiasZ;
m_perspective_zbias.set(&zbias, services);

m_cam_pos_setting.set(cam_pos, services);
}
5 changes: 4 additions & 1 deletion src/client/shadows/shadowsshadercallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class ShadowDepthShaderCB : public video::IShaderConstantSetCallBack
m_color_map_sampler_setting("ColorMapSampler"),
m_perspective_bias0("xyPerspectiveBias0"),
m_perspective_bias1("xyPerspectiveBias1"),
m_perspective_zbias("zPerspectiveBias")
m_perspective_zbias("zPerspectiveBias"),
m_cam_pos_setting("CameraPos")
{}

void OnSetMaterial(const video::SMaterial &material) override {}
Expand All @@ -43,6 +44,7 @@ class ShadowDepthShaderCB : public video::IShaderConstantSetCallBack

f32 MaxFar{2048.0f}, MapRes{1024.0f};
f32 PerspectiveBiasXY {0.9f}, PerspectiveBiasZ {0.5f};
v3f CameraPos;

private:
CachedVertexShaderSetting<f32, 16> m_light_mvp_setting;
Expand All @@ -52,4 +54,5 @@ class ShadowDepthShaderCB : public video::IShaderConstantSetCallBack
CachedVertexShaderSetting<f32> m_perspective_bias0;
CachedVertexShaderSetting<f32> m_perspective_bias1;
CachedVertexShaderSetting<f32> m_perspective_zbias;
CachedVertexShaderSetting<f32, 4> m_cam_pos_setting;
};