UNPKG

@soonspacejs/plugin-pathfinding

Version:

Pathfinding plugin for SoonSpace.js

2 lines (1 loc) 3.18 kB
import{Matrix4 as e,Mesh as s,InstancedMesh as r,MeshStandardMaterial as t}from"three";import{Crowd as o}from"@recast-navigation/core";import{generateSoloNavMesh as i}from"@recast-navigation/generators";import{DebugDrawer as a,CrowdHelper as d}from"@recast-navigation/three";import h from"soonspacejs";import{StaticGeometryGenerator as n}from"three-mesh-bvh";function c(r){const t=r.name,o=r.geometry,i=r.material,a=r.count,d=[];for(let h=0;h<a;h++){const a=new e;r.getMatrixAt(h,a);const n=new s(o,i);n.name=`${t}_${h}`,d.push(n),a.premultiply(r.matrixWorld),n.matrix.copy(a),n.matrixWorld.copy(a),a.decompose(n.position,n.quaternion,n.scale)}return d}function p(e){var s;const t=[];e.forEach((e=>{e.traverse((e=>{"Mesh"===e.type&&(e instanceof r?t.push(...c(e)):t.push(e))}))}));const o=new n(t);o.applyWorldTransforms=!0,o.attributes=["position","index"];const i=o.generate();return{positions:i.attributes.position.array,indices:null===(s=i.index)||void 0===s?void 0:s.array}}const{warn:l}=h.utils;class w{constructor(e){this.ssp=e,this.navMesh=null,this.crowd=null,this.debugDrawer=null,this.crowdHelper=null,this._crowdUpdate=()=>{var e;this.crowd&&(this.crowd.update(this.ssp.viewport.state.delta),null===(e=this.crowdHelper)||void 0===e||e.update(),this.crowd.getActiveAgentCount()>0&&this.ssp.render())}}createSoloNavMesh(e,s={}){if(this.navMesh)return l("navmesh 已经存在,请先调用 disposeNavMesh()"),this.navMesh;const{positions:r,indices:t}=p(e),o=i(r,t,s);return o.success?(this.navMesh=o.navMesh,this.navMesh):null}disposeSoloNavMesh(){var e;null===(e=this.navMesh)||void 0===e||e.destroy(),this.navMesh=null}createCrowd(e){return this.crowd?(l("crowd 已经存在,请先调用 disposeCrowd()"),this.crowd):this.navMesh?(this.crowd=new o(this.navMesh,e),this.ssp.signals.beforeRender.add(this._crowdUpdate),this.crowd):(l("navmesh 不存在,请先调用 createSoloNavMesh()"),null)}disposeCrowd(){var e;null===(e=this.crowd)||void 0===e||e.destroy(),this.crowd=null,this.ssp.signals.beforeRender.remove(this._crowdUpdate)}createDebugDrawer(){return this.debugDrawer?(l("debug drawer 已经存在,请先调用 disposeDebugDrawer()"),this.debugDrawer):this.navMesh?(this.debugDrawer=new a,this.debugDrawer.drawNavMesh(this.navMesh,0),this.ssp.addObject(this.debugDrawer),this.debugDrawer):(l("navmesh 不存在,请先调用 createSoloNavMesh()"),null)}disposeDebugDrawer(){this.debugDrawer&&(this.debugDrawer.dispose(),this.ssp.removeObject(this.debugDrawer),this.debugDrawer=null)}createCrowdHelper(e={agentMaterial:new t({color:65280})}){return this.crowdHelper?(l("crowd helper 已经存在,请先调用 disposeCrowdHelper()"),this.crowdHelper):this.crowd?(this.crowdHelper=new d(this.crowd,e),this.ssp.addObject(this.crowdHelper),this.crowdHelper):(l("crowd 不存在,请先调用 createCrowd()"),null)}disposeCrowdHelper(){this.crowdHelper&&(this.crowdHelper.agentMeshes.forEach((e=>{e.geometry.dispose(),e.material.dispose()})),this.ssp.removeObject(this.crowdHelper))}dispose(){this.disposeSoloNavMesh(),this.disposeCrowd(),this.disposeDebugDrawer(),this.disposeCrowdHelper()}}export{c as deInstancingMesh,w as default,p as getVertices};