Example #1
0
        static void OnNavMeshTileGenerated(
			long navMeshId, int x, int y, int nextX, int nextY, uint flags,

			int vertCount, float[] vertices,

			int pCount, int pVCount,																	// polygons
				uint[] pFlinks,
				ushort[] pVerts,
				ushort[] pNeighbors,
				ushort[] pFlags,
				ushort[] pvCounts,
				byte[] pAreas,
				byte[] pTypes,

			int lCount,																						// links
				uint[] lRefs,
				uint[] lNextLinks,
				byte[] lEdges,
				byte[] lSides,
				byte[] lBMins,
				byte[] lBMaxs,

            int dCount,
                ushort[] dVBases,
                ushort[] dVCounts,
                ushort[] dTBases,
                ushort[] dTCounts,

            int dVCount,
                float[] dVerts,

            int dTCount,
                byte[] dTris,

			int omCount,																					// off mesh connections
				float[] omPos,
				float[] omRads,
				ushort[] omPolys,
				byte[] omFlags,
				byte[] omSides
				)
        {
            NavMesh mesh;
            if (!UnfinishedNavMeshes.TryGetValue(navMeshId, out mesh))
            {
                throw new Exception("Tile sent for non-existing mesh: " + navMeshId);
            }

            // vertices
            var realVertCount = vertCount / 3;
            var realVerts = new Vector3[realVertCount];
            var idx = 0;
            for (var i = 0; i < realVertCount; i++)
            {
                realVerts[i] = new Vector3(vertices[idx++], vertices[idx++], vertices[idx++]);
            }

            // polygons
            var polys = new NavMeshPolygon[pCount];
            var vidx = 0;
            for (var i = 0; i < pCount; i++)
            {
                var verts = new ushort[MaxVertsPerPolygon];
                var neighbors = new ushort[MaxVertsPerPolygon];
                Array.Copy(pVerts, vidx, verts, 0, MaxVertsPerPolygon);
                Array.Copy(pNeighbors, vidx, neighbors, 0, MaxVertsPerPolygon);
                polys[i] = new NavMeshPolygon
                {
                    FirstLink = pFlinks[i],
                    Flags = pFlags[i],
                    Neighbors = neighbors,
                    Vertices = verts,
                    Area = pAreas[i],
                    Type = (NavMeshPolyTypes)pTypes[i],
                    VertCount = pvCounts[i]
                };

                //var count = 1;
                //for (var j = 1; j < MaxVertsPerPolygon; j++)
                //{
                //    if (polys[i].Vertices[j] <= 0) continue;
                //    count++;
                //}
                //polys[i].VertCount = (byte)count;

                vidx += MaxVertsPerPolygon;
            }

            // links
            var links = new NavMeshPolyLink[lCount];
            for (var i = 0; i < lCount; i++)
            {
                links[i] = new NavMeshPolyLink
                {
                    Bmax = lBMaxs[i],
                    Bmin = lBMins[i],
                    Edge = lEdges[i],
                    Next = lNextLinks[i],
                    Reference = lRefs[i],
                    Side = lSides[i]
                };
            }

            var details = new NavMeshPolyDetail[dCount];
            for (var i = 0; i < dCount; i++)
            {
                details[i] = new NavMeshPolyDetail
                {
                    TriBase = dTBases[i],
                    TriCount = dTCounts[i],
                    VertBase = dVBases[i],
                    VertCount = dVCounts[i]
                };
            }

            if (dVCount == 0) Console.WriteLine("Managed dVertCount: {0}", dVCount);
            var detailVerts = new Vector3[dVCount/3];
            var dVIdx = 0;
            for (var i = 0; i < (dVCount/3); i++)
            {
                detailVerts[i] = new Vector3
                {
                    X = dVerts[dVIdx++],
                    Y = dVerts[dVIdx++],
                    Z = dVerts[dVIdx++]
                };
            }

            if (dTCount == 0) Console.WriteLine("Managed dTriCount: {0}", dTCount);
            var detailTris = new NavMeshDetailTriIndex[dTCount/4];
            var dTIdx = 0;
            for (var i = 0; i < (dTCount/4); i++)
            {
                detailTris[i] = new NavMeshDetailTriIndex
                {
                    Index0 = dTris[dTIdx++],
                    Index1 = dTris[dTIdx++],
                    Index2 = dTris[dTIdx++]
                };
                dTIdx++;
            }

            // Off-mesh connections
            var omCons = new NavOffMeshConnection[omCount];
            var pidx = 0;
            for (var i = 0; i < omCount; i++)
            {
                var pos = new[]{
                    new Vector3(omPos[pidx++], omPos[pidx++], omPos[pidx++]),
                    new Vector3(omPos[pidx++], omPos[pidx++], omPos[pidx++])
                };

                omCons[i] = new NavOffMeshConnection
                {
                    Flags = omFlags[i],
                    Polygon = omPolys[i],
                    Positions = pos,
                    Radius = omRads[i],
                    Side = omSides[i]
                };
            }

            var tile = new NavMeshTile
            {
                X = x,
                Y = y,
                NextX = nextX,
                NextY = nextY,
                Flags = flags,
                Vertices = realVerts,
                Polygons = polys,
                Links = links,
                OffMeshConnections = omCons,
                DetailPolygons = details,
                DetailedVertices = detailVerts,
                DetailedTriangles = detailTris
            };

            mesh.Tiles[x, y] = tile;

            if (mesh.Initialized)
            {
                var evt = NavMeshTileAdded;
                if (evt != null)
                {
                    evt(tile);
                }
            }
        }
