diff --git a/sdk/jme3-navmesh-gen/src/com/jme3/gde/nmgen/NavMeshGenerator.java b/sdk/jme3-navmesh-gen/src/com/jme3/gde/nmgen/NavMeshGenerator.java index 7d9265df3..305fd196c 100644 --- a/sdk/jme3-navmesh-gen/src/com/jme3/gde/nmgen/NavMeshGenerator.java +++ b/sdk/jme3-navmesh-gen/src/com/jme3/gde/nmgen/NavMeshGenerator.java @@ -17,6 +17,8 @@ import java.nio.FloatBuffer; import org.critterai.nmgen.IntermediateData; import org.critterai.nmgen.NavmeshGenerator; import org.critterai.nmgen.TriangleMesh; +import org.openide.DialogDisplayer; +import org.openide.NotifyDescriptor; public class NavMeshGenerator implements Savable { @@ -37,8 +39,8 @@ public class NavMeshGenerator implements Savable { private int maxVertsPerPoly = 6; private float contourSampleDistance = 25; private float contourMaxDeviation = 25; - private IntermediateData intermediateData; + private int timeout = 10000; public NavMeshGenerator() { } @@ -61,7 +63,7 @@ public class NavMeshGenerator implements Savable { System.out.println("Contour Sample Dist: " + contourSampleDistance); System.out.println("Contour Max Dev.: " + contourMaxDeviation); } - + public void setIntermediateData(IntermediateData data) { this.intermediateData = data; } @@ -89,7 +91,8 @@ public class NavMeshGenerator implements Savable { indices[i] = ib.get(i); } - TriangleMesh triMesh = nmgen.build(positions, indices, intermediateData); + + TriangleMesh triMesh = buildNavMesh(positions, indices, intermediateData); if (triMesh == null) { return null; } @@ -106,51 +109,79 @@ public class NavMeshGenerator implements Savable { return mesh2; } + private TriangleMesh buildNavMesh(float[] positions, int[] indices, IntermediateData intermediateData) { + MeshBuildRunnable runnable = new MeshBuildRunnable(positions, indices, intermediateData); + try { + execute(runnable, timeout); + } catch (TimeoutException ex) { + DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("NavMesh Generation timed out.")); + } + return runnable.getTriMesh(); + } + + private static void execute(Thread task, long timeout) throws TimeoutException { + task.start(); + try { + task.join(timeout); + } catch (InterruptedException e) { + } + if (task.isAlive()) { + task.interrupt(); + throw new TimeoutException(); + } + } + + private static void execute(Runnable task, long timeout) throws TimeoutException { + Thread t = new Thread(task, "Timeout guard"); + t.setDaemon(true); + execute(t, timeout); + } + public Mesh terrain2mesh(Terrain terr) { float[] heights = terr.getHeightMap(); int length = heights.length; int side = (int) FastMath.sqrt(heights.length); float[] vertices = new float[length * 3]; - int[] indices = new int[(side-1)*(side-1)*6]; - - Vector3f scale = ((Node)terr).getWorldScale().clone(); - Vector3f trans = ((Node)terr).getWorldTranslation().clone(); - trans.x -= terr.getTerrainSize()/2f; - trans.z -= terr.getTerrainSize()/2f; + int[] indices = new int[(side - 1) * (side - 1) * 6]; + + Vector3f scale = ((Node) terr).getWorldScale().clone(); + Vector3f trans = ((Node) terr).getWorldTranslation().clone(); + trans.x -= terr.getTerrainSize() / 2f; + trans.z -= terr.getTerrainSize() / 2f; float offsetX = trans.x * scale.x; float offsetZ = trans.z * scale.z; // do vertices - int i=0; - for (int z=0; z(), generator), mesh); Mesh optiMesh = generator.optimize(mesh); + if(optiMesh == null) return null; final Geometry navMesh = new Geometry("NavMesh"); Material material = new Material(pm, "Common/MatDefs/Misc/Unshaded.j3md");