INavigationEngine.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import { TransformNode } from "../Meshes/transformNode";
  2. import { Vector3 } from "../Maths/math";
  3. import { Mesh } from "../Meshes/mesh";
  4. import { Scene } from "../scene";
  5. /**
  6. * Navigation plugin interface to add navigation constrained by a navigation mesh
  7. */
  8. export interface INavigationEnginePlugin {
  9. /**
  10. * plugin name
  11. */
  12. name: string;
  13. /**
  14. * Creates a navigation mesh
  15. * @param meshes array of all the geometry used to compute the navigatio mesh
  16. * @param parameters bunch of parameters used to filter geometry
  17. */
  18. createNavMesh(meshes: Array<Mesh>, parameters: INavMeshParameters): void;
  19. /**
  20. * Create a navigation mesh debug mesh
  21. * @param scene is where the mesh will be added
  22. * @returns debug display mesh
  23. */
  24. createDebugNavMesh(scene: Scene): Mesh;
  25. /**
  26. * Get a navigation mesh constrained position, closest to the parameter position
  27. * @param position world position
  28. * @returns the closest point to position constrained by the navigation mesh
  29. */
  30. getClosestPoint(position: Vector3): Vector3;
  31. /**
  32. * Get a navigation mesh constrained position, closest to the parameter position
  33. * @param position world position
  34. * @param result output the closest point to position constrained by the navigation mesh
  35. */
  36. getClosestPointToRef(position: Vector3, result: Vector3): void;
  37. /**
  38. * Get a navigation mesh constrained position, within a particular radius
  39. * @param position world position
  40. * @param maxRadius the maximum distance to the constrained world position
  41. * @returns the closest point to position constrained by the navigation mesh
  42. */
  43. getRandomPointAround(position: Vector3, maxRadius: number): Vector3;
  44. /**
  45. * Get a navigation mesh constrained position, within a particular radius
  46. * @param position world position
  47. * @param maxRadius the maximum distance to the constrained world position
  48. * @param result output the closest point to position constrained by the navigation mesh
  49. */
  50. getRandomPointAroundToRef(position: Vector3, maxRadius: number, result: Vector3): void;
  51. /**
  52. * Compute the final position from a segment made of destination-position
  53. * @param position world position
  54. * @param destination world position
  55. * @returns the resulting point along the navmesh
  56. */
  57. moveAlong(position: Vector3, destination: Vector3): Vector3;
  58. /**
  59. * Compute the final position from a segment made of destination-position
  60. * @param position world position
  61. * @param destination world position
  62. * @param result output the resulting point along the navmesh
  63. */
  64. moveAlongToRef(position: Vector3, destination: Vector3, result: Vector3): void;
  65. /**
  66. * Compute a navigation path from start to end. Returns an empty array if no path can be computed
  67. * @param start world position
  68. * @param end world position
  69. * @returns array containing world position composing the path
  70. */
  71. computePath(start: Vector3, end: Vector3): Vector3[];
  72. /**
  73. * If this plugin is supported
  74. * @returns true if plugin is supported
  75. */
  76. isSupported(): boolean;
  77. /**
  78. * Create a new Crowd so you can add agents
  79. * @param maxAgents the maximum agent count in the crowd
  80. * @param maxAgentRadius the maximum radius an agent can have
  81. * @param scene to attach the crowd to
  82. * @returns the crowd you can add agents to
  83. */
  84. createCrowd(maxAgents: number, maxAgentRadius: number, scene: Scene): ICrowd;
  85. /**
  86. * Set the Bounding box extent for doing spatial queries (getClosestPoint, getRandomPointAround, ...)
  87. * The queries will try to find a solution within those bounds
  88. * default is (1,1,1)
  89. * @param extent x,y,z value that define the extent around the queries point of reference
  90. */
  91. setDefaultQueryExtent(extent: Vector3): void;
  92. /**
  93. * Get the Bounding box extent specified by setDefaultQueryExtent
  94. * @returns the box extent values
  95. */
  96. getDefaultQueryExtent(): Vector3;
  97. /**
  98. * build the navmesh from a previously saved state using getNavmeshData
  99. * @param data the Uint8Array returned by getNavmeshData
  100. */
  101. buildFromNavmeshData(data: Uint8Array): void;
  102. /**
  103. * returns the navmesh data that can be used later. The navmesh must be built before retrieving the data
  104. * @returns data the Uint8Array that can be saved and reused
  105. */
  106. getNavmeshData(): Uint8Array;
  107. /**
  108. * Get the Bounding box extent result specified by setDefaultQueryExtent
  109. * @param result output the box extent values
  110. */
  111. getDefaultQueryExtentToRef(result: Vector3): void;
  112. /**
  113. * Release all resources
  114. */
  115. dispose(): void;
  116. }
  117. /**
  118. * Crowd Interface. A Crowd is a collection of moving agents constrained by a navigation mesh
  119. */
  120. export interface ICrowd {
  121. /**
  122. * Add a new agent to the crowd with the specified parameter a corresponding transformNode.
  123. * You can attach anything to that node. The node position is updated in the scene update tick.
  124. * @param pos world position that will be constrained by the navigation mesh
  125. * @param parameters agent parameters
  126. * @param transform hooked to the agent that will be update by the scene
  127. * @returns agent index
  128. */
  129. addAgent(pos: Vector3, parameters: IAgentParameters, transform: TransformNode): number;
  130. /**
  131. * Returns the agent position in world space
  132. * @param index agent index returned by addAgent
  133. * @returns world space position
  134. */
  135. getAgentPosition(index: number): Vector3;
  136. /**
  137. * Gets the agent position result in world space
  138. * @param index agent index returned by addAgent
  139. * @param result output world space position
  140. */
  141. getAgentPositionToRef(index: number, result: Vector3): void;
  142. /**
  143. * Gets the agent velocity in world space
  144. * @param index agent index returned by addAgent
  145. * @returns world space velocity
  146. */
  147. getAgentVelocity(index: number): Vector3;
  148. /**
  149. * Gets the agent velocity result in world space
  150. * @param index agent index returned by addAgent
  151. * @param result output world space velocity
  152. */
  153. getAgentVelocityToRef(index: number, result: Vector3): void;
  154. /**
  155. * remove a particular agent previously created
  156. * @param index agent index returned by addAgent
  157. */
  158. removeAgent(index: number): void;
  159. /**
  160. * get the list of all agents attached to this crowd
  161. * @returns list of agent indices
  162. */
  163. getAgents() : number[];
  164. /**
  165. * Tick update done by the Scene. Agent position/velocity/acceleration is updated by this function
  166. * @param deltaTime in seconds
  167. */
  168. update(deltaTime: number): void;
  169. /**
  170. * Asks a particular agent to go to a destination. That destination is constrained by the navigation mesh
  171. * @param index agent index returned by addAgent
  172. * @param destination targeted world position
  173. */
  174. agentGoto(index: number, destination: Vector3): void;
  175. /**
  176. * Teleport the agent to a new position
  177. * @param index agent index returned by addAgent
  178. * @param destination targeted world position
  179. */
  180. agentTeleport(index: number, destination: Vector3): void;
  181. /**
  182. * Update agent parameters
  183. * @param index agent index returned by addAgent
  184. * @param parameters agent parameters
  185. */
  186. updateAgentParameters(index: number, parameters: IAgentParameters): void;
  187. /**
  188. * Set the Bounding box extent for doing spatial queries (getClosestPoint, getRandomPointAround, ...)
  189. * The queries will try to find a solution within those bounds
  190. * default is (1,1,1)
  191. * @param extent x,y,z value that define the extent around the queries point of reference
  192. */
  193. setDefaultQueryExtent(extent: Vector3): void;
  194. /**
  195. * Get the Bounding box extent specified by setDefaultQueryExtent
  196. * @returns the box extent values
  197. */
  198. getDefaultQueryExtent(): Vector3;
  199. /**
  200. * Get the Bounding box extent result specified by setDefaultQueryExtent
  201. * @param result output the box extent values
  202. */
  203. getDefaultQueryExtentToRef(result: Vector3): void;
  204. /**
  205. * Release all resources
  206. */
  207. dispose() : void;
  208. }
  209. /**
  210. * Configures an agent
  211. */
  212. export interface IAgentParameters {
  213. /**
  214. * Agent radius. [Limit: >= 0]
  215. */
  216. radius: number;
  217. /**
  218. * Agent height. [Limit: > 0]
  219. */
  220. height: number;
  221. /**
  222. * Maximum allowed acceleration. [Limit: >= 0]
  223. */
  224. maxAcceleration: number;
  225. /**
  226. * Maximum allowed speed. [Limit: >= 0]
  227. */
  228. maxSpeed: number;
  229. /**
  230. * Defines how close a collision element must be before it is considered for steering behaviors. [Limits: > 0]
  231. */
  232. collisionQueryRange: number;
  233. /**
  234. * The path visibility optimization range. [Limit: > 0]
  235. */
  236. pathOptimizationRange: number;
  237. /**
  238. * How aggresive the agent manager should be at avoiding collisions with this agent. [Limit: >= 0]
  239. */
  240. separationWeight: number;
  241. }
  242. /**
  243. * Configures the navigation mesh creation
  244. */
  245. export interface INavMeshParameters {
  246. /**
  247. * The xz-plane cell size to use for fields. [Limit: > 0] [Units: wu]
  248. */
  249. cs: number;
  250. /**
  251. * The y-axis cell size to use for fields. [Limit: > 0] [Units: wu]
  252. */
  253. ch: number;
  254. /**
  255. * The maximum slope that is considered walkable. [Limits: 0 <= value < 90] [Units: Degrees]
  256. */
  257. walkableSlopeAngle: number;
  258. /**
  259. * Minimum floor to 'ceiling' height that will still allow the floor area to
  260. * be considered walkable. [Limit: >= 3] [Units: vx]
  261. */
  262. walkableHeight: number;
  263. /**
  264. * Maximum ledge height that is considered to still be traversable. [Limit: >=0] [Units: vx]
  265. */
  266. walkableClimb: number;
  267. /**
  268. * The distance to erode/shrink the walkable area of the heightfield away from
  269. * obstructions. [Limit: >=0] [Units: vx]
  270. */
  271. walkableRadius: number;
  272. /**
  273. * The maximum allowed length for contour edges along the border of the mesh. [Limit: >=0] [Units: vx]
  274. */
  275. maxEdgeLen: number;
  276. /**
  277. * The maximum distance a simplfied contour's border edges should deviate
  278. * the original raw contour. [Limit: >=0] [Units: vx]
  279. */
  280. maxSimplificationError: number;
  281. /**
  282. * The minimum number of cells allowed to form isolated island areas. [Limit: >=0] [Units: vx]
  283. */
  284. minRegionArea: number;
  285. /**
  286. * Any regions with a span count smaller than this value will, if possible,
  287. * be merged with larger regions. [Limit: >=0] [Units: vx]
  288. */
  289. mergeRegionArea: number;
  290. /**
  291. * The maximum number of vertices allowed for polygons generated during the
  292. * contour to polygon conversion process. [Limit: >= 3]
  293. */
  294. maxVertsPerPoly: number;
  295. /**
  296. * Sets the sampling distance to use when generating the detail mesh.
  297. * (For height detail only.) [Limits: 0 or >= 0.9] [Units: wu]
  298. */
  299. detailSampleDist: number;
  300. /**
  301. * The maximum distance the detail mesh surface should deviate from heightfield
  302. * data. (For height detail only.) [Limit: >=0] [Units: wu]
  303. */
  304. detailSampleMaxError: number;
  305. }