NoJobEngine 2.0.7
Loading...
Searching...
No Matches
NavMeshGeometryExtractor.cpp
Go to the documentation of this file.
2
7
8#include <cstdint>
9#include <filesystem>
10
11#include <assimp/Importer.hpp>
12#include <assimp/postprocess.h>
13#include <assimp/scene.h>
14
15#include <glm/glm.hpp>
16
17namespace NoJob
18{
20 Scene& scene)
21 {
22 NavMeshSourceGeometry geometry;
23
24 for (Entity entity : scene.GetEntities())
25 {
26 if (!entity.HasComponent<MeshComponent>())
27 continue;
28
29 const auto& meshComponent =
30 entity.GetComponent<MeshComponent>();
31
32 if (!meshComponent.MeshAsset)
33 continue;
34
35 const std::filesystem::path meshPath =
37 meshComponent.MeshAsset);
38
39 // Procedural meshes currently have no source asset.
40 // They are intentionally ignored by the first navigation
41 // geometry extraction implementation.
42 if (meshPath.empty())
43 continue;
44
45 const std::filesystem::path absolutePath =
47
48 Assimp::Importer importer;
49
50 const aiScene* importedScene =
51 importer.ReadFile(
52 absolutePath.string(),
53 aiProcess_Triangulate |
54 aiProcess_JoinIdenticalVertices |
55 aiProcess_SortByPType);
56
57 if (!importedScene ||
58 !importedScene->HasMeshes())
59 {
60 continue;
61 }
62
63 const glm::mat4 worldTransform =
64 scene.GetWorldTransform(entity);
65
66 for (unsigned int meshIndex = 0;
67 meshIndex < importedScene->mNumMeshes;
68 ++meshIndex)
69 {
70 const aiMesh* sourceMesh =
71 importedScene->mMeshes[meshIndex];
72
73 if (!sourceMesh)
74 continue;
75
76 const std::uint32_t baseVertex =
77 static_cast<std::uint32_t>(
78 geometry.Vertices.size());
79
80 for (unsigned int vertexIndex = 0;
81 vertexIndex < sourceMesh->mNumVertices;
82 ++vertexIndex)
83 {
84 const aiVector3D& sourcePosition =
85 sourceMesh->mVertices[vertexIndex];
86
87 const glm::vec4 localPosition(
88 sourcePosition.x,
89 sourcePosition.y,
90 sourcePosition.z,
91 1.0f);
92
93 const glm::vec4 worldPosition =
94 worldTransform * localPosition;
95
96 geometry.Vertices.emplace_back(
97 worldPosition.x,
98 worldPosition.y,
99 worldPosition.z);
100 }
101
102 for (unsigned int faceIndex = 0;
103 faceIndex < sourceMesh->mNumFaces;
104 ++faceIndex)
105 {
106 const aiFace& face =
107 sourceMesh->mFaces[faceIndex];
108
109 if (face.mNumIndices != 3)
110 continue;
111
112 geometry.Indices.push_back(
113 baseVertex +
114 face.mIndices[0]);
115
116 geometry.Indices.push_back(
117 baseVertex +
118 face.mIndices[1]);
119
120 geometry.Indices.push_back(
121 baseVertex +
122 face.mIndices[2]);
123 }
124 }
125 }
126
127 return geometry;
128 }
129}
AssetManager declarations for the Asset subsystem.
Components declarations for the Scenes and Native Scripting subsystem.
Mesh declarations for the Rendering subsystem.
NavMeshGeometryExtractor declarations for the Navigation subsystem.
Scene declarations for the Scenes and Native Scripting subsystem.
static std::filesystem::path GetMeshPath(const std::shared_ptr< Mesh > &mesh)
static std::filesystem::path ResolveProjectPath(const std::filesystem::path &path)
static NavMeshSourceGeometry Extract(Scene &scene)
std::vector< Entity > GetEntities()
Definition Scene.cpp:231
glm::mat4 GetWorldTransform(Entity entity) const
Definition Scene.cpp:1736
std::vector< glm::vec3 > Vertices
std::vector< uint32_t > Indices