GenerateNodes() public method

public GenerateNodes ( Vector3 vectorVertices, int triangles, Vector3 &originalVertices, Int3 &vertices ) : void
vectorVertices UnityEngine.Vector3
triangles int
originalVertices UnityEngine.Vector3
vertices Int3
return void
示例#1
0
        public void ScanCRecast()
        {
            //#if (!UNITY_EDITOR && !UNITY_STANDALONE_OSX && !UNITY_STANDALONE_WIN) || UNITY_WEBPLAYER || UNITY_IPHONE || UNITY_ANDROID || UNITY_DASHBOARD_WIDGET || UNITY_XBOX360 || UNITY_PS3
        #if (UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN)
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo.FileName = GetRecastPath();             //"/Users/arong/Recast/build/Debug/Recast";
            //System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo ();
            //startInfo.UseShellExecute = true;
            myProcess.StartInfo.UseShellExecute        = false;
            myProcess.StartInfo.RedirectStandardInput  = true;
            myProcess.StartInfo.RedirectStandardOutput = true;
            myProcess.StartInfo.Arguments = "";

            MeshFilter[] filters;
            ExtraMesh[]  extraMeshes;
            //Get all meshes which should be used
            CollectMeshes(out filters, out extraMeshes);

            Vector3[] inputVerts;
            int[]     inputTris;
            //Get polygon soup from meshes
            Voxelize.CollectMeshes(filters, extraMeshes, forcedBounds, out inputVerts, out inputTris);

            //Bild .obj file
            System.Text.StringBuilder arguments = new System.Text.StringBuilder();
            arguments.Append("o recastMesh.obj\n");
            for (int i = 0; i < inputVerts.Length; i++)
            {
                arguments.Append("v " + inputVerts[i].x.ToString("0.000") + " ").Append(inputVerts[i].y.ToString("0.000") + " ").Append(inputVerts[i].z.ToString("0.000"));
                arguments.Append("\n");
            }

            //Build .obj file tris
            for (int i = 0; i < inputTris.Length - 2; i += 3)
            {
                arguments.Append("f " + (inputTris[i] + 1) + "//0 ").Append((inputTris[i + 1] + 1) + "//0 ").Append((inputTris[i + 2] + 1) + "//0");
                //Debug.DrawLine (inputVerts[inputTris[i]],inputVerts[inputTris[i+1]],Color.red);
                //Debug.DrawLine (inputVerts[inputTris[i+1]],inputVerts[inputTris[i+2]],Color.red);
                //Debug.DrawLine (inputVerts[inputTris[i+2]],inputVerts[inputTris[i]],Color.red);

                arguments.Append("\n");
            }

            string tmpPath = System.IO.Path.GetTempPath();
            tmpPath += "recastMesh.obj";

            try {
                using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(tmpPath)) {
                    outfile.Write(arguments.ToString());
                }

                myProcess.StartInfo.Arguments = tmpPath
                                                + "\n" + cellSize + "\n" +
                                                cellHeight + "\n" +
                                                walkableHeight + "\n" +
                                                walkableClimb + "\n" +
                                                maxSlope + "\n" +
                                                maxEdgeLength + "\n" +
                                                contourMaxError + "\n" +
                                                regionMinSize + "\n" +
                                                characterRadius;


                /*		public int erosionRadius = 2; /< Voxels to erode away from the edges of the mesh /
                 * public float contourMaxError = 2F; /< Max distance from simplified edge to real edge /
                 *
                 * public float cellSize = 0.5F; /< Voxel sample size (x,z) /
                 * public float cellHeight = 0.4F; /< Voxel sample size (y) /
                 * public float walkableHeight = 2F; /< Character height/
                 * public float walkableClimb = 0.5F; /< Height the character can climb /
                 * public float maxSlope = 30; /< Max slope in degrees the character can traverse /
                 * public float maxEdgeLength = 20; /< Longer edges will be subdivided. Reducing this value can im
                 * public bool useCRecast = true;
                 * public bool includeOutOfBounds = false;*/

                myProcess.Start();
                System.IO.StreamReader sOut = myProcess.StandardOutput;

                //string result = sOut.ReadToEnd ();
                //Debug.Log (result);
                //return;

                bool      failed            = false;
                bool      startedVerts      = false;
                int       readVerts         = 0;
                bool      startedTris       = false;
                int       vCount            = -1;
                int       readTris          = 0;
                int       trisCount         = 0;
                Vector3[] verts             = null;
                int[]     tris              = null;
                int       internalVertCount = 0;

                Vector3 bmin = Vector3.zero;

                float cs = 1F;
                float ch = 1F;

                while (sOut.Peek() >= 0)
                {
                    string line = sOut.ReadLine();
                    int    resultInt;

                    if (line == "")
                    {
                        continue;
                    }

                    if (!int.TryParse(line, out resultInt))
                    {
                        //Debug.Log ("Syntax Error at '"+line+"'");
                        failed = true;
                        break;
                    }

                    if (!startedVerts)
                    {
                        verts = new Vector3[resultInt];

                        if (resultInt == 0)
                        {
                            failed = true;
                            break;
                        }

                        bmin.x = float.Parse(sOut.ReadLine());
                        bmin.y = float.Parse(sOut.ReadLine());
                        bmin.z = float.Parse(sOut.ReadLine());
                        cs     = float.Parse(sOut.ReadLine());
                        ch     = float.Parse(sOut.ReadLine());

                        startedVerts = true;
                        //Debug.Log ("Starting Reading "+resultInt+" verts "+bmin.ToString ()+" - "+cs+" * "+ch);
                    }
                    else if (readVerts < verts.Length)
                    {
                        resultInt *= 1;
                        if (internalVertCount == 0)
                        {
                            verts[readVerts].x = resultInt * cs + bmin.x;
                        }
                        else if (internalVertCount == 1)
                        {
                            verts[readVerts].y = resultInt * ch + bmin.y;
                        }
                        else
                        {
                            verts[readVerts].z = resultInt * cs + bmin.z;
                        }

                        internalVertCount++;

                        if (internalVertCount == 3)
                        {
                            internalVertCount = 0;
                            readVerts++;
                        }
                    }
                    else if (!startedTris)
                    {
                        trisCount   = resultInt;
                        startedTris = true;
                    }
                    else if (vCount == -1)
                    {
                        vCount = resultInt;
                        tris   = new int[trisCount * vCount];
                        //Debug.Log ("Starting Reading "+trisCount+" - "+tris.Length+" tris at vertsCount "+readVerts);
                        //Debug.Log ("Max vertices per polygon: "+vCount);
                    }
                    else if (readTris < tris.Length)
                    {
                        tris[readTris] = resultInt;
                        readTris++;
                    }
                }

                if (!myProcess.HasExited)
                {
                    myProcess.Kill();
                }

                sOut.Close();
                myProcess.Close();

                if (failed)
                {
                    return;
                }

                matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);

                NavMeshGraph.GenerateNodes(this, verts, tris, out _vectorVertices, out _vertices);
            } finally {
                //Debug.Log (tmpPath);
                System.IO.File.Delete(tmpPath);
            }
        #else
            Debug.LogError("The C++ version of recast can only be used in editor or standalone mode, I'm sure it cannot be used in the webplayer, but other platforms are not tested yet\n" +
                           "If you are in the Unity Editor, try switching Platform to Standalone just when scanning, scanned graphs can be cached to enable them to be used in a webplayer");
            _vectorVertices = new Vector3[0];
            _vertices       = new Int3[0];
            nodes           = new Node[0];
        #endif
        }
