Unreal RecastNavigation 开源项目详解

news/2024/10/4 5:34:18

0 前言

Recastnavigation是一个游戏AI导航库,像Unity,UE引擎中都集成了这个开源项目, HALO中使用的也是这个开源库。导航最重要的就是为NPC寻路,以及其他的寻路需求。

需要注明的是,这个寻路库虽然厉害。但是他的核心是 平面寻路。也就是重力方向一直朝着 -Y 方向。如果是星球地形,既重力方向朝向球心,在任何一点重力方向都是不同的。那么这个开源库就不能使用了。

1 Recastnavigation 下载与编译

主页: https://github.com/recastnavigation/recastnavigation/tree/main?tab=readme-ov-file
编译: https://github.com/recastnavigation/recastnavigation/blob/main/Docs/_2_BuildingAndIntegrating.md

编译细节
这个项目 Recast and Detour 不依赖其他的一些库,但是 RecastDemo 即可视化的示例是需要SDL2库的,SDL2库可以理解为创建窗口应用程序所需要的库。所以我们需要下载SDL。

同时也需要 premake5 编译工具。

1.1 Windows

  • Grab the latest SDL2 development library release from https://github.com/libsdl-org/SDL and unzip it into RecastDemo/Contrib. Rename the SDL folder such that the path RecastDemo/Contrib/SDL/lib/x86 is valid.
    • https://github.com/libsdl-org/SDL/releases

SDL2-devel-2.28.2-VC.zip 像这样的文件名的release才可以,不要直接下载 source code

// 成功后的文件夹应该是这个样子。
RecastDemo/Contribfastlzreadme-sdl.txtSDLstb_truetype.h

●Navigate to the RecastDemo folder and run premake5 vs2022
●Open Build/vs2022/recastnavigation.sln in Visual Studio 2022 or Jetbrains Rider.
●Set RecastDemo as the startup project, build, and run.

2. 代码详解

// Figure out how big the raster voxel grid will be based on the input geometry bounds.
rcCalcGridSize// Voxelize the input geometry
rcAllocHeightfield
rcCreateHeightfield
rcMarkWalkableTriangles
rcRasterizeTriangles// Clean up the voxel data and filter out non-walkable areas.
rcFilterLowHangingWalkableObstacles
rcFilterLedgeSpans
rcFilterWalkableLowHeightSpans// Consolidate the voxel data into a more compact representation
rcAllocCompactHeightfield
rcBuildCompactHeightfield// Further refine the voxel representation
rcErodeWalkableArea
rcBuildDistanceField
rcBuildRegions// Triangulate the navmesh polygons from the voxel data
rcAllocContourSet
rcBuildContours
rcAllocPolyMesh
rcBuildPolyMesh// Package the mesh with additional metadata that's useful at runtime.
rcAllocPolyMeshDetail
rcBuildPolyMeshDetail// Cleanup
rcFreeHeightField
rcFreeCompactHeightfield
rcFreeContourSet

代码详解主要为2个部分。

  1. 数据载入
  2. 高度场建立

2.1 数据载入

首先Sample_SoloMesh::handleBuild()会调用InputGeom::getMesh()

那么Mesh数据从哪里来呢?

  1. InputGeom::loadMesh()
  2. InputGeom::load()
  3. rcMeshLoaderObj::addVertex(float x, float y, float z, int& cap) 其中cap是顶点的内存容量,以存8个顶点为开始,内存短缺后以2倍速度扩大。存储皆为1维数组,顶点与顶点之间的 stride==3
  4. void rcMeshLoaderObj::addTriangle(int a, int b, int c, int& cap)

有了Mesh之后,开始计算xz平面的栅格数量,设置为 *sizeX = &m_cfg.width, *sizeZ = &m_cfg.height。平面边缘,只要占到一半以上的cellSize,就认为是一个cell。

void rcCalcGridSize(const float* minBounds, const float* maxBounds, const float cellSize, int* sizeX, int* sizeZ)
{	// (0, 0.5) + 0.5  -> 0// [0.5, 0.999) + 0.5 -> 1*sizeX = (int)((maxBounds[0] - minBounds[0]) / cellSize + 0.5f);*sizeZ = (int)((maxBounds[2] - minBounds[2]) / cellSize + 0.5f);
}

rcCreateHeightfield(m_ctx, *m_solid, m_cfg.width, m_cfg.height, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch)相当于构造函数,初始化高度场信息HeightField or HeightMap

m_triareas = new unsigned char[ntris];角色代理可以走的三角形。

rcMarkWalkableTriangles(m_ctx, m_cfg.walkableSlopeAngle, verts, nverts, tris, ntris, m_triareas);这个的关键就是三角形的法向量的 y 分量,等于三角形面与 x-z 平面的夹角 cos 值。运用相似三角形。

2.2 高度场建立

主要就是利用x-z平面的网格与映射到平面上的三角形的交点,通过相似三角形获得这个交点 (x-z)-> Y在三维三角形的高度。

这个切割方法是 Sutherland-Hodgman polygon-clipping algorithm 的变种。

