recastJSPlugin.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import type { INavigationEnginePlugin, ICrowd, IAgentParameters, INavMeshParameters, IObstacle } from "../../Navigation/INavigationEngine";
  2. import { Mesh } from "../../Meshes/mesh";
  3. import type { Scene } from "../../scene";
  4. import { Vector3 } from "../../Maths/math";
  5. import type { TransformNode } from "../../Meshes/transformNode";
  6. import { Observable } from "../../Misc/observable";
  7. /**
  8. * RecastJS navigation plugin
  9. */
  10. export declare class RecastJSPlugin implements INavigationEnginePlugin {
  11. /**
  12. * Reference to the Recast library
  13. */
  14. bjsRECAST: any;
  15. /**
  16. * plugin name
  17. */
  18. name: string;
  19. /**
  20. * the first navmesh created. We might extend this to support multiple navmeshes
  21. */
  22. navMesh: any;
  23. private _maximumSubStepCount;
  24. private _timeStep;
  25. private _timeFactor;
  26. private _tempVec1;
  27. private _tempVec2;
  28. private _worker;
  29. /**
  30. * Initializes the recastJS plugin
  31. * @param recastInjection can be used to inject your own recast reference
  32. */
  33. constructor(recastInjection?: any);
  34. /**
  35. * Set worker URL to be used when generating a new navmesh
  36. * @param workerURL url string
  37. * @returns boolean indicating if worker is created
  38. */
  39. setWorkerURL(workerURL: string | URL): boolean;
  40. /**
  41. * Set the time step of the navigation tick update.
  42. * Default is 1/60.
  43. * A value of 0 will disable fixed time update
  44. * @param newTimeStep the new timestep to apply to this world.
  45. */
  46. setTimeStep(newTimeStep?: number): void;
  47. /**
  48. * Get the time step of the navigation tick update.
  49. * @returns the current time step
  50. */
  51. getTimeStep(): number;
  52. /**
  53. * If delta time in navigation tick update is greater than the time step
  54. * a number of sub iterations are done. If more iterations are need to reach deltatime
  55. * they will be discarded.
  56. * A value of 0 will set to no maximum and update will use as many substeps as needed
  57. * @param newStepCount the maximum number of iterations
  58. */
  59. setMaximumSubStepCount(newStepCount?: number): void;
  60. /**
  61. * Get the maximum number of iterations per navigation tick update
  62. * @returns the maximum number of iterations
  63. */
  64. getMaximumSubStepCount(): number;
  65. /**
  66. * Time factor applied when updating crowd agents (default 1). A value of 0 will pause crowd updates.
  67. * @param value the time factor applied at update
  68. */
  69. set timeFactor(value: number);
  70. /**
  71. * Get the time factor used for crowd agent update
  72. * @returns the time factor
  73. */
  74. get timeFactor(): number;
  75. /**
  76. * Creates a navigation mesh
  77. * @param meshes array of all the geometry used to compute the navigation mesh
  78. * @param parameters bunch of parameters used to filter geometry
  79. * @param completion callback when data is available from the worker. Not used without a worker
  80. */
  81. createNavMesh(meshes: Array<Mesh>, parameters: INavMeshParameters, completion?: (navmeshData: Uint8Array) => void): void;
  82. /**
  83. * Create a navigation mesh debug mesh
  84. * @param scene is where the mesh will be added
  85. * @returns debug display mesh
  86. */
  87. createDebugNavMesh(scene: Scene): Mesh;
  88. /**
  89. * Get a navigation mesh constrained position, closest to the parameter position
  90. * @param position world position
  91. * @returns the closest point to position constrained by the navigation mesh
  92. */
  93. getClosestPoint(position: Vector3): Vector3;
  94. /**
  95. * Get a navigation mesh constrained position, closest to the parameter position
  96. * @param position world position
  97. * @param result output the closest point to position constrained by the navigation mesh
  98. */
  99. getClosestPointToRef(position: Vector3, result: Vector3): void;
  100. /**
  101. * Get a navigation mesh constrained position, within a particular radius
  102. * @param position world position
  103. * @param maxRadius the maximum distance to the constrained world position
  104. * @returns the closest point to position constrained by the navigation mesh
  105. */
  106. getRandomPointAround(position: Vector3, maxRadius: number): Vector3;
  107. /**
  108. * Get a navigation mesh constrained position, within a particular radius
  109. * @param position world position
  110. * @param maxRadius the maximum distance to the constrained world position
  111. * @param result output the closest point to position constrained by the navigation mesh
  112. */
  113. getRandomPointAroundToRef(position: Vector3, maxRadius: number, result: Vector3): void;
  114. /**
  115. * Compute the final position from a segment made of destination-position
  116. * @param position world position
  117. * @param destination world position
  118. * @returns the resulting point along the navmesh
  119. */
  120. moveAlong(position: Vector3, destination: Vector3): Vector3;
  121. /**
  122. * Compute the final position from a segment made of destination-position
  123. * @param position world position
  124. * @param destination world position
  125. * @param result output the resulting point along the navmesh
  126. */
  127. moveAlongToRef(position: Vector3, destination: Vector3, result: Vector3): void;
  128. private _convertNavPathPoints;
  129. /**
  130. * Compute a navigation path from start to end. Returns an empty array if no path can be computed
  131. * Path is straight.
  132. * @param start world position
  133. * @param end world position
  134. * @returns array containing world position composing the path
  135. */
  136. computePath(start: Vector3, end: Vector3): Vector3[];
  137. /**
  138. * Compute a navigation path from start to end. Returns an empty array if no path can be computed.
  139. * Path follows navigation mesh geometry.
  140. * @param start world position
  141. * @param end world position
  142. * @returns array containing world position composing the path
  143. */
  144. computePathSmooth(start: Vector3, end: Vector3): Vector3[];
  145. /**
  146. * Create a new Crowd so you can add agents
  147. * @param maxAgents the maximum agent count in the crowd
  148. * @param maxAgentRadius the maximum radius an agent can have
  149. * @param scene to attach the crowd to
  150. * @returns the crowd you can add agents to
  151. */
  152. createCrowd(maxAgents: number, maxAgentRadius: number, scene: Scene): ICrowd;
  153. /**
  154. * Set the Bounding box extent for doing spatial queries (getClosestPoint, getRandomPointAround, ...)
  155. * The queries will try to find a solution within those bounds
  156. * default is (1,1,1)
  157. * @param extent x,y,z value that define the extent around the queries point of reference
  158. */
  159. setDefaultQueryExtent(extent: Vector3): void;
  160. /**
  161. * Get the Bounding box extent specified by setDefaultQueryExtent
  162. * @returns the box extent values
  163. */
  164. getDefaultQueryExtent(): Vector3;
  165. /**
  166. * build the navmesh from a previously saved state using getNavmeshData
  167. * @param data the Uint8Array returned by getNavmeshData
  168. */
  169. buildFromNavmeshData(data: Uint8Array): void;
  170. /**
  171. * returns the navmesh data that can be used later. The navmesh must be built before retrieving the data
  172. * @returns data the Uint8Array that can be saved and reused
  173. */
  174. getNavmeshData(): Uint8Array;
  175. /**
  176. * Get the Bounding box extent result specified by setDefaultQueryExtent
  177. * @param result output the box extent values
  178. */
  179. getDefaultQueryExtentToRef(result: Vector3): void;
  180. /**
  181. * Disposes
  182. */
  183. dispose(): void;
  184. /**
  185. * Creates a cylinder obstacle and add it to the navigation
  186. * @param position world position
  187. * @param radius cylinder radius
  188. * @param height cylinder height
  189. * @returns the obstacle freshly created
  190. */
  191. addCylinderObstacle(position: Vector3, radius: number, height: number): IObstacle;
  192. /**
  193. * Creates an oriented box obstacle and add it to the navigation
  194. * @param position world position
  195. * @param extent box size
  196. * @param angle angle in radians of the box orientation on Y axis
  197. * @returns the obstacle freshly created
  198. */
  199. addBoxObstacle(position: Vector3, extent: Vector3, angle: number): IObstacle;
  200. /**
  201. * Removes an obstacle created by addCylinderObstacle or addBoxObstacle
  202. * @param obstacle obstacle to remove from the navigation
  203. */
  204. removeObstacle(obstacle: IObstacle): void;
  205. /**
  206. * If this plugin is supported
  207. * @returns true if plugin is supported
  208. */
  209. isSupported(): boolean;
  210. /**
  211. * Returns the seed used for randomized functions like `getRandomPointAround`
  212. * @returns seed number
  213. */
  214. getRandomSeed(): number;
  215. /**
  216. * Set the seed used for randomized functions like `getRandomPointAround`
  217. * @param seed number used as seed for random functions
  218. */
  219. setRandomSeed(seed: number): void;
  220. }
  221. /**
  222. * Recast detour crowd implementation
  223. */
  224. export declare class RecastJSCrowd implements ICrowd {
  225. /**
  226. * Recast/detour plugin
  227. */
  228. bjsRECASTPlugin: RecastJSPlugin;
  229. /**
  230. * Link to the detour crowd
  231. */
  232. recastCrowd: any;
  233. /**
  234. * One transform per agent
  235. */
  236. transforms: TransformNode[];
  237. /**
  238. * All agents created
  239. */
  240. agents: number[];
  241. /**
  242. * agents reach radius
  243. */
  244. reachRadii: number[];
  245. /**
  246. * true when a destination is active for an agent and notifier hasn't been notified of reach
  247. */
  248. private _agentDestinationArmed;
  249. /**
  250. * agent current target
  251. */
  252. private _agentDestination;
  253. /**
  254. * Link to the scene is kept to unregister the crowd from the scene
  255. */
  256. private _scene;
  257. /**
  258. * Observer for crowd updates
  259. */
  260. private _onBeforeAnimationsObserver;
  261. /**
  262. * Fires each time an agent is in reach radius of its destination
  263. */
  264. onReachTargetObservable: Observable<{
  265. agentIndex: number;
  266. destination: Vector3;
  267. }>;
  268. /**
  269. * Constructor
  270. * @param plugin recastJS plugin
  271. * @param maxAgents the maximum agent count in the crowd
  272. * @param maxAgentRadius the maximum radius an agent can have
  273. * @param scene to attach the crowd to
  274. * @returns the crowd you can add agents to
  275. */
  276. constructor(plugin: RecastJSPlugin, maxAgents: number, maxAgentRadius: number, scene: Scene);
  277. /**
  278. * Add a new agent to the crowd with the specified parameter a corresponding transformNode.
  279. * You can attach anything to that node. The node position is updated in the scene update tick.
  280. * @param pos world position that will be constrained by the navigation mesh
  281. * @param parameters agent parameters
  282. * @param transform hooked to the agent that will be update by the scene
  283. * @returns agent index
  284. */
  285. addAgent(pos: Vector3, parameters: IAgentParameters, transform: TransformNode): number;
  286. /**
  287. * Returns the agent position in world space
  288. * @param index agent index returned by addAgent
  289. * @returns world space position
  290. */
  291. getAgentPosition(index: number): Vector3;
  292. /**
  293. * Returns the agent position result in world space
  294. * @param index agent index returned by addAgent
  295. * @param result output world space position
  296. */
  297. getAgentPositionToRef(index: number, result: Vector3): void;
  298. /**
  299. * Returns the agent velocity in world space
  300. * @param index agent index returned by addAgent
  301. * @returns world space velocity
  302. */
  303. getAgentVelocity(index: number): Vector3;
  304. /**
  305. * Returns the agent velocity result in world space
  306. * @param index agent index returned by addAgent
  307. * @param result output world space velocity
  308. */
  309. getAgentVelocityToRef(index: number, result: Vector3): void;
  310. /**
  311. * Returns the agent next target point on the path
  312. * @param index agent index returned by addAgent
  313. * @returns world space position
  314. */
  315. getAgentNextTargetPath(index: number): Vector3;
  316. /**
  317. * Returns the agent next target point on the path
  318. * @param index agent index returned by addAgent
  319. * @param result output world space position
  320. */
  321. getAgentNextTargetPathToRef(index: number, result: Vector3): void;
  322. /**
  323. * Gets the agent state
  324. * @param index agent index returned by addAgent
  325. * @returns agent state
  326. */
  327. getAgentState(index: number): number;
  328. /**
  329. * returns true if the agent in over an off mesh link connection
  330. * @param index agent index returned by addAgent
  331. * @returns true if over an off mesh link connection
  332. */
  333. overOffmeshConnection(index: number): boolean;
  334. /**
  335. * Asks a particular agent to go to a destination. That destination is constrained by the navigation mesh
  336. * @param index agent index returned by addAgent
  337. * @param destination targeted world position
  338. */
  339. agentGoto(index: number, destination: Vector3): void;
  340. /**
  341. * Teleport the agent to a new position
  342. * @param index agent index returned by addAgent
  343. * @param destination targeted world position
  344. */
  345. agentTeleport(index: number, destination: Vector3): void;
  346. /**
  347. * Update agent parameters
  348. * @param index agent index returned by addAgent
  349. * @param parameters agent parameters
  350. */
  351. updateAgentParameters(index: number, parameters: IAgentParameters): void;
  352. /**
  353. * remove a particular agent previously created
  354. * @param index agent index returned by addAgent
  355. */
  356. removeAgent(index: number): void;
  357. /**
  358. * get the list of all agents attached to this crowd
  359. * @returns list of agent indices
  360. */
  361. getAgents(): number[];
  362. /**
  363. * Tick update done by the Scene. Agent position/velocity/acceleration is updated by this function
  364. * @param deltaTime in seconds
  365. */
  366. update(deltaTime: number): void;
  367. /**
  368. * Set the Bounding box extent for doing spatial queries (getClosestPoint, getRandomPointAround, ...)
  369. * The queries will try to find a solution within those bounds
  370. * default is (1,1,1)
  371. * @param extent x,y,z value that define the extent around the queries point of reference
  372. */
  373. setDefaultQueryExtent(extent: Vector3): void;
  374. /**
  375. * Get the Bounding box extent specified by setDefaultQueryExtent
  376. * @returns the box extent values
  377. */
  378. getDefaultQueryExtent(): Vector3;
  379. /**
  380. * Get the Bounding box extent result specified by setDefaultQueryExtent
  381. * @param result output the box extent values
  382. */
  383. getDefaultQueryExtentToRef(result: Vector3): void;
  384. /**
  385. * Get the next corner points composing the path (max 4 points)
  386. * @param index agent index returned by addAgent
  387. * @returns array containing world position composing the path
  388. */
  389. getCorners(index: number): Vector3[];
  390. /**
  391. * Release all resources
  392. */
  393. dispose(): void;
  394. }