Exemplo n.º 1
0
        //public void FromMesh(Mesh mesh)
        //{
        //    int[] adjacency=new int[mesh.NumberFaces*3];
        //    mesh.GenerateAdjacency(1,adjacency);
        //    Mesh m=mesh.Optimize(MeshFlags.SimplifyFace,adjacency);
        //    CustomVertex.PositionOnly[] vdata = (CustomVertex.PositionOnly[])m.LockVertexBuffer(typeof(CustomVertex.PositionOnly), LockFlags.None, m.NumberVertices);
        //    int[] ind = (int[])m.LockIndexBuffer(typeof(int), LockFlags.None, mesh.NumberFaces * 3);
        //    m_vertices = new MyVector[vdata.Length];
        //    for(int i=0;i<m_vertices.Length;i++)
        //    {
        //        m_vertices[i].X = vdata[i].X;
        //        m_vertices[i].Y = vdata[i].Y;
        //        m_vertices[i].Z = vdata[i].Z;
        //    }
        //}
        /// <summary>
        /// Generates collision mesh from arrays
        /// </summary>
        /// <param name="vertices">An Array of vertices</param>
        /// <param name="faces">3 zero based indices per face</param>
        public static CollisionMesh FromArrays(MyVector[] vertices, uint[] faces)
        {
            CollisionMesh mesh = new CollisionMesh();
            mesh.m_vertices = (MyVector[])vertices.Clone();
            mesh.m_faces = new CFace[faces.Length / 3];
            List<CEdge> edges = new List<CEdge>();

            mesh.boundingShpereRadius = 0;

            for (int i = 0; i < vertices.Length; i++)
            {
                if (vertices[i].Length > mesh.boundingShpereRadius)
                    mesh.boundingShpereRadius = vertices[i].Length;
            }
            for (int i = 0; i < faces.Length; i += 3)
            {
                mesh.m_faces[i / 3].v1 = faces[i];
                mesh.m_faces[i / 3].v2 = faces[i + 1];
                mesh.m_faces[i / 3].v3 = faces[i + 2];
                mesh.m_faces[i / 3].n = ((mesh.m_vertices[faces[i + 1]] - mesh.m_vertices[faces[i]]) ^
                    (mesh.m_vertices[faces[i + 2]] - mesh.m_vertices[faces[i]])).Normalize();

                bool be1 = false, be2 = false, be3 = false;
                CEdge e1, e2, e3;
                e1 = new CEdge();
                e1.v1 = faces[i];
                e1.v2 = faces[i + 1];

                e2 = new CEdge();
                e2.v1 = faces[i + 1];
                e2.v2 = faces[i + 2];

                e3 = new CEdge();
                e3.v1 = faces[i + 2];
                e3.v2 = faces[i];
                for (int j = 0; j < edges.Count; j++)
                {
                    if (!be1 && edges[j].CompareTo(e1) == 0)
                        be1 = true;

                    if (!be2 && edges[j].CompareTo(e2) == 0)
                        be2 = true;

                    if (!be3 && edges[j].CompareTo(e3) == 0)
                        be3 = true;
                }
                if (!be1)
                    edges.Add(e1);

                if (!be2)
                    edges.Add(e2);

                if (!be3)
                    edges.Add(e3);

            }

            mesh.m_edges = edges.ToArray();
            mesh.m_hardPoints = (MyVector[])vertices.Clone();
            return mesh;
        }