Sutherland-Hodgman polygon-clipping algorithm

  • 这个算法最重要的就是判断顶点在 切割窗口某一条 Edge 两侧的哪一侧。
  • Edge存在切割窗口的那一侧是 in, 不存在则是 out.
    https://stackoverflow.com/questions/21638509/determine-voxels-that-a-triangle-is-in
    Abhishek Sharma : https://www.youtube.com/watch?v=OGW9Cqr5RRg&t=333s&pp=ygUTIFN1dGhlcmxhbmQtSG9kZ21hbg%3D%3D

2.2.1 高度场建立的核心函数。

bool rcRasterizeTriangles(rcContext* context,const float* verts, const int /*nv*/,const int* tris, const unsigned char* triAreaIDs, const int numTris,rcHeightfield& heightfield, const int flagMergeThreshold)
{rcAssert(context != NULL);rcScopedTimer timer(context, RC_TIMER_RASTERIZE_TRIANGLES);// Rasterize the triangles.const float inverseCellSize = 1.0f / heightfield.cs;const float inverseCellHeight = 1.0f / heightfield.ch;for (int triIndex = 0; triIndex < numTris; ++triIndex){const float* v0 = &verts[tris[triIndex * 3 + 0] * 3];const float* v1 = &verts[tris[triIndex * 3 + 1] * 3];const float* v2 = &verts[tris[triIndex * 3 + 2] * 3];if (!rasterizeTri(v0, v1, v2, triAreaIDs[triIndex], heightfield, heightfield.bmin, heightfield.bmax, heightfield.cs, inverseCellSize, inverseCellHeight, flagMergeThreshold)){context->log(RC_LOG_ERROR, "rcRasterizeTriangles: Out of memory.");return false;}}return true;
}

2.2.2 图解

每个 cell 上面是一个 spanList。 他存储着所有在这个cell上面的 所有 三角形在这个cell上的高度信息。

image

X. Ref

  1. A* : https://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html#heuristics-for-grid-maps
  2. UE DOC: https://www.unrealdoc.com/p/navigation-mesh

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/44359.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

编写单个函数的ROP链

什么是ROP链 在我初识栈溢出那篇博客已经详细的讲了函数的调用过程(基于X86框架),不了解的可以看一下,没有这个理论基础,是学不好ROP的。现在我们说一下什么是ROP。 ROP链就是通过返回地址的修改来完成的编程,调用特定的函数的一种编程模式。我们可以联想一下你做的最简单…

计组要我命第五天中

要命计组(6) 开始时间 2024-06-1612:38:07 结束时间 2024-06-16 15:01:11 前言:吃完饭,睡会觉,继续计组[fine]. 总结:中场休息!

聊聊育种大模型

从去年年末开始到现在,大语言模型(Large Language Models,LLM)热度依旧不减。 有实力烧钱的机构在训练自己的LLM,没实力想凑热闹的更聚焦在垂直领域的应用上,绝大多数企业属于后者。 每个行业多多少都有人在做基础模型的微调和打造私有知识库,以期不被时代抛弃。 医疗、…

MyBatis 的缓存机制

1. MyBatis 的缓存机制 @目录1. MyBatis 的缓存机制2. 准备工作3. MyBatis 的一级缓存3.1 一级缓存失效情况/条件4. MyBatis 的二级缓存5. MyBatis 集成 EhCache 第三方缓存6. 总结:7. 最后:缓存(Cache) 缓存的作用:通过减少 IO 的方式,来提高程序的执行效率 。 MyBatis …

KAN:使用 Microsoft 的 KubeAI Application Nucleus简化边缘 AI

我们需要的是在 Kubernetes 上构建和管理边缘机器学习应用程序的一致方法,一种可以加快开发和交付速度的方法。这就是 KAN 的作用,即 KubeAI Application Nexus。正如介绍性博客文章所指出的那样,这个名字来自一个普通话动词,翻译为“观看”或“看”。KAN 是一个开源项目,…

word中批量修改mathtype公式格式

1、打开一个公式,设置好格式、字体大小等 2、预置/公式预置/保持到文佳,假设保存文件命名为measurement.eqp 3、打开word,mathtype/格式化公式,选择保存的measurement.eqp文件,确定即可

lnmp

一、编译安装Nginx 1.前期准备#安装依赖 yum -y install pcre-devel zlib-devel gcc gcc-c++ make#创建用户 useradd -M -s /sbin/nologin nginx#获取源码包 wget https://nginx.org/download/nginx-1.24.0.tar.gzcd /opt tar xf nginx-1.24.0.tar.gz #解压2.开始编译安装cd …

手把手教你改造 Sentinel Dashboard 实现配置持久化

一. 概述 Sentinel客户端默认情况下接收到 Dashboard 推送的规则配置后,可以实时生效。但是有一个致命缺陷,Dashboard和业务服务并没有持久化这些配置,当业务服务重启后,这些规则配置将全部丢失。 Sentinel 提供两种方式修改规则:通过 API 直接修改 (loadRules) 通过 Data…