示例#1
0
        private static void WriteColor(string statement, ObjMaterialColor color, StreamWriter stream)
        {
            stream.Write(statement);

            if (color.IsSpectral)
            {
                stream.Write(" spectral ");
                stream.Write(color.SpectralFileName);

                if (color.SpectralFactor != 1.0f)
                {
                    stream.Write(' ');
                    stream.Write(color.SpectralFactor.ToString("F6", CultureInfo.InvariantCulture));
                }

                stream.WriteLine();
            }
            else
            {
                if (color.UseXYZColorSpace)
                {
                    stream.Write(" xyz");
                }

                stream.WriteLine(
                    " {0} {1} {2}",
                    color.Color.X.ToString("F6", CultureInfo.InvariantCulture),
                    color.Color.Y.ToString("F6", CultureInfo.InvariantCulture),
                    color.Color.Z.ToString("F6", CultureInfo.InvariantCulture));
            }
        }
示例#2
0
        private static ObjMaterialColor ParseMaterialColor(string statement, string[] values)
        {
            if (values.Length < 2)
            {
                throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a color."));
            }

            var color = new ObjMaterialColor();

            int index = 1;

            switch (values[1].ToLowerInvariant())
            {
            case "spectral":
                index++;

                if (values.Length - index < 1)
                {
                    throw new InvalidDataException(string.Concat("A ", statement, " spectral statement must specify a file name."));
                }

                if (!Path.HasExtension(values[index]))
                {
                    throw new InvalidDataException("A filename must have an extension.");
                }

                color.SpectralFileName = values[index];
                index++;

                if (values.Length > index)
                {
                    color.SpectralFactor = float.Parse(values[index], CultureInfo.InvariantCulture);
                    index++;
                }

                break;

            case "xyz":
                index++;

                if (values.Length - index < 1)
                {
                    throw new InvalidDataException(string.Concat("A ", statement, " xyz statement must specify a color."));
                }

                color.UseXYZColorSpace = true;

                var xyz = new ObjVector3();

                xyz.X = float.Parse(values[index], CultureInfo.InvariantCulture);
                index++;

                if (values.Length > index)
                {
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " xyz statement must specify a XYZ color."));
                    }

                    xyz.Y  = float.Parse(values[index], CultureInfo.InvariantCulture);
                    xyz.Z  = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
                    index += 2;
                }
                else
                {
                    xyz.Y = xyz.X;
                    xyz.Z = xyz.X;
                }

                color.Color = xyz;
                break;

            default:
                var rgb = new ObjVector3();

                rgb.X = float.Parse(values[index], CultureInfo.InvariantCulture);
                index++;

                if (values.Length > index)
                {
                    if (values.Length - index < 2)
                    {
                        throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a RGB color."));
                    }

                    rgb.Y  = float.Parse(values[index], CultureInfo.InvariantCulture);
                    rgb.Z  = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
                    index += 2;
                }
                else
                {
                    rgb.Y = rgb.X;
                    rgb.Z = rgb.X;
                }

                color.Color = rgb;
                break;
            }

            if (index != values.Length)
            {
                throw new InvalidDataException(string.Concat("A ", statement, " statement has too many values."));
            }

            return(color);
        }