2026-03-26 20:43:17 +08:00
|
|
|
#include "Rendering/RenderSceneExtractor.h"
|
|
|
|
|
|
|
|
|
|
#include "Components/CameraComponent.h"
|
|
|
|
|
#include "Components/GameObject.h"
|
2026-04-01 00:41:56 +08:00
|
|
|
#include "Components/LightComponent.h"
|
2026-03-27 11:56:23 +08:00
|
|
|
#include "Rendering/RenderMaterialUtility.h"
|
2026-04-01 19:03:58 +08:00
|
|
|
#include "Rendering/RenderSceneUtility.h"
|
2026-03-26 20:43:17 +08:00
|
|
|
#include "Scene/Scene.h"
|
|
|
|
|
|
2026-03-27 11:56:23 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2026-03-26 20:43:17 +08:00
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace Rendering {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
bool IsUsableCamera(const Components::CameraComponent* camera) {
|
|
|
|
|
return camera != nullptr &&
|
|
|
|
|
camera->IsEnabled() &&
|
|
|
|
|
camera->GetGameObject() != nullptr &&
|
|
|
|
|
camera->GetGameObject()->IsActiveInHierarchy();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:55:48 +08:00
|
|
|
RenderLightType ToRenderLightType(Components::LightType lightType) {
|
|
|
|
|
switch (lightType) {
|
|
|
|
|
case Components::LightType::Directional:
|
|
|
|
|
return RenderLightType::Directional;
|
|
|
|
|
case Components::LightType::Point:
|
|
|
|
|
return RenderLightType::Point;
|
|
|
|
|
case Components::LightType::Spot:
|
|
|
|
|
return RenderLightType::Spot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RenderLightType::Point;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenderDirectionalLightData BuildDirectionalLightData(
|
|
|
|
|
const Components::LightComponent& light) {
|
|
|
|
|
RenderDirectionalLightData lightData;
|
|
|
|
|
lightData.enabled = true;
|
|
|
|
|
lightData.castsShadows = light.GetCastsShadows();
|
|
|
|
|
lightData.intensity = light.GetIntensity();
|
|
|
|
|
lightData.color = light.GetColor();
|
|
|
|
|
lightData.direction = BuildRenderLightDirection(light);
|
|
|
|
|
return lightData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenderAdditionalLightData BuildAdditionalLightData(
|
|
|
|
|
const Components::LightComponent& light) {
|
|
|
|
|
RenderAdditionalLightData lightData;
|
|
|
|
|
lightData.type = ToRenderLightType(light.GetLightType());
|
|
|
|
|
lightData.enabled = true;
|
|
|
|
|
lightData.castsShadows = light.GetCastsShadows();
|
|
|
|
|
lightData.color = light.GetColor();
|
|
|
|
|
lightData.intensity = light.GetIntensity();
|
|
|
|
|
|
|
|
|
|
switch (light.GetLightType()) {
|
|
|
|
|
case Components::LightType::Directional:
|
|
|
|
|
lightData.direction = BuildRenderLightDirection(light);
|
|
|
|
|
break;
|
|
|
|
|
case Components::LightType::Point:
|
|
|
|
|
lightData.position = light.transform().GetPosition();
|
|
|
|
|
lightData.range = light.GetRange();
|
|
|
|
|
break;
|
|
|
|
|
case Components::LightType::Spot:
|
|
|
|
|
lightData.position = light.transform().GetPosition();
|
|
|
|
|
lightData.direction = BuildRenderLightDirection(light);
|
|
|
|
|
lightData.range = light.GetRange();
|
|
|
|
|
lightData.spotAngle = light.GetSpotAngle();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lightData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float ComputeAdditionalLightInfluence(
|
|
|
|
|
const Components::LightComponent& light,
|
|
|
|
|
const Math::Vector3& cameraPosition) {
|
|
|
|
|
if (light.GetIntensity() <= Math::EPSILON) {
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (light.GetLightType() == Components::LightType::Directional) {
|
|
|
|
|
return light.GetIntensity();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const float range = std::max(light.GetRange(), 0.001f);
|
|
|
|
|
const float rangeSq = range * range;
|
|
|
|
|
const Math::Vector3 lightPosition = light.transform().GetPosition();
|
|
|
|
|
const float distanceSq = (lightPosition - cameraPosition).SqrMagnitude();
|
|
|
|
|
if (distanceSq >= rangeSq) {
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return light.GetIntensity() / std::max(distanceSq, Math::EPSILON);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t GetAdditionalLightGroupRank(const Components::LightComponent& light) {
|
|
|
|
|
return light.GetLightType() == Components::LightType::Directional ? 0u : 1u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t GetAdditionalLightTypeRank(const Components::LightComponent& light) {
|
|
|
|
|
switch (light.GetLightType()) {
|
|
|
|
|
case Components::LightType::Directional:
|
|
|
|
|
return 0u;
|
|
|
|
|
case Components::LightType::Point:
|
|
|
|
|
return 1u;
|
|
|
|
|
case Components::LightType::Spot:
|
|
|
|
|
return 2u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 3u;
|
2026-04-01 00:41:56 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 11:56:23 +08:00
|
|
|
bool CompareVisibleItems(const VisibleRenderItem& lhs, const VisibleRenderItem& rhs) {
|
|
|
|
|
if (lhs.renderQueue != rhs.renderQueue) {
|
|
|
|
|
return lhs.renderQueue < rhs.renderQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool isTransparentQueue = IsTransparentRenderQueue(lhs.renderQueue);
|
|
|
|
|
if (lhs.cameraDistanceSq != rhs.cameraDistanceSq) {
|
|
|
|
|
return isTransparentQueue
|
|
|
|
|
? lhs.cameraDistanceSq > rhs.cameraDistanceSq
|
|
|
|
|
: lhs.cameraDistanceSq < rhs.cameraDistanceSq;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lhs.gameObject != rhs.gameObject) {
|
|
|
|
|
return lhs.gameObject < rhs.gameObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lhs.sectionIndex < rhs.sectionIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 20:43:17 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
RenderSceneData RenderSceneExtractor::Extract(
|
|
|
|
|
const Components::Scene& scene,
|
|
|
|
|
Components::CameraComponent* overrideCamera,
|
|
|
|
|
uint32_t viewportWidth,
|
|
|
|
|
uint32_t viewportHeight) const {
|
|
|
|
|
RenderSceneData sceneData;
|
|
|
|
|
sceneData.camera = SelectCamera(scene, overrideCamera);
|
|
|
|
|
if (sceneData.camera == nullptr) {
|
|
|
|
|
return sceneData;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 19:03:58 +08:00
|
|
|
sceneData.cameraData = BuildRenderCameraData(*sceneData.camera, viewportWidth, viewportHeight);
|
2026-03-27 16:57:04 +08:00
|
|
|
const Math::Vector3 cameraPosition = sceneData.cameraData.worldPosition;
|
2026-04-01 01:42:06 +08:00
|
|
|
const uint32_t cullingMask = sceneData.camera->GetCullingMask();
|
2026-03-27 16:57:04 +08:00
|
|
|
|
|
|
|
|
const std::vector<Components::GameObject*> rootGameObjects = scene.GetRootGameObjects();
|
|
|
|
|
for (Components::GameObject* rootGameObject : rootGameObjects) {
|
2026-04-01 01:42:06 +08:00
|
|
|
ExtractVisibleItems(rootGameObject, cameraPosition, cullingMask, sceneData.visibleItems);
|
2026-03-27 16:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::stable_sort(
|
|
|
|
|
sceneData.visibleItems.begin(),
|
|
|
|
|
sceneData.visibleItems.end(),
|
|
|
|
|
CompareVisibleItems);
|
2026-04-05 15:55:48 +08:00
|
|
|
ExtractLighting(scene, cameraPosition, cullingMask, sceneData.lighting);
|
2026-03-27 16:57:04 +08:00
|
|
|
|
|
|
|
|
return sceneData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenderSceneData RenderSceneExtractor::ExtractForCamera(
|
|
|
|
|
const Components::Scene& scene,
|
|
|
|
|
Components::CameraComponent& camera,
|
|
|
|
|
uint32_t viewportWidth,
|
|
|
|
|
uint32_t viewportHeight) const {
|
|
|
|
|
RenderSceneData sceneData;
|
|
|
|
|
if (!IsUsableCamera(&camera)) {
|
|
|
|
|
return sceneData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sceneData.camera = &camera;
|
2026-04-01 19:03:58 +08:00
|
|
|
sceneData.cameraData = BuildRenderCameraData(camera, viewportWidth, viewportHeight);
|
2026-03-27 11:56:23 +08:00
|
|
|
const Math::Vector3 cameraPosition = sceneData.cameraData.worldPosition;
|
2026-04-01 01:42:06 +08:00
|
|
|
const uint32_t cullingMask = camera.GetCullingMask();
|
2026-03-26 20:43:17 +08:00
|
|
|
|
|
|
|
|
const std::vector<Components::GameObject*> rootGameObjects = scene.GetRootGameObjects();
|
|
|
|
|
for (Components::GameObject* rootGameObject : rootGameObjects) {
|
2026-04-01 01:42:06 +08:00
|
|
|
ExtractVisibleItems(rootGameObject, cameraPosition, cullingMask, sceneData.visibleItems);
|
2026-03-26 20:43:17 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 11:56:23 +08:00
|
|
|
std::stable_sort(
|
|
|
|
|
sceneData.visibleItems.begin(),
|
|
|
|
|
sceneData.visibleItems.end(),
|
|
|
|
|
CompareVisibleItems);
|
2026-04-05 15:55:48 +08:00
|
|
|
ExtractLighting(scene, cameraPosition, cullingMask, sceneData.lighting);
|
2026-03-27 11:56:23 +08:00
|
|
|
|
2026-03-26 20:43:17 +08:00
|
|
|
return sceneData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Components::CameraComponent* RenderSceneExtractor::SelectCamera(
|
|
|
|
|
const Components::Scene& scene,
|
|
|
|
|
Components::CameraComponent* overrideCamera) const {
|
|
|
|
|
if (IsUsableCamera(overrideCamera)) {
|
|
|
|
|
return overrideCamera;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::vector<Components::CameraComponent*> cameras = scene.FindObjectsOfType<Components::CameraComponent>();
|
|
|
|
|
|
|
|
|
|
Components::CameraComponent* primaryCamera = nullptr;
|
|
|
|
|
for (Components::CameraComponent* camera : cameras) {
|
|
|
|
|
if (!IsUsableCamera(camera)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (camera->IsPrimary()) {
|
|
|
|
|
if (primaryCamera == nullptr || camera->GetDepth() > primaryCamera->GetDepth()) {
|
|
|
|
|
primaryCamera = camera;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (primaryCamera != nullptr) {
|
|
|
|
|
return primaryCamera;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Components::CameraComponent* camera : cameras) {
|
|
|
|
|
if (IsUsableCamera(camera)) {
|
|
|
|
|
return camera;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 00:41:56 +08:00
|
|
|
void RenderSceneExtractor::ExtractLighting(
|
|
|
|
|
const Components::Scene& scene,
|
2026-04-05 15:55:48 +08:00
|
|
|
const Math::Vector3& cameraPosition,
|
|
|
|
|
uint32_t cullingMask,
|
2026-04-01 00:41:56 +08:00
|
|
|
RenderLightingData& lightingData) const {
|
2026-04-05 15:55:48 +08:00
|
|
|
lightingData = {};
|
|
|
|
|
|
2026-04-01 00:41:56 +08:00
|
|
|
const std::vector<Components::LightComponent*> lights =
|
|
|
|
|
scene.FindObjectsOfType<Components::LightComponent>();
|
|
|
|
|
|
2026-04-05 15:55:48 +08:00
|
|
|
Components::LightComponent* mainDirectionalLight =
|
|
|
|
|
FindMainDirectionalLight(scene, cullingMask);
|
|
|
|
|
if (mainDirectionalLight != nullptr) {
|
|
|
|
|
lightingData.mainDirectionalLight = BuildDirectionalLightData(*mainDirectionalLight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct AdditionalLightCandidate {
|
|
|
|
|
RenderAdditionalLightData data = {};
|
|
|
|
|
float influence = 0.0f;
|
|
|
|
|
uint32_t groupRank = 0u;
|
|
|
|
|
uint32_t typeRank = 0u;
|
|
|
|
|
Components::GameObject::ID gameObjectId = Components::GameObject::INVALID_ID;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<AdditionalLightCandidate> candidates;
|
|
|
|
|
candidates.reserve(lights.size());
|
2026-04-01 00:41:56 +08:00
|
|
|
for (Components::LightComponent* light : lights) {
|
2026-04-05 15:55:48 +08:00
|
|
|
if (light == nullptr ||
|
|
|
|
|
!IsLightVisibleForCullingMask(*light, cullingMask) ||
|
|
|
|
|
light == mainDirectionalLight) {
|
2026-04-01 00:41:56 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:55:48 +08:00
|
|
|
AdditionalLightCandidate candidate;
|
|
|
|
|
candidate.data = BuildAdditionalLightData(*light);
|
|
|
|
|
candidate.influence = ComputeAdditionalLightInfluence(*light, cameraPosition);
|
|
|
|
|
candidate.groupRank = GetAdditionalLightGroupRank(*light);
|
|
|
|
|
candidate.typeRank = GetAdditionalLightTypeRank(*light);
|
|
|
|
|
candidate.gameObjectId = light->GetGameObject() != nullptr
|
|
|
|
|
? light->GetGameObject()->GetID()
|
|
|
|
|
: Components::GameObject::INVALID_ID;
|
|
|
|
|
candidates.push_back(candidate);
|
2026-04-01 00:41:56 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 15:55:48 +08:00
|
|
|
std::sort(
|
|
|
|
|
candidates.begin(),
|
|
|
|
|
candidates.end(),
|
|
|
|
|
[](const AdditionalLightCandidate& lhs, const AdditionalLightCandidate& rhs) {
|
|
|
|
|
if (lhs.groupRank != rhs.groupRank) {
|
|
|
|
|
return lhs.groupRank < rhs.groupRank;
|
|
|
|
|
}
|
2026-04-01 00:41:56 +08:00
|
|
|
|
2026-04-05 15:55:48 +08:00
|
|
|
if (lhs.influence != rhs.influence) {
|
|
|
|
|
return lhs.influence > rhs.influence;
|
|
|
|
|
}
|
2026-04-01 00:41:56 +08:00
|
|
|
|
2026-04-05 15:55:48 +08:00
|
|
|
if (lhs.typeRank != rhs.typeRank) {
|
|
|
|
|
return lhs.typeRank < rhs.typeRank;
|
|
|
|
|
}
|
2026-04-01 00:41:56 +08:00
|
|
|
|
2026-04-05 15:55:48 +08:00
|
|
|
return lhs.gameObjectId < rhs.gameObjectId;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const uint32_t additionalLightCount = static_cast<uint32_t>(
|
|
|
|
|
std::min(candidates.size(), static_cast<size_t>(RenderLightingData::kMaxAdditionalLightCount)));
|
|
|
|
|
lightingData.additionalLightCount = additionalLightCount;
|
|
|
|
|
for (uint32_t index = 0; index < additionalLightCount; ++index) {
|
|
|
|
|
lightingData.additionalLights[index] = candidates[index].data;
|
|
|
|
|
}
|
2026-04-01 00:41:56 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 11:56:23 +08:00
|
|
|
void RenderSceneExtractor::ExtractVisibleItems(
|
2026-03-26 20:43:17 +08:00
|
|
|
Components::GameObject* gameObject,
|
2026-03-27 11:56:23 +08:00
|
|
|
const Math::Vector3& cameraPosition,
|
2026-04-01 01:42:06 +08:00
|
|
|
uint32_t cullingMask,
|
2026-03-27 11:56:23 +08:00
|
|
|
std::vector<VisibleRenderItem>& visibleItems) const {
|
2026-03-26 20:43:17 +08:00
|
|
|
if (gameObject == nullptr || !gameObject->IsActiveInHierarchy()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 01:42:06 +08:00
|
|
|
const uint32_t gameObjectLayerMask = 1u << gameObject->GetLayer();
|
|
|
|
|
const bool isVisibleInCameraMask = (cullingMask & gameObjectLayerMask) != 0;
|
|
|
|
|
|
2026-04-01 19:03:58 +08:00
|
|
|
if (isVisibleInCameraMask) {
|
|
|
|
|
AppendRenderItemsForGameObject(*gameObject, cameraPosition, visibleItems);
|
2026-03-26 20:43:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Components::GameObject* child : gameObject->GetChildren()) {
|
2026-04-01 01:42:06 +08:00
|
|
|
ExtractVisibleItems(child, cameraPosition, cullingMask, visibleItems);
|
2026-03-26 20:43:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Rendering
|
|
|
|
|
} // namespace XCEngine
|