Example #1
0
 public static ReaderBase FindReaderForExtension(string extension)
 {
     if (((IList)FbxReader.GetExtensions()).Contains(extension))
     {
         return(new FbxReader());
     }
     if (((IList)GltfReader.GetExtensions()).Contains(extension))
     {
         return(new GltfReader());
     }
     if (((IList)ObjReader.GetExtensions()).Contains(extension))
     {
         return(new ObjReader());
     }
     if (((IList)StlReader.GetExtensions()).Contains(extension))
     {
         return(new StlReader());
     }
     if (((IList)PlyReader.GetExtensions()).Contains(extension))
     {
         return(new PlyReader());
     }
     if (((IList)ThreeMfReader.GetExtensions()).Contains(extension))
     {
         return(new ThreeMfReader());
     }
     return(null);
 }
        public void LoadPly(string path)
        {
            var reader = new PlyReader();
            var objCol = reader.Read(path);

            AttachModelList(objCol);
        }
        public MeshData Convert(PlyReader reader, PlyHeader header)
        {
            var normalizedHeader = new PlyHeaderNormalizer(header);

            var elements = normalizedHeader.BaseHeader.Elements;

            var faces = new List <IndexedTriangle>(elements[normalizedHeader.FacesId].InstanceCount);

            var meshData = new MeshData
            {
                Vertices = new Vector3[elements[normalizedHeader.VerticesId].InstanceCount],
                Faces    = null,
                Normals  = new Vector3[normalizedHeader.NormalsId == -1 ? 0 : elements[normalizedHeader.NormalsId].InstanceCount]
            };

            int currVertex            = 0;
            int currVertexPropWritten = 0;
            int currNormal            = 0;
            int currNormalPropWritten = 0;
            int currFace = 0;

            // calling progressReporter.Report is expensive, limit it.
            long totalLoad            = meshData.Vertices.Length + faces.Capacity + meshData.Normals.Length;
            int  lastProgressReported = 10;

            // since the structure of the file is not known
            // and will be determined at the runtime
            // we don't know what elements and properties come first.
            // not the prettiest but definitely the fastest way to do this.
            for (int i = 0; i < elements.Count; ++i)
            {
                for (int j = 0; j < elements[i].InstanceCount; ++j)
                {
                    // call the progressReporter function on every 10% progress
                    int progress = (int)((currVertex + currNormal + currFace) * 100 / totalLoad);
                    if (progress > lastProgressReported + 10 && ProgressReporter != null)
                    {
                        ProgressReporter.Report(progress);
                        lastProgressReported = progress;
                    }

                    // go through every property of the current element
                    for (int k = 0; k < elements[i].Properties.Count; ++k)
                    {
                        bool ignored = true;

                        // if the current element was found by the normalizedHeader
                        // to be a vertex element, parse the vertices
                        if (i == normalizedHeader.VerticesId)
                        {
                            if (k == normalizedHeader.VertexX)
                            {
                                meshData.Vertices[currVertex].X = reader.ReadProperty <float>();
                                currVertexPropWritten++;
                                ignored = false;
                            }
                            else if (k == normalizedHeader.VertexY)
                            {
                                meshData.Vertices[currVertex].Y = reader.ReadProperty <float>();
                                currVertexPropWritten++;
                                ignored = false;
                            }
                            else if (k == normalizedHeader.VertexZ)
                            {
                                meshData.Vertices[currVertex].Z = reader.ReadProperty <float>();
                                currVertexPropWritten++;
                                ignored = false;
                            }
                            if (currVertexPropWritten == 3)
                            {
                                currVertexPropWritten = 0;
                                currVertex++;
                            }
                        }
                        // if it is a normal element..
                        if (i == normalizedHeader.NormalsId)
                        {
                            if (k == normalizedHeader.NormalX)
                            {
                                meshData.Normals[currNormal].X = reader.ReadProperty <float>();
                                currNormalPropWritten++;
                                ignored = false;
                            }
                            else if (k == normalizedHeader.NormalY)
                            {
                                meshData.Normals[currNormal].X = reader.ReadProperty <float>();
                                currNormalPropWritten++;
                                ignored = false;
                            }
                            else if (k == normalizedHeader.NormalZ)
                            {
                                meshData.Normals[currNormal].X = reader.ReadProperty <float>();
                                currNormalPropWritten++;
                                ignored = false;
                            }
                            if (currNormalPropWritten == 3)
                            {
                                currNormal++;
                                currNormalPropWritten = 0;
                            }
                        }
                        // if it is a face element..
                        if (i == normalizedHeader.FacesId)
                        {
                            if (k == normalizedHeader.VerticesId)
                            {
                                // read the vertices of a face
                                List <int> verts = new List <int>(3);
                                foreach (int index in reader.ReadArray <int>())
                                {
                                    verts.Add(index);
                                }

                                // if we got 3 vertices, its a normal triangle
                                // and the triangle to the list
                                if (verts.Count == 3)
                                {
                                    var t = new IndexedTriangle
                                    {
                                        Vertex1 = verts[0],
                                        Vertex2 = verts[1],
                                        Vertex3 = verts[2]
                                    };
                                    faces.Add(t);
                                }
                                else if (verts.Count == 4)
                                {
                                    faces.Add(new IndexedTriangle(verts[0], verts[1], verts[3]));
                                    faces.Add(new IndexedTriangle(verts[1], verts[2], verts[3]));
                                }
                                else
                                {
                                    throw new NotSupportedException("Only triangular faces are currently supported.");
                                }
                                currFace++;
                                ignored = false;
                            }
                        }
                        if (ignored)
                        {
                            reader.SkipProperty();
                        }
                    }
                }
            }

            meshData.Faces = faces.ToArray();

            if (meshData.Normals.Length == 0)
            {
                meshData.Normals = null;
            }

            ProgressReporter?.Report(100);
            return(meshData);
        }