做不好的网站违法吗,个人可以采集视频做网站吗,wordpress linux 中文,网站服务器有哪些类型有哪些类型有哪些类型有哪些类型有哪些前言因为Unity版本的更新迭代#xff0c;老版本的A*插件在新版本Unity已经无法正常使用#xff0c;包括一些运行时代码也已经过时#xff0c;重新接入要花费很多时间#xff0c;干脆接入一个新的寻路方案吧。这里选择的是久负盛名的https://github.com/recastnavigation/re…前言因为Unity版本的更新迭代老版本的A*插件在新版本Unity已经无法正常使用包括一些运行时代码也已经过时重新接入要花费很多时间干脆接入一个新的寻路方案吧。这里选择的是久负盛名的https://github.com/recastnavigation/recastnavigation但因为他是基于C的所以我们要使用C#的P/Invoke来调用它的dll来实现寻路。由于篇幅与操作复杂的原因本文会更加注重大体的工作流程而不会有太多细节上的图片但是会有一个配套的详细教学视频供大家学习视频链接接入C版本recastnavigation寻路库到Unity/服务端中_哔哩哔哩 (゜-゜)つロ 干杯~-bilibiliwww.bilibili.comC/C#的桥接源码也会以在码云开源的形式分享给大家完整的示例可以在我的Moba项目 https://gitee.com/NKG_admin/NKGMobaBasedOnET 中看到。通过本文和配套视频你将能学习到recastnavigation的大体设计思路使用方式Unity/服务器接入recastnavigation的完整流程。感谢footman大佬在我学习过程中给予的支持本文中部分内容也来自大佬的文章。前置内容Premake认识https://blog.csdn.net/wuguyannian/article/details/92175725SDL认识https://baike.baidu.com/item/SDL/224181?fraladdinP/Invoke认识https://zhuanlan.zhihu.com/p/30746354环境VS 2019以及完整的C编译环境Rider For Unreal Engine 2020.2下面简称RiderUnity 2019.4.8 lts.Net Core 2.2recastnavigation masterhttps://github.com/recastnavigation/recastnavigation/commit/9337e124182697de93acb656ef25766486738807正文下载并运行recastnavigation Demo先将下载的SDL库放到recastnavigation-masterRecastDemoContrib并且需要改名为SDL应该得到如下目录recastnavigation-masterRecastDemoContribSDLlibx64然后将下载premake.exe放入recastnavigation-masterRecastDemo然后通过命令行控制premake编译recastnavigation为sln工程 C:UsersAdministratorf:F:cd F:Downloadrecastnavigation-masterrecastnavigation-masterRecastDemoF:Downloadrecastnavigation-masterrecastnavigation-masterRecastDemopremake5 vs2019Building configurations...Running action vs2019...Generated Build/vs2019/recastnavigation.sln...Generated Build/vs2019/DebugUtils.vcxproj...Generated Build/vs2019/DebugUtils.vcxproj.filters...Generated Build/vs2019/Detour.vcxproj...Generated Build/vs2019/Detour.vcxproj.filters...Generated Build/vs2019/DetourCrowd.vcxproj...Generated Build/vs2019/DetourCrowd.vcxproj.filters...Generated Build/vs2019/DetourTileCache.vcxproj...Generated Build/vs2019/DetourTileCache.vcxproj.filters...Generated Build/vs2019/Recast.vcxproj...Generated Build/vs2019/Recast.vcxproj.filters...Generated Build/vs2019/RecastDemo.vcxproj...Generated Build/vs2019/RecastDemo.vcxproj.user...Generated Build/vs2019/RecastDemo.vcxproj.filters...Generated Build/vs2019/Tests.vcxproj...Generated Build/vs2019/Tests.vcxproj.user...Generated Build/vs2019/Tests.vcxproj.filters...Done (150ms).然后目录中会生成一个Build文件夹里面是我们编译出来的sln工程recastnavigation-masterRecastDemoBuildvs2019recastnavigation.sln我们用Rider打开新生成的sln文件不出意外的话我们直接就可以构建并运行RecastDemo工程也就是我们在它的Github首页看到的逼格超高的图片好了杯就先装到这后面会详细介绍UI内容构建用于P/Invoke的dll让我们新建一个名为RecastNavDll的工程包含CRecastHelper.cpp/h和RecastDll.cpp/h这四个文件并编写其逻辑主要是逻辑封装和为P/Invoke做准备。然后添加项目Detour和Recast的引用。最后编辑解决方案属性准备将我们的RecastNavDll工程导出为x64的dll提前测试如果按照我给出的源码来编译的话不会出现逻辑问题但如果有魔改的需要可能就需要在C这边快速测试所以还会提供一个测试工程MyTest其中包含MyTest.cpp/h和tools.cpp/h直接编译并且用终端装载命令行即可进行测试Unity客户端/.Net Core服务端接入如果是Unity接入recast很简单直接将我们导出的RecastNavDll.dll放入Plugins目录下即可。如果是服务端我们需要新建子工程并且添加RecastNavDll.dll链接并且将csproj文件中CopyToOutputDirectory设置为Always。 ItemGroupNone IncludeF:Downloadrecastnavigation-masterrecastnavigation-masterRecastDemoBuildvs2019x64ReleaseRecastNavDll.dllLinkRecastNavDll.dll/LinkCopyToOutputDirectoryAlways/CopyToOutputDirectory/None/ItemGroupdll导入之后我们就可以在C#这边搭桥了我们通过一个RecastInterface.cs文件作为P/Invoke的桥梁recastnavigation工作流程我们可以通过官方自带的RecastDemo来跟踪源码得知工作方式初始化Recast引擎——recast_init加载地图——recast_loadmap(int id, const char* path)id为地图的id因为我们某些游戏中可能会有多个地图寻路实例例如Moba游戏每一场游戏中的地图寻路都是独立的需要id来区分path就是寻路数据的完整路径包含文件名这个寻路数据我们可以通过RecastDemo来得到寻路——recast_findpath(int id, const float* spos, const float* epos)寻路的结果其实只是返回从起点到终点之间所有经过的凸多边形的序号id为地图idspos为起始点epos为中点我们可以把它们理解为C#中的Vector3计算实际路径——recast_smooth(int id, float step_size, float slop)计算平滑路径其实是根据findpath得到的【从起点到终点所经过的凸多边形的序号】得到真正的路径三维坐标所以这一步是不可缺少的得到凸多边形id序列——recast_getpathpoly(int id)得到pathfind以后从起点到终点所经过的所有凸多边形id的序列得到寻路路径坐标序列——recast_getpathsmooth(int id)得到smooth以后路线的三维坐标序列释放地图——recast_freemap(int id)游戏结束后记得释放地图寻路数据资源嗷释放Recast引擎——recast_fini()如果我们在客户端使用游戏流程结束要使用这个释放Recast引擎性能测试大家一定很关心recastnavigation库的性能如何我做了个压测以这张地图为例进行随机寻路包括合法点和非法点 public static void BenchmarkSample(){BenchmarkHelper.Profile(寻路100000次, BenchmarkRecast, 100000);}private static void BenchmarkRecast(){if (RecastInterface.FindPath(100,new System.Numerics.Vector3(-RandomHelper.RandomNumber(2, 50) - RandomHelper.RandFloat(),RandomHelper.RandomNumber(-1, 5) RandomHelper.RandFloat(), RandomHelper.RandomNumber(3, 20) RandomHelper.RandFloat()),new System.Numerics.Vector3(-RandomHelper.RandomNumber(2, 50) - RandomHelper.RandFloat(),RandomHelper.RandomNumber(-1, 5) RandomHelper.RandFloat(), RandomHelper.RandomNumber(3, 20) RandomHelper.RandFloat()))){RecastInterface.Smooth(100, 2f, 0.5f);{int smoothCount 0;float[] smooths RecastInterface.GetPathSmooth(100, out smoothCount);ListVector3 results new ListVector3();for (int i 0; i smoothCount; i){Vector3 node new Vector3(smooths[i * 3], smooths[i * 3 1], smooths[i * 3 2]);results.Add(node);}}}}得到的结果是寻路次数耗时1001.4293ms10000152.3692ms1000001365.5992ms完全可以满足服务器寻路需求另外这还是单线程寻路可以自己做多线程优化ET框架接入recastnavigation我们要替换服务端的寻路组件最后只需要RecastPathComponent和RecastPathProcessor两个文件即可完成寻路。完整工作流我们要先在Unity中使用NavMesh烘焙然后使用NavMesh/Export Scene工具导出obj文件然后在RecastDemo中进行读取并烘焙Sample-Solo MeshInput Mesh-我们导出的obj文件需要放到recastnavigation-masterRecastDemoBinMeshes目录下调制参数Build Save保存为nav文件会导出为recastnavigation-masterRecastDemoBinsolo_navmesh.bin通过对比我们发现它与Unity中坐标点差别就是x轴坐标是相反的所以我们进行寻路的时候对数据进行了处理。然后我们把导出的bin文件放到我们ET项目中路径随意只要在程序里定义好就行比如我这里是 项目根目录ConfigRecastNavDatasolo_navmesh.bin服务端程序中读取就是读取的这个路径 /// summary/// 5v5地图的Nav数据路径/// /summarypublic const string Moba5V5MapNavDataPath ../Config/RecastNavData/solo_navmesh.bin;
进行寻路即是 RecastPathComponent recastPathComponent Game.Scene.GetComponentRecastPathComponent();RecastPath recastPath ReferencePool.AcquireRecastPath();recastPath.StartPos unit.Position;recastPath.EndPos new Vector3(target.x, target.y, target.z);self.RecastPath recastPath;//TODO 因为目前阶段只有一张地图所以默认mapId为10001recastPathComponent.SearchPath(10001, self.RecastPath);