Ejemplo n.º 1
0
        unsafe private void ProcessMesh(hkaiNavMesh mesh)
        {
            var verts      = mesh.m_vertices;
            int indexCount = 0;

            foreach (var f in mesh.m_faces)
            {
                // Simple formula for indices count for a triangulation of a poly
                indexCount += (f.m_numEdges - 2) * 3;
            }

            var MeshIndices  = new int[indexCount * 3];
            var MeshVertices = new NavmeshLayout[indexCount * 3];

            PickingVertices = new Vector3[indexCount * 3];
            PickingIndices  = new int[indexCount * 3];

            var factory = Scene.Renderer.Factory;

            int idx = 0;

            int maxcluster = 0;

            for (int id = 0; id < mesh.m_faces.Count; id++)
            {
                if (mesh.m_faces[id].m_clusterIndex > maxcluster)
                {
                    maxcluster = mesh.m_faces[id].m_clusterIndex;
                }

                var sedge  = mesh.m_faces[id].m_startEdgeIndex;
                var ecount = mesh.m_faces[id].m_numEdges;

                // Use simple algorithm for convex polygon trianglization
                for (int t = 0; t < ecount - 2; t++)
                {
                    if (ecount > 3)
                    {
                        //ecount = ecount;
                    }
                    var end   = (t + 2 >= ecount) ? sedge : sedge + t + 2;
                    var vert1 = mesh.m_vertices[mesh.m_edges[sedge].m_a];
                    var vert2 = mesh.m_vertices[mesh.m_edges[sedge + t + 1].m_a];
                    var vert3 = mesh.m_vertices[mesh.m_edges[end].m_a];

                    MeshVertices[idx]     = new NavmeshLayout();
                    MeshVertices[idx + 1] = new NavmeshLayout();
                    MeshVertices[idx + 2] = new NavmeshLayout();

                    MeshVertices[idx].Position     = new Vector3(vert1.X, vert1.Y, vert1.Z);
                    MeshVertices[idx + 1].Position = new Vector3(vert2.X, vert2.Y, vert2.Z);
                    MeshVertices[idx + 2].Position = new Vector3(vert3.X, vert3.Y, vert3.Z);
                    PickingVertices[idx]           = new Vector3(vert1.X, vert1.Y, vert1.Z);
                    PickingVertices[idx + 1]       = new Vector3(vert2.X, vert2.Y, vert2.Z);
                    PickingVertices[idx + 2]       = new Vector3(vert3.X, vert3.Y, vert3.Z);
                    var n = Vector3.Normalize(Vector3.Cross(MeshVertices[idx + 2].Position - MeshVertices[idx].Position, MeshVertices[idx + 1].Position - MeshVertices[idx].Position));
                    MeshVertices[idx].Normal[0]     = (sbyte)(n.X * 127.0f);
                    MeshVertices[idx].Normal[1]     = (sbyte)(n.Y * 127.0f);
                    MeshVertices[idx].Normal[2]     = (sbyte)(n.Z * 127.0f);
                    MeshVertices[idx + 1].Normal[0] = (sbyte)(n.X * 127.0f);
                    MeshVertices[idx + 1].Normal[1] = (sbyte)(n.Y * 127.0f);
                    MeshVertices[idx + 1].Normal[2] = (sbyte)(n.Z * 127.0f);
                    MeshVertices[idx + 2].Normal[0] = (sbyte)(n.X * 127.0f);
                    MeshVertices[idx + 2].Normal[1] = (sbyte)(n.Y * 127.0f);
                    MeshVertices[idx + 2].Normal[2] = (sbyte)(n.Z * 127.0f);

                    MeshVertices[idx].Color[0]     = (byte)(157);
                    MeshVertices[idx].Color[1]     = (byte)(53);
                    MeshVertices[idx].Color[2]     = (byte)(255);
                    MeshVertices[idx].Color[3]     = (byte)(255);
                    MeshVertices[idx + 1].Color[0] = (byte)(157);
                    MeshVertices[idx + 1].Color[1] = (byte)(53);
                    MeshVertices[idx + 1].Color[2] = (byte)(255);
                    MeshVertices[idx + 1].Color[3] = (byte)(255);
                    MeshVertices[idx + 2].Color[0] = (byte)(157);
                    MeshVertices[idx + 2].Color[1] = (byte)(53);
                    MeshVertices[idx + 2].Color[2] = (byte)(255);
                    MeshVertices[idx + 2].Color[3] = (byte)(255);

                    MeshVertices[idx].Barycentric[0]     = (byte)(0);
                    MeshVertices[idx].Barycentric[1]     = (byte)(0);
                    MeshVertices[idx + 1].Barycentric[0] = (byte)(1);
                    MeshVertices[idx + 1].Barycentric[1] = (byte)(0);
                    MeshVertices[idx + 2].Barycentric[0] = (byte)(0);
                    MeshVertices[idx + 2].Barycentric[1] = (byte)(1);

                    MeshIndices[idx]        = idx;
                    MeshIndices[idx + 1]    = idx + 1;
                    MeshIndices[idx + 2]    = idx + 2;
                    PickingIndices[idx]     = idx;
                    PickingIndices[idx + 1] = idx + 1;
                    PickingIndices[idx + 2] = idx + 2;

                    idx += 3;
                }
            }

            VertexCount = MeshVertices.Length;
            IndexCount  = MeshIndices.Length;

            uint buffersize = (uint)IndexCount * 4u;

            if (VertexCount > 0)
            {
                fixed(void *ptr = PickingVertices)
                {
                    Bounds = BoundingBox.CreateFromPoints((Vector3 *)ptr, PickingVertices.Count(), 12, Quaternion.Identity, Vector3.Zero, Vector3.One);
                }
            }
            else
            {
                Bounds = new BoundingBox();
            }

            uint vbuffersize = (uint)MeshVertices.Length * NavmeshLayout.SizeInBytes;

            GeomBuffer = Scene.Renderer.GeometryBufferAllocator.Allocate(vbuffersize, buffersize, (int)NavmeshLayout.SizeInBytes, 4, (h) =>
            {
                h.FillIBuffer(MeshIndices, () =>
                {
                    MeshIndices = null;
                });
                h.FillVBuffer(MeshVertices, () =>
                {
                    MeshVertices = null;
                });
            });
        }
        unsafe private void ProcessMesh(NVM mesh)
        {
            var verts        = mesh.Vertices;
            var MeshIndices  = new int[mesh.Triangles.Count * 3];
            var MeshVertices = new NavmeshLayout[mesh.Triangles.Count * 3];

            PickingVertices = new Vector3[mesh.Triangles.Count * 3];
            PickingIndices  = new int[mesh.Triangles.Count * 3];

            var factory = Scene.Renderer.Factory;

            for (int id = 0; id < mesh.Triangles.Count; id++)
            {
                var i     = id * 3;
                var vert1 = mesh.Vertices[mesh.Triangles[id].VertexIndex1];
                var vert2 = mesh.Vertices[mesh.Triangles[id].VertexIndex2];
                var vert3 = mesh.Vertices[mesh.Triangles[id].VertexIndex3];

                MeshVertices[i]     = new NavmeshLayout();
                MeshVertices[i + 1] = new NavmeshLayout();
                MeshVertices[i + 2] = new NavmeshLayout();

                MeshVertices[i].Position     = new Vector3(vert1.X, vert1.Y, vert1.Z);
                MeshVertices[i + 1].Position = new Vector3(vert2.X, vert2.Y, vert2.Z);
                MeshVertices[i + 2].Position = new Vector3(vert3.X, vert3.Y, vert3.Z);
                PickingVertices[i]           = new Vector3(vert1.X, vert1.Y, vert1.Z);
                PickingVertices[i + 1]       = new Vector3(vert2.X, vert2.Y, vert2.Z);
                PickingVertices[i + 2]       = new Vector3(vert3.X, vert3.Y, vert3.Z);
                var n = Vector3.Normalize(Vector3.Cross(MeshVertices[i + 2].Position - MeshVertices[i].Position, MeshVertices[i + 1].Position - MeshVertices[i].Position));
                MeshVertices[i].Normal[0]     = (sbyte)(n.X * 127.0f);
                MeshVertices[i].Normal[1]     = (sbyte)(n.Y * 127.0f);
                MeshVertices[i].Normal[2]     = (sbyte)(n.Z * 127.0f);
                MeshVertices[i + 1].Normal[0] = (sbyte)(n.X * 127.0f);
                MeshVertices[i + 1].Normal[1] = (sbyte)(n.Y * 127.0f);
                MeshVertices[i + 1].Normal[2] = (sbyte)(n.Z * 127.0f);
                MeshVertices[i + 2].Normal[0] = (sbyte)(n.X * 127.0f);
                MeshVertices[i + 2].Normal[1] = (sbyte)(n.Y * 127.0f);
                MeshVertices[i + 2].Normal[2] = (sbyte)(n.Z * 127.0f);

                MeshVertices[i].Color[0]     = (byte)(157);
                MeshVertices[i].Color[1]     = (byte)(53);
                MeshVertices[i].Color[2]     = (byte)(255);
                MeshVertices[i].Color[3]     = (byte)(255);
                MeshVertices[i + 1].Color[0] = (byte)(157);
                MeshVertices[i + 1].Color[1] = (byte)(53);
                MeshVertices[i + 1].Color[2] = (byte)(255);
                MeshVertices[i + 1].Color[3] = (byte)(255);
                MeshVertices[i + 2].Color[0] = (byte)(157);
                MeshVertices[i + 2].Color[1] = (byte)(53);
                MeshVertices[i + 2].Color[2] = (byte)(255);
                MeshVertices[i + 2].Color[3] = (byte)(255);

                MeshVertices[i].Barycentric[0]     = (byte)(0);
                MeshVertices[i].Barycentric[1]     = (byte)(0);
                MeshVertices[i + 1].Barycentric[0] = (byte)(1);
                MeshVertices[i + 1].Barycentric[1] = (byte)(0);
                MeshVertices[i + 2].Barycentric[0] = (byte)(0);
                MeshVertices[i + 2].Barycentric[1] = (byte)(1);

                MeshIndices[i]        = i;
                MeshIndices[i + 1]    = i + 1;
                MeshIndices[i + 2]    = i + 2;
                PickingIndices[i]     = i;
                PickingIndices[i + 1] = i + 1;
                PickingIndices[i + 2] = i + 2;
            }

            VertexCount = MeshVertices.Length;
            IndexCount  = MeshIndices.Length;

            uint buffersize = (uint)IndexCount * 4u;

            if (VertexCount > 0)
            {
                fixed(void *ptr = PickingVertices)
                {
                    Bounds = BoundingBox.CreateFromPoints((Vector3 *)ptr, PickingVertices.Count(), 12, Quaternion.Identity, Vector3.Zero, Vector3.One);
                }
            }
            else
            {
                Bounds = new BoundingBox();
            }

            uint vbuffersize = (uint)MeshVertices.Length * NavmeshLayout.SizeInBytes;

            GeomBuffer = Scene.Renderer.GeometryBufferAllocator.Allocate(vbuffersize, buffersize, (int)NavmeshLayout.SizeInBytes, 4, (h) =>
            {
                h.FillIBuffer(MeshIndices, () =>
                {
                    MeshIndices = null;
                });
                h.FillVBuffer(MeshVertices, () =>
                {
                    MeshVertices = null;
                });
            });
        }