Example #1
0
        /// <summary>
        /// Find the distance from a point to a triangle.
        /// </summary>
        /// <param name="indexPoly">Current polygon's index</param>
        /// <param name="pos">Current position</param>
        /// <param name="h">Resulting height</param>
        /// <returns>True, if a height is found. False, if otherwise.</returns>
        public bool ClosestHeight(int indexPoly, Vector3 pos, out float h)
        {
            Poly poly = Polys[indexPoly];

            PolyMeshDetail.MeshData pd = DetailMeshes[indexPoly];

            //find height at the location
            for (int j = 0; j < DetailMeshes[indexPoly].TriangleCount; j++)
            {
                PolyMeshDetail.TriangleData t = DetailTris[pd.TriangleIndex + j];
                Vector3[] v = new Vector3[3];

                for (int k = 0; k < 3; k++)
                {
                    if (t[k] < poly.VertCount)
                    {
                        v[k] = Verts[poly.Verts[t[k]]];
                    }
                    else
                    {
                        v[k] = DetailVerts[pd.VertexIndex + (t[k] - poly.VertCount)];
                    }
                }

                if (Distance.PointToTriangle(pos, v[0], v[1], v[2], out h))
                {
                    return(true);
                }
            }

            h = float.MaxValue;
            return(false);
        }
        private NavTile DeserializeMeshTile(ref Stream stream, NavPolyIdManager manager, out NavPolyId baseRef)
        {
            NavTile tile;

            using (var binaryReader = new BinaryReader(stream))
            {
                var id = binaryReader.ReadInt32();
                baseRef = new NavPolyId(id);

                var x        = binaryReader.ReadInt32();
                var y        = binaryReader.ReadInt32();
                var location = new Vector2i(x, y);

                var layer = binaryReader.ReadInt32();

                tile      = new NavTile(location, layer, manager, baseRef);
                tile.Salt = binaryReader.ReadInt32();

                var minX = binaryReader.ReadSingle();
                var minY = binaryReader.ReadSingle();
                var minZ = binaryReader.ReadSingle();
                var maxX = binaryReader.ReadSingle();
                var maxY = binaryReader.ReadSingle();
                var maxZ = binaryReader.ReadSingle();
                tile.Bounds = new BBox3(minX, minY, minZ, maxX, maxY, maxZ);

                var polysCount = binaryReader.ReadInt32();
                var polys      = new NavPoly[polysCount];

                for (var i = 0; i < polysCount; i++)
                {
                    var poly = new NavPoly();
                    poly.PolyType = (NavPolyType)binaryReader.ReadByte();

                    var polyLinksCount = binaryReader.ReadInt32();

                    for (var j = 0; j < polyLinksCount; j++)
                    {
                        var navPolyId = binaryReader.ReadInt32();

                        var link = new Link();
                        link.Reference = new NavPolyId(navPolyId);
                        link.Edge      = binaryReader.ReadInt32();
                        link.Side      = (BoundarySide)binaryReader.ReadByte();
                        link.BMin      = binaryReader.ReadInt32();
                        link.BMax      = binaryReader.ReadInt32();

                        poly.Links.Add(link);
                    }

                    var polyVertsCount = binaryReader.ReadInt32();
                    poly.Verts = new int[polyVertsCount];

                    for (var j = 0; j < polyVertsCount; j++)
                    {
                        poly.Verts[j] = binaryReader.ReadInt32();
                    }

                    var polyNeisCount = binaryReader.ReadInt32();
                    poly.Neis = new int[polyNeisCount];

                    for (var j = 0; j < polyNeisCount; j++)
                    {
                        poly.Neis[j] = binaryReader.ReadInt32();
                    }

                    var polyTag = binaryReader.ReadByte();

                    if (polyTag == 0xFE)
                    {
                        poly.Tag = null;
                    }
                    else
                    {
                        poly.Tag = (OffMeshConnectionFlags)polyTag;
                    }

                    poly.VertCount = binaryReader.ReadInt32();

                    var areaId = binaryReader.ReadByte();
                    poly.Area = new Area(areaId);

                    polys[i] = poly;
                }

                tile.Polys     = polys;
                tile.PolyCount = polysCount;

                var vertsCount = binaryReader.ReadInt32();
                var verts      = new Vector3[vertsCount];

                for (var i = 0; i < vertsCount; i++)
                {
                    var vx   = binaryReader.ReadSingle();
                    var vy   = binaryReader.ReadSingle();
                    var vz   = binaryReader.ReadSingle();
                    var vert = new Vector3(vx, vy, vz);

                    verts[i] = vert;
                }

                tile.Verts = verts;

                var detailMeshesCount = binaryReader.ReadInt32();
                var detailMeshes      = new PolyMeshDetail.MeshData[detailMeshesCount];

                for (var i = 0; i < detailMeshesCount; i++)
                {
                    var detailMesh = new PolyMeshDetail.MeshData();
                    detailMesh.VertexIndex   = binaryReader.ReadInt32();
                    detailMesh.VertexCount   = binaryReader.ReadInt32();
                    detailMesh.TriangleIndex = binaryReader.ReadInt32();
                    detailMesh.TriangleCount = binaryReader.ReadInt32();

                    detailMeshes[i] = detailMesh;
                }

                tile.DetailMeshes = detailMeshes;

                var detailVertsCount = binaryReader.ReadInt32();
                var detailVerts      = new Vector3[detailVertsCount];

                for (var i = 0; i < detailVertsCount; i++)
                {
                    var vx         = binaryReader.ReadSingle();
                    var vy         = binaryReader.ReadSingle();
                    var vz         = binaryReader.ReadSingle();
                    var detailVert = new Vector3(vx, vy, vz);

                    detailVerts[i] = detailVert;
                }

                tile.DetailVerts = detailVerts;

                var detailTrisCount = binaryReader.ReadInt32();
                var detailTris      = new PolyMeshDetail.TriangleData[detailTrisCount];

                for (var i = 0; i < detailTrisCount; i++)
                {
                    var hash0     = binaryReader.ReadInt32();
                    var hash1     = binaryReader.ReadInt32();
                    var hash2     = binaryReader.ReadInt32();
                    var flags     = binaryReader.ReadInt32();
                    var detailTri = new PolyMeshDetail.TriangleData(hash0, hash1, hash2, flags);

                    detailTris[i] = detailTri;
                }

                tile.DetailTris = detailTris;

                var offMeshConnectionsCount = binaryReader.ReadInt32();

                for (var i = 0; i < offMeshConnectionsCount; i++)
                {
                }

                var nodesCount = binaryReader.ReadInt32();
                var nodes      = new BVTree.Node[nodesCount];

                for (var i = 0; i < nodesCount; i++)
                {
                    var node = new BVTree.Node();
                    node.Bounds.Min.X = binaryReader.ReadInt32();
                    node.Bounds.Min.Y = binaryReader.ReadInt32();
                    node.Bounds.Min.Z = binaryReader.ReadInt32();
                    node.Bounds.Max.X = binaryReader.ReadInt32();
                    node.Bounds.Max.Y = binaryReader.ReadInt32();
                    node.Bounds.Max.Z = binaryReader.ReadInt32();
                    node.Index        = binaryReader.ReadInt32();

                    nodes[i] = node;
                }

                tile.BVTree = new BVTree(nodes);

                tile.BvQuantFactor = binaryReader.ReadSingle();
                tile.BvNodeCount   = binaryReader.ReadInt32();
                tile.WalkableClimb = binaryReader.ReadSingle();
            }

            return(tile);
        }