第一節
Crowd Navigation System
@@群組導航系統@@

A Navigation Mesh (or navmesh for short) is a surface topology describing the space where an agent can go based on constraints. Based on parameters like agent radius, agent climbing capability, agent height,... the navmesh generation computes a topology from source meshes (the world geometry). Then, this topology can have a debug display (blue mesh on screenshot above) to validate the parameters.
@@導航網格(或者簡稱為導航網格),是一個用來描述運動物體在約束條件下的移動范圍的表面拓撲物件,基于一些引數,比如運動物體的半徑、運動物體的爬坡能力、運動物體的高度,,,導航網格生成器根據源網格(場景世界的多邊形結構),計算出一個拓撲物件,然后,這個(不可見的)拓撲結構可以具有一個可見的除錯顯示(上圖中的藍色區域)用來驗證引數設定效果,@@
A demo can be found at: Crowd Navigation Demo
@@可以在這里找到一個群組導航的例子:@@
https://playground.babylonjs.com/#HFY257#4
第二節
Creating A Navigation Mesh
@@建立一個導航網格@@
Table Of Contents
@@內容串列@@
How to use the navigation mesh?
@@如何使用導航網格?@@
Parameters
@@引數@@
Queries
@@查詢@@
Baking result
@@烘焙計算結果@@
How to use the navigation mesh?
@@如何使用導航網格?@@
There are many cases to use a navigation mesh: AI and path finding, replace physics for collision detection (only allow player to go where it's possible instead of using collision detection) and many more cases theBabylon.js users will find.
@@導航網格可以用在很多地方:AI與尋路、代替物理引擎進行碰撞檢測(只允許玩家前往某些地點,而不是使用碰撞檢測)以及Babylon.js的用戶將找到的更多地方,@@
First, create the navigation plugin
@@首先,建立一個導航插件物件@@
let navigationPlugin = new BABYLON.RecastJSPlugin();
Prepare some parameters for the agent constraints (described below)
@@為運動物體的約束條件設定一些引數(具體描述在后面)@@
var parameters = {
cs: 0.2,
ch: 0.2,
walkableSlopeAngle: 35,
walkableHeight: 1,
walkableClimb: 1,
walkableRadius: 1,
maxEdgeLen: 12.,
maxSimplificationError: 1.3,
minRegionArea: 8,
mergeRegionArea: 20,
maxVertsPerPoly: 6,
detailSampleDist: 6,
detailSampleMaxError: 1,
};
Call the navigation mesh generation with the parameters and the list of meshes
@@使用這些引數以及網格(世界中的障礙物)串列呼叫導航網格生成器,@@
navigationPlugin.createNavMesh([groundMesh, wallMesh1, wallMesh2, stair1, stair2], parameters);
And that's it! you can now use the navigation mesh with the crowd system or make queries.
@@然后就歐了!現在你可以通過群組系統使用這個導航網格,或者使用查詢功能,@@
Optionaly, you can get a display of the navmesh to ensure it corresponds to your space constraints
@@可選的,你可以選擇顯示導航網格,以確保它與你的空間約束設定相對應,@@
navmeshdebug = navigationPlugin.createDebugNavMesh(scene);
var matdebug = new BABYLON.StandardMaterial('matdebug', scene);
matdebug.diffuseColor = new BABYLON.Color3(0.1, 0.2, 1);
matdebug.alpha = 0.2;
navmeshdebug.material = matdebug;
Parameters
@@引數@@
cs - The meshes are voxelized in order to compute walkable navmesh. This parameter in world unit define the widthand depth of 1 voxel.
@@網格串列中的網格將被轉化為“體素”,好用來計算可到達的導航網格,這個以世界單位表示的引數,定義了1個體素的寬度,@@
ch - Same as cs but for height of the voxel.
@@與cs一樣,但定義的是1個體素的高度@@
walkableSlopeAngle - Angle in degree for the maximum walkable slop.
@@可行走斜坡的最大坡度,以角度單位表示@@
walkableHeight - The height in voxel units that is allowd to walk in.
@@運動物體可以到達的最大高度,以體素的個數表示@@
walkableClimb - The delta in voxel units that can be climbed.
@@運動物體可以跨越的最大高度差,以體素的個數表示@@
walkableRadius - the radius in voxel units of the agents.
@@運動物體的半徑,以體素的個數表示@@
maxEdgeLen - The maximum allowed length for contour edges along the border of the mesh. Voxel units.
@@導航網格的邊緣的每條邊的最大長度,以體素尺寸為單位,@@
maxSimplificationError - The maximum distance a simplified contour's border edges should deviate the original raw contour. Voxel units.
@@導航網格是實際場景的簡化,導航網格邊緣到原始場景輪廓的最大偏離,@@
minRegionArea - The minimum number of cells allowed to form isolated island areas. Voxel units.
@@可以組成孤立島嶼的最少單元格,意思是過于小的區域將被忽略,@@
mergeRegionArea - Any regions with a span count smaller than this value will, if possible, be merged with larger regions. Voxel units.
@@范圍小于這個值的區域將在可能的情況下和更大的區域合并在一起,意思是過小的區域將與較大的區域融合,這個值一般比mergeRegionArea 更大,@@
maxVertsPerPoly - The maximum number of vertices allowed for polygons generated during the contour to polygon conversion process. Must be > 3.
@@每個多邊形的最大頂點數量-在輪廓轉變為多邊形的程序中,對于每個多邊形允許生成的最大頂點數量,必須大于3.@@
detailSampleDist - Sets the sampling distance to use when generating the detail mesh. World units.
@@細節采樣距離-設定生成細節網格時的采樣距離,使用世界長度單位@@
detailSampleMaxError - The maximum distance the detail mesh surface should deviate from heightfield data. World Units.
@@細節采樣最大誤差-細節網格的表面與高度場資料之間的最大偏離,使用世界長度單位@@
(地形-》輪廓(高度場資料)-》導航網格)
Queries
@@查詢@@
Basically, query functions help at getting constraint point and vector by the navigation mesh.
@@總的來說,查詢方法用來從導航網格獲取符合約束條件的點和向量@@
getClosestPoint(position: Vector3): Vector3;
getRandomPointAround(position: Vector3, maxRadius: number): Vector3;
moveAlong(position: Vector3, destination: Vector3): Vector3;
Respectively:
@@依次是:@@
- get a point on the navmesh close to a world position parameter
- @@在導航網格上獲取一個靠近某個世界坐標位置的點@@
- get a random world position, on the navmesh, inside a circle of maxRadius.
- @@在最大半徑的限制內,在導航網格上取一個隨機世界坐標系位置,@@
- constraint a segment by the navmesh and returns the ending world position. Like walking on the navmesh and stopping at the edge.
- @@在導航網格內約束一條線段,并且回傳其終點的世界坐標位置,就像在導航網格上走直線,然后在到達導航網格的邊緣時停止,@@
When the query can't find a valid solution, the value (0,0,0) is returned.
@@當查詢方法無法找到可用的結果時,將回傳(0,0,0)@@
Those functions use a bounding box for querying the world. The solution returned is within that bound. To properly set the default box extent to get a finer or broader result, call:
@@這些方法使用邊界盒在世界中進行查詢,回傳的記過也在這個邊界以內,可以通過以下方法來調整默認的邊界盒,以獲得更精確或更粗略的結果,@@
setDefaultQueryExtent(extent: Vector3): void;
If your query returns a point too far from the expected result, use a smaller extent.
@@如果你的查詢回傳了一個距離期望結果過遠的點,使用一個較小的擴展值,(邊界盒是由實際的網格擴展一些消除細節后形成的,邊界盒越大則與實際網格相差越遠)@@
It's possible to get a path built for navigation as a point array. It's up to the user to use this array for drawing prediction path, trigger events,...
@@可以以點陣列的方式從導航物件獲取一條導航路徑,然后用戶可以使用這個陣列繪制預計路線、觸發事件等等@@
var pathPoints = navigationPlugin.computePath(crowd.getAgentPosition(agent), navigationPlugin.getClosestPoint(destinationPoint));
pathLine = BABYLON.MeshBuilder.CreateDashedLines("ribbon", {points: pathPoints, updatable: true, instance: pathLine}, scene);
Baking result
@@烘培計算結果@@
Building a navigation mesh can take a lot of cpu and network resources. In order to lower the download size and cpu needed, it's possible to bake the result of the navigation mesh computation to a byte stream. That byte stream can later be restored to get the navigation mesh back.
@@建立導航網格可能需要消耗很多cpu和網路資源(下載recast庫?),為了降低下載量和cpu消耗,可以把計算出的導航網格烘焙為一個二進制流,這個二進制流之后又可以被恢復為導航網格@@
To retrieve the binary representation of the computed navigation mesh:
@@檢索計算出的導航網格的二進制表示:@@
var binaryData = https://www.cnblogs.com/ljzc002/p/navigationPlugin.getNavmeshData();
binaryData is an Uint8Array that you can save to a file for example. To restore an UInt8Array to a navigation mesh:
@@這里binaryData 物件是一段Uint8Array 型的資料,你可以把這段資料保存為檔案,也可以使用以下方法把它恢復為導航網格:@@
navigationPlugin.buildFromNavmeshData(uint8array);
第三節
Crowd Agents
@@群組運動物體@@
Table Of Contents
@@內容串列@@
Crowds and navigation agents
@@群組與導航運動物體@@
How to use it?
@@如何使用@@
Agent Parameters
@@運動物體引數@@
Teleport
傳送
Agent orientation and next path target
運動物體的朝向與下一個路徑目標
Crowds and navigation agents
@@群組與導航運動物體@@

Now we have a navmesh, we can create autonomous agents and make them navigate within that navmesh constraint. The agents will find the best path to that destination while avoinding other crowd agents. An agent is attach to a Transform. That means that you have to attach a mesh to see them but also that you can attach pretty much anything.
@@現在我們有了一個導航網格,接下來我們可以建立自主的運動物體,然后讓它們在導航網格的限制下導航,這些運動物體將找到最佳的到達目的地的路徑,并在這一程序中避免和群組中的其他運動物體重疊,運動物體默認被系結在一個變換節點上,如果你想看到它,則你應該系結一個網格,當然你還可以系結其他的東西,@@
A demo can be found at: Crowd and Navigation Agents
@@可以在這里找到一個群組與導航運動物體的例子:@@
https://playground.babylonjs.com/#X5XCVT#240
Click anywhere on the navmesh to make the agents go to that location.
@@點擊導航網格上的任意位置,將使運動物體向那個位置運動,@@
How to use it?
@@如何使用?@@
First thing is to create a crowd that all agents will belong to. Parameters are the maximum number of agents in the crowd, the maximum agent radius and the scene.
@@第一件事是建立一個包含所有運動物體的群組物件,引數是群組中的最大運動物體數量、運動物體的最大半徑以及場景物件,@@
var crowd = navigationPlugin.createCrowd(10, 0.1, scene);
Then to create an agent and attach it to a transform, call:
然后用以下陳述句建立一個運動物體,并把它系結到一個變換節點上:
var agentIndex = crowd.addAgent(position, agentParameters, transform);
And that's it! You will get a non moving agent. We now want to move it.
@@成功了!你將獲得一個未運動的運動物體,接下來我們要移動它,@@
crowd.agentGoto(agentIndex, navigationPlugin.getClosestPoint(endPoint));
This code will get the closest position on the navmesh to endPoint. Then it asks the agent to go to that position. Depending on your agent parameters, it will get there faster of slower.
@@這段代碼將獲取導航網格上最靠近目標點的位置,然后它將讓運動物體移動到那個位置,取決于你的運動物體的引數,他將或快或慢地到達那里,@@
Agent Parameters
@@運動物體的引數@@
radius - Radius of the agent. World Unit.
@@半徑-運動物體的半徑,世界單位@@
height - Heigh in World Unit.
@@高度-以世界單位表示的高度,@@
maxAcceleration - Acceleration max in World Unit per second per second
@@最大加速度-以世界長度單位/(秒*秒)表示的最大加速度@@
maxSpeed - Max speed in World Unit per second.
@@最大速度-以世界單位長度/秒表示的最大速度,@@
collisionQueryRange - The agent collision system will take care of others within that radius in World Unit.
@@碰撞查詢范圍-以世界單位表示的半徑,運動物體的碰撞檢測系統將考慮這個半徑范圍內的物體,@@
pathOptimizationRange - How the path will be optimized and made more straight.
@@路徑優化范圍-路徑將如何被優化,并變得更直@@
separationWeight - How hard the system will try to separate the agent. A Value of 0 means it will not try and agents might collide.
@@分離權重-表示這個系統將用多少努力去嘗試保持這個運動物體的獨立,如果設為0值,則將不努力保持這個運動物體與其他運動物體的分離并可能導致相撞@@
You can update any of these parameters, per agent, by calling :
@@你可以修改群組中的任何一個運動物體的引數,通過呼叫以下代碼:@@
// change speed and max speed 修改速度和最大速度
crowd.updateAgentParameters(agentIndex, {maxSpeed:10, maxAcceleration:200});
Teleport
@@傳送@@
You can teleport an agent to any position using this call:
@@你可以用以下方法把一個運動物體傳送到任意位置:@@
crowd.agentTeleport(agentIndex, navigationPlugin.getClosestPoint(destinationPoint));
Please note the navigation state is reseted when teleporting. You'll have to call agentGoto to choose a new destination.
@@請注意傳送物體的導航狀態將在傳送時被重置,你必須呼叫agentGoto 方法重新設定目的地,@@
Agent orientation and next path target
@@運動物體的朝向以及下一個路徑目標@@
Recastjs crowd system does not handle agent orientation. But the velocity is available and it's possible to orient the geometry toward it. To do so, you will need to use Math.atan2 like in the following example. Please take care of the length of the velocity vector. If it's not big enough, you may encounter jittering.
@@Recast.js的群組系統并不處理運動物體的朝向,但是物體的移動速度是可以被獲取的,并且可以設定多邊形朝向這一速度方向,要做到這一點,你需要像下面的例子這樣使用Math.atan2函式,請注意速度向量長度,如果這個長度不夠大物體可能不斷抖動,@@
let velocity = crowd.getAgentVelocity(agentIndex);
if (velocity.length() > 0.2)//防抖
{
var desiredRotation = Math.atan2(velocity.x, velocity.z);
// interpolate the rotation on Y to get a smoother orientation change 在Y軸的旋轉中插值,以獲得一個平滑的朝向變化效果,
ag.mesh.rotation.y = ag.mesh.rotation.y + (desiredRotation - ag.mesh.rotation.y) * 0.05;
}
In this PG Agent Orientation and Next Path Targeting
在這個訓練場中查看運動物體朝向與下一個路徑目標的例子
https://playground.babylonjs.com/#6AE0RP
The agent's cube is oriented by the velocity and a grey little box is placed at the position of the next path corner.
@@代表運動物體的方塊根據速度改變朝向,并且一個灰色的小盒子被放置在下一段路徑的拐角上,@@
第四節
Adding and removing obstacles
@@添加和移除障礙物@@
Table Of Contents
@@內容串列@@
Prerequisites
@@先決條件@@
Obstacles API
@@障礙API@@
Prerequisites
@@先決條件@@
Internaly, navigation mesh with support for obstacles differs from 'standard' use case with one static navigation mesh. Obstacles mark tiles as dirty. Those tiles need to be reprocessed to compute a portion of the navigation mesh. To do so, Recast introduces the concept of tiles. Each tile is a square portion of the nav mesh. Tiles a processed each frame. Making huge changes processed of a couple of frames.
@@在內部,導航網格通過對靜態的網格的“基礎”使用來進行障礙分辨,障礙將把地塊標記為“臟的”,這些地塊將被加工為導航網格的一部分,為了做到這一點,Recast庫引入了地塊的概念,每個地塊是導航網格一個方塊區域,每一幀里都會處理一次地塊計算,較多的幀數會產生大量的地塊修改計算,@@
In order to build a Tiled nav mesh instead of a single mesh, add the following lines to the nav mesh creation parameters:
@@為了建立一個“地塊化”的導航網格,而不是一個單獨的導航網格,在導航網格的構造引數里添加以下行@@
var navmeshParameters = {
cs: 0.2,
ch: 0.2,
walkableSlopeAngle: 0,
walkableHeight: 0.0,
walkableClimb: 0,
walkableRadius: 1,
maxEdgeLen: 12.,
maxSimplificationError: 1.3,
minRegionArea: 8,
mergeRegionArea: 20,
maxVertsPerPoly: 6,
detailSampleDist: 6,
detailSampleMaxError: 15,
borderSize: 1,
tileSize:20
};
Note the addition of tileSize. It's the world unit size of each tile. If it's not present or has a value a zero, it falls back to the standard use and obstacles won't work. Also, depending on your use case, this value must be carrefully chosen to trade between too many tiles and too much cpu intensive updates.
@@注意添加的“tileSize”引數,這是以世界單位表示的每個地塊的尺寸,如果不提供這個引數或者把它設為0,則導航網格將回退回基礎模式,并且障礙物不會生效,另外,取決于你的使用場景,這個值必須被仔細選擇,在更多的地塊和更高的cpu更新密度間取舍@@
Obstacles API
@@障礙物API@@
Once the navigation mesh is updated to take tiles into account, obstacles are accessible thru 3 simple functions:
@@一旦導航網格被設為考慮地塊的,可以使用這三個簡單的方法控制障礙物@@
addCylinderObstacle(position: Vector3, radius: number, height: number): IObstacle;
addBoxObstacle(position: Vector3, extent: Vector3, angle: number): IObstacle;
removeObstacle(obstacle: IObstacle): void;
Keep a list of added obstacles in order to remove them later. An obstacle that's not colliding with the navigation mesh will have no influence.
@@將添加的障礙物保存為一個串列,以便后期移除它們,一個與導航網格不接觸的障礙物將不會對導航產生影響,@@
Adding a door to a navigation mesh
@@向導航網格中添加一扇門@@
https://playground.babylonjs.com/#WCSDE1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/285707.html
標籤:其他
上一篇:Popovers
下一篇:直方圖統計和顯示
