INavigationEngine.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. * Set the time step of the navigation tick update.
  114. * Default is 1/60.
  115. * A value of 0 will disable fixed time update
  116. * @param newTimeStep the new timestep to apply to this world.
  117. */
  118. setTimeStep(newTimeStep: number): void;
  119. /**
  120. * Get the time step of the navigation tick update.
  121. * @returns the current time step
  122. */
  123. getTimeStep(): number;
  124. /**
  125. * If delta time in navigation tick update is greater than the time step
  126. * a number of sub iterations are done. If more iterations are need to reach deltatime
  127. * they will be discarded.
  128. * A value of 0 will set to no maximum and update will use as many substeps as needed
  129. * @param newStepCount the maximum number of iterations
  130. */
  131. setMaximumSubStepCount(newStepCount: number): void;
  132. /**
  133. * Get the maximum number of iterations per navigation tick update
  134. * @returns the maximum number of iterations
  135. */
  136. getMaximumSubStepCount(): number;
  137. /**
  138. * Release all resources
  139. */
  140. dispose(): void;
  141. }
  142. /**
  143. * Crowd Interface. A Crowd is a collection of moving agents constrained by a navigation mesh
  144. */
  145. export interface ICrowd {
  146. /**
  147. * Add a new agent to the crowd with the specified parameter a corresponding transformNode.
  148. * You can attach anything to that node. The node position is updated in the scene update tick.
  149. * @param pos world position that will be constrained by the navigation mesh
  150. * @param parameters agent parameters
  151. * @param transform hooked to the agent that will be update by the scene
  152. * @returns agent index
  153. */
  154. addAgent(pos: Vector3, parameters: IAgentParameters, transform: TransformNode): number;
  155. /**
  156. * Returns the agent position in world space
  157. * @param index agent index returned by addAgent
  158. * @returns world space position
  159. */
  160. getAgentPosition(index: number): Vector3;
  161. /**
  162. * Gets the agent position result in world space
  163. * @param index agent index returned by addAgent
  164. * @param result output world space position
  165. */
  166. getAgentPositionToRef(index: number, result: Vector3): void;
  167. /**
  168. * Gets the agent velocity in world space
  169. * @param index agent index returned by addAgent
  170. * @returns world space velocity
  171. */
  172. getAgentVelocity(index: number): Vector3;
  173. /**
  174. * Gets the agent velocity result in world space
  175. * @param index agent index returned by addAgent
  176. * @param result output world space velocity
  177. */
  178. getAgentVelocityToRef(index: number, result: Vector3): void;
  179. /**
  180. * Gets the agent next target point on the path
  181. * @param index agent index returned by addAgent
  182. * @returns world space position
  183. */
  184. getAgentNextTargetPath(index: number): Vector3;
  185. /**
  186. * Gets the agent state
  187. * @param index agent index returned by addAgent
  188. * @returns agent state
  189. */
  190. getAgentState(index: number): number;
  191. /**
  192. * returns true if the agent in over an off mesh link connection
  193. * @param index agent index returned by addAgent
  194. * @returns true if over an off mesh link connection
  195. */
  196. overOffmeshConnection(index: number): boolean;
  197. /**
  198. * Gets the agent next target point on the path
  199. * @param index agent index returned by addAgent
  200. * @param result output world space position
  201. */
  202. getAgentNextTargetPathToRef(index: number, result: Vector3): void;
  203. /**
  204. * remove a particular agent previously created
  205. * @param index agent index returned by addAgent
  206. */
  207. removeAgent(index: number): void;
  208. /**
  209. * get the list of all agents attached to this crowd
  210. * @returns list of agent indices
  211. */
  212. getAgents() : number[];
  213. /**
  214. * Tick update done by the Scene. Agent position/velocity/acceleration is updated by this function
  215. * @param deltaTime in seconds
  216. */
  217. update(deltaTime: number): void;
  218. /**
  219. * Asks a particular agent to go to a destination. That destination is constrained by the navigation mesh
  220. * @param index agent index returned by addAgent
  221. * @param destination targeted world position
  222. */
  223. agentGoto(index: number, destination: Vector3): void;
  224. /**
  225. * Teleport the agent to a new position
  226. * @param index agent index returned by addAgent
  227. * @param destination targeted world position
  228. */
  229. agentTeleport(index: number, destination: Vector3): void;
  230. /**
  231. * Update agent parameters
  232. * @param index agent index returned by addAgent
  233. * @param parameters agent parameters
  234. */
  235. updateAgentParameters(index: number, parameters: IAgentParameters): void;
  236. /**
  237. * Set the Bounding box extent for doing spatial queries (getClosestPoint, getRandomPointAround, ...)
  238. * The queries will try to find a solution within those bounds
  239. * default is (1,1,1)
  240. * @param extent x,y,z value that define the extent around the queries point of reference
  241. */
  242. setDefaultQueryExtent(extent: Vector3): void;
  243. /**
  244. * Get the Bounding box extent specified by setDefaultQueryExtent
  245. * @returns the box extent values
  246. */
  247. getDefaultQueryExtent(): Vector3;
  248. /**
  249. * Get the Bounding box extent result specified by setDefaultQueryExtent
  250. * @param result output the box extent values
  251. */
  252. getDefaultQueryExtentToRef(result: Vector3): void;
  253. /**
  254. * Release all resources
  255. */
  256. dispose() : void;
  257. }
  258. /**
  259. * Configures an agent
  260. */
  261. export interface IAgentParameters {
  262. /**
  263. * Agent radius. [Limit: >= 0]
  264. */
  265. radius: number;
  266. /**
  267. * Agent height. [Limit: > 0]
  268. */
  269. height: number;
  270. /**
  271. * Maximum allowed acceleration. [Limit: >= 0]
  272. */
  273. maxAcceleration: number;
  274. /**
  275. * Maximum allowed speed. [Limit: >= 0]
  276. */
  277. maxSpeed: number;
  278. /**
  279. * Defines how close a collision element must be before it is considered for steering behaviors. [Limits: > 0]
  280. */
  281. collisionQueryRange: number;
  282. /**
  283. * The path visibility optimization range. [Limit: > 0]
  284. */
  285. pathOptimizationRange: number;
  286. /**
  287. * How aggresive the agent manager should be at avoiding collisions with this agent. [Limit: >= 0]
  288. */
  289. separationWeight: number;
  290. }
  291. /**
  292. * Configures the navigation mesh creation
  293. */
  294. export interface INavMeshParameters {
  295. /**
  296. * The xz-plane cell size to use for fields. [Limit: > 0] [Units: wu]
  297. */
  298. cs: number;
  299. /**
  300. * The y-axis cell size to use for fields. [Limit: > 0] [Units: wu]
  301. */
  302. ch: number;
  303. /**
  304. * The maximum slope that is considered walkable. [Limits: 0 <= value < 90] [Units: Degrees]
  305. */
  306. walkableSlopeAngle: number;
  307. /**
  308. * Minimum floor to 'ceiling' height that will still allow the floor area to
  309. * be considered walkable. [Limit: >= 3] [Units: vx]
  310. */
  311. walkableHeight: number;
  312. /**
  313. * Maximum ledge height that is considered to still be traversable. [Limit: >=0] [Units: vx]
  314. */
  315. walkableClimb: number;
  316. /**
  317. * The distance to erode/shrink the walkable area of the heightfield away from
  318. * obstructions. [Limit: >=0] [Units: vx]
  319. */
  320. walkableRadius: number;
  321. /**
  322. * The maximum allowed length for contour edges along the border of the mesh. [Limit: >=0] [Units: vx]
  323. */
  324. maxEdgeLen: number;
  325. /**
  326. * The maximum distance a simplfied contour's border edges should deviate
  327. * the original raw contour. [Limit: >=0] [Units: vx]
  328. */
  329. maxSimplificationError: number;
  330. /**
  331. * The minimum number of cells allowed to form isolated island areas. [Limit: >=0] [Units: vx]
  332. */
  333. minRegionArea: number;
  334. /**
  335. * Any regions with a span count smaller than this value will, if possible,
  336. * be merged with larger regions. [Limit: >=0] [Units: vx]
  337. */
  338. mergeRegionArea: number;
  339. /**
  340. * The maximum number of vertices allowed for polygons generated during the
  341. * contour to polygon conversion process. [Limit: >= 3]
  342. */
  343. maxVertsPerPoly: number;
  344. /**
  345. * Sets the sampling distance to use when generating the detail mesh.
  346. * (For height detail only.) [Limits: 0 or >= 0.9] [Units: wu]
  347. */
  348. detailSampleDist: number;
  349. /**
  350. * The maximum distance the detail mesh surface should deviate from heightfield
  351. * data. (For height detail only.) [Limit: >=0] [Units: wu]
  352. */
  353. detailSampleMaxError: number;
  354. }