@recast-navigation/three 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -10
- package/dist/helpers/crowd-helper.d.ts +9 -4
- package/dist/helpers/nav-mesh-helper.d.ts +15 -5
- package/dist/index.es.js +1 -1
- package/dist/index.es.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -26,7 +26,9 @@ import * as RecastNavigationThree from '@recast-navigation/three';
|
|
|
26
26
|
|
|
27
27
|
### `threeToNavMesh`
|
|
28
28
|
|
|
29
|
-
As a convenience, this package exports a function for converting an array of `Mesh` objects to a `NavMesh
|
|
29
|
+
As a convenience, this package exports a function for converting an array of `Mesh` objects to a `NavMesh`.
|
|
30
|
+
|
|
31
|
+
Currently only indexed geometries are supported.
|
|
30
32
|
|
|
31
33
|
```ts
|
|
32
34
|
import * as THREE from 'three';
|
|
@@ -81,12 +83,12 @@ You can optionally pass custom materials to the NavMeshHelper constructor.
|
|
|
81
83
|
|
|
82
84
|
```ts
|
|
83
85
|
const navMeshMaterial = new THREE.MeshBasicMaterial({ color: 'red' });
|
|
84
|
-
const
|
|
86
|
+
const obstacleMaterial = new THREE.MeshBasicMaterial({ color: 'blue' });
|
|
85
87
|
|
|
86
88
|
const navMeshHelper = new NavMeshHelper({
|
|
87
89
|
navMesh,
|
|
88
90
|
navMeshMaterial,
|
|
89
|
-
|
|
91
|
+
obstacleMaterial,
|
|
90
92
|
});
|
|
91
93
|
```
|
|
92
94
|
|
|
@@ -99,23 +101,22 @@ import { CrowdHelper } from 'recast-navigation/three';
|
|
|
99
101
|
|
|
100
102
|
const navMesh = new NavMesh();
|
|
101
103
|
/* ... */
|
|
104
|
+
|
|
102
105
|
const crowd = new Crowd({ navMesh, maxAgents: 1, maxAgentRadius: 1 });
|
|
103
106
|
|
|
104
107
|
const crowdHelper = new CrowdHelper({ crowd });
|
|
105
108
|
|
|
106
109
|
const scene = new THREE.Scene();
|
|
107
110
|
|
|
108
|
-
scene.add(
|
|
109
|
-
scene.add(navMeshHelper.obstacles);
|
|
111
|
+
scene.add(crowdHelper.agents);
|
|
110
112
|
|
|
111
|
-
|
|
112
|
-
navMeshHelper.updateObstacles();
|
|
113
|
+
crowdHelper.update();
|
|
113
114
|
```
|
|
114
115
|
|
|
115
|
-
You can optionally
|
|
116
|
+
You can optionally provide a custom material for displaying agents.
|
|
116
117
|
|
|
117
118
|
```ts
|
|
118
|
-
const
|
|
119
|
+
const agentMaterial = new THREE.MeshBasicMaterial({ color: 'red' });
|
|
119
120
|
|
|
120
|
-
const crowdHelper = new CrowdHelper({ crowd,
|
|
121
|
+
const crowdHelper = new CrowdHelper({ crowd, agentMaterial });
|
|
121
122
|
```
|
|
@@ -2,14 +2,19 @@ import { Crowd } from '@recast-navigation/core';
|
|
|
2
2
|
import { Group, Material, Mesh } from 'three';
|
|
3
3
|
export declare type CrowdHelperParams = {
|
|
4
4
|
crowd: Crowd;
|
|
5
|
-
|
|
5
|
+
agentMaterial?: Material;
|
|
6
6
|
};
|
|
7
7
|
export declare class CrowdHelper {
|
|
8
8
|
agents: Group;
|
|
9
9
|
agentMeshes: Map<number, Mesh>;
|
|
10
10
|
recastCrowd: Crowd;
|
|
11
|
-
|
|
12
|
-
constructor({ crowd,
|
|
13
|
-
|
|
11
|
+
agentMaterial: Material;
|
|
12
|
+
constructor({ crowd, agentMaterial }: CrowdHelperParams);
|
|
13
|
+
/**
|
|
14
|
+
* Update the three debug view of the crowd agents.
|
|
15
|
+
*
|
|
16
|
+
* This should be called after updating the crowd.
|
|
17
|
+
*/
|
|
18
|
+
update(): void;
|
|
14
19
|
private createAgentMesh;
|
|
15
20
|
}
|
|
@@ -3,16 +3,26 @@ import { Group, Material, Mesh } from 'three';
|
|
|
3
3
|
export declare type NavMeshHelperParams = {
|
|
4
4
|
navMesh: NavMesh;
|
|
5
5
|
navMeshMaterial?: Material;
|
|
6
|
-
|
|
6
|
+
obstacleMaterial?: Material;
|
|
7
7
|
};
|
|
8
8
|
export declare class NavMeshHelper {
|
|
9
9
|
navMesh: Mesh;
|
|
10
10
|
obstacles: Group;
|
|
11
11
|
obstacleMeshes: Map<ObstacleRef, Mesh>;
|
|
12
12
|
navMeshMaterial: Material;
|
|
13
|
-
|
|
13
|
+
obstacleMaterial: Material;
|
|
14
14
|
recastNavMesh: NavMesh;
|
|
15
|
-
constructor({ navMesh, navMeshMaterial,
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
constructor({ navMesh, navMeshMaterial, obstacleMaterial, }: NavMeshHelperParams);
|
|
16
|
+
/**
|
|
17
|
+
* Update the three debug nav mesh.
|
|
18
|
+
*
|
|
19
|
+
* This should be called after updating the nav mesh.
|
|
20
|
+
*/
|
|
21
|
+
updateNavMesh(): void;
|
|
22
|
+
/**
|
|
23
|
+
* Update the obstacle meshes.
|
|
24
|
+
*
|
|
25
|
+
* This should be called after adding or removing obstacles.
|
|
26
|
+
*/
|
|
27
|
+
updateObstacles(): void;
|
|
18
28
|
}
|
package/dist/index.es.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{MeshBasicMaterial as e,Group as t,Vector3 as s,CapsuleGeometry as o,Mesh as a,BufferGeometry as n,BufferAttribute as r,BoxGeometry as i,CylinderGeometry as h}from"three";import{NavMesh as c}from"@recast-navigation/core";class l{agents;agentMeshes=new Map;recastCrowd;
|
|
1
|
+
import{MeshBasicMaterial as e,Group as t,Vector3 as s,CapsuleGeometry as o,Mesh as a,BufferGeometry as n,BufferAttribute as r,BoxGeometry as i,CylinderGeometry as h}from"three";import{NavMesh as c}from"@recast-navigation/core";class l{agents;agentMeshes=new Map;recastCrowd;agentMaterial;constructor({crowd:s,agentMaterial:o}){this.recastCrowd=s,this.agentMaterial=o??new e({color:"blue",wireframe:!0}),this.agents=new t,this.update()}update(){const e=this.recastCrowd.getAgents(),t=new Set(this.agentMeshes.keys());for(const o of e){t.delete(o);const e=this.recastCrowd.getAgentPosition(o),a=this.recastCrowd.getAgentVelocity(o),n=this.recastCrowd.getAgentParameters(o);let r=this.agentMeshes.get(o);void 0===r&&(r=this.createAgentMesh(n),this.agents.add(r),this.agentMeshes.set(o,r)),r.position.set(e.x,e.y+n.height/2,e.z),r.lookAt((new s).copy(r.position).add(a))}for(const e of t){const t=this.agentMeshes.get(e);t&&(this.agents.remove(t),this.agentMeshes.delete(e))}}createAgentMesh(e){const t=new o(e.radius,e.height-2*e.radius);return new a(t,this.agentMaterial)}}class g{navMesh;obstacles;obstacleMeshes=new Map;navMeshMaterial;obstacleMaterial;recastNavMesh;constructor({navMesh:s,navMeshMaterial:o,obstacleMaterial:r}){this.recastNavMesh=s,this.navMeshMaterial=o||new e({color:"blue",wireframe:!0}),this.obstacleMaterial=r||new e({color:"red",wireframe:!0}),this.navMesh=new a(new n,this.navMeshMaterial),this.obstacles=new t,this.updateNavMesh(),this.updateObstacles()}updateNavMesh(){const{positions:e,indices:t}=this.recastNavMesh.getDebugNavMesh();for(let e=0;e<t.length;e+=3){const s=t[e];t[e]=t[e+2],t[e+2]=s}const s=this.navMesh.geometry;s.setAttribute("position",new r(new Float32Array(e),3)),s.setIndex(new r(new Uint16Array(t),1)),s.computeVertexNormals()}updateObstacles(){const e=new Set(this.obstacleMeshes.keys());for(const[t,s]of this.recastNavMesh.obstacles){let o=this.obstacleMeshes.get(t);if(e.delete(t),!o){const{position:e}=s,o=new a(void 0,this.obstacleMaterial);if(o.position.copy(e),"box"===s.type){const{extent:e,angle:t}=s;o.geometry=new i(2*e.x,2*e.y,2*e.z),o.rotation.y=t}else{if("cylinder"!==s.type)throw new Error(`Unknown obstacle type: ${s}`);{const{radius:e,height:t}=s;o.geometry=new h(e,e,t,16),o.position.y+=t/2}}this.obstacles.add(o),this.obstacleMeshes.set(t,o)}}for(const t of e){const e=this.obstacleMeshes.get(t);e&&(this.obstacles.remove(e),this.obstacleMeshes.delete(t))}}}const M=e=>{let t,o,a;const n=[],r=[];let i=0;for(t=0;t<e.length;t++)if(e[t]){const h=e[t],c=h.geometry.getAttribute("position").array;if(!c)continue;let l=h.geometry.getIndex()?.array;if(!l){const e=[];for(let t=0;t<c.length/3;t++)e.push(t);l=e}for(o=0;o<l.length;o++)n.push(l[o]+i);const g=new s;for(a=0;a<c.length;a+=3)g.set(c[a],c[a+1],c[a+2]),h.localToWorld(g),r.push(g.x,g.y,g.z);i+=c.length/3}return[r,n]},w=(e,t={})=>{const[s,o]=M(e),a=new c;return a.build(s,o,t),a};export{l as CrowdHelper,g as NavMeshHelper,w as threeToNavMesh,M as threeToNavMeshArgs};
|
|
2
2
|
//# sourceMappingURL=index.es.js.map
|
package/dist/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/helpers/crowd-helper.ts","../src/helpers/nav-mesh-helper.ts","../src/utils/three-to-nav-mesh.ts"],"sourcesContent":["import { Crowd, CrowdAgentParams } from '@recast-navigation/core';\nimport {\n CapsuleGeometry,\n Group,\n Material,\n Mesh,\n MeshBasicMaterial,\n Vector3,\n} from 'three';\n\nexport type CrowdHelperParams = {\n crowd: Crowd;\n crowdMaterial?: Material;\n};\n\nexport class CrowdHelper {\n agents: Group;\n\n agentMeshes: Map<number, Mesh> = new Map();\n\n recastCrowd: Crowd;\n\n crowdMaterial: Material;\n\n constructor({ crowd, crowdMaterial }: CrowdHelperParams) {\n this.recastCrowd = crowd;\n \n this.crowdMaterial = crowdMaterial ?? new MeshBasicMaterial({ color: 'blue', wireframe: true });\n\n this.agents = new Group();\n\n this.updateAgents();\n\n crowd.onPostUpdate.add(() => {\n this.updateAgents()\n })\n }\n\n private updateAgents() {\n const agentsIndices = this.recastCrowd.getAgents();\n\n const unseen = new Set(this.agentMeshes.keys());\n\n for (const i of agentsIndices) {\n unseen.delete(i);\n\n const position = this.recastCrowd.getAgentPosition(i);\n const velocity = this.recastCrowd.getAgentVelocity(i);\n const agentParams = this.recastCrowd.getAgentParameters(i);\n\n let agentMesh = this.agentMeshes.get(i);\n\n if (agentMesh === undefined) {\n agentMesh = this.createAgentMesh(agentParams);\n \n this.agents.add(agentMesh);\n this.agentMeshes.set(i, agentMesh);\n }\n\n agentMesh.position.set(\n position.x,\n position.y + agentParams.height / 2,\n position.z\n );\n agentMesh.lookAt(\n new Vector3().copy(agentMesh.position).add(velocity as Vector3)\n ); \n }\n\n for (const agentId of unseen) {\n const agentMesh = this.agentMeshes.get(agentId);\n\n if (agentMesh) {\n this.agents.remove(agentMesh);\n this.agentMeshes.delete(agentId);\n }\n }\n }\n\n private createAgentMesh(agentParams: CrowdAgentParams): Mesh {\n const geometry = new CapsuleGeometry(\n agentParams.radius,\n agentParams.height - agentParams.radius * 2\n );\n\n return new Mesh(geometry, this.crowdMaterial);\n }\n}\n","import { NavMesh, ObstacleRef } from '@recast-navigation/core';\nimport {\n BoxGeometry,\n BufferAttribute,\n BufferGeometry,\n CylinderGeometry,\n Group,\n Material,\n Mesh,\n MeshBasicMaterial,\n Vector3,\n} from 'three';\n\nexport type NavMeshHelperParams = {\n navMesh: NavMesh;\n navMeshMaterial?: Material;\n obstaclesMaterial?: Material;\n};\n\nexport class NavMeshHelper {\n navMesh: Mesh;\n\n obstacles: Group;\n\n obstacleMeshes: Map<ObstacleRef, Mesh> = new Map();\n\n navMeshMaterial: Material;\n\n obstaclesMaterial: Material;\n\n recastNavMesh: NavMesh;\n\n constructor({\n navMesh,\n navMeshMaterial,\n obstaclesMaterial,\n }: NavMeshHelperParams) {\n this.recastNavMesh = navMesh;\n\n this.navMeshMaterial = navMeshMaterial\n ? navMeshMaterial\n : new MeshBasicMaterial({ color: 'blue', wireframe: true });\n this.obstaclesMaterial = obstaclesMaterial\n ? obstaclesMaterial\n : new MeshBasicMaterial({ color: 'red', wireframe: true });\n\n this.navMesh = new Mesh(new BufferGeometry(), this.navMeshMaterial);\n\n this.obstacles = new Group();\n\n this.updateNavMesh();\n this.updateObstacles();\n\n navMesh.onNavMeshChange.add(() => {\n this.updateNavMesh();\n });\n\n navMesh.onObstacleChange.add(() => {\n this.updateObstacles();\n });\n }\n\n private updateNavMesh() {\n const { positions, indices } = this.recastNavMesh.getDebugNavMesh();\n\n // Set the winding order of the affected faces to clockwise\n for (let i = 0; i < indices.length; i += 3) {\n const tmp = indices[i];\n indices[i] = indices[i + 2];\n indices[i + 2] = tmp;\n }\n\n const geometry = new BufferGeometry();\n\n geometry.setAttribute(\n 'position',\n new BufferAttribute(new Float32Array(positions), 3)\n );\n\n geometry.setIndex(new BufferAttribute(new Uint16Array(indices), 1));\n\n geometry.computeVertexNormals();\n\n this.navMesh.geometry = geometry;\n }\n\n private updateObstacles() { \n const unseen = new Set(this.obstacleMeshes.keys());\n\n for (const [ref, obstacle] of this.recastNavMesh.obstacles) {\n let obstacleMesh = this.obstacleMeshes.get(ref);\n\n unseen.delete(ref);\n\n if (!obstacleMesh) {\n const { position } = obstacle;\n\n const mesh = new Mesh(undefined, this.obstaclesMaterial);\n\n mesh.position.copy(position as Vector3);\n\n if (obstacle.type === 'box') {\n const { extent, angle } = obstacle;\n\n mesh.geometry = new BoxGeometry(\n extent.x * 2,\n extent.y * 2,\n extent.z * 2\n );\n\n mesh.rotation.y = angle;\n } else if (obstacle.type === 'cylinder') {\n const { radius, height } = obstacle;\n\n mesh.geometry = new CylinderGeometry(radius, radius, height, 16);\n\n mesh.position.y += height / 2;\n } else {\n throw new Error(`Unknown obstacle type: ${obstacle}`);\n }\n\n this.obstacles.add(mesh);\n this.obstacleMeshes.set(ref, mesh);\n }\n }\n\n for (const ref of unseen) {\n const obstacleMesh = this.obstacleMeshes.get(ref);\n\n if (obstacleMesh) {\n this.obstacles.remove(obstacleMesh);\n this.obstacleMeshes.delete(ref);\n }\n }\n }\n}\n","import { NavMesh, NavMeshConfig } from '@recast-navigation/core';\nimport { BufferAttribute, Mesh, Vector3 } from 'three';\n\nexport const threeToNavMeshArgs = (\n meshes: Mesh[]\n): [positions: number[], indices: number[]] => {\n let index: number;\n let tri: number;\n let pt: number;\n\n const indices: number[] = [];\n const positions: number[] = [];\n\n let offset = 0;\n for (index = 0; index < meshes.length; index++) {\n if (meshes[index]) {\n const mesh = meshes[index];\n\n const meshIndices = mesh.geometry.getIndex()?.array;\n if (!meshIndices) {\n // todo: to indexed?\n continue;\n }\n\n const meshPositions = (mesh.geometry.getAttribute(\n 'position'\n ) as BufferAttribute)!.array;\n if (!meshPositions) {\n continue;\n }\n\n for (tri = 0; tri < meshIndices.length; tri++) {\n indices.push(meshIndices[tri] + offset);\n }\n\n const position = new Vector3();\n for (pt = 0; pt < meshPositions.length; pt += 3) {\n position.set(\n meshPositions[pt],\n meshPositions[pt + 1],\n meshPositions[pt + 2]\n );\n mesh.localToWorld(position);\n\n positions.push(position.x, position.y, position.z);\n }\n\n offset += meshPositions.length / 3;\n }\n }\n\n return [positions, indices];\n};\n\nexport const threeToNavMesh = (\n meshes: Mesh[],\n navMeshConfig: Partial<NavMeshConfig> = {}\n): NavMesh => {\n const [positions, indices] = threeToNavMeshArgs(meshes);\n\n const navMesh = new NavMesh();\n navMesh.build(positions, indices, navMeshConfig);\n\n return navMesh;\n};\n"],"names":["CrowdHelper","agents","agentMeshes","Map","recastCrowd","crowdMaterial","constructor","crowd","this","MeshBasicMaterial","color","wireframe","Group","updateAgents","onPostUpdate","add","agentsIndices","getAgents","unseen","Set","keys","i","delete","position","getAgentPosition","velocity","getAgentVelocity","agentParams","getAgentParameters","agentMesh","get","undefined","createAgentMesh","set","x","y","height","z","lookAt","Vector3","copy","agentId","remove","geometry","CapsuleGeometry","radius","Mesh","NavMeshHelper","navMesh","obstacles","obstacleMeshes","navMeshMaterial","obstaclesMaterial","recastNavMesh","BufferGeometry","updateNavMesh","updateObstacles","onNavMeshChange","onObstacleChange","positions","indices","getDebugNavMesh","length","tmp","setAttribute","BufferAttribute","Float32Array","setIndex","Uint16Array","computeVertexNormals","ref","obstacle","obstacleMesh","mesh","type","extent","angle","BoxGeometry","rotation","Error","CylinderGeometry","threeToNavMeshArgs","meshes","index","tri","pt","offset","meshIndices","getIndex","array","meshPositions","getAttribute","push","localToWorld","threeToNavMesh","navMeshConfig","NavMesh","build"],"mappings":"yOAeaA,EACXC,OAEAC,YAAiC,IAAIC,IAErCC,YAEAC,cAEAC,aAAYC,MAAEA,EAAKF,cAAEA,IACnBG,KAAKJ,YAAcG,EAEnBC,KAAKH,cAAgBA,GAAiB,IAAII,EAAkB,CAAEC,MAAO,OAAQC,WAAW,IAExFH,KAAKP,OAAS,IAAIW,EAElBJ,KAAKK,eAELN,EAAMO,aAAaC,KAAI,KACrBP,KAAKK,cAAc,GAEtB,CAEOA,eACN,MAAMG,EAAgBR,KAAKJ,YAAYa,YAEjCC,EAAS,IAAIC,IAAIX,KAAKN,YAAYkB,QAExC,IAAK,MAAMC,KAAKL,EAAe,CAC7BE,EAAOI,OAAOD,GAEd,MAAME,EAAWf,KAAKJ,YAAYoB,iBAAiBH,GAC7CI,EAAWjB,KAAKJ,YAAYsB,iBAAiBL,GAC7CM,EAAcnB,KAAKJ,YAAYwB,mBAAmBP,GAExD,IAAIQ,EAAYrB,KAAKN,YAAY4B,IAAIT,QAEnBU,IAAdF,IACFA,EAAYrB,KAAKwB,gBAAgBL,GAEjCnB,KAAKP,OAAOc,IAAIc,GAChBrB,KAAKN,YAAY+B,IAAIZ,EAAGQ,IAG1BA,EAAUN,SAASU,IACjBV,EAASW,EACTX,EAASY,EAAIR,EAAYS,OAAS,EAClCb,EAASc,GAEXR,EAAUS,QACR,IAAIC,GAAUC,KAAKX,EAAUN,UAAUR,IAAIU,GAE9C,CAED,IAAK,MAAMgB,KAAWvB,EAAQ,CAC5B,MAAMW,EAAYrB,KAAKN,YAAY4B,IAAIW,GAEnCZ,IACFrB,KAAKP,OAAOyC,OAAOb,GACnBrB,KAAKN,YAAYoB,OAAOmB,GAE3B,CACF,CAEOT,gBAAgBL,GACtB,MAAMgB,EAAW,IAAIC,EACnBjB,EAAYkB,OACZlB,EAAYS,OAA8B,EAArBT,EAAYkB,QAGnC,OA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GlC,MAAM/D,EAAW,IAAIgB,EACrB,IAAK8C,EAAK,EAAGA,EAAKK,EAAc5B,OAAQuB,GAAM,EAC5C9D,EAASU,IACPyD,EAAcL,GACdK,EAAcL,EAAK,GACnBK,EAAcL,EAAK,IAErBZ,EAAKoB,aAAatE,GAElBoC,EAAUiC,KAAKrE,EAASW,EAAGX,EAASY,EAAGZ,EAASc,GAGlDiD,GAAUI,EAAc5B,OAAS,CAClC,CAGH,MAAO,CAACH,EAAWC,EAAQ,EAGhBkC,EAAiB,CAC5BZ,EACAa,EAAwC,MAExC,MAAOpC,EAAWC,GAAWqB,EAAmBC,GAE1ClC,EAAU,IAAIgD,EAGpB,OAFAhD,EAAQiD,MAAMtC,EAAWC,EAASmC,GAE3B/C,CAAO"}
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/helpers/crowd-helper.ts","../src/helpers/nav-mesh-helper.ts","../src/utils/three-to-nav-mesh.ts"],"sourcesContent":["import { Crowd, CrowdAgentParams } from '@recast-navigation/core';\nimport {\n CapsuleGeometry,\n Group,\n Material,\n Mesh,\n MeshBasicMaterial,\n Vector3,\n} from 'three';\n\nexport type CrowdHelperParams = {\n crowd: Crowd;\n agentMaterial?: Material;\n};\n\nexport class CrowdHelper {\n agents: Group;\n\n agentMeshes: Map<number, Mesh> = new Map();\n\n recastCrowd: Crowd;\n\n agentMaterial: Material;\n\n constructor({ crowd, agentMaterial }: CrowdHelperParams) {\n this.recastCrowd = crowd;\n \n this.agentMaterial = agentMaterial ?? new MeshBasicMaterial({ color: 'blue', wireframe: true });\n\n this.agents = new Group();\n\n this.update();\n }\n\n /**\n * Update the three debug view of the crowd agents.\n * \n * This should be called after updating the crowd.\n */\n update() {\n const agentsIndices = this.recastCrowd.getAgents();\n\n const unseen = new Set(this.agentMeshes.keys());\n\n for (const i of agentsIndices) {\n unseen.delete(i);\n\n const position = this.recastCrowd.getAgentPosition(i);\n const velocity = this.recastCrowd.getAgentVelocity(i);\n const agentParams = this.recastCrowd.getAgentParameters(i);\n\n let agentMesh = this.agentMeshes.get(i);\n\n if (agentMesh === undefined) {\n agentMesh = this.createAgentMesh(agentParams);\n \n this.agents.add(agentMesh);\n this.agentMeshes.set(i, agentMesh);\n }\n\n agentMesh.position.set(\n position.x,\n position.y + agentParams.height / 2,\n position.z\n );\n agentMesh.lookAt(\n new Vector3().copy(agentMesh.position).add(velocity as Vector3)\n ); \n }\n\n for (const agentId of unseen) {\n const agentMesh = this.agentMeshes.get(agentId);\n\n if (agentMesh) {\n this.agents.remove(agentMesh);\n this.agentMeshes.delete(agentId);\n }\n }\n }\n\n private createAgentMesh(agentParams: CrowdAgentParams): Mesh {\n const geometry = new CapsuleGeometry(\n agentParams.radius,\n agentParams.height - agentParams.radius * 2\n );\n\n return new Mesh(geometry, this.agentMaterial);\n }\n}\n","import { NavMesh, ObstacleRef } from '@recast-navigation/core';\nimport {\n BoxGeometry,\n BufferAttribute,\n BufferGeometry,\n CylinderGeometry,\n Group,\n Material,\n Mesh,\n MeshBasicMaterial,\n Vector3,\n} from 'three';\n\nexport type NavMeshHelperParams = {\n navMesh: NavMesh;\n navMeshMaterial?: Material;\n obstacleMaterial?: Material;\n};\n\nexport class NavMeshHelper {\n navMesh: Mesh;\n\n obstacles: Group;\n\n obstacleMeshes: Map<ObstacleRef, Mesh> = new Map();\n\n navMeshMaterial: Material;\n\n obstacleMaterial: Material;\n\n recastNavMesh: NavMesh;\n\n constructor({\n navMesh,\n navMeshMaterial,\n obstacleMaterial,\n }: NavMeshHelperParams) {\n this.recastNavMesh = navMesh;\n\n this.navMeshMaterial = navMeshMaterial\n ? navMeshMaterial\n : new MeshBasicMaterial({ color: 'blue', wireframe: true });\n this.obstacleMaterial = obstacleMaterial\n ? obstacleMaterial\n : new MeshBasicMaterial({ color: 'red', wireframe: true });\n\n this.navMesh = new Mesh(new BufferGeometry(), this.navMeshMaterial);\n\n this.obstacles = new Group();\n\n this.updateNavMesh();\n this.updateObstacles();\n }\n\n /**\n * Update the three debug nav mesh.\n * \n * This should be called after updating the nav mesh.\n */\n updateNavMesh() {\n const { positions, indices } = this.recastNavMesh.getDebugNavMesh();\n\n // Set the winding order of the affected faces to clockwise\n for (let i = 0; i < indices.length; i += 3) {\n const tmp = indices[i];\n indices[i] = indices[i + 2];\n indices[i + 2] = tmp;\n }\n\n const geometry = this.navMesh.geometry;\n\n geometry.setAttribute(\n 'position',\n new BufferAttribute(new Float32Array(positions), 3)\n );\n\n geometry.setIndex(new BufferAttribute(new Uint16Array(indices), 1));\n\n geometry.computeVertexNormals();\n }\n\n /**\n * Update the obstacle meshes.\n *\n * This should be called after adding or removing obstacles.\n */\n updateObstacles() { \n const unseen = new Set(this.obstacleMeshes.keys());\n\n for (const [ref, obstacle] of this.recastNavMesh.obstacles) {\n let obstacleMesh = this.obstacleMeshes.get(ref);\n\n unseen.delete(ref);\n\n if (!obstacleMesh) {\n const { position } = obstacle;\n\n const mesh = new Mesh(undefined, this.obstacleMaterial);\n\n mesh.position.copy(position as Vector3);\n\n if (obstacle.type === 'box') {\n const { extent, angle } = obstacle;\n\n mesh.geometry = new BoxGeometry(\n extent.x * 2,\n extent.y * 2,\n extent.z * 2\n );\n\n mesh.rotation.y = angle;\n } else if (obstacle.type === 'cylinder') {\n const { radius, height } = obstacle;\n\n mesh.geometry = new CylinderGeometry(radius, radius, height, 16);\n\n mesh.position.y += height / 2;\n } else {\n throw new Error(`Unknown obstacle type: ${obstacle}`);\n }\n\n this.obstacles.add(mesh);\n this.obstacleMeshes.set(ref, mesh);\n }\n }\n\n for (const ref of unseen) {\n const obstacleMesh = this.obstacleMeshes.get(ref);\n\n if (obstacleMesh) {\n this.obstacles.remove(obstacleMesh);\n this.obstacleMeshes.delete(ref);\n }\n }\n }\n}\n","import { NavMesh, NavMeshConfig } from '@recast-navigation/core';\nimport { BufferAttribute, Mesh, Vector3 } from 'three';\n\nexport const threeToNavMeshArgs = (\n meshes: Mesh[]\n): [positions: number[], indices: number[]] => {\n let index: number;\n let tri: number;\n let pt: number;\n\n const indices: number[] = [];\n const positions: number[] = [];\n\n let offset = 0;\n for (index = 0; index < meshes.length; index++) {\n if (meshes[index]) {\n const mesh = meshes[index];\n\n const meshPositions = (mesh.geometry.getAttribute(\n 'position'\n ) as BufferAttribute)!.array;\n\n if (!meshPositions) {\n continue;\n }\n\n let meshIndices = mesh.geometry.getIndex()?.array;\n\n if (!meshIndices) {\n const indices = [];\n for (let i = 0; i < meshPositions.length / 3; i++) {\n indices.push(i);\n }\n meshIndices = indices;\n }\n\n for (tri = 0; tri < meshIndices.length; tri++) {\n indices.push(meshIndices[tri] + offset);\n }\n\n const position = new Vector3();\n for (pt = 0; pt < meshPositions.length; pt += 3) {\n position.set(\n meshPositions[pt],\n meshPositions[pt + 1],\n meshPositions[pt + 2]\n );\n mesh.localToWorld(position);\n\n positions.push(position.x, position.y, position.z);\n }\n\n offset += meshPositions.length / 3;\n }\n }\n\n return [positions, indices];\n};\n\nexport const threeToNavMesh = (\n meshes: Mesh[],\n navMeshConfig: Partial<NavMeshConfig> = {}\n): NavMesh => {\n const [positions, indices] = threeToNavMeshArgs(meshes);\n\n const navMesh = new NavMesh();\n navMesh.build(positions, indices, navMeshConfig);\n\n return navMesh;\n};\n"],"names":["CrowdHelper","agents","agentMeshes","Map","recastCrowd","agentMaterial","constructor","crowd","this","MeshBasicMaterial","color","wireframe","Group","update","agentsIndices","getAgents","unseen","Set","keys","i","delete","position","getAgentPosition","velocity","getAgentVelocity","agentParams","getAgentParameters","agentMesh","get","undefined","createAgentMesh","add","set","x","y","height","z","lookAt","Vector3","copy","agentId","remove","geometry","CapsuleGeometry","radius","Mesh","NavMeshHelper","navMesh","obstacles","obstacleMeshes","navMeshMaterial","obstacleMaterial","recastNavMesh","BufferGeometry","updateNavMesh","updateObstacles","positions","indices","getDebugNavMesh","length","tmp","setAttribute","BufferAttribute","Float32Array","setIndex","Uint16Array","computeVertexNormals","ref","obstacle","obstacleMesh","mesh","type","extent","angle","BoxGeometry","rotation","Error","CylinderGeometry","threeToNavMeshArgs","meshes","index","tri","pt","offset","meshPositions","getAttribute","array","meshIndices","getIndex","push","localToWorld","threeToNavMesh","navMeshConfig","NavMesh","build"],"mappings":"yOAeaA,EACXC,OAEAC,YAAiC,IAAIC,IAErCC,YAEAC,cAEAC,aAAYC,MAAEA,EAAKF,cAAEA,IACnBG,KAAKJ,YAAcG,EAEnBC,KAAKH,cAAgBA,GAAiB,IAAII,EAAkB,CAAEC,MAAO,OAAQC,WAAW,IAExFH,KAAKP,OAAS,IAAIW,EAElBJ,KAAKK,QACN,CAODA,SACE,MAAMC,EAAgBN,KAAKJ,YAAYW,YAEjCC,EAAS,IAAIC,IAAIT,KAAKN,YAAYgB,QAExC,IAAK,MAAMC,KAAKL,EAAe,CAC7BE,EAAOI,OAAOD,GAEd,MAAME,EAAWb,KAAKJ,YAAYkB,iBAAiBH,GAC7CI,EAAWf,KAAKJ,YAAYoB,iBAAiBL,GAC7CM,EAAcjB,KAAKJ,YAAYsB,mBAAmBP,GAExD,IAAIQ,EAAYnB,KAAKN,YAAY0B,IAAIT,QAEnBU,IAAdF,IACFA,EAAYnB,KAAKsB,gBAAgBL,GAEjCjB,KAAKP,OAAO8B,IAAIJ,GAChBnB,KAAKN,YAAY8B,IAAIb,EAAGQ,IAG1BA,EAAUN,SAASW,IACjBX,EAASY,EACTZ,EAASa,EAAIT,EAAYU,OAAS,EAClCd,EAASe,GAEXT,EAAUU,QACR,IAAIC,GAAUC,KAAKZ,EAAUN,UAAUU,IAAIR,GAE9C,CAED,IAAK,MAAMiB,KAAWxB,EAAQ,CAC5B,MAAMW,EAAYnB,KAAKN,YAAY0B,IAAIY,GAEnCb,IACFnB,KAAKP,OAAOwC,OAAOd,GACnBnB,KAAKN,YAAYkB,OAAOoB,GAE3B,CACF,CAEOV,gBAAgBL,GACtB,MAAMiB,EAAW,IAAIC,EACnBlB,EAAYmB,OACZnB,EAAYU,OAA8B,EAArBV,EAAYmB,QAGnC,OAAO,IAAIC,EAAKH,EAAU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B,EACrB,IAAK4C,EAAK,EAAGA,EAAKE,EAAczB,OAAQuB,GAAM,EAC5C7D,EAASW,IACPoD,EAAcF,GACdE,EAAcF,EAAK,GACnBE,EAAcF,EAAK,IAErBZ,EAAKoB,aAAarE,GAElBmC,EAAUiC,KAAKpE,EAASY,EAAGZ,EAASa,EAAGb,EAASe,GAGlD+C,GAAUC,EAAczB,OAAS,CAClC,CAGH,MAAO,CAACH,EAAWC,EAAQ,EAGhBkC,EAAiB,CAC5BZ,EACAa,EAAwC,MAExC,MAAOpC,EAAWC,GAAWqB,EAAmBC,GAE1ChC,EAAU,IAAI8C,EAGpB,OAFA9C,EAAQ+C,MAAMtC,EAAWC,EAASmC,GAE3B7C,CAAO"}
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"email": "isaac@isaacmason.com",
|
|
7
7
|
"url": "https://isaacmason.com/"
|
|
8
8
|
},
|
|
9
|
-
"version": "0.0.
|
|
9
|
+
"version": "0.0.4",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"homepage": "https://github.com/isaac-mason/recast-navigation-js",
|
|
12
12
|
"bugs": {
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"build": "rollup --config rollup.config.js --bundleConfigAsCjs"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@recast-navigation/core": "0.0.
|
|
27
|
+
"@recast-navigation/core": "0.0.4",
|
|
28
28
|
"three": "^0.150.1"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|