feat: add basic forward lighting coverage

This commit is contained in:
2026-04-01 00:41:56 +08:00
parent ac2b7c1fa2
commit 3373119eee
10 changed files with 31741 additions and 7 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"
@@ -22,6 +23,13 @@ bool IsUsableCamera(const Components::CameraComponent* camera) {
camera->GetGameObject()->IsActiveInHierarchy();
}
bool IsUsableLight(const Components::LightComponent* light) {
return light != nullptr &&
light->IsEnabled() &&
light->GetGameObject() != nullptr &&
light->GetGameObject()->IsActiveInHierarchy();
}
bool CompareVisibleItems(const VisibleRenderItem& lhs, const VisibleRenderItem& rhs) {
if (lhs.renderQueue != rhs.renderQueue) {
return lhs.renderQueue < rhs.renderQueue;
@@ -66,6 +74,7 @@ RenderSceneData RenderSceneExtractor::Extract(
sceneData.visibleItems.begin(),
sceneData.visibleItems.end(),
CompareVisibleItems);
ExtractLighting(scene, sceneData.lighting);
return sceneData;
}
@@ -93,6 +102,7 @@ RenderSceneData RenderSceneExtractor::ExtractForCamera(
sceneData.visibleItems.begin(),
sceneData.visibleItems.end(),
CompareVisibleItems);
ExtractLighting(scene, sceneData.lighting);
return sceneData;
}
@@ -171,6 +181,46 @@ RenderCameraData RenderSceneExtractor::BuildCameraData(
return cameraData;
}
void RenderSceneExtractor::ExtractLighting(
const Components::Scene& scene,
RenderLightingData& lightingData) const {
const std::vector<Components::LightComponent*> lights =
scene.FindObjectsOfType<Components::LightComponent>();
Components::LightComponent* mainDirectionalLight = nullptr;
for (Components::LightComponent* light : lights) {
if (!IsUsableLight(light) ||
light->GetLightType() != Components::LightType::Directional) {
continue;
}
if (mainDirectionalLight == nullptr ||
light->GetIntensity() > mainDirectionalLight->GetIntensity()) {
mainDirectionalLight = light;
}
}
if (mainDirectionalLight == nullptr) {
lightingData = {};
return;
}
RenderDirectionalLightData lightData;
lightData.enabled = true;
lightData.intensity = mainDirectionalLight->GetIntensity();
lightData.color = mainDirectionalLight->GetColor();
Math::Vector3 direction = mainDirectionalLight->transform().GetForward() * -1.0f;
if (direction.SqrMagnitude() <= Math::EPSILON) {
direction = Math::Vector3::Back();
} else {
direction = direction.Normalized();
}
lightData.direction = direction;
lightingData.mainDirectionalLight = lightData;
}
void RenderSceneExtractor::ExtractVisibleItems(
Components::GameObject* gameObject,
const Math::Vector3& cameraPosition,