示例#1
0
        /// <summary>
        /// Converts the whole geometry to a <see cref="Mesh"/>.
        /// </summary>
        /// <returns>An equivalent instance of <see cref="Mesh"/>.</returns>
        public Mesh ToMesh()
        {
            // TODO: make a big case decision based on HasTexCoords and HasNormals around the implementation and implement each case individually

            Dictionary <TripleInx, int> _vDict = new Dictionary <TripleInx, int>();

            List <ushort> mTris      = new List <ushort>();
            List <float3> mVerts     = new List <float3>();
            List <float2> mTexCoords = (HasTexCoords) ? new List <float2>() : null;
            List <float3> mNormals   = (HasNormals) ? new List <float3>() : null;

            foreach (Face f in _faces)
            {
                int[] mFace = new int[f.InxVert.Length];
                for (int i = 0; i < f.InxVert.Length; i++)
                {
                    TripleInx ti = new TripleInx()
                    {
                        iV = f.InxVert[i],
                        iT = (HasTexCoords) ? f.InxTexCoord[i] : 0,
                        iN = (HasNormals) ? f.InxNormal[i] : 0
                    };
                    int inx;
                    if (!_vDict.TryGetValue(ti, out inx))
                    {
                        // Create a new vertex triplet combination
                        int vInx = f.InxVert[i];
                        mVerts.Add(new float3((float)_vertices[vInx].x, (float)_vertices[vInx].y,
                                              (float)_vertices[vInx].z));
                        if (HasTexCoords)
                        {
                            int tInx = f.InxTexCoord[i];
                            mTexCoords.Add(new float2((float)_texCoords[tInx].x, (float)_texCoords[tInx].y));
                        }
                        if (HasNormals)
                        {
                            int nInx = f.InxNormal[i];
                            mNormals.Add(new float3((float)_normals[nInx].x, (float)_normals[nInx].y,
                                                    (float)_normals[nInx].z));
                        }
                        inx = mVerts.Count - 1;
                        _vDict.Add(ti, inx);
                    }
                    mFace[i] = inx;
                }
                mTris.AddRange(Triangulate(f, mFace));
            }

            Mesh m = new Mesh();

            m.Vertices = mVerts.ToArray();
            if (HasNormals)
            {
                m.Normals = mNormals.ToArray();
            }
            if (HasTexCoords)
            {
                m.UVs = mTexCoords.ToArray();
            }

            m.Triangles = mTris.ToArray();
            return(m);
        }
        /// <summary>
        /// Converts the whole geomentry to a <see cref="Mesh"/>.
        /// </summary>
        /// <returns>An equivalent instance of <see cref="Mesh"/>.</returns>
        public Mesh ToMesh()
        {
            // TODO: make a big case decision based on HasTexCoords and HasNormals around the implementation and implement each case individually

            Dictionary<TripleInx, int> _vDict = new Dictionary<TripleInx, int>();

            List<ushort> mTris = new List<ushort>();
            List<float3> mVerts = new List<float3>();
            List<float2> mTexCoords = (HasTexCoords) ? new List<float2>() : null;
            List<float3> mNormals = (HasNormals) ? new List<float3>() : null;

            foreach (Face f in _faces)
            {
                int[] mFace = new int[f.InxVert.Length];
                for (int i = 0; i < f.InxVert.Length; i++)
                {
                    TripleInx ti = new TripleInx()
                                       {
                                           iV = f.InxVert[i],
                                           iT = (HasTexCoords) ? f.InxTexCoord[i] : 0,
                                           iN = (HasNormals) ? f.InxNormal[i] : 0
                                       };
                    int inx;
                    if (!_vDict.TryGetValue(ti, out inx))
                    {
                        // Create a new vertex triplet combination
                        int vInx = f.InxVert[i];
                        mVerts.Add(new float3((float) _vertices[vInx].x, (float) _vertices[vInx].y,
                                              (float) _vertices[vInx].z));
                        if (HasTexCoords)
                        {
                            int tInx = f.InxTexCoord[i];
                            mTexCoords.Add(new float2((float) _texCoords[tInx].x, (float) _texCoords[tInx].y));
                        }
                        if (HasNormals)
                        {
                            int nInx = f.InxNormal[i];
                            mNormals.Add(new float3((float) _normals[nInx].x, (float) _normals[nInx].y,
                                                    (float) _normals[nInx].z));
                        }
                        inx = mVerts.Count - 1;
                        _vDict.Add(ti, inx);
                    }
                    mFace[i] = inx;
                }
                mTris.AddRange(Triangulate(f, mFace));
            }

            Mesh m = new Mesh();
            m.Vertices = mVerts.ToArray();
            if (HasNormals)
                m.Normals = mNormals.ToArray();
            if (HasTexCoords)
                m.UVs = mTexCoords.ToArray();

            m.Triangles = mTris.ToArray();
            return m;
        }