@recast-navigation/three 0.0.7 → 0.1.1
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/dist/index.es.js +1 -1
- package/dist/index.es.js.map +1 -1
- package/package.json +2 -2
package/dist/index.es.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{MeshBasicMaterial as e,Group as t,Vector3 as s,CapsuleGeometry as a,Mesh as o,BufferGeometry as n,BufferAttribute as r,BoxGeometry as i,CylinderGeometry as h}from"three";import{NavMeshGenerator as c}from"@recast-navigation/core";class l{agents;agentMeshes=new Map;recastCrowd;agentMaterial;constructor({crowd:s,agentMaterial:a}){this.recastCrowd=s,this.agentMaterial=a??new e({color:"red"}),this.agents=new t,this.updateAgents()}updateAgents(){const e=this.recastCrowd.getAgents(),t=new Set(this.agentMeshes.keys());for(const a of e){t.delete(a);const e=this.recastCrowd.getAgentPosition(a),o=this.recastCrowd.getAgentVelocity(a),n=this.recastCrowd.getAgentParameters(a);let r=this.agentMeshes.get(a);void 0===r&&(r=this.createAgentMesh(n),this.agents.add(r),this.agentMeshes.set(a,r)),r.position.set(e.x,e.y+n.height/2,e.z),r.lookAt((new s).copy(r.position).add(o))}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 a(e.radius,e.height-2*e.radius);return new o(t,this.agentMaterial)}}class g{navMesh;navMeshMaterial;recastNavMesh;constructor({navMesh:t,navMeshMaterial:s}){this.recastNavMesh=t,this.navMeshMaterial=s||new e({color:"orange",transparent:!0,opacity:.7}),this.navMesh=new o(new n,this.navMeshMaterial),this.updateNavMesh()}updateNavMesh(){const{positions:e,indices:t}=this.recastNavMesh.getDebugNavMesh()
|
|
1
|
+
import{MeshBasicMaterial as e,Group as t,Vector3 as s,CapsuleGeometry as a,Mesh as o,BufferGeometry as n,BufferAttribute as r,BoxGeometry as i,CylinderGeometry as h}from"three";import{NavMeshGenerator as c}from"@recast-navigation/core";class l{agents;agentMeshes=new Map;recastCrowd;agentMaterial;constructor({crowd:s,agentMaterial:a}){this.recastCrowd=s,this.agentMaterial=a??new e({color:"red"}),this.agents=new t,this.updateAgents()}updateAgents(){const e=this.recastCrowd.getAgents(),t=new Set(this.agentMeshes.keys());for(const a of e){t.delete(a);const e=this.recastCrowd.getAgentPosition(a),o=this.recastCrowd.getAgentVelocity(a),n=this.recastCrowd.getAgentParameters(a);let r=this.agentMeshes.get(a);void 0===r&&(r=this.createAgentMesh(n),this.agents.add(r),this.agentMeshes.set(a,r)),r.position.set(e.x,e.y+n.height/2,e.z),r.lookAt((new s).copy(r.position).add(o))}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 a(e.radius,e.height-2*e.radius);return new o(t,this.agentMaterial)}}class g{navMesh;navMeshMaterial;recastNavMesh;constructor({navMesh:t,navMeshMaterial:s}){this.recastNavMesh=t,this.navMeshMaterial=s||new e({color:"orange",transparent:!0,opacity:.7}),this.navMesh=new o(new n,this.navMeshMaterial),this.updateNavMesh()}updateNavMesh(){const{positions:e,indices:t}=this.recastNavMesh.getDebugNavMesh(),s=this.navMesh.geometry;s.setAttribute("position",new r(new Float32Array(e),3)),s.setIndex(new r(new Uint16Array(t),1)),s.computeVertexNormals()}}class M{tileCache;obstacles;obstacleMeshes=new Map;obstacleMaterial;constructor({tileCache:s,obstacleMaterial:a}){this.tileCache=s,this.obstacleMaterial=a||new e({color:"red",wireframe:!0,wireframeLinewidth:2}),this.obstacles=new t,this.updateObstacles()}updateObstacles(){const e=new Set(this.obstacleMeshes.keys());for(const[t,s]of this.tileCache.obstacles){let a=this.obstacleMeshes.get(t);if(e.delete(t),!a){const{position:e}=s,a=new o(void 0,this.obstacleMaterial);if(a.position.copy(e),"box"===s.type){const{extent:e,angle:t}=s;a.geometry=new i(2*e.x,2*e.y,2*e.z),a.rotation.y=t}else{if("cylinder"!==s.type)throw new Error(`Unknown obstacle type: ${s}`);{const{radius:e,height:t}=s;a.geometry=new h(e,e,t,16),a.position.y+=t/2}}this.obstacles.add(a),this.obstacleMeshes.set(t,a)}}for(const t of e){const e=this.obstacleMeshes.get(t);e&&(this.obstacles.remove(e),this.obstacleMeshes.delete(t))}}}const w=new s,d=e=>{const t=[];let s=0,a=0;for(const o of e){const e=o.geometry.attributes.position;e&&3===e.itemSize&&(s+=e.array.length,a+=o.geometry.getIndex()?.array.length??e.array.length/3,t.push(o))}const o=new Float32Array(s),n=new Uint32Array(a);let r=0,i=0;for(const e of t){const t=e.geometry.attributes.position.array,s=w;for(let a=0;a<t.length;a+=3)s.set(t[a],t[a+1],t[a+2]),e.localToWorld(s),o[a+i]=s.x,o[a+i+1]=s.y,o[a+i+2]=s.z;const a=e.geometry.getIndex()?.array;if(a)for(let e=0;e<a.length;e++)n[e+r]=a[e]+r;else for(let e=0;e<t.length/3;e++)n[e+r]=e+r;i+=t.length,r+=t.length/3}for(let e=0;e<n.length;e+=3){const t=n[e];n[e]=n[e+2],n[e+2]=t}return[o,n]},y=(e,t={})=>{const[s,a]=d(e);return(new c).generate(s,a,t)};export{l as CrowdHelper,g as NavMeshHelper,M as TileCacheHelper,d as getPositionsAndIndices,y as threeToNavMesh};
|
|
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/helpers/tile-cache-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: 'red' });\n\n this.agents = new Group();\n\n this.updateAgents();\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 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.agentMaterial);\n }\n}\n","import { NavMesh } from '@recast-navigation/core';\nimport {\n BufferAttribute,\n BufferGeometry,\n Material,\n Mesh,\n MeshBasicMaterial,\n} from 'three';\n\nexport type NavMeshHelperParams = {\n navMesh: NavMesh;\n navMeshMaterial?: Material;\n};\n\nexport class NavMeshHelper {\n navMesh: Mesh;\n\n navMeshMaterial: Material;\n\n recastNavMesh: NavMesh;\n\n constructor({ navMesh, navMeshMaterial }: NavMeshHelperParams) {\n this.recastNavMesh = navMesh;\n\n this.navMeshMaterial = navMeshMaterial\n ? navMeshMaterial\n : new MeshBasicMaterial({\n color: 'orange',\n transparent: true,\n opacity: 0.7,\n });\n\n this.navMesh = new Mesh(new BufferGeometry(), this.navMeshMaterial);\n\n this.updateNavMesh();\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 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","import { ObstacleRef, TileCache } from '@recast-navigation/core';\nimport {\n BoxGeometry,\n CylinderGeometry,\n Group,\n Material,\n Mesh,\n MeshBasicMaterial,\n Vector3,\n} from 'three';\n\nexport type TileCacheHelperParams = {\n tileCache: TileCache;\n obstacleMaterial?: Material;\n};\n\nexport class TileCacheHelper {\n tileCache: TileCache;\n\n obstacles: Group;\n\n obstacleMeshes: Map<ObstacleRef, Mesh> = new Map();\n\n obstacleMaterial: Material;\n\n constructor({ tileCache, obstacleMaterial }: TileCacheHelperParams) {\n this.tileCache = tileCache;\n\n this.obstacleMaterial = obstacleMaterial\n ? obstacleMaterial\n : new MeshBasicMaterial({ color: 'red', wireframe: true, wireframeLinewidth: 2 });\n\n this.obstacles = new Group();\n\n this.updateObstacles();\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.tileCache.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 {\n NavMeshConfig,\n NavMeshGenerator,\n NavMeshGeneratorResult,\n} from '@recast-navigation/core';\nimport { BufferAttribute, Mesh, Vector3 } from 'three';\n\nconst tmpVec3 = new Vector3();\n\nexport const getPositionsAndIndices = (\n meshes: Mesh[]\n): [positions: Float32Array, indices: Uint32Array] => {\n const meshesToMerge: Mesh[] = [];\n\n let mergedPositionsLength = 0;\n let mergedIndicesLength = 0;\n\n for (const mesh of meshes) {\n const positionAttribute = mesh.geometry.attributes\n .position as BufferAttribute;\n\n if (!positionAttribute || positionAttribute.itemSize !== 3) {\n continue;\n }\n\n mergedPositionsLength += positionAttribute.array.length;\n\n mergedIndicesLength +=\n mesh.geometry.getIndex()?.array.length ??\n positionAttribute.array.length / 3;\n\n meshesToMerge.push(mesh);\n }\n\n const mergedPositions = new Float32Array(mergedPositionsLength);\n const mergedIndices = new Uint32Array(mergedIndicesLength);\n\n let indicesOffset = 0;\n let positionsOffset = 0;\n\n for (const mesh of meshesToMerge) {\n const positionAttribute = mesh.geometry.attributes\n .position as BufferAttribute;\n const positionArray = positionAttribute.array;\n\n const position = tmpVec3;\n for (let pt = 0; pt < positionArray.length; pt += 3) {\n position.set(\n positionArray[pt],\n positionArray[pt + 1],\n positionArray[pt + 2]\n );\n mesh.localToWorld(position);\n\n mergedPositions[pt + positionsOffset] = position.x;\n mergedPositions[pt + positionsOffset + 1] = position.y;\n mergedPositions[pt + positionsOffset + 2] = position.z;\n }\n\n const index = mesh.geometry.getIndex()?.array;\n\n if (index) {\n for (let tri = 0; tri < index.length; tri++) {\n mergedIndices[tri + indicesOffset] = index[tri] + indicesOffset;\n }\n } else {\n for (let i = 0; i < positionArray.length / 3; i++) {\n mergedIndices[i + indicesOffset] = i + indicesOffset;\n }\n }\n\n positionsOffset += positionArray.length;\n indicesOffset += positionArray.length / 3;\n }\n\n for (let i = 0; i < mergedIndices.length; i += 3) {\n const tmp = mergedIndices[i];\n mergedIndices[i] = mergedIndices[i + 2];\n mergedIndices[i + 2] = tmp;\n }\n\n return [mergedPositions, mergedIndices];\n};\n\nexport const threeToNavMesh = (\n meshes: Mesh[],\n navMeshConfig: Partial<NavMeshConfig> = {}\n): NavMeshGeneratorResult => {\n const [positions, indices] = getPositionsAndIndices(meshes);\n\n const navMeshGenerator = new NavMeshGenerator();\n\n return navMeshGenerator.generate(positions, indices, navMeshConfig);\n};\n"],"names":["CrowdHelper","agents","agentMeshes","Map","recastCrowd","agentMaterial","constructor","crowd","this","MeshBasicMaterial","color","Group","updateAgents","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","navMeshMaterial","recastNavMesh","transparent","opacity","BufferGeometry","updateNavMesh","positions","indices","getDebugNavMesh","length","tmp","setAttribute","BufferAttribute","Float32Array","setIndex","Uint16Array","computeVertexNormals","TileCacheHelper","tileCache","obstacles","obstacleMeshes","obstacleMaterial","wireframe","wireframeLinewidth","updateObstacles","ref","obstacle","obstacleMesh","mesh","type","extent","angle","BoxGeometry","rotation","Error","CylinderGeometry","tmpVec3","getPositionsAndIndices","meshes","meshesToMerge","mergedPositionsLength","mergedIndicesLength","positionAttribute","attributes","itemSize","array","getIndex","push","mergedPositions","mergedIndices","Uint32Array","indicesOffset","positionsOffset","positionArray","pt","localToWorld","index","tri","threeToNavMesh","navMeshConfig","NavMeshGenerator","generate"],"mappings":"kPAeaA,EACXC,OAEAC,YAAiC,IAAIC,IAErCC,YAEAC,cAEAC,aAAYC,MAAEA,EAAKF,cAAEA,IACnBG,KAAKJ,YAAcG,EAEnBC,KAAKH,cAAgBA,GAAiB,IAAII,EAAkB,CAAEC,MAAO,QAErEF,KAAKP,OAAS,IAAIU,EAElBH,KAAKI,cACN,CAODA,eACE,MAAMC,EAAgBL,KAAKJ,YAAYU,YAEjCC,EAAS,IAAIC,IAAIR,KAAKN,YAAYe,QAExC,IAAK,MAAMC,KAAKL,EAAe,CAC7BE,EAAOI,OAAOD,GAEd,MAAME,EAAWZ,KAAKJ,YAAYiB,iBAAiBH,GAC7CI,EAAWd,KAAKJ,YAAYmB,iBAAiBL,GAC7CM,EAAchB,KAAKJ,YAAYqB,mBAAmBP,GAExD,IAAIQ,EAAYlB,KAAKN,YAAYyB,IAAIT,QAEnBU,IAAdF,IACFA,EAAYlB,KAAKqB,gBAAgBL,GAEjChB,KAAKP,OAAO6B,IAAIJ,GAChBlB,KAAKN,YAAY6B,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,EAAYlB,KAAKN,YAAYyB,IAAIY,GAEnCb,IACFlB,KAAKP,OAAOuC,OAAOd,GACnBlB,KAAKN,YAAYiB,OAAOoB,GAE3B,CACF,CAEOV,gBAAgBL,GACtB,MAAMiB,EAAW,IAAIC,EACnBlB,EAAYmB,OACZnB,EA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AAIU,EAAgB,EAChBC,EAAkB,EAEtB,IAAK,MAAMxB,KAAQW,EAAe,CAChC,MAEMc,EAFoBzB,EAAKlC,SAASiD,WACrCtE,SACqCwE,MAElCxE,EAAW+D,EACjB,IAAK,IAAIkB,EAAK,EAAGA,EAAKD,EAAc5C,OAAQ6C,GAAM,EAChDjF,EAASW,IACPqE,EAAcC,GACdD,EAAcC,EAAK,GACnBD,EAAcC,EAAK,IAErB1B,EAAK2B,aAAalF,GAElB2E,EAAgBM,EAAKF,GAAmB/E,EAASY,EACjD+D,EAAgBM,EAAKF,EAAkB,GAAK/E,EAASa,EACrD8D,EAAgBM,EAAKF,EAAkB,GAAK/E,EAASe,EAGvD,MAAMoE,EAAQ5B,EAAKlC,SAASoD,YAAYD,MAExC,GAAIW,EACF,IAAK,IAAIC,EAAM,EAAGA,EAAMD,EAAM/C,OAAQgD,IACpCR,EAAcQ,EAAMN,GAAiBK,EAAMC,GAAON,OAGpD,IAAK,IAAIhF,EAAI,EAAGA,EAAIkF,EAAc5C,OAAS,EAAGtC,IAC5C8E,EAAc9E,EAAIgF,GAAiBhF,EAAIgF,EAI3CC,GAAmBC,EAAc5C,OACjC0C,GAAiBE,EAAc5C,OAAS,CACzC,CAED,IAAK,IAAItC,EAAI,EAAGA,EAAI8E,EAAcxC,OAAQtC,GAAK,EAAG,CAChD,MAAMuC,EAAMuC,EAAc9E,GAC1B8E,EAAc9E,GAAK8E,EAAc9E,EAAI,GACrC8E,EAAc9E,EAAI,GAAKuC,CACxB,CAED,MAAO,CAACsC,EAAiBC,EAAc,EAG5BS,EAAiB,CAC5BpB,EACAqB,EAAwC,MAExC,MAAOrD,EAAWC,GAAW8B,EAAuBC,GAIpD,OAFyB,IAAIsB,GAELC,SAASvD,EAAWC,EAASoD,EAAc"}
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/helpers/crowd-helper.ts","../src/helpers/nav-mesh-helper.ts","../src/helpers/tile-cache-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: 'red' });\n\n this.agents = new Group();\n\n this.updateAgents();\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 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.agentMaterial);\n }\n}\n","import { NavMesh } from '@recast-navigation/core';\nimport {\n BufferAttribute,\n BufferGeometry,\n Material,\n Mesh,\n MeshBasicMaterial,\n} from 'three';\n\nexport type NavMeshHelperParams = {\n navMesh: NavMesh;\n navMeshMaterial?: Material;\n};\n\nexport class NavMeshHelper {\n navMesh: Mesh;\n\n navMeshMaterial: Material;\n\n recastNavMesh: NavMesh;\n\n constructor({ navMesh, navMeshMaterial }: NavMeshHelperParams) {\n this.recastNavMesh = navMesh;\n\n this.navMeshMaterial = navMeshMaterial\n ? navMeshMaterial\n : new MeshBasicMaterial({\n color: 'orange',\n transparent: true,\n opacity: 0.7,\n });\n\n this.navMesh = new Mesh(new BufferGeometry(), this.navMeshMaterial);\n\n this.updateNavMesh();\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 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","import { ObstacleRef, TileCache } from '@recast-navigation/core';\nimport {\n BoxGeometry,\n CylinderGeometry,\n Group,\n Material,\n Mesh,\n MeshBasicMaterial,\n Vector3,\n} from 'three';\n\nexport type TileCacheHelperParams = {\n tileCache: TileCache;\n obstacleMaterial?: Material;\n};\n\nexport class TileCacheHelper {\n tileCache: TileCache;\n\n obstacles: Group;\n\n obstacleMeshes: Map<ObstacleRef, Mesh> = new Map();\n\n obstacleMaterial: Material;\n\n constructor({ tileCache, obstacleMaterial }: TileCacheHelperParams) {\n this.tileCache = tileCache;\n\n this.obstacleMaterial = obstacleMaterial\n ? obstacleMaterial\n : new MeshBasicMaterial({ color: 'red', wireframe: true, wireframeLinewidth: 2 });\n\n this.obstacles = new Group();\n\n this.updateObstacles();\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.tileCache.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 {\n NavMeshConfig,\n NavMeshGenerator,\n NavMeshGeneratorResult,\n} from '@recast-navigation/core';\nimport { BufferAttribute, Mesh, Vector3 } from 'three';\n\nconst tmpVec3 = new Vector3();\n\nexport const getPositionsAndIndices = (\n meshes: Mesh[]\n): [positions: Float32Array, indices: Uint32Array] => {\n const meshesToMerge: Mesh[] = [];\n\n let mergedPositionsLength = 0;\n let mergedIndicesLength = 0;\n\n for (const mesh of meshes) {\n const positionAttribute = mesh.geometry.attributes\n .position as BufferAttribute;\n\n if (!positionAttribute || positionAttribute.itemSize !== 3) {\n continue;\n }\n\n mergedPositionsLength += positionAttribute.array.length;\n\n mergedIndicesLength +=\n mesh.geometry.getIndex()?.array.length ??\n positionAttribute.array.length / 3;\n\n meshesToMerge.push(mesh);\n }\n\n const mergedPositions = new Float32Array(mergedPositionsLength);\n const mergedIndices = new Uint32Array(mergedIndicesLength);\n\n let indicesOffset = 0;\n let positionsOffset = 0;\n\n for (const mesh of meshesToMerge) {\n const positionAttribute = mesh.geometry.attributes\n .position as BufferAttribute;\n const positionArray = positionAttribute.array;\n\n const position = tmpVec3;\n for (let pt = 0; pt < positionArray.length; pt += 3) {\n position.set(\n positionArray[pt],\n positionArray[pt + 1],\n positionArray[pt + 2]\n );\n mesh.localToWorld(position);\n\n mergedPositions[pt + positionsOffset] = position.x;\n mergedPositions[pt + positionsOffset + 1] = position.y;\n mergedPositions[pt + positionsOffset + 2] = position.z;\n }\n\n const index = mesh.geometry.getIndex()?.array;\n\n if (index) {\n for (let tri = 0; tri < index.length; tri++) {\n mergedIndices[tri + indicesOffset] = index[tri] + indicesOffset;\n }\n } else {\n for (let i = 0; i < positionArray.length / 3; i++) {\n mergedIndices[i + indicesOffset] = i + indicesOffset;\n }\n }\n\n positionsOffset += positionArray.length;\n indicesOffset += positionArray.length / 3;\n }\n\n for (let i = 0; i < mergedIndices.length; i += 3) {\n const tmp = mergedIndices[i];\n mergedIndices[i] = mergedIndices[i + 2];\n mergedIndices[i + 2] = tmp;\n }\n\n return [mergedPositions, mergedIndices];\n};\n\nexport const threeToNavMesh = (\n meshes: Mesh[],\n navMeshConfig: Partial<NavMeshConfig> = {}\n): NavMeshGeneratorResult => {\n const [positions, indices] = getPositionsAndIndices(meshes);\n\n const navMeshGenerator = new NavMeshGenerator();\n\n return navMeshGenerator.generate(positions, indices, navMeshConfig);\n};\n"],"names":["CrowdHelper","agents","agentMeshes","Map","recastCrowd","agentMaterial","constructor","crowd","this","MeshBasicMaterial","color","Group","updateAgents","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","navMeshMaterial","recastNavMesh","transparent","opacity","BufferGeometry","updateNavMesh","positions","indices","getDebugNavMesh","setAttribute","BufferAttribute","Float32Array","setIndex","Uint16Array","computeVertexNormals","TileCacheHelper","tileCache","obstacles","obstacleMeshes","obstacleMaterial","wireframe","wireframeLinewidth","updateObstacles","ref","obstacle","obstacleMesh","mesh","type","extent","angle","BoxGeometry","rotation","Error","CylinderGeometry","tmpVec3","getPositionsAndIndices","meshes","meshesToMerge","mergedPositionsLength","mergedIndicesLength","positionAttribute","attributes","itemSize","array","length","getIndex","push","mergedPositions","mergedIndices","Uint32Array","indicesOffset","positionsOffset","positionArray","pt","localToWorld","index","tri","tmp","threeToNavMesh","navMeshConfig","NavMeshGenerator","generate"],"mappings":"kPAeaA,EACXC,OAEAC,YAAiC,IAAIC,IAErCC,YAEAC,cAEAC,aAAYC,MAAEA,EAAKF,cAAEA,IACnBG,KAAKJ,YAAcG,EAEnBC,KAAKH,cAAgBA,GAAiB,IAAII,EAAkB,CAAEC,MAAO,QAErEF,KAAKP,OAAS,IAAIU,EAElBH,KAAKI,cACN,CAODA,eACE,MAAMC,EAAgBL,KAAKJ,YAAYU,YAEjCC,EAAS,IAAIC,IAAIR,KAAKN,YAAYe,QAExC,IAAK,MAAMC,KAAKL,EAAe,CAC7BE,EAAOI,OAAOD,GAEd,MAAME,EAAWZ,KAAKJ,YAAYiB,iBAAiBH,GAC7CI,EAAWd,KAAKJ,YAAYmB,iBAAiBL,GAC7CM,EAAchB,KAAKJ,YAAYqB,mBAAmBP,GAExD,IAAIQ,EAAYlB,KAAKN,YAAYyB,IAAIT,QAEnBU,IAAdF,IACFA,EAAYlB,KAAKqB,gBAAgBL,GAEjChB,KAAKP,OAAO6B,IAAIJ,GAChBlB,KAAKN,YAAY6B,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,EAAYlB,KAAKN,YAAYyB,IAAIY,GA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IAAK,MAAMzB,KAAQW,EAAe,CAChC,MAEMe,EAFoB1B,EAAKhC,SAAS+C,WACrCpE,SACqCsE,MAElCtE,EAAW6D,EACjB,IAAK,IAAImB,EAAK,EAAGA,EAAKD,EAAcR,OAAQS,GAAM,EAChDhF,EAASW,IACPoE,EAAcC,GACdD,EAAcC,EAAK,GACnBD,EAAcC,EAAK,IAErB3B,EAAK4B,aAAajF,GAElB0E,EAAgBM,EAAKF,GAAmB9E,EAASY,EACjD8D,EAAgBM,EAAKF,EAAkB,GAAK9E,EAASa,EACrD6D,EAAgBM,EAAKF,EAAkB,GAAK9E,EAASe,EAGvD,MAAMmE,EAAQ7B,EAAKhC,SAASmD,YAAYF,MAExC,GAAIY,EACF,IAAK,IAAIC,EAAM,EAAGA,EAAMD,EAAMX,OAAQY,IACpCR,EAAcQ,EAAMN,GAAiBK,EAAMC,GAAON,OAGpD,IAAK,IAAI/E,EAAI,EAAGA,EAAIiF,EAAcR,OAAS,EAAGzE,IAC5C6E,EAAc7E,EAAI+E,GAAiB/E,EAAI+E,EAI3CC,GAAmBC,EAAcR,OACjCM,GAAiBE,EAAcR,OAAS,CACzC,CAED,IAAK,IAAIzE,EAAI,EAAGA,EAAI6E,EAAcJ,OAAQzE,GAAK,EAAG,CAChD,MAAMsF,EAAMT,EAAc7E,GAC1B6E,EAAc7E,GAAK6E,EAAc7E,EAAI,GACrC6E,EAAc7E,EAAI,GAAKsF,CACxB,CAED,MAAO,CAACV,EAAiBC,EAAc,EAG5BU,EAAiB,CAC5BtB,EACAuB,EAAwC,MAExC,MAAOrD,EAAWC,GAAW4B,EAAuBC,GAIpD,OAFyB,IAAIwB,GAELC,SAASvD,EAAWC,EAASoD,EAAc"}
|
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.
|
|
9
|
+
"version": "0.1.1",
|
|
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.
|
|
27
|
+
"@recast-navigation/core": "0.1.1"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"three": "0.x.x"
|