|
@@ -1,12 +1,11 @@
|
|
|
-//import { Quaternion, Vector3, Matrix } from "../../Maths/math";
|
|
|
-import { INavigationEnginePlugin } from "../../Navigation/INavigationEngine";
|
|
|
+import { INavigationEnginePlugin, ICrowd } from "../../Navigation/INavigationEngine";
|
|
|
import { Logger } from "../../Misc/logger";
|
|
|
-//import { VertexBuffer } from "../../Meshes/buffer";
|
|
|
import { VertexData } from "../../Meshes/mesh.vertexData";
|
|
|
-//import { Nullable } from "../../types";
|
|
|
import { AbstractMesh } from "../../Meshes/abstractMesh";
|
|
|
import { Mesh } from "../../Meshes/mesh";
|
|
|
import { Scene } from "../../scene";
|
|
|
+import { Vector3 } from '../../Maths/math';
|
|
|
+import { TransformNode } from "../../Meshes/transformNode";
|
|
|
|
|
|
declare var Recast: any;
|
|
|
|
|
@@ -19,7 +18,7 @@ export class RecastJSPlugin implements INavigationEnginePlugin {
|
|
|
*/
|
|
|
public bjsRECAST: any = {};
|
|
|
public name: string = "RecastJSPlugin";
|
|
|
- private navMesh: any;
|
|
|
+ public navMesh: any;
|
|
|
/**
|
|
|
* Initializes the recastJS plugin
|
|
|
*/
|
|
@@ -38,46 +37,52 @@ export class RecastJSPlugin implements INavigationEnginePlugin {
|
|
|
this.navMesh = new this.bjsRECAST.NavMesh();
|
|
|
var meshIndices = mesh.getIndices();
|
|
|
var positions = mesh.getVerticesData('position');
|
|
|
-
|
|
|
- Logger.Error(`mesh infos vt=${mesh.getTotalVertices()} indices = ${mesh.getTotalIndices()}`);
|
|
|
- this.navMesh.Build(positions, mesh.getTotalVertices(), meshIndices, mesh.getTotalIndices(), rc);
|
|
|
+ this.navMesh.build(positions, mesh.getTotalVertices(), meshIndices, mesh.getTotalIndices(), rc);
|
|
|
}
|
|
|
|
|
|
createDebugNavMesh(scene: Scene): Mesh {
|
|
|
var tri: number;
|
|
|
var pt: number;
|
|
|
- var debugNavMesh = this.navMesh.GetDebugNavMesh();
|
|
|
- let triangleCount = debugNavMesh.TriangleCount();
|
|
|
- Logger.Error(`navmesh has ${triangleCount} triangles`);
|
|
|
+ var debugNavMesh = this.navMesh.getDebugNavMesh();
|
|
|
+ let triangleCount = debugNavMesh.getTriangleCount();
|
|
|
|
|
|
var indices = [];
|
|
|
var positions = [];
|
|
|
for (tri = 0; tri < triangleCount*3; tri++)
|
|
|
{
|
|
|
indices.push(tri);
|
|
|
- Logger.Error(`tri in=${tri}`);
|
|
|
}
|
|
|
for (tri = 0; tri < triangleCount; tri++)
|
|
|
{
|
|
|
for (pt = 0; pt < 3 ; pt++)
|
|
|
{
|
|
|
- let point = debugNavMesh.GetTriangle(tri).GetPoint(pt);
|
|
|
- positions.push(point.x(), point.y(), point.z());
|
|
|
- Logger.Error(`tri x=${point.x()} y=${point.y()} x=${point.z()}`);
|
|
|
+ let point = debugNavMesh.getTriangle(tri).getPoint(pt);
|
|
|
+ positions.push(point.x, point.y, point.z);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
var mesh = new Mesh("NavMeshDebug", scene);
|
|
|
var vertexData = new VertexData();
|
|
|
|
|
|
vertexData.indices = indices;
|
|
|
vertexData.positions = positions;
|
|
|
vertexData.applyToMesh(mesh, false);
|
|
|
+ return mesh;
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
+ getClosestPoint(position: Vector3) : Vector3
|
|
|
+ {
|
|
|
+ var p = new this.bjsRECAST.Vec3(position.x, position.y, position.z);
|
|
|
+ var ret = this.navMesh.getClosestPoint(p);
|
|
|
+ var pr = new Vector3(ret.x, ret.y, ret.z);
|
|
|
+ return pr;
|
|
|
+ }
|
|
|
|
|
|
- return mesh;
|
|
|
+ createCrowd(maxAgents: number, maxAgentRadius: number, scene: Scene) : ICrowd
|
|
|
+ {
|
|
|
+ var crowd = new RecastJSCrowd(this, maxAgents, maxAgentRadius);
|
|
|
+ scene.addCrowd(crowd);
|
|
|
+ return crowd;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -94,3 +99,62 @@ export class RecastJSPlugin implements INavigationEnginePlugin {
|
|
|
return this.bjsRECAST !== undefined;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+export class RecastJSCrowd implements ICrowd {
|
|
|
+ public bjsRECASTPlugin: RecastJSPlugin;
|
|
|
+ public recastCrowd: any = {};
|
|
|
+ public transforms:TransformNode[];
|
|
|
+ public agents:number[];
|
|
|
+ public constructor(plugin: RecastJSPlugin, maxAgents: number, maxAgentRadius: number) {
|
|
|
+ this.bjsRECASTPlugin = plugin;
|
|
|
+ this.recastCrowd = new this.bjsRECASTPlugin.bjsRECAST.Crowd(maxAgents, maxAgentRadius, this.bjsRECASTPlugin.navMesh.getNavMesh());
|
|
|
+ this.transforms = new Array<TransformNode>();
|
|
|
+ this.agents = new Array<number>();
|
|
|
+ }
|
|
|
+
|
|
|
+ addAgent(pos: Vector3, transform:TransformNode): number
|
|
|
+ {
|
|
|
+ var agentParams = new this.bjsRECASTPlugin.bjsRECAST.dtCrowdAgentParams();
|
|
|
+ agentParams.radius = 0.1;
|
|
|
+ agentParams.height = 0.1;
|
|
|
+ agentParams.maxAcceleration = 1.0;
|
|
|
+ agentParams.maxSpeed = 1.0;
|
|
|
+ agentParams.collisionQueryRange = 1.0;
|
|
|
+ agentParams.pathOptimizationRange = 1.0;
|
|
|
+ agentParams.separationWeight = 1.0;
|
|
|
+ agentParams.updateFlags = 7;
|
|
|
+ agentParams.obstacleAvoidanceType = 0;
|
|
|
+ agentParams.queryFilterType = 0;
|
|
|
+ agentParams.userData = 0;
|
|
|
+
|
|
|
+ var agentIndex = this.recastCrowd.addAgent(new this.bjsRECASTPlugin.bjsRECAST.Vec3(pos.x, pos.y, pos.z), agentParams);
|
|
|
+ this.transforms.push(transform);
|
|
|
+ this.agents.push(agentIndex);
|
|
|
+ return agentIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ getAgentPosition(index: number): Vector3 {
|
|
|
+ var agentPos = this.recastCrowd.getAgentPosition(index);
|
|
|
+ return new Vector3(agentPos.x, agentPos.y, agentPos.z);
|
|
|
+ }
|
|
|
+
|
|
|
+ agentGoto(index: number, destination: Vector3): void {
|
|
|
+ this.recastCrowd.agentGoto(index, new this.bjsRECASTPlugin.bjsRECAST.Vec3(destination.x, destination.y, destination.z));
|
|
|
+ }
|
|
|
+
|
|
|
+ removeAgent(index: number): void {
|
|
|
+ this.recastCrowd.removeAgent(index);
|
|
|
+ }
|
|
|
+
|
|
|
+ update(deltaTime: number): void {
|
|
|
+ // update crowd
|
|
|
+ this.recastCrowd.update(deltaTime);
|
|
|
+
|
|
|
+ // update transforms
|
|
|
+ var index:number;
|
|
|
+ for (index = 0; index < this.agents.length; index++)
|
|
|
+ {
|
|
|
+ this.transforms[index].position = this.getAgentPosition(this.agents[index]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|