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-01 00:41:56 +08:00
|
|
|
bool IsUsableLight(const Components::LightComponent* light) {
|
|
|
|
|
return light != nullptr &&
|
|
|
|
|
light->IsEnabled() &&
|
|
|
|
|
light->GetGameObject() != nullptr &&
|
|
|
|
|
light->GetGameObject()->IsActiveInHierarchy();
|
|
|
|
|
}
|
|
|
|
|
|
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-01 00:41:56 +08:00
|
|
|
ExtractLighting(scene, 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-01 00:41:56 +08:00
|
|
|
ExtractLighting(scene, 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,
|
|
|
|
|
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;
|
2026-04-04 20:03:23 +08:00
|
|
|
lightData.castsShadows = mainDirectionalLight->GetCastsShadows();
|
2026-04-01 00:41:56 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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
|