168 auto* renderer = app->GetRenderer();
169 if (!renderer)
return;
173 for (
size_t i = 0; i < m_lights.GetSize(); ++i) {
174 Light* light = m_lights[i];
179 shadowLight = dirLight;
188 for (
size_t i = 0; i < m_lights.GetSize(); ++i) {
189 Light* light = m_lights[i];
190 if (!light || !light->
IsEnabled())
continue;
193 anyDirLight = dirLight;
200 renderer->SetShadowPassEnabled(shadowLight !=
nullptr);
206 m_hasShadowLightVP =
false;
207 static bool warned =
false;
208 if (!warned) {
SLEAK_WARN(
"UpdateShadowData: No directional light found!"); warned =
true; }
213 auto color = activeLight->
GetColor();
230 + dir * (-shadowDist);
247 float left = -frustumSize, right = frustumSize;
248 float bottom = -frustumSize, top = frustumSize;
250 lightProj(0, 0) = 2.0f / (right - left);
251 lightProj(1, 1) = 2.0f / (top - bottom);
252 lightProj(2, 2) = 1.0f / (farP - nearP);
253 lightProj(3, 0) = -(right + left) / (right - left);
254 lightProj(3, 1) = -(top + bottom) / (top - bottom);
255 lightProj(3, 2) = -nearP / (farP - nearP);
266 const float shadowMapSize =
267 static_cast<float>(renderer->GetShadowMapResolution());
268 const float halfShadow = shadowMapSize * 0.5f;
278 float clipX = lightVP_raw(3, 0);
279 float clipY = lightVP_raw(3, 1);
280 float clipW = lightVP_raw(3, 3);
282 float ndcX = clipX / clipW;
283 float ndcY = clipY / clipW;
284 float texX = ndcX * halfShadow;
285 float texY = ndcY * halfShadow;
286 float roundedX = std::round(texX);
287 float roundedY = std::round(texY);
288 float offsetNdcX = (roundedX - texX) / halfShadow;
289 float offsetNdcY = (roundedY - texY) / halfShadow;
290 lightProj(3, 0) += offsetNdcX;
291 lightProj(3, 1) += offsetNdcY;
295 lightVP = lightView * lightProj;
303 static bool s_latched =
false;
304 static float s_frozenVP[16];
305 if (s_freeze && shadowLight) {
307 std::memcpy(s_frozenVP, &lightVP(0, 0),
sizeof(s_frozenVP));
309 SLEAK_WARN(
"shadowfreeze: light frustum latched");
311 std::memcpy(&lightVP(0, 0), s_frozenVP,
sizeof(s_frozenVP));
317 renderer->SetLightVP(&lightVP(0, 0));
318 std::memcpy(m_shadowLightVP, &lightVP(0, 0),
sizeof(m_shadowLightVP));
319 m_hasShadowLightVP = shadowLight !=
nullptr;
338 ubo.
Ambient[3] = m_ambientIntensity;
347 static uint64_t s_sceneFrames = 0;
352 ubo.
CameraPos[3] = s_fixedStep ?
static_cast<float>(s_sceneFrames++) / 60.0f
359 std::memcpy(ubo.
LightVP, &lightVP(0, 0),
sizeof(
float) * 16);
370 if (Invert4x4(&camVP(0, 0), invVP)) {
372 std::memcpy(&invVPm(0, 0), invVP,
sizeof(
float) * 16);
373 std::memcpy(&lightVPm(0, 0), ubo.
LightVP,
sizeof(
float) * 16);
375 std::memcpy(ubo.
NdcToShadow, &comp(0, 0),
sizeof(
float) * 16);
384 1.0f /
static_cast<float>(renderer->GetShadowMapResolution());
413 for (
size_t i = 0; i < m_lights.GetSize() && ubo.
NumExtraLights < 3; ++i) {
414 Light* light = m_lights[i];
415 if (!light || !light->
IsEnabled())
continue;
416 if (light == activeLight)
continue;
418 if (!dlight)
continue;
420 auto eColor = dlight->GetColor();
433 renderer->UpdateShadowLightUBO(&ubo,
sizeof(ubo));