Add mesh bounds metadata
This commit is contained in:
@@ -16,6 +16,7 @@ void Mesh::Release() {
|
||||
m_attributes = VertexAttribute::Position;
|
||||
m_indexCount = 0;
|
||||
m_use32BitIndex = false;
|
||||
m_bounds = Math::Bounds();
|
||||
UpdateMemorySize();
|
||||
SetInvalid();
|
||||
}
|
||||
|
||||
@@ -20,8 +20,31 @@ struct ImportedMeshData {
|
||||
std::vector<StaticMeshVertex> vertices;
|
||||
std::vector<Core::uint32> indices;
|
||||
VertexAttribute attributes = VertexAttribute::Position;
|
||||
Math::Bounds bounds;
|
||||
};
|
||||
|
||||
Math::Bounds ComputeBounds(const std::vector<StaticMeshVertex>& vertices) {
|
||||
if (vertices.empty()) {
|
||||
return Math::Bounds();
|
||||
}
|
||||
|
||||
Math::Vector3 min = vertices.front().position;
|
||||
Math::Vector3 max = vertices.front().position;
|
||||
|
||||
for (const StaticMeshVertex& vertex : vertices) {
|
||||
min.x = std::min(min.x, vertex.position.x);
|
||||
min.y = std::min(min.y, vertex.position.y);
|
||||
min.z = std::min(min.z, vertex.position.z);
|
||||
max.x = std::max(max.x, vertex.position.x);
|
||||
max.y = std::max(max.y, vertex.position.y);
|
||||
max.z = std::max(max.z, vertex.position.z);
|
||||
}
|
||||
|
||||
Math::Bounds bounds;
|
||||
bounds.SetMinMax(min, max);
|
||||
return bounds;
|
||||
}
|
||||
|
||||
Math::Matrix4 ConvertAssimpMatrix(const aiMatrix4x4& matrix) {
|
||||
Math::Matrix4 result;
|
||||
result.m[0][0] = matrix.a1; result.m[0][1] = matrix.a2; result.m[0][2] = matrix.a3; result.m[0][3] = matrix.a4;
|
||||
@@ -129,6 +152,7 @@ ImportedMeshData ImportSingleMesh(const aiMesh& mesh,
|
||||
}
|
||||
|
||||
result.attributes = attributes;
|
||||
result.bounds = ComputeBounds(result.vertices);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -169,6 +193,7 @@ void ProcessNode(const aiNode& node,
|
||||
section.startIndex = startIndex;
|
||||
section.indexCount = static_cast<Core::uint32>(meshData.indices.size());
|
||||
section.materialID = mesh->mMaterialIndex;
|
||||
section.bounds = meshData.bounds;
|
||||
sections.PushBack(section);
|
||||
|
||||
attributes = attributes | meshData.attributes;
|
||||
@@ -287,6 +312,7 @@ LoadResult MeshLoader::Load(const Containers::String& path, const ImportSettings
|
||||
for (const MeshSection& section : sections) {
|
||||
mesh->AddSection(section);
|
||||
}
|
||||
mesh->SetBounds(ComputeBounds(vertices));
|
||||
|
||||
return LoadResult(mesh);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user