Example #2
0
        static void OnNavMeshTileGenerated(
            long navMeshId, int x, int y, int nextX, int nextY, uint flags,

            int vertCount, float[] vertices,

            int pCount, int pVCount,                                                                                                                                                    // polygons
            uint[] pFlinks,
            ushort[] pVerts,
            ushort[] pNeighbors,
            ushort[] pFlags,
            ushort[] pvCounts,
            byte[] pAreas,
            byte[] pTypes,

            int lCount,                                                                                                                                                                                         // links
            uint[] lRefs,
            uint[] lNextLinks,
            byte[] lEdges,
            byte[] lSides,
            byte[] lBMins,
            byte[] lBMaxs,

            int dCount,
            ushort[] dVBases,
            ushort[] dVCounts,
            ushort[] dTBases,
            ushort[] dTCounts,

            int dVCount,
            float[] dVerts,

            int dTCount,
            byte[] dTris,

            int omCount,                                                                                                                                                                                // off mesh connections
            float[] omPos,
            float[] omRads,
            ushort[] omPolys,
            byte[] omFlags,
            byte[] omSides
            )
        {
            NavMesh mesh;

            if (!UnfinishedNavMeshes.TryGetValue(navMeshId, out mesh))
            {
                throw new Exception("Tile sent for non-existing mesh: " + navMeshId);
            }

            // vertices
            var realVertCount = vertCount / 3;
            var realVerts     = new Vector3[realVertCount];
            var idx           = 0;

            for (var i = 0; i < realVertCount; i++)
            {
                realVerts[i] = new Vector3(vertices[idx++], vertices[idx++], vertices[idx++]);
            }

            // polygons
            var polys = new NavMeshPolygon[pCount];
            var vidx  = 0;

            for (var i = 0; i < pCount; i++)
            {
                var verts     = new ushort[MaxVertsPerPolygon];
                var neighbors = new ushort[MaxVertsPerPolygon];
                Array.Copy(pVerts, vidx, verts, 0, MaxVertsPerPolygon);
                Array.Copy(pNeighbors, vidx, neighbors, 0, MaxVertsPerPolygon);
                polys[i] = new NavMeshPolygon
                {
                    FirstLink = pFlinks[i],
                    Flags     = pFlags[i],
                    Neighbors = neighbors,
                    Vertices  = verts,
                    Area      = pAreas[i],
                    Type      = (NavMeshPolyTypes)pTypes[i],
                    VertCount = pvCounts[i]
                };

                //var count = 1;
                //for (var j = 1; j < MaxVertsPerPolygon; j++)
                //{
                //    if (polys[i].Vertices[j] <= 0) continue;
                //    count++;
                //}
                //polys[i].VertCount = (byte)count;

                vidx += MaxVertsPerPolygon;
            }

            // links
            var links = new NavMeshPolyLink[lCount];

            for (var i = 0; i < lCount; i++)
            {
                links[i] = new NavMeshPolyLink
                {
                    Bmax      = lBMaxs[i],
                    Bmin      = lBMins[i],
                    Edge      = lEdges[i],
                    Next      = lNextLinks[i],
                    Reference = lRefs[i],
                    Side      = lSides[i]
                };
            }

            var details = new NavMeshPolyDetail[dCount];

            for (var i = 0; i < dCount; i++)
            {
                details[i] = new NavMeshPolyDetail
                {
                    TriBase   = dTBases[i],
                    TriCount  = dTCounts[i],
                    VertBase  = dVBases[i],
                    VertCount = dVCounts[i]
                };
            }

            if (dVCount == 0)
            {
                Console.WriteLine("Managed dVertCount: {0}", dVCount);
            }
            var detailVerts = new Vector3[dVCount / 3];
            var dVIdx       = 0;

            for (var i = 0; i < (dVCount / 3); i++)
            {
                detailVerts[i] = new Vector3
                {
                    X = dVerts[dVIdx++],
                    Y = dVerts[dVIdx++],
                    Z = dVerts[dVIdx++]
                };
            }

            if (dTCount == 0)
            {
                Console.WriteLine("Managed dTriCount: {0}", dTCount);
            }
            var detailTris = new NavMeshDetailTriIndex[dTCount / 4];
            var dTIdx      = 0;

            for (var i = 0; i < (dTCount / 4); i++)
            {
                detailTris[i] = new NavMeshDetailTriIndex
                {
                    Index0 = dTris[dTIdx++],
                    Index1 = dTris[dTIdx++],
                    Index2 = dTris[dTIdx++]
                };
                dTIdx++;
            }

            // Off-mesh connections
            var omCons = new NavOffMeshConnection[omCount];
            var pidx   = 0;

            for (var i = 0; i < omCount; i++)
            {
                var pos = new[] {
                    new Vector3(omPos[pidx++], omPos[pidx++], omPos[pidx++]),
                    new Vector3(omPos[pidx++], omPos[pidx++], omPos[pidx++])
                };

                omCons[i] = new NavOffMeshConnection
                {
                    Flags     = omFlags[i],
                    Polygon   = omPolys[i],
                    Positions = pos,
                    Radius    = omRads[i],
                    Side      = omSides[i]
                };
            }

            var tile = new NavMeshTile
            {
                X                  = x,
                Y                  = y,
                NextX              = nextX,
                NextY              = nextY,
                Flags              = flags,
                Vertices           = realVerts,
                Polygons           = polys,
                Links              = links,
                OffMeshConnections = omCons,
                DetailPolygons     = details,
                DetailedVertices   = detailVerts,
                DetailedTriangles  = detailTris
            };

            mesh.Tiles[x, y] = tile;

            if (mesh.Initialized)
            {
                var evt = NavMeshTileAdded;
                if (evt != null)
                {
                    evt(tile);
                }
            }
        }