Cedric Guillemet 6 rokov pred
rodič
commit
fec82fb620

+ 34 - 0
src/Navigation/INavigationEngine.ts

@@ -0,0 +1,34 @@
+//import { Nullable } from "../types";
+//import { Vector3, Quaternion } from "../Maths/math";
+import { AbstractMesh } from "../Meshes/abstractMesh";
+import { Mesh } from "../Meshes/mesh";
+import { Scene } from "../scene";
+
+
+/** @hidden */
+export interface INavigationEnginePlugin {
+    name: string;
+    createMavMesh(mesh: AbstractMesh): void;
+    createDebugNavMesh(scene: Scene): Mesh;
+    dispose(): void;
+    isSupported(): boolean;
+    check(): void;
+}
+
+/**
+ * Interface used to define a navigation engine
+ */
+export interface INavigationEngine {
+    
+    /**
+     * Release all resources
+     */
+    dispose(): void;
+
+    /**
+     * Builds a navmesh from a mesh
+     */
+    createMavMesh(mesh: AbstractMesh): void;
+    createDebugNavMesh(scene: Scene): Mesh;
+    check(): void;
+}

+ 51 - 0
src/Navigation/NavigationEngine.ts

@@ -0,0 +1,51 @@
+//import { Nullable } from "../types";
+//import { Vector3 } from "../Maths/math";
+import { INavigationEngine, INavigationEnginePlugin } from "./INavigationEngine";
+import { _DevTools } from '../Misc/devTools';
+import { AbstractMesh } from "../Meshes/abstractMesh";
+import { Mesh } from "../Meshes/mesh";
+import { Scene } from "../scene";
+
+/**
+ * Class used to control navigation engine
+ */
+export class NavigationEngine implements INavigationEngine {
+
+    /**
+     * Factory used to create the default navigation plugin.
+     * @returns The default navigation plugin
+     */
+    public static DefaultPluginFactory(): INavigationEnginePlugin {
+        throw _DevTools.WarnImport("RecastJSPlugin");
+    }
+
+    /**
+     * Creates a new Navigation Engine
+     * @param _navigationPlugin defines the plugin to use (RecastJS by default)
+     */
+    constructor(private _navigationPlugin: INavigationEnginePlugin = NavigationEngine.DefaultPluginFactory()) {
+        if (!this._navigationPlugin.isSupported()) {
+            throw new Error("Navigation Engine " + this._navigationPlugin.name + " cannot be found. "
+                + "Please make sure it is included.");
+        }
+
+    }
+    createMavMesh(mesh: AbstractMesh): void {
+        this._navigationPlugin.createMavMesh(mesh);
+    }
+
+    createDebugNavMesh(scene: Scene): Mesh {
+        return this._navigationPlugin.createDebugNavMesh(scene);
+    }
+
+    /**
+     * Release all resources
+     */
+    public dispose(): void {
+        this._navigationPlugin.dispose();
+    }
+
+    public check(): void {
+        this._navigationPlugin.check();
+    }
+}

+ 1 - 0
src/Navigation/Plugins/index.ts

@@ -0,0 +1 @@
+export * from "./recastJSPlugin";

+ 96 - 0
src/Navigation/Plugins/recastJSPlugin.ts

@@ -0,0 +1,96 @@
+//import { Quaternion, Vector3, Matrix } from "../../Maths/math";
+import { INavigationEnginePlugin } 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";
+
+declare var Recast: any;
+
+/**
+ * RecastJS navigation plugin
+ */
+export class RecastJSPlugin implements INavigationEnginePlugin {
+    /**
+     * Reference to the Recast library
+     */
+    public bjsRECAST: any = {};
+    public name: string = "RecastJSPlugin";
+    private navMesh: any;
+    /**
+     * Initializes the recastJS plugin
+     */
+    public constructor(recastInjection: any = Recast) {
+        this.bjsRECAST = recastInjection();
+
+        if (!this.isSupported()) {
+            Logger.Error("RecastJS is not available. Please make sure you included the js file.");
+            return;
+        }
+        this.check();
+    }
+
+    createMavMesh(mesh: AbstractMesh): void {
+        var rc = new this.bjsRECAST.rcConfig();
+        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);
+    }
+
+    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 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()}`);
+            }
+        }
+        
+
+        var mesh = new Mesh("NavMeshDebug", scene);
+        var vertexData = new VertexData();
+
+        vertexData.indices = indices;
+        vertexData.positions = positions;
+        vertexData.applyToMesh(mesh, false);
+
+        
+
+        return mesh;
+    }
+
+    /**
+     * Disposes
+     */
+    public dispose() {
+        // Dispose of world
+    }
+
+    public check() {
+    }
+
+    public isSupported(): boolean {
+        return this.bjsRECAST !== undefined;
+    }
+}

+ 3 - 0
src/Navigation/index.ts

@@ -0,0 +1,3 @@
+export * from "./INavigationEngine";
+export * from "./NavigationEngine";
+export * from "./Plugins/index";

+ 1 - 0
src/index.ts

@@ -23,6 +23,7 @@ export * from "./Materials/index";
 export * from "./Maths/index";
 export * from "./Meshes/index";
 export * from "./Morph/index";
+export * from "./Navigation/index";
 export * from "./node";
 export * from "./Offline/index";
 export * from "./Particles/index";