144 lines
5.2 KiB
C++
144 lines
5.2 KiB
C++
|
|
#include "Rendering/RenderSceneUtility.h"
|
||
|
|
|
||
|
|
#include "Components/CameraComponent.h"
|
||
|
|
#include "Components/GameObject.h"
|
||
|
|
#include "Components/MeshFilterComponent.h"
|
||
|
|
#include "Components/MeshRendererComponent.h"
|
||
|
|
#include "Components/TransformComponent.h"
|
||
|
|
#include "Rendering/RenderMaterialUtility.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);
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace Rendering
|
||
|
|
} // namespace XCEngine
|