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) {
37 glm::vec3 n=glm::cross(t.b-t.a,t.c-t.a);
38 if(glm::length(n)<1e-6f)
continue;
40 t.slope=glm::degrees(std::acos(glm::clamp(n.y,-1.0f,1.0f)));
42 if(t.slope>maxSlopeAngle)
continue;
43 triangles.push_back(t);bounds(t.a);bounds(t.b);bounds(t.c);
45 std::vector<Box> boxes;
49 if(collider.IsTrigger)
continue;
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);
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);}
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)){
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;
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;
101 for(
float dx:{-0.49f,0.49f})
for(
float dz:{-0.49f,0.49f}){
103 if(!floorHeight(px+dx*cellSize,pz+dz*cellSize,cornerH)||std::abs(cornerH-h)>cellSize*0.6f)
106 if(valid){walk[index(x,z)]=1;heights[index(x,z)]=h;}
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]);
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)];
119 bool compatible=
true;
120 for(
int dz=-1;dz<=1;++dz)
for(
int dx=-1;dx<=1;++dx){
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;
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);