示例#1
0
        private static MtlColor ParseColor(IList <string> args)
        {
            if (args.Count == 0)
            {
                throw new ArgumentException("not enough argments", nameof(args));
            }
            args = TextParser.ParseArgs(args);
            switch (args[0])
            {
            case "xyz":
                if (args.Count == 2)
                {
                    return(new ColorXyz(float.Parse(args[1])));
                }
                if (args.Count >= 4)
                {
                    return(new ColorXyz(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3])));
                }
                throw new ArgumentException("not enough argments, need 2 or 4", nameof(args));

            case "spectral":
                if (args.Count == 2)
                {
                    return(new ColorSpectral(args[1]));
                }
                if (args.Count >= 3)
                {
                    return(new ColorSpectral(args[1], float.Parse(args[2])));
                }
                throw new ArgumentException("not enough argments, need 2 or 3", nameof(args));

            default:
                if (args.Count == 1)
                {
                    return(new ColorRgb(float.Parse(args[0])));
                }
                if (args.Count == 3)
                {
                    return(new ColorRgb(float.Parse(args[0]), float.Parse(args[1]), float.Parse(args[2])));
                }
                throw new ArgumentException("not enough argments, need 1 or 3", nameof(args));
            }
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="encoding">default utf-8</param>
        /// <returns></returns>
        public static ObjModel Parse(string inputFile, Encoding encoding = null)
        {
            encoding = encoding ?? TextParser.DefaultEncoding;
            // https://stackoverflow.com/questions/2161895/reading-large-text-files-with-streams-in-c-sharp
            using (var fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (var bs = new BufferedStream(fs))
                    using (var sr = new StreamReader(bs, encoding))
                    {
                        var nameBuilder   = new StringBuilder();
                        var model         = new ObjModel();
                        var groupBuilder  = model.GetMapBuilder("default");
                        var objectBuilder = model.GetMapBuilder("default");
                        var meshBuilder   = model.GetMapBuilder("");
                        TextParser.Lex(sr, (key, args) =>
                        {
                            switch (key)
                            {
                            case "mtllib":
                                model.MaterialLibaries.AddRange(args);
                                break;

                            case "v":
                                args = TextParser.ParseArgs(args);
                                model.Positions.Add(ParsePoint(args));
                                break;

                            case "vt":
                                args = TextParser.ParseArgs(args);
                                model.TextureCoords.Add(ParseUv(args));
                                break;

                            case "vn":
                                args = TextParser.ParseArgs(args);
                                model.Normals.Add(ParseNormal(args));
                                break;

                            case "vp":
                                break;

                            case "p":
                                args = TextParser.ParseArgs(args);
                                foreach (var a in args)
                                {
                                    var index = FindIndex(model.Positions, a);
                                    model.Points.Add(index);
                                }
                                break;

                            case "l":
                                args = TextParser.ParseArgs(args);
                                FillLineSegments(model, args);
                                break;

                            case "fo":
                            case "f":
                                args = TextParser.ParseArgs(args);
                                FillPolygon(model, args);
                                break;

                            case "g":
                                args = TextParser.ParseArgs(args);
                                groupBuilder.Start(args[0]);
                                break;

                            case "o":
                                args = TextParser.ParseArgs(args);
                                objectBuilder.Start(args[0]);
                                if (String.IsNullOrEmpty(model.Name))
                                {
                                    model.Name = args[0];
                                }
                                break;

                            case "usemtl":
                                meshBuilder.Start(args[0]);
                                break;
                            }
                        });
                        objectBuilder.End();
                        groupBuilder.End();
                        meshBuilder.End();
                        model.Objects = objectBuilder.Result;
                        model.Groups  = groupBuilder.Result;
                        model.Meshes  = meshBuilder.Result;

                        return(model);
                    }
        }