Bläddra i källkod

Merge pull request #9432 from CedricGuillemet/navInstances

Added support for thin instances in navigation mesh creation
David Catuhe 4 år sedan
förälder
incheckning
878aa106ea
2 ändrade filer med 33 tillägg och 13 borttagningar
  1. 4 0
      dist/preview release/what's new.md
  2. 29 13
      src/Navigation/Plugins/recastJSPlugin.ts

+ 4 - 0
dist/preview release/what's new.md

@@ -15,6 +15,10 @@
 
 - Added support for EXT_meshopt_compression for glTF loader. ([zeux](https://github.com/zeux))
 
+### Navigation
+
+- Added support for thin instances in navigation mesh creation ([CedricGuillemet](https://github.com/CedricGuillemet))
+
 ### Materials
 
 - Added an `OcclusionMaterial` to simplify depth-only rendering of geometry ([rgerd](https://github.com/rgerd))

+ 29 - 13
src/Navigation/Plugins/recastJSPlugin.ts

@@ -3,7 +3,7 @@ import { Logger } from "../../Misc/logger";
 import { VertexData } from "../../Meshes/mesh.vertexData";
 import { Mesh } from "../../Meshes/mesh";
 import { Scene } from "../../scene";
-import { Epsilon, Vector3 } from '../../Maths/math';
+import { Epsilon, Vector3, Matrix } from '../../Maths/math';
 import { TransformNode } from "../../Meshes/transformNode";
 import { Observer } from "../../Misc/observable";
 import { Nullable } from "../../types";
@@ -134,21 +134,37 @@ export class RecastJSPlugin implements INavigationEnginePlugin {
                     continue;
                 }
 
-                const wm = mesh.computeWorldMatrix(true);
-
-                for (tri = 0; tri < meshIndices.length; tri++) {
-                    indices.push(meshIndices[tri] + offset);
+                var worldMatrices = [];
+                const worldMatrix = mesh.computeWorldMatrix(true);
+
+                if (mesh.hasThinInstances) {
+                    let thinMatrices = (mesh as Mesh).thinInstanceGetWorldMatrices();
+                    for (let instanceIndex = 0; instanceIndex < thinMatrices.length; instanceIndex++) {
+                        const tmpMatrix = new Matrix();
+                        let thinMatrix = thinMatrices[instanceIndex];
+                        thinMatrix.multiplyToRef(worldMatrix, tmpMatrix);
+                        worldMatrices.push(tmpMatrix);
+                    }
+                } else {
+                    worldMatrices.push(worldMatrix);
                 }
 
-                var transformed = Vector3.Zero();
-                var position = Vector3.Zero();
-                for (pt = 0; pt < meshPositions.length; pt += 3) {
-                    Vector3.FromArrayToRef(meshPositions, pt, position);
-                    Vector3.TransformCoordinatesToRef(position, wm, transformed);
-                    positions.push(transformed.x, transformed.y, transformed.z);
+                for (let matrixIndex = 0; matrixIndex < worldMatrices.length; matrixIndex ++) {
+                    const wm = worldMatrices[matrixIndex];
+                    for (tri = 0; tri < meshIndices.length; tri++) {
+                        indices.push(meshIndices[tri] + offset);
+                    }
+
+                    var transformed = Vector3.Zero();
+                    var position = Vector3.Zero();
+                    for (pt = 0; pt < meshPositions.length; pt += 3) {
+                        Vector3.FromArrayToRef(meshPositions, pt, position);
+                        Vector3.TransformCoordinatesToRef(position, wm, transformed);
+                        positions.push(transformed.x, transformed.y, transformed.z);
+                    }
+
+                    offset += meshPositions.length / 3;
                 }
-
-                offset += meshPositions.length / 3;
             }
         }