NoJobEngine
2.0.7
Toggle main menu visibility
Loading...
Searching...
No Matches
NavMeshGeometryExtractor.cpp
Go to the documentation of this file.
1
#include "
Engine/AI/Navigation/NavMeshGeometryExtractor.h
"
2
3
#include "
Engine/Asset/AssetManager.h
"
4
#include "
Engine/Renderer/Mesh.h
"
5
#include "
Engine/Scene/Components.h
"
6
#include "
Engine/Scene/Scene.h
"
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
17
namespace
NoJob
18
{
19
NavMeshSourceGeometry
NavMeshGeometryExtractor::Extract
(
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 =
36
AssetManager::GetMeshPath
(
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 =
46
AssetManager::ResolveProjectPath
(meshPath);
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.h
AssetManager declarations for the Asset subsystem.
Components.h
Components declarations for the Scenes and Native Scripting subsystem.
Mesh.h
Mesh declarations for the Rendering subsystem.
NavMeshGeometryExtractor.h
NavMeshGeometryExtractor declarations for the Navigation subsystem.
Scene.h
Scene declarations for the Scenes and Native Scripting subsystem.
NoJob::AssetManager::GetMeshPath
static std::filesystem::path GetMeshPath(const std::shared_ptr< Mesh > &mesh)
Definition
AssetManager.cpp:617
NoJob::AssetManager::ResolveProjectPath
static std::filesystem::path ResolveProjectPath(const std::filesystem::path &path)
Definition
AssetManager.cpp:60
NoJob::Entity
Definition
Entity.h:16
NoJob::NavMeshGeometryExtractor::Extract
static NavMeshSourceGeometry Extract(Scene &scene)
Definition
NavMeshGeometryExtractor.cpp:19
NoJob::Scene
Definition
Scene.h:31
NoJob::Scene::GetEntities
std::vector< Entity > GetEntities()
Definition
Scene.cpp:231
NoJob::Scene::GetWorldTransform
glm::mat4 GetWorldTransform(Entity entity) const
Definition
Scene.cpp:1736
NoJob
Definition
BTActionNode.h:13
NoJob::MeshComponent
Definition
Components.h:340
NoJob::NavMeshSourceGeometry
Definition
NavMeshGenerator.h:32
NoJob::NavMeshSourceGeometry::Vertices
std::vector< glm::vec3 > Vertices
Definition
NavMeshGenerator.h:33
NoJob::NavMeshSourceGeometry::Indices
std::vector< uint32_t > Indices
Definition
NavMeshGenerator.h:34
Engine
AI
Navigation
NavMeshGeometryExtractor.cpp
Generated by
1.18.0