Group rendering extraction files
This commit is contained in:
231
engine/src/Rendering/Extraction/RenderSceneUtility.cpp
Normal file
231
engine/src/Rendering/Extraction/RenderSceneUtility.cpp
Normal file
@@ -0,0 +1,231 @@
|
||||
#include "Rendering/Extraction/RenderSceneUtility.h"
|
||||
|
||||
#include "Components/CameraComponent.h"
|
||||
#include "Components/GameObject.h"
|
||||
#include "Components/LightComponent.h"
|
||||
#include "Components/MeshFilterComponent.h"
|
||||
#include "Components/MeshRendererComponent.h"
|
||||
#include "Components/TransformComponent.h"
|
||||
#include "Rendering/RenderMaterialResolve.h"
|
||||
#include "Scene/Scene.h"
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Rendering {
|
||||
|
||||
RenderCameraData BuildRenderCameraData(
|
||||
const Components::CameraComponent& camera,
|
||||
uint32_t viewportWidth,
|
||||
uint32_t viewportHeight) {
|
||||
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;
|
||||
}
|
||||
|
||||
void AppendRenderItemsForGameObject(
|
||||
Components::GameObject& gameObject,
|
||||
const Math::Vector3& cameraPosition,
|
||||
std::vector<VisibleRenderItem>& outVisibleItems) {
|
||||
if (!gameObject.IsActiveInHierarchy()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto* meshFilter = gameObject.GetComponent<Components::MeshFilterComponent>();
|
||||
auto* meshRenderer = gameObject.GetComponent<Components::MeshRendererComponent>();
|
||||
if (meshFilter == nullptr ||
|
||||
meshRenderer == nullptr ||
|
||||
!meshFilter->IsEnabled() ||
|
||||
!meshRenderer->IsEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Resources::Mesh* mesh = meshFilter->GetMesh();
|
||||
if (mesh == nullptr || !mesh->IsValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
outVisibleItems.push_back(visibleItem);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
outVisibleItems.push_back(visibleItem);
|
||||
}
|
||||
|
||||
bool CompareVisibleRenderItemsStable(
|
||||
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;
|
||||
}
|
||||
|
||||
const Core::uint64 lhsObjectId = lhs.gameObject != nullptr ? lhs.gameObject->GetID() : 0u;
|
||||
const Core::uint64 rhsObjectId = rhs.gameObject != nullptr ? rhs.gameObject->GetID() : 0u;
|
||||
if (lhsObjectId != rhsObjectId) {
|
||||
return lhsObjectId < rhsObjectId;
|
||||
}
|
||||
|
||||
if (lhs.hasSection != rhs.hasSection) {
|
||||
return lhs.hasSection && !rhs.hasSection;
|
||||
}
|
||||
|
||||
if (lhs.sectionIndex != rhs.sectionIndex) {
|
||||
return lhs.sectionIndex < rhs.sectionIndex;
|
||||
}
|
||||
|
||||
if (lhs.materialIndex != rhs.materialIndex) {
|
||||
return lhs.materialIndex < rhs.materialIndex;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<VisibleRenderItem> CollectRenderItemsForEntityIds(
|
||||
const Components::Scene& scene,
|
||||
const std::vector<uint64_t>& entityIds,
|
||||
const Math::Vector3& cameraPosition) {
|
||||
std::vector<VisibleRenderItem> visibleItems;
|
||||
std::unordered_set<uint64_t> visitedEntityIds;
|
||||
visitedEntityIds.reserve(entityIds.size());
|
||||
|
||||
for (uint64_t entityId : entityIds) {
|
||||
if (entityId == 0 || !visitedEntityIds.insert(entityId).second) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Components::GameObject* gameObject = scene.FindByID(entityId);
|
||||
if (gameObject == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
AppendRenderItemsForGameObject(*gameObject, cameraPosition, visibleItems);
|
||||
}
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user