< Summary

Line coverage
100%
Covered lines: 398
Uncovered lines: 0
Coverable lines: 398
Total lines: 950
Line coverage: 100%
Branch coverage
98%
Covered branches: 460
Total branches: 467
Branch coverage: 98.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FromStream(...)97.8%319319100%
ParseMaterialColor(...)100%2222100%
ParseMaterialMap(...)100%126126100%

File(s)

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.Media.WavefrontObj/636b700b450d7d3262bf9218a7cc67966be4ced8/JeremyAnsel.Media.WavefrontObj/JeremyAnsel.Media.WavefrontObj/ObjMaterialFileReader.cs

#LineLine coverage
 1// <copyright file="ObjMaterialFileReader.cs" company="Jérémy Ansel">
 2// Copyright (c) 2017, 2019 Jérémy Ansel
 3// </copyright>
 4// <license>
 5// Licensed under the MIT license. See LICENSE.txt
 6// </license>
 7
 8#if !NET6_0_OR_GREATER
 9
 10using System.Diagnostics.CodeAnalysis;
 11using System.Globalization;
 12
 13namespace JeremyAnsel.Media.WavefrontObj
 14{
 15    internal static class ObjMaterialFileReader
 16    {
 17        [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = "
 18        public static ObjMaterialFile FromStream(Stream? stream)
 19        {
 31320            if (stream == null)
 21            {
 122                throw new ArgumentNullException(nameof(stream));
 23            }
 24
 31225            var mtl = new ObjMaterialFile();
 31226            var lineReader = new LineReader();
 27
 31228            ObjMaterial? currentMaterial = null;
 29
 164730            foreach (string currentLine in lineReader.Read(stream))
 31            {
 58332                string[] values = currentLine.Split(LineReader.LineSeparators, StringSplitOptions.RemoveEmptyEntries);
 33
 58334                switch (values[0].ToLowerInvariant())
 35                {
 36                    case "newmtl":
 27737                        if (values.Length < 2)
 38                        {
 139                            throw new InvalidDataException("A newmtl statement must specify a name.");
 40                        }
 41
 27642                        currentMaterial = new ObjMaterial
 27643                        {
 27644                            Name = string.Join(" ", values, 1, values.Length - 1)
 27645                        };
 46
 27647                        mtl.Materials.Add(currentMaterial);
 48
 27649                        break;
 50
 51                    case "ka":
 1552                        if (currentMaterial == null)
 53                        {
 154                            throw new InvalidDataException("The material name is not specified.");
 55                        }
 56
 1457                        currentMaterial.AmbientColor = ObjMaterialFileReader.ParseMaterialColor("Ka", values);
 858                        break;
 59
 60                    case "kd":
 1561                        if (currentMaterial == null)
 62                        {
 163                            throw new InvalidDataException("The material name is not specified.");
 64                        }
 65
 1466                        currentMaterial.DiffuseColor = ObjMaterialFileReader.ParseMaterialColor("Kd", values);
 867                        break;
 68
 69                    case "ke":
 1570                        if (currentMaterial == null)
 71                        {
 172                            throw new InvalidDataException("The material name is not specified.");
 73                        }
 74
 1475                        currentMaterial.EmissiveColor = ObjMaterialFileReader.ParseMaterialColor("Ke", values);
 876                        break;
 77
 78                    case "ks":
 1579                        if (currentMaterial == null)
 80                        {
 181                            throw new InvalidDataException("The material name is not specified.");
 82                        }
 83
 1484                        currentMaterial.SpecularColor = ObjMaterialFileReader.ParseMaterialColor("Ks", values);
 885                        break;
 86
 87                    case "tf":
 1588                        if (currentMaterial == null)
 89                        {
 190                            throw new InvalidDataException("The material name is not specified.");
 91                        }
 92
 1493                        currentMaterial.TransmissionColor = ObjMaterialFileReader.ParseMaterialColor("Tf", values);
 894                        break;
 95
 96                    case "illum":
 497                        if (currentMaterial == null)
 98                        {
 199                            throw new InvalidDataException("The material name is not specified.");
 100                        }
 101
 3102                        if (values.Length < 2)
 103                        {
 1104                            throw new InvalidDataException("An illum statement must specify an illumination model.");
 105                        }
 106
 2107                        if (values.Length != 2)
 108                        {
 1109                            throw new InvalidDataException("An illum statement has too many values.");
 110                        }
 111
 1112                        currentMaterial.IlluminationModel = int.Parse(values[1], CultureInfo.InvariantCulture);
 1113                        break;
 114
 115                    case "d":
 7116                        if (currentMaterial == null)
 117                        {
 1118                            throw new InvalidDataException("The material name is not specified.");
 119                        }
 120
 6121                        if (values.Length < 2)
 122                        {
 1123                            throw new InvalidDataException("A d statement must specify a factor.");
 124                        }
 125
 5126                        if (string.Equals(values[1], "-halo", StringComparison.OrdinalIgnoreCase))
 127                        {
 3128                            if (values.Length < 3)
 129                            {
 1130                                throw new InvalidDataException("A d statement must specify a factor.");
 131                            }
 132
 2133                            if (values.Length != 3)
 134                            {
 1135                                throw new InvalidDataException("A d statement has too many values.");
 136                            }
 137
 1138                            currentMaterial.IsHaloDissolve = true;
 1139                            currentMaterial.DissolveFactor = float.Parse(values[2], CultureInfo.InvariantCulture);
 140                        }
 141                        else
 142                        {
 2143                            if (values.Length != 2)
 144                            {
 1145                                throw new InvalidDataException("A d statement has too many values.");
 146                            }
 147
 1148                            currentMaterial.DissolveFactor = float.Parse(values[1], CultureInfo.InvariantCulture);
 149                        }
 150
 1151                        break;
 152
 153                    case "ns":
 4154                        if (currentMaterial == null)
 155                        {
 1156                            throw new InvalidDataException("The material name is not specified.");
 157                        }
 158
 3159                        if (values.Length < 2)
 160                        {
 1161                            throw new InvalidDataException("A Ns statement must specify a specular exponent.");
 162                        }
 163
 2164                        if (values.Length != 2)
 165                        {
 1166                            throw new InvalidDataException("A Ns statement has too many values.");
 167                        }
 168
 1169                        currentMaterial.SpecularExponent = float.Parse(values[1], CultureInfo.InvariantCulture);
 1170                        break;
 171
 172                    case "sharpness":
 4173                        if (currentMaterial == null)
 174                        {
 1175                            throw new InvalidDataException("The material name is not specified.");
 176                        }
 177
 3178                        if (values.Length < 2)
 179                        {
 1180                            throw new InvalidDataException("A sharpness statement must specify a sharpness value.");
 181                        }
 182
 2183                        if (values.Length != 2)
 184                        {
 1185                            throw new InvalidDataException("A sharpness statement has too many values.");
 186                        }
 187
 1188                        currentMaterial.Sharpness = int.Parse(values[1], CultureInfo.InvariantCulture);
 1189                        break;
 190
 191                    case "ni":
 4192                        if (currentMaterial == null)
 193                        {
 1194                            throw new InvalidDataException("The material name is not specified.");
 195                        }
 196
 3197                        if (values.Length < 2)
 198                        {
 1199                            throw new InvalidDataException("A Ni statement must specify an optical density.");
 200                        }
 201
 2202                        if (values.Length != 2)
 203                        {
 1204                            throw new InvalidDataException("A Ni statement has too many values.");
 205                        }
 206
 1207                        currentMaterial.OpticalDensity = float.Parse(values[1], CultureInfo.InvariantCulture);
 1208                        break;
 209
 210                    case "map_aat":
 6211                        if (currentMaterial == null)
 212                        {
 1213                            throw new InvalidDataException("The material name is not specified.");
 214                        }
 215
 5216                        if (values.Length < 2)
 217                        {
 1218                            throw new InvalidDataException("A map_aat statement must specify a value.");
 219                        }
 220
 4221                        if (values.Length != 2)
 222                        {
 1223                            throw new InvalidDataException("A map_aat statement has too many values.");
 224                        }
 225
 3226                        if (string.Equals(values[1], "on", StringComparison.OrdinalIgnoreCase))
 227                        {
 1228                            currentMaterial.IsAntiAliasingEnabled = true;
 229                        }
 2230                        else if (string.Equals(values[1], "off", StringComparison.OrdinalIgnoreCase))
 231                        {
 1232                            currentMaterial.IsAntiAliasingEnabled = false;
 233                        }
 234                        else
 235                        {
 1236                            throw new InvalidDataException("A map_aat statement must specify on or off.");
 237                        }
 238
 239                        break;
 240
 241                    case "map_ka":
 67242                        if (currentMaterial == null)
 243                        {
 1244                            throw new InvalidDataException("The material name is not specified.");
 245                        }
 246
 66247                        currentMaterial.AmbientMap = ObjMaterialFileReader.ParseMaterialMap("map_Ka", values);
 32248                        break;
 249
 250                    case "map_kd":
 7251                        if (currentMaterial == null)
 252                        {
 1253                            throw new InvalidDataException("The material name is not specified.");
 254                        }
 255
 6256                        currentMaterial.DiffuseMap = ObjMaterialFileReader.ParseMaterialMap("map_Kd", values);
 5257                        break;
 258
 259                    case "map_ke":
 7260                        if (currentMaterial == null)
 261                        {
 1262                            throw new InvalidDataException("The material name is not specified.");
 263                        }
 264
 6265                        currentMaterial.EmissiveMap = ObjMaterialFileReader.ParseMaterialMap("map_Ke", values);
 5266                        break;
 267
 268                    case "map_ks":
 7269                        if (currentMaterial == null)
 270                        {
 1271                            throw new InvalidDataException("The material name is not specified.");
 272                        }
 273
 6274                        currentMaterial.SpecularMap = ObjMaterialFileReader.ParseMaterialMap("map_Ks", values);
 5275                        break;
 276
 277                    case "map_ns":
 7278                        if (currentMaterial == null)
 279                        {
 1280                            throw new InvalidDataException("The material name is not specified.");
 281                        }
 282
 6283                        currentMaterial.SpecularExponentMap = ObjMaterialFileReader.ParseMaterialMap("map_Ns", values);
 5284                        break;
 285
 286                    case "map_d":
 287                    case "map_tr":
 7288                        if (currentMaterial == null)
 289                        {
 1290                            throw new InvalidDataException("The material name is not specified.");
 291                        }
 292
 6293                        currentMaterial.DissolveMap = ObjMaterialFileReader.ParseMaterialMap("map_d", values);
 5294                        break;
 295
 296                    case "decal":
 297                    case "map_decal":
 5298                        if (currentMaterial == null)
 299                        {
 1300                            throw new InvalidDataException("The material name is not specified.");
 301                        }
 302
 4303                        currentMaterial.DecalMap = ObjMaterialFileReader.ParseMaterialMap("decal", values);
 3304                        break;
 305
 306                    case "disp":
 307                    case "map_disp":
 5308                        if (currentMaterial == null)
 309                        {
 1310                            throw new InvalidDataException("The material name is not specified.");
 311                        }
 312
 4313                        currentMaterial.DispMap = ObjMaterialFileReader.ParseMaterialMap("disp", values);
 3314                        break;
 315
 316                    case "bump":
 317                    case "map_bump":
 3318                        if (currentMaterial == null)
 319                        {
 1320                            throw new InvalidDataException("The material name is not specified.");
 321                        }
 322
 2323                        currentMaterial.BumpMap = ObjMaterialFileReader.ParseMaterialMap("bump", values);
 1324                        break;
 325
 326                    case "refl":
 327                    case "map_refl":
 28328                        if (currentMaterial == null)
 329                        {
 1330                            throw new InvalidDataException("The material name is not specified.");
 331                        }
 332
 27333                        if (values.Length < 4)
 334                        {
 4335                            throw new InvalidDataException("A refl statement must specify a type and a file name.");
 336                        }
 337
 23338                        if (!string.Equals(values[1], "-type", StringComparison.OrdinalIgnoreCase))
 339                        {
 1340                            throw new InvalidDataException("A refl statement must specify a type.");
 341                        }
 342
 22343                        switch (values[2].ToLowerInvariant())
 344                        {
 345                            case "sphere":
 3346                                currentMaterial.ReflectionMap.Sphere = ObjMaterialFileReader.ParseMaterialMap("refl", va
 3347                                break;
 348
 349                            case "cube_top":
 3350                                currentMaterial.ReflectionMap.CubeTop = ObjMaterialFileReader.ParseMaterialMap("refl", v
 3351                                break;
 352
 353                            case "cube_bottom":
 3354                                currentMaterial.ReflectionMap.CubeBottom = ObjMaterialFileReader.ParseMaterialMap("refl"
 3355                                break;
 356
 357                            case "cube_front":
 3358                                currentMaterial.ReflectionMap.CubeFront = ObjMaterialFileReader.ParseMaterialMap("refl",
 3359                                break;
 360
 361                            case "cube_back":
 3362                                currentMaterial.ReflectionMap.CubeBack = ObjMaterialFileReader.ParseMaterialMap("refl", 
 3363                                break;
 364
 365                            case "cube_left":
 3366                                currentMaterial.ReflectionMap.CubeLeft = ObjMaterialFileReader.ParseMaterialMap("refl", 
 3367                                break;
 368
 369                            case "cube_right":
 3370                                currentMaterial.ReflectionMap.CubeRight = ObjMaterialFileReader.ParseMaterialMap("refl",
 3371                                break;
 372                        }
 373
 374                        break;
 375                    case "pr":
 4376                        if (currentMaterial == null)
 377                        {
 1378                            throw new InvalidDataException("The material name is not specified.");
 379                        }
 380
 3381                        if (values.Length < 2)
 382                        {
 1383                            throw new InvalidDataException("A Pr statement must specify an optical density.");
 384                        }
 385
 2386                        if (values.Length != 2)
 387                        {
 1388                            throw new InvalidDataException("A Pr statement has too many values.");
 389                        }
 390
 1391                        currentMaterial.Roughness = float.Parse(values[1], CultureInfo.InvariantCulture);
 1392                        break;
 393                    case "map_pr":
 7394                        if (currentMaterial == null)
 395                        {
 1396                            throw new InvalidDataException("The material name is not specified.");
 397                        }
 398
 6399                        currentMaterial.RoughnessMap = ObjMaterialFileReader.ParseMaterialMap("map_Pr", values);
 5400                        break;
 401                    case "pm":
 4402                        if (currentMaterial == null)
 403                        {
 1404                            throw new InvalidDataException("The material name is not specified.");
 405                        }
 406
 3407                        if (values.Length < 2)
 408                        {
 1409                            throw new InvalidDataException("A Pm statement must specify an optical density.");
 410                        }
 411
 2412                        if (values.Length != 2)
 413                        {
 1414                            throw new InvalidDataException("A Pm statement has too many values.");
 415                        }
 416
 1417                        currentMaterial.Metallic = float.Parse(values[1], CultureInfo.InvariantCulture);
 1418                        break;
 419                    case "map_pm":
 7420                        if (currentMaterial == null)
 421                        {
 1422                            throw new InvalidDataException("The material name is not specified.");
 423                        }
 424
 6425                        currentMaterial.MetallicMap = ObjMaterialFileReader.ParseMaterialMap("map_Pm", values);
 5426                        break;
 427                    case "ps":
 4428                        if (currentMaterial == null)
 429                        {
 1430                            throw new InvalidDataException("The material name is not specified.");
 431                        }
 432
 3433                        if (values.Length < 2)
 434                        {
 1435                            throw new InvalidDataException("A Ps statement must specify an optical density.");
 436                        }
 437
 2438                        if (values.Length != 2)
 439                        {
 1440                            throw new InvalidDataException("A Ps statement has too many values.");
 441                        }
 442
 1443                        currentMaterial.Sheen = float.Parse(values[1], CultureInfo.InvariantCulture);
 1444                        break;
 445                    case "map_ps":
 7446                        if (currentMaterial == null)
 447                        {
 1448                            throw new InvalidDataException("The material name is not specified.");
 449                        }
 450
 6451                        currentMaterial.SheenMap = ObjMaterialFileReader.ParseMaterialMap("map_Ps", values);
 5452                        break;
 453                    case "pc":
 4454                        if (currentMaterial == null)
 455                        {
 1456                            throw new InvalidDataException("The material name is not specified.");
 457                        }
 458
 3459                        if (values.Length < 2)
 460                        {
 1461                            throw new InvalidDataException("A Pc statement must specify an optical density.");
 462                        }
 463
 2464                        if (values.Length != 2)
 465                        {
 1466                            throw new InvalidDataException("A Pc statement has too many values.");
 467                        }
 468
 1469                        currentMaterial.ClearCoatThickness = float.Parse(values[1], CultureInfo.InvariantCulture);
 1470                        break;
 471                    case "pcr":
 4472                        if (currentMaterial == null)
 473                        {
 1474                            throw new InvalidDataException("The material name is not specified.");
 475                        }
 476
 3477                        if (values.Length < 2)
 478                        {
 1479                            throw new InvalidDataException("A Pcr statement must specify an optical density.");
 480                        }
 481
 2482                        if (values.Length != 2)
 483                        {
 1484                            throw new InvalidDataException("A Pcr statement has too many values.");
 485                        }
 486
 1487                        currentMaterial.ClearCoatRoughness = float.Parse(values[1], CultureInfo.InvariantCulture);
 1488                        break;
 489                    case "aniso":
 4490                        if (currentMaterial == null)
 491                        {
 1492                            throw new InvalidDataException("The material name is not specified.");
 493                        }
 494
 3495                        if (values.Length < 2)
 496                        {
 1497                            throw new InvalidDataException("A aniso statement must specify an optical density.");
 498                        }
 499
 2500                        if (values.Length != 2)
 501                        {
 1502                            throw new InvalidDataException("A aniso statement has too many values.");
 503                        }
 504
 1505                        currentMaterial.Anisotropy = float.Parse(values[1], CultureInfo.InvariantCulture);
 1506                        break;
 507                    case "anisor":
 4508                        if (currentMaterial == null)
 509                        {
 1510                            throw new InvalidDataException("The material name is not specified.");
 511                        }
 512
 3513                        if (values.Length < 2)
 514                        {
 1515                            throw new InvalidDataException("A anisor statement must specify an optical density.");
 516                        }
 517
 2518                        if (values.Length != 2)
 519                        {
 1520                            throw new InvalidDataException("A anisor statement has too many values.");
 521                        }
 522
 1523                        currentMaterial.AnisotropyRotation = float.Parse(values[1], CultureInfo.InvariantCulture);
 1524                        break;
 525                    case "norm":
 7526                        if (currentMaterial == null)
 527                        {
 1528                            throw new InvalidDataException("The material name is not specified.");
 529                        }
 530
 6531                        currentMaterial.Norm = ObjMaterialFileReader.ParseMaterialMap("norm", values);
 532                        break;
 533                }
 534            }
 535
 169536            mtl.HeaderText = string.Join("\n", lineReader.HeaderTextLines.ToArray());
 537
 169538            return mtl;
 539        }
 540
 541        [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = "
 542        private static ObjMaterialColor ParseMaterialColor(string statement, string[] values)
 543        {
 70544            if (values.Length < 2)
 545            {
 5546                throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a color."));
 547            }
 548
 65549            var color = new ObjMaterialColor();
 550
 65551            int index = 1;
 552
 65553            switch (values[1].ToLowerInvariant())
 554            {
 555                case "spectral":
 25556                    index++;
 557
 25558                    if (values.Length - index < 1)
 559                    {
 5560                        throw new InvalidDataException(string.Concat("A ", statement, " spectral statement must specify 
 561                    }
 562
 20563                    color.SpectralFileName = values[index];
 20564                    index++;
 565
 20566                    if (values.Length > index)
 567                    {
 10568                        color.SpectralFactor = float.Parse(values[index], CultureInfo.InvariantCulture);
 10569                        index++;
 570                    }
 571
 10572                    break;
 573
 574                case "xyz":
 575                    {
 20576                        index++;
 577
 20578                        if (values.Length - index < 1)
 579                        {
 5580                            throw new InvalidDataException(string.Concat("A ", statement, " xyz statement must specify a
 581                        }
 582
 15583                        color.UseXYZColorSpace = true;
 584
 15585                        var xyz = new ObjVector3();
 586
 15587                        xyz.X = float.Parse(values[index], CultureInfo.InvariantCulture);
 15588                        index++;
 589
 15590                        if (values.Length > index)
 591                        {
 10592                            if (values.Length - index < 2)
 593                            {
 5594                                throw new InvalidDataException(string.Concat("A ", statement, " xyz statement must speci
 595                            }
 596
 5597                            xyz.Y = float.Parse(values[index], CultureInfo.InvariantCulture);
 5598                            xyz.Z = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
 5599                            index += 2;
 600                        }
 601                        else
 602                        {
 5603                            xyz.Y = xyz.X;
 5604                            xyz.Z = xyz.X;
 605                        }
 606
 10607                        color.Color = xyz;
 10608                        break;
 609                    }
 610
 611                default:
 612                    {
 20613                        var rgb = new ObjVector3();
 614
 20615                        rgb.X = float.Parse(values[index], CultureInfo.InvariantCulture);
 20616                        index++;
 617
 20618                        if (values.Length > index)
 619                        {
 15620                            if (values.Length - index < 2)
 621                            {
 5622                                throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a
 623                            }
 624
 10625                            rgb.Y = float.Parse(values[index], CultureInfo.InvariantCulture);
 10626                            rgb.Z = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
 10627                            index += 2;
 628                        }
 629                        else
 630                        {
 5631                            rgb.Y = rgb.X;
 5632                            rgb.Z = rgb.X;
 633                        }
 634
 15635                        color.Color = rgb;
 636                        break;
 637                    }
 638            }
 639
 45640            if (index != values.Length)
 641            {
 5642                throw new InvalidDataException(string.Concat("A ", statement, " statement has too many values."));
 643            }
 644
 40645            return color;
 646        }
 647
 648        [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = "
 649        private static ObjMaterialMap ParseMaterialMap(string statement, string[] values)
 650        {
 151651            var map = new ObjMaterialMap();
 652
 463653            for (int index = 0; index < values.Length;)
 654            {
 207655                index++;
 656
 207657                if (values.Length - index < 1)
 658                {
 22659                    throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a filename.")
 660                }
 661
 185662                switch (values[index].ToLowerInvariant())
 663                {
 664                    case "-type":
 23665                        if (values.Length - index < 2)
 666                        {
 1667                            throw new InvalidDataException(string.Concat("A ", statement, " -type option must specify a 
 668                        }
 669
 22670                        index++;
 22671                        break;
 672
 673                    case "-blenu":
 5674                        if (values.Length - index < 2)
 675                        {
 1676                            throw new InvalidDataException(string.Concat("A ", statement, " -blenu option must specify a
 677                        }
 678
 4679                        if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase))
 680                        {
 1681                            map.IsHorizontalBlendingEnabled = true;
 682                        }
 3683                        else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase))
 684                        {
 1685                            map.IsHorizontalBlendingEnabled = false;
 686                        }
 687                        else
 688                        {
 2689                            throw new InvalidDataException(string.Concat("A ", statement, " -blenu option must specify o
 690                        }
 691
 2692                        index++;
 2693                        break;
 694
 695                    case "-blenv":
 5696                        if (values.Length - index < 2)
 697                        {
 1698                            throw new InvalidDataException(string.Concat("A ", statement, " -blenv option must specify a
 699                        }
 700
 4701                        if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase))
 702                        {
 1703                            map.IsVerticalBlendingEnabled = true;
 704                        }
 3705                        else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase))
 706                        {
 1707                            map.IsVerticalBlendingEnabled = false;
 708                        }
 709                        else
 710                        {
 2711                            throw new InvalidDataException(string.Concat("A ", statement, " -blenv option must specify o
 712                        }
 713
 2714                        index++;
 2715                        break;
 716
 717                    case "-bm":
 3718                        if (values.Length - index < 2)
 719                        {
 1720                            throw new InvalidDataException(string.Concat("A ", statement, " -bm option must specify a va
 721                        }
 722
 2723                        map.BumpMultiplier = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
 724
 2725                        index++;
 2726                        break;
 727
 728                    case "-boost":
 3729                        if (values.Length - index < 2)
 730                        {
 1731                            throw new InvalidDataException(string.Concat("A ", statement, " -boost option must specify a
 732                        }
 733
 2734                        map.Boost = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
 735
 2736                        index++;
 2737                        break;
 738
 739                    case "-cc":
 5740                        if (values.Length - index < 2)
 741                        {
 1742                            throw new InvalidDataException(string.Concat("A ", statement, " -cc option must specify a va
 743                        }
 744
 4745                        if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase))
 746                        {
 1747                            map.IsColorCorrectionEnabled = true;
 748                        }
 3749                        else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase))
 750                        {
 1751                            map.IsColorCorrectionEnabled = false;
 752                        }
 753                        else
 754                        {
 2755                            throw new InvalidDataException(string.Concat("A ", statement, " -cc option must specify on o
 756                        }
 757
 2758                        index++;
 2759                        break;
 760
 761                    case "-clamp":
 5762                        if (values.Length - index < 2)
 763                        {
 1764                            throw new InvalidDataException(string.Concat("A ", statement, " -clamp option must specify a
 765                        }
 766
 4767                        if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase))
 768                        {
 1769                            map.IsClampingEnabled = true;
 770                        }
 3771                        else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase))
 772                        {
 1773                            map.IsClampingEnabled = false;
 774                        }
 775                        else
 776                        {
 2777                            throw new InvalidDataException(string.Concat("A ", statement, " -clamp option must specify o
 778                        }
 779
 2780                        index++;
 2781                        break;
 782
 783                    case "-imfchan":
 9784                        if (values.Length - index < 2)
 785                        {
 1786                            throw new InvalidDataException(string.Concat("A ", statement, " -imfchan option must specify
 787                        }
 788
 8789                        switch (values[index + 1].ToLowerInvariant())
 790                        {
 791                            case "r":
 1792                                map.ScalarChannel = ObjMapChannel.Red;
 1793                                break;
 794
 795                            case "g":
 1796                                map.ScalarChannel = ObjMapChannel.Green;
 1797                                break;
 798
 799                            case "b":
 1800                                map.ScalarChannel = ObjMapChannel.Blue;
 1801                                break;
 802
 803                            case "m":
 1804                                map.ScalarChannel = ObjMapChannel.Matte;
 1805                                break;
 806
 807                            case "l":
 1808                                map.ScalarChannel = ObjMapChannel.Luminance;
 1809                                break;
 810
 811                            case "z":
 1812                                map.ScalarChannel = ObjMapChannel.Depth;
 1813                                break;
 814
 815                            default:
 2816                                throw new InvalidDataException(string.Concat("A ", statement, " -imfchan option must spe
 817                        }
 818
 6819                        index++;
 6820                        break;
 821
 822                    case "-mm":
 4823                        if (values.Length - index < 3)
 824                        {
 2825                            throw new InvalidDataException(string.Concat("A ", statement, " -mm option must specify a ba
 826                        }
 827
 2828                        map.ModifierBase = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
 2829                        map.ModifierGain = float.Parse(values[index + 2], CultureInfo.InvariantCulture);
 830
 2831                        index += 2;
 2832                        break;
 833
 834                    case "-o":
 5835                        if (values.Length - index < 2)
 836                        {
 1837                            throw new InvalidDataException(string.Concat("A ", statement, " -o option must specify at le
 838                        }
 839
 4840                        var offset = new ObjVector3();
 841
 4842                        offset.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
 843
 4844                        if (values.Length - index > 3)
 845                        {
 2846                            offset.Y = float.Parse(values[index + 2], CultureInfo.InvariantCulture);
 847
 2848                            if (values.Length - index > 4)
 849                            {
 1850                                offset.Z = float.Parse(values[index + 3], CultureInfo.InvariantCulture);
 1851                                index++;
 852                            }
 853
 2854                            index++;
 855                        }
 856
 4857                        index++;
 858
 4859                        map.Offset = offset;
 4860                        break;
 861
 862                    case "-s":
 863                        {
 5864                            if (values.Length - index < 2)
 865                            {
 1866                                throw new InvalidDataException(string.Concat("A ", statement, " -s option must specify a
 867                            }
 868
 4869                            var scale = new ObjVector3(1.0f, 1.0f, 1.0f);
 870
 4871                            scale.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
 872
 4873                            if (values.Length - index > 3)
 874                            {
 2875                                scale.Y = float.Parse(values[index + 2], CultureInfo.InvariantCulture);
 876
 2877                                if (values.Length - index > 4)
 878                                {
 1879                                    scale.Z = float.Parse(values[index + 3], CultureInfo.InvariantCulture);
 1880                                    index++;
 881                                }
 882
 2883                                index++;
 884                            }
 885
 4886                            index++;
 887
 4888                            map.Scale = scale;
 4889                            break;
 890                        }
 891
 892                    case "-t":
 893                        {
 5894                            if (values.Length - index < 2)
 895                            {
 1896                                throw new InvalidDataException(string.Concat("A ", statement, " -t option must specify a
 897                            }
 898
 4899                            var turbulence = new ObjVector3();
 900
 4901                            turbulence.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture);
 902
 4903                            if (values.Length - index > 3)
 904                            {
 2905                                turbulence.Y = float.Parse(values[index + 2], CultureInfo.InvariantCulture);
 906
 2907                                if (values.Length - index > 4)
 908                                {
 1909                                    turbulence.Z = float.Parse(values[index + 3], CultureInfo.InvariantCulture);
 1910                                    index++;
 911                                }
 912
 2913                                index++;
 914                            }
 915
 4916                            index++;
 917
 4918                            map.Turbulence = turbulence;
 4919                            break;
 920                        }
 921
 922                    case "-texres":
 3923                        if (values.Length - index < 2)
 924                        {
 1925                            throw new InvalidDataException(string.Concat("A ", statement, " -texres option must specify 
 926                        }
 927
 2928                        map.TextureResolution = int.Parse(values[index + 1], CultureInfo.InvariantCulture);
 929
 2930                        index++;
 2931                        break;
 932
 933                    default:
 934                        {
 105935                            string filename = string.Join(" ", values, index, values.Length - index);
 936
 105937                            map.FileName = filename;
 105938                            index = values.Length;
 939                            break;
 940                        }
 941                }
 942            }
 943
 105944            return map;
 945        }
 946    }
 947}
 948
 949#endif
 950