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-26 20:43:17 +08:00
|
|
|
#include "Components/MeshFilterComponent.h"
|
|
|
|
|
#include "Components/MeshRendererComponent.h"
|
|
|
|
|
#include "Components/TransformComponent.h"
|
2026-03-27 11:56:23 +08:00
|
|
|
#include "Rendering/RenderMaterialUtility.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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sceneData.cameraData = BuildCameraData(*sceneData.camera, viewportWidth, viewportHeight);
|
2026-03-27 16:57:04 +08:00
|
|
|
const Math::Vector3 cameraPosition = sceneData.cameraData.worldPosition;
|
|
|
|
|
|
|
|
|
|
const std::vector<Components::GameObject*> rootGameObjects = scene.GetRootGameObjects();
|
|
|
|
|
for (Components::GameObject* rootGameObject : rootGameObjects) {
|
|
|
|
|
ExtractVisibleItems(rootGameObject, cameraPosition, sceneData.visibleItems);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
sceneData.cameraData = BuildCameraData(camera, viewportWidth, viewportHeight);
|
2026-03-27 11:56:23 +08:00
|
|
|
const Math::Vector3 cameraPosition = sceneData.cameraData.worldPosition;
|
2026-03-26 20:43:17 +08:00
|
|
|
|
|
|
|
|
const std::vector<Components::GameObject*> rootGameObjects = scene.GetRootGameObjects();
|
|
|
|
|
for (Components::GameObject* rootGameObject : rootGameObjects) {
|
2026-03-27 11:56:23 +08:00
|
|
|
ExtractVisibleItems(rootGameObject, cameraPosition, 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenderCameraData RenderSceneExtractor::BuildCameraData(
|
|
|
|
|
const Components::CameraComponent& camera,
|
|
|
|
|
uint32_t viewportWidth,
|
|
|
|
|
uint32_t viewportHeight) const {
|
|
|
|
|
RenderCameraData cameraData;
|
|
|
|
|
cameraData.viewportWidth = viewportWidth;
|
|
|
|
|
cameraData.viewportHeight = viewportHeight;
|
|
|
|
|
cameraData.worldPosition = camera.transform().GetPosition();
|
|
|
|
|
cameraData.clearColor = camera.GetClearColor();
|
|
|
|
|
|
|
|
|
|
const Math::Matrix4x4 view = camera.transform().GetWorldToLocalMatrix();
|
|
|
|
|
const float aspect = viewportHeight > 0
|
|
|
|
|
? static_cast<float>(viewportWidth) / static_cast<float>(viewportHeight)
|
|
|
|
|
: 1.0f;
|
|
|
|
|
|
|
|
|
|
Math::Matrix4x4 projection = Math::Matrix4x4::Identity();
|
|
|
|
|
if (camera.GetProjectionType() == Components::CameraProjectionType::Perspective) {
|
|
|
|
|
projection = Math::Matrix4x4::Perspective(
|
|
|
|
|
camera.GetFieldOfView() * Math::DEG_TO_RAD,
|
|
|
|
|
aspect,
|
|
|
|
|
camera.GetNearClipPlane(),
|
|
|
|
|
camera.GetFarClipPlane());
|
|
|
|
|
} else {
|
|
|
|
|
const float orthoSize = camera.GetOrthographicSize();
|
|
|
|
|
projection = Math::Matrix4x4::Orthographic(
|
|
|
|
|
-orthoSize * aspect,
|
|
|
|
|
orthoSize * aspect,
|
|
|
|
|
-orthoSize,
|
|
|
|
|
orthoSize,
|
|
|
|
|
camera.GetNearClipPlane(),
|
|
|
|
|
camera.GetFarClipPlane());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cameraData.view = view.Transpose();
|
|
|
|
|
cameraData.projection = projection.Transpose();
|
|
|
|
|
cameraData.viewProjection = (projection * view).Transpose();
|
|
|
|
|
return cameraData;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
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,
|
|
|
|
|
std::vector<VisibleRenderItem>& visibleItems) const {
|
2026-03-26 20:43:17 +08:00
|
|
|
if (gameObject == nullptr || !gameObject->IsActiveInHierarchy()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* meshFilter = gameObject->GetComponent<Components::MeshFilterComponent>();
|
|
|
|
|
auto* meshRenderer = gameObject->GetComponent<Components::MeshRendererComponent>();
|
|
|
|
|
if (meshFilter != nullptr &&
|
|
|
|
|
meshRenderer != nullptr &&
|
|
|
|
|
meshFilter->IsEnabled() &&
|
|
|
|
|
meshRenderer->IsEnabled()) {
|
|
|
|
|
Resources::Mesh* mesh = meshFilter->GetMesh();
|
|
|
|
|
if (mesh != nullptr && mesh->IsValid()) {
|
2026-03-27 11:56:23 +08:00
|
|
|
const Math::Matrix4x4 localToWorld = gameObject->GetTransform()->GetLocalToWorldMatrix();
|
|
|
|
|
const Math::Vector3 worldPosition = localToWorld.GetTranslation();
|
|
|
|
|
const float cameraDistanceSq = (worldPosition - cameraPosition).SqrMagnitude();
|
|
|
|
|
const Containers::Array<Resources::MeshSection>& sections = mesh->GetSections();
|
|
|
|
|
|
|
|
|
|
if (!sections.Empty()) {
|
|
|
|
|
for (size_t sectionIndex = 0; sectionIndex < sections.Size(); ++sectionIndex) {
|
|
|
|
|
const Resources::MeshSection& section = sections[sectionIndex];
|
|
|
|
|
|
|
|
|
|
VisibleRenderItem visibleItem;
|
|
|
|
|
visibleItem.gameObject = gameObject;
|
|
|
|
|
visibleItem.meshFilter = meshFilter;
|
|
|
|
|
visibleItem.meshRenderer = meshRenderer;
|
|
|
|
|
visibleItem.mesh = mesh;
|
|
|
|
|
visibleItem.materialIndex = section.materialID;
|
|
|
|
|
visibleItem.sectionIndex = static_cast<Core::uint32>(sectionIndex);
|
|
|
|
|
visibleItem.hasSection = true;
|
|
|
|
|
visibleItem.material = ResolveMaterial(meshRenderer, mesh, section.materialID);
|
|
|
|
|
visibleItem.renderQueue = ResolveMaterialRenderQueue(visibleItem.material);
|
|
|
|
|
visibleItem.cameraDistanceSq = cameraDistanceSq;
|
|
|
|
|
visibleItem.localToWorld = localToWorld;
|
|
|
|
|
visibleItems.push_back(visibleItem);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
VisibleRenderItem visibleItem;
|
|
|
|
|
visibleItem.gameObject = gameObject;
|
|
|
|
|
visibleItem.meshFilter = meshFilter;
|
|
|
|
|
visibleItem.meshRenderer = meshRenderer;
|
|
|
|
|
visibleItem.mesh = mesh;
|
|
|
|
|
visibleItem.materialIndex = 0;
|
|
|
|
|
visibleItem.sectionIndex = 0;
|
|
|
|
|
visibleItem.hasSection = false;
|
|
|
|
|
visibleItem.material = ResolveMaterial(meshRenderer, mesh, 0);
|
|
|
|
|
visibleItem.renderQueue = ResolveMaterialRenderQueue(visibleItem.material);
|
|
|
|
|
visibleItem.cameraDistanceSq = cameraDistanceSq;
|
|
|
|
|
visibleItem.localToWorld = localToWorld;
|
|
|
|
|
visibleItems.push_back(visibleItem);
|
|
|
|
|
}
|
2026-03-26 20:43:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Components::GameObject* child : gameObject->GetChildren()) {
|
2026-03-27 11:56:23 +08:00
|
|
|
ExtractVisibleItems(child, cameraPosition, visibleItems);
|
2026-03-26 20:43:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Rendering
|
|
|
|
|
} // namespace XCEngine
|