Extract bounded additional scene lights

This commit is contained in:
2026-04-05 15:55:48 +08:00
parent 2c96f0d164
commit 18f53bd920
6 changed files with 555 additions and 93 deletions

View File

@@ -2,6 +2,7 @@
#include "Components/CameraComponent.h"
#include "Components/GameObject.h"
#include "Components/LightComponent.h"
#include "Components/MeshFilterComponent.h"
#include "Components/MeshRendererComponent.h"
#include "Components/TransformComponent.h"
@@ -139,5 +140,57 @@ std::vector<VisibleRenderItem> CollectRenderItemsForEntityIds(
return visibleItems;
}
bool IsUsableLight(const Components::LightComponent* light) {
return light != nullptr &&
light->IsEnabled() &&
light->GetGameObject() != nullptr &&
light->GetGameObject()->IsActiveInHierarchy();
}
bool IsLightVisibleForCullingMask(
const Components::LightComponent& light,
uint32_t cullingMask) {
if (!IsUsableLight(&light)) {
return false;
}
const Components::GameObject* gameObject = light.GetGameObject();
const uint32_t gameObjectLayerMask = 1u << gameObject->GetLayer();
return (cullingMask & gameObjectLayerMask) != 0u;
}
Math::Vector3 BuildRenderLightDirection(
const Components::LightComponent& light) {
Math::Vector3 direction = light.transform().GetForward() * -1.0f;
if (direction.SqrMagnitude() <= Math::EPSILON) {
return Math::Vector3::Back();
}
return direction.Normalized();
}
Components::LightComponent* FindMainDirectionalLight(
const Components::Scene& scene,
uint32_t cullingMask) {
const std::vector<Components::LightComponent*> lights =
scene.FindObjectsOfType<Components::LightComponent>();
Components::LightComponent* mainDirectionalLight = nullptr;
for (Components::LightComponent* light : lights) {
if (light == nullptr ||
!IsLightVisibleForCullingMask(*light, cullingMask) ||
light->GetLightType() != Components::LightType::Directional) {
continue;
}
if (mainDirectionalLight == nullptr ||
light->GetIntensity() > mainDirectionalLight->GetIntensity()) {
mainDirectionalLight = light;
}
}
return mainDirectionalLight;
}
} // namespace Rendering
} // namespace XCEngine