コード例 #1
0
        private static Material UseMaterial(WavefrontMaterialLoader materialLoader, string line)
        {
            var node     = UseMaterialRegex.Match(line).Groups[1].Value;
            var material = materialLoader.FindByName(node);

            if (material == null)
            {
                var rgbNode = node;
                for (var c = '0'; c <= '9'; c++)
                {
                    rgbNode = rgbNode.Replace(c, '-');
                }

                if (rgbNode.IndexOf("R-", StringComparison.Ordinal) != -1 &&
                    rgbNode.IndexOf("G-", StringComparison.Ordinal) != -1 &&
                    rgbNode.IndexOf("-B", StringComparison.Ordinal) != -1)
                {
                    var red = byte.Parse(node.Substring(1, node.IndexOf('G')));
                    var grn = byte.Parse(node.Substring(node.IndexOf('G') + 1, node.IndexOf('B')));
                    var blu = byte.Parse(node.Substring(node.IndexOf('B') + 1));
                    material = new Material {
                        Diffuse = ColorF.FromRgb(red, grn, blu)
                    };
                    materialLoader.AddCustomMaterial(node, material);
                }
                else
                {
                    material = new Material();
                }
            }

            return(material);
        }
コード例 #2
0
        public Model LoadModel(string name)
        {
            var directives = Encoding.ASCII.GetString(LoadResource(name));
            var wavefrontMaterialLoader = new WavefrontMaterialLoader(this, directives);

            return(new WavefrontLoader().LoadModel(wavefrontMaterialLoader, directives));
        }
コード例 #3
0
        public Model LoadModel(WavefrontMaterialLoader materialLoader, string directives)
        {
            var model    = new Model();
            var material = new Material();

            using var br = new StringReader(directives);
            var line = br.ReadLine();

            while (line != null)
            {
                if (line.Length > 0)
                {
                    var type = line.Substring(0, line.IndexOf(' ')).Trim().ToLower();
                    if (type.Equals("v"))
                    {
                        var match = VertexRegex.Match(line.Trim());
                        var x     = float.Parse(match.Groups[1].Value);
                        var y     = float.Parse(match.Groups[2].Value);
                        var z     = float.Parse(match.Groups[3].Value);
                        model.AddVertex(x, y, z);
                    }
                    else if (type.Equals("f"))
                    {
                        LoadFace(model, line.Trim(), material);
                    }
                    else if (type.Equals("g"))
                    {
                        model.CurrentGroup = GroupRegex.Match(line.Trim()).Groups[1].Value;
                    }
                    else if (type.Equals("usemtl"))
                    {
                        material = UseMaterial(materialLoader, line.Trim());
                    }
                }

                line = br.ReadLine();
            }

            return(model);
        }