NoJobEngine 2.0.7
Loading...
Searching...
No Matches
NavMeshVoxelizer.cpp
Go to the documentation of this file.
4#include <glm/gtc/matrix_inverse.hpp>
5#include <algorithm>
6#include <cmath>
7#include <limits>
8#include <vector>
9
10namespace NoJob {
11namespace {
12struct Box { glm::mat4 world, inverse; glm::vec3 half; float bottom, top; bool floor; };
13struct Tri { glm::vec3 a,b,c; float slope; };
14float cross2(float ax,float az,float bx,float bz) { return ax*bz-az*bx; }
15bool heightAt(const Tri& t,float x,float z,float& y) {
16 float d=cross2(t.b.x-t.a.x,t.b.z-t.a.z,t.c.x-t.a.x,t.c.z-t.a.z);
17 if(std::abs(d)<1e-7f) return false;
18 float u=cross2(x-t.a.x,z-t.a.z,t.c.x-t.a.x,t.c.z-t.a.z)/d;
19 float v=cross2(t.b.x-t.a.x,t.b.z-t.a.z,x-t.a.x,z-t.a.z)/d;
20 if(u < -1e-4f || v < -1e-4f || u+v > 1.0001f) return false;
21 y=t.a.y+u*(t.b.y-t.a.y)+v*(t.c.y-t.a.y); return true;
22}
23}
25 float cellSize,float agentRadius,float agentHeight,float maxSlopeAngle) {
27 cellSize=std::max(cellSize,0.1f); agentRadius=std::max(agentRadius,0.0f);
28 agentHeight=std::max(agentHeight,0.1f);
29 std::vector<Tri> triangles;
30 float minX=std::numeric_limits<float>::max(), minZ=minX;
31 float maxX=-minX,maxZ=-minX;
32 auto bounds=[&](const glm::vec3& p){minX=std::min(minX,p.x);maxX=std::max(maxX,p.x);minZ=std::min(minZ,p.z);maxZ=std::max(maxZ,p.z);};
33 for(size_t i=0;i+2<source.Indices.size();i+=3) {
34 const auto a=source.Indices[i],b=source.Indices[i+1],c=source.Indices[i+2];
35 if(a>=source.Vertices.size()||b>=source.Vertices.size()||c>=source.Vertices.size())continue;
36 Tri t{source.Vertices[a],source.Vertices[b],source.Vertices[c],0};
37 glm::vec3 n=glm::cross(t.b-t.a,t.c-t.a);
38 if(glm::length(n)<1e-6f)continue;
39 n=glm::normalize(n);
40 t.slope=glm::degrees(std::acos(glm::clamp(n.y,-1.0f,1.0f)));
41 // Imported meshes may use reversed winding; accept upward-facing geometry only.
42 if(t.slope>maxSlopeAngle)continue;
43 triangles.push_back(t);bounds(t.a);bounds(t.b);bounds(t.c);
44 }
45 std::vector<Box> boxes;
46 for(Entity e:scene.GetEntities()) {
47 if(!e.HasComponent<BoxColliderComponent>())continue;
48 const auto& collider=e.GetComponent<BoxColliderComponent>();
49 if(collider.IsTrigger)continue;
50 if(e.HasComponent<RigidbodyComponent>() &&
51 e.GetComponent<RigidbodyComponent>().Type!=RigidbodyType::Static)continue;
52 const glm::mat4 w=scene.GetWorldTransform(e);
53 if(std::abs(glm::determinant(w))<1e-7f)continue;
54 const glm::vec3 half=glm::max(collider.Size*0.5f,glm::vec3(0.001f));
55 float bottom=std::numeric_limits<float>::max(),top=-bottom;
56 glm::vec3 lo(std::numeric_limits<float>::max()),hi(-std::numeric_limits<float>::max());
57 for(int ix=-1;ix<=1;ix+=2)for(int iy=-1;iy<=1;iy+=2)for(int iz=-1;iz<=1;iz+=2){
58 glm::vec3 p=glm::vec3(w*glm::vec4(half*glm::vec3(ix,iy,iz),1));
59 lo=glm::min(lo,p);hi=glm::max(hi,p);bottom=std::min(bottom,p.y);top=std::max(top,p.y);
60 }
61 // Thin, nearly horizontal boxes are walkable floors; tall boxes are obstacles.
62 glm::vec3 up=glm::normalize(glm::vec3(w*glm::vec4(0,1,0,0)));
63 bool floor=std::abs(up.y)>0.98f && (hi.y-lo.y)<=std::max(0.35f, std::min(hi.x-lo.x,hi.z-lo.z)*0.25f);
64 boxes.push_back({w,glm::inverse(w),half,bottom,top,floor});
65 if(floor){bounds(lo);bounds(hi);}
66 }
67 if(minX>maxX||minZ>maxZ)return result;
68 const int nx=static_cast<int>(std::ceil((maxX-minX)/cellSize));
69 const int nz=static_cast<int>(std::ceil((maxZ-minZ)/cellSize));
70 if(nx<=0||nz<=0||static_cast<long long>(nx)*nz>250000)return result;
71 std::vector<float> heights(static_cast<size_t>(nx)*nz,0);
72 std::vector<unsigned char> walk(static_cast<size_t>(nx)*nz,0);
73 auto index=[&](int x,int z){return static_cast<size_t>(z)*nx+x;};
74 auto floorHeight=[&](float x,float z,float& y){
75 bool found=false; y=-std::numeric_limits<float>::max();
76 for(const auto& t:triangles){float h;if(heightAt(t,x,z,h) && (!found||h>y)){y=h;found=true;}}
77 for(const auto& b:boxes)if(b.floor){
78 glm::vec3 local=glm::vec3(b.inverse*glm::vec4(x,b.top,z,1));
79 if(std::abs(local.x)<=b.half.x+1e-4f && std::abs(local.z)<=b.half.z+1e-4f && (!found||b.top>y)){
80 y=b.top;found=true;
81 }
82 }
83 return found;
84 };
85 auto blocked=[&](float x,float y,float z){
86 const float r=agentRadius;
87 const float offsets[3]={-r,0.0f,r};
88 for(const auto& b:boxes)if(!b.floor && b.top>y+0.05f && b.bottom<y+agentHeight){
89 for(float dx:offsets)for(float dz:offsets){
90 glm::vec3 local=glm::vec3(b.inverse*glm::vec4(x+dx,(std::max(y,b.bottom)+std::min(y+agentHeight,b.top))*0.5f,z+dz,1));
91 if(glm::all(glm::lessThanEqual(glm::abs(local),b.half+glm::vec3(1e-4f))))return true;
92 }
93 }
94 return false;
95 };
96 for(int z=0;z<nz;++z)for(int x=0;x<nx;++x){
97 float px=minX+(x+0.5f)*cellSize,pz=minZ+(z+0.5f)*cellSize,h;
98 if(!floorHeight(px,pz,h)||blocked(px,h,pz))continue;
99 // Ensure cell corners have ground at roughly the same elevation.
100 bool valid=true;
101 for(float dx:{-0.49f,0.49f})for(float dz:{-0.49f,0.49f}){
102 float cornerH;
103 if(!floorHeight(px+dx*cellSize,pz+dz*cellSize,cornerH)||std::abs(cornerH-h)>cellSize*0.6f)
104 valid=false;
105 }
106 if(valid){walk[index(x,z)]=1;heights[index(x,z)]=h;}
107 }
108 // Shared vertex IDs allow BuildAdjacency to connect adjacent cells.
109 std::vector<int> vertices(static_cast<size_t>(nx+1)*(nz+1),-1);
110 auto vertex=[&](int x,int z,float y)->uint32_t{
111 size_t k=static_cast<size_t>(z)*(nx+1)+x;
112 if(vertices[k]<0){vertices[k]=static_cast<int>(result.Vertices.size());
113 result.Vertices.emplace_back(minX+x*cellSize,y,minZ+z*cellSize);}
114 return static_cast<uint32_t>(vertices[k]);
115 };
116 for(int z=0;z<nz;++z)for(int x=0;x<nx;++x)if(walk[index(x,z)]){
117 float h=heights[index(x,z)];
118 // Adjacent cells of differing heights need separate vertices; do not bridge steep steps.
119 bool compatible=true;
120 for(int dz=-1;dz<=1;++dz)for(int dx=-1;dx<=1;++dx){
121 int xx=x+dx,zz=z+dz;
122 if(xx>=0&&zz>=0&&xx<nx&&zz<nz&&walk[index(xx,zz)] &&
123 std::abs(heights[index(xx,zz)]-h)>0.05f)compatible=false;
124 }
125 if(!compatible)continue;
126 uint32_t a=vertex(x,z,h),b=vertex(x+1,z,h),c=vertex(x+1,z+1,h),d=vertex(x,z+1,h);
127 result.Indices.insert(result.Indices.end(),{a,d,b,b,d,c});
128 }
129 return result;
130}
131}
Components declarations for the Scenes and Native Scripting subsystem.
NavMeshVoxelizer declarations for the Navigation subsystem.
Scene declarations for the Scenes and Native Scripting subsystem.
static NavMeshSourceGeometry Rasterize(Scene &scene, const NavMeshSourceGeometry &source, float cellSize, float agentRadius, float agentHeight, float maxSlopeAngle)
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