示例#2
0
        public override void Scan()
        {
            AstarProfiler.Reset();
            //AstarProfiler.StartProfile ("Base Scan");

            //base.Scan ();

            //AstarProfiler.EndProfile ("Base Scan");
            if (useCRecast)
            {
                ScanCRecast();
            }
            else
            {
                MeshFilter[] filters;
                ExtraMesh[]  extraMeshes;

                if (!CollectMeshes(out filters, out extraMeshes))
                {
                    nodes = new Node[0];
                    return;
                }

                Voxelize vox = new Voxelize(cellHeight, cellSize, walkableClimb, walkableHeight, maxSlope);

                vox.maxEdgeLength      = maxEdgeLength;
                vox.forcedBounds       = forcedBounds;
                vox.includeOutOfBounds = includeOutOfBounds;


                //g.GetComponent<Voxelize>();
                vox.VoxelizeMesh(filters, extraMeshes);

                /*bool[,] open = new bool[width,depth];
                 * int[,] visited = new int[width+1,depth+1];
                 *
                 * for (int z=0;z<depth;z++) {
                 *      for (int x = 0;x < width;x++) {
                 *              open[x,z] = graphNodes[z*width+x].walkable;
                 *      }
                 * }*/

                /*for (int i=0;i<depth*width;i++) {
                 *      open[i] = graphNodes[i].walkable;
                 * }
                 *
                 *
                 * int wd = width*depth;
                 *
                 * List<int> boundary = new List<int>();
                 *
                 * int p = 0;
                 *
                 * for (int i=0;i<wd;i++) {
                 *      if (!open[i]) {
                 *              boundary.Add (i);
                 *
                 *              p = i;
                 *
                 *              int backtrack = i-1;
                 *
                 *
                 *      }*/

                vox.ErodeWalkableArea(Mathf.CeilToInt(2 * characterRadius / cellSize));


                vox.BuildDistanceField();

                vox.BuildRegions();

                VoxelContourSet cset = new VoxelContourSet();

                vox.BuildContours(contourMaxError, 1, cset, Voxelize.RC_CONTOUR_TESS_WALL_EDGES);

                VoxelMesh mesh;

                vox.BuildPolyMesh(cset, 3, out mesh);


                Vector3[] vertices = new Vector3[mesh.verts.Length];

                AstarProfiler.StartProfile("Build Nodes");

                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i] = (Vector3)mesh.verts[i];
                }

                matrix = Matrix4x4.TRS(vox.voxelOffset, Quaternion.identity, Int3.Precision * Voxelize.CellScale);
                //Int3.Precision*Voxelize.CellScale+(Int3)vox.voxelOffset

                //GenerateNodes (this,vectorVertices,triangles, out originalVertices, out _vertices);

                NavMeshGraph.GenerateNodes(this, vertices, mesh.tris, out _vectorVertices, out _vertices);

                AstarProfiler.EndProfile("Build Nodes");

                AstarProfiler.PrintResults();
            }
        }