| | 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 | | using System.Diagnostics.CodeAnalysis; |
| | 9 | | using System.Globalization; |
| | 10 | |
|
| | 11 | | namespace JeremyAnsel.Media.WavefrontObj |
| | 12 | | { |
| | 13 | | internal static class ObjMaterialFileReader |
| | 14 | | { |
| | 15 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | 16 | | public static ObjMaterialFile FromStream(Stream? stream) |
| | 17 | | { |
| 678 | 18 | | if (stream == null) |
| | 19 | | { |
| 3 | 20 | | throw new ArgumentNullException(nameof(stream)); |
| | 21 | | } |
| | 22 | |
|
| 675 | 23 | | var mtl = new ObjMaterialFile(); |
| 675 | 24 | | var lineReader = new LineReader(); |
| | 25 | |
|
| 675 | 26 | | ObjMaterial? currentMaterial = null; |
| | 27 | |
|
| 3474 | 28 | | foreach (var values in lineReader.Read(stream)) |
| | 29 | | { |
| 1269 | 30 | | switch (values[0].ToLowerInvariant()) |
| | 31 | | { |
| | 32 | | case "newmtl": |
| 609 | 33 | | if (values.Length < 2) |
| | 34 | | { |
| 3 | 35 | | throw new InvalidDataException("A newmtl statement must specify a name."); |
| | 36 | | } |
| | 37 | |
|
| 606 | 38 | | currentMaterial = new ObjMaterial |
| 606 | 39 | | { |
| 606 | 40 | | Name = string.Join(" ", values, 1, values.Length - 1) |
| 606 | 41 | | }; |
| | 42 | |
|
| 606 | 43 | | mtl.Materials.Add(currentMaterial); |
| | 44 | |
|
| 606 | 45 | | break; |
| | 46 | |
|
| | 47 | | case "ka": |
| 42 | 48 | | if (currentMaterial == null) |
| | 49 | | { |
| 3 | 50 | | throw new InvalidDataException("The material name is not specified."); |
| | 51 | | } |
| | 52 | |
|
| 39 | 53 | | currentMaterial.AmbientColor = ObjMaterialFileReader.ParseMaterialColor("Ka", values); |
| 18 | 54 | | break; |
| | 55 | |
|
| | 56 | | case "kd": |
| 42 | 57 | | if (currentMaterial == null) |
| | 58 | | { |
| 3 | 59 | | throw new InvalidDataException("The material name is not specified."); |
| | 60 | | } |
| | 61 | |
|
| 39 | 62 | | currentMaterial.DiffuseColor = ObjMaterialFileReader.ParseMaterialColor("Kd", values); |
| 18 | 63 | | break; |
| | 64 | |
|
| | 65 | | case "ke": |
| 42 | 66 | | if (currentMaterial == null) |
| | 67 | | { |
| 3 | 68 | | throw new InvalidDataException("The material name is not specified."); |
| | 69 | | } |
| | 70 | |
|
| 39 | 71 | | currentMaterial.EmissiveColor = ObjMaterialFileReader.ParseMaterialColor("Ke", values); |
| 18 | 72 | | break; |
| | 73 | |
|
| | 74 | | case "ks": |
| 42 | 75 | | if (currentMaterial == null) |
| | 76 | | { |
| 3 | 77 | | throw new InvalidDataException("The material name is not specified."); |
| | 78 | | } |
| | 79 | |
|
| 39 | 80 | | currentMaterial.SpecularColor = ObjMaterialFileReader.ParseMaterialColor("Ks", values); |
| 18 | 81 | | break; |
| | 82 | |
|
| | 83 | | case "tf": |
| 42 | 84 | | if (currentMaterial == null) |
| | 85 | | { |
| 3 | 86 | | throw new InvalidDataException("The material name is not specified."); |
| | 87 | | } |
| | 88 | |
|
| 39 | 89 | | currentMaterial.TransmissionColor = ObjMaterialFileReader.ParseMaterialColor("Tf", values); |
| 18 | 90 | | break; |
| | 91 | |
|
| | 92 | | case "illum": |
| 12 | 93 | | if (currentMaterial == null) |
| | 94 | | { |
| 3 | 95 | | throw new InvalidDataException("The material name is not specified."); |
| | 96 | | } |
| | 97 | |
|
| 9 | 98 | | if (values.Length < 2) |
| | 99 | | { |
| 3 | 100 | | throw new InvalidDataException("An illum statement must specify an illumination model."); |
| | 101 | | } |
| | 102 | |
|
| 6 | 103 | | if (values.Length != 2) |
| | 104 | | { |
| 3 | 105 | | throw new InvalidDataException("An illum statement has too many values."); |
| | 106 | | } |
| | 107 | |
|
| 3 | 108 | | currentMaterial.IlluminationModel = int.Parse(values[1], CultureInfo.InvariantCulture); |
| 3 | 109 | | break; |
| | 110 | |
|
| | 111 | | case "d": |
| 21 | 112 | | if (currentMaterial == null) |
| | 113 | | { |
| 3 | 114 | | throw new InvalidDataException("The material name is not specified."); |
| | 115 | | } |
| | 116 | |
|
| 18 | 117 | | if (values.Length < 2) |
| | 118 | | { |
| 3 | 119 | | throw new InvalidDataException("A d statement must specify a factor."); |
| | 120 | | } |
| | 121 | |
|
| 15 | 122 | | if (string.Equals(values[1], "-halo", StringComparison.OrdinalIgnoreCase)) |
| | 123 | | { |
| 9 | 124 | | if (values.Length < 3) |
| | 125 | | { |
| 3 | 126 | | throw new InvalidDataException("A d statement must specify a factor."); |
| | 127 | | } |
| | 128 | |
|
| 6 | 129 | | if (values.Length != 3) |
| | 130 | | { |
| 3 | 131 | | throw new InvalidDataException("A d statement has too many values."); |
| | 132 | | } |
| | 133 | |
|
| 3 | 134 | | currentMaterial.IsHaloDissolve = true; |
| 3 | 135 | | currentMaterial.DissolveFactor = float.Parse(values[2], CultureInfo.InvariantCulture); |
| | 136 | | } |
| | 137 | | else |
| | 138 | | { |
| 6 | 139 | | if (values.Length != 2) |
| | 140 | | { |
| 3 | 141 | | throw new InvalidDataException("A d statement has too many values."); |
| | 142 | | } |
| | 143 | |
|
| 3 | 144 | | currentMaterial.DissolveFactor = float.Parse(values[1], CultureInfo.InvariantCulture); |
| | 145 | | } |
| | 146 | |
|
| 3 | 147 | | break; |
| | 148 | |
|
| | 149 | | case "ns": |
| 12 | 150 | | if (currentMaterial == null) |
| | 151 | | { |
| 3 | 152 | | throw new InvalidDataException("The material name is not specified."); |
| | 153 | | } |
| | 154 | |
|
| 9 | 155 | | if (values.Length < 2) |
| | 156 | | { |
| 3 | 157 | | throw new InvalidDataException("A Ns statement must specify a specular exponent."); |
| | 158 | | } |
| | 159 | |
|
| 6 | 160 | | if (values.Length != 2) |
| | 161 | | { |
| 3 | 162 | | throw new InvalidDataException("A Ns statement has too many values."); |
| | 163 | | } |
| | 164 | |
|
| 3 | 165 | | currentMaterial.SpecularExponent = float.Parse(values[1], CultureInfo.InvariantCulture); |
| 3 | 166 | | break; |
| | 167 | |
|
| | 168 | | case "sharpness": |
| 12 | 169 | | if (currentMaterial == null) |
| | 170 | | { |
| 3 | 171 | | throw new InvalidDataException("The material name is not specified."); |
| | 172 | | } |
| | 173 | |
|
| 9 | 174 | | if (values.Length < 2) |
| | 175 | | { |
| 3 | 176 | | throw new InvalidDataException("A sharpness statement must specify a sharpness value."); |
| | 177 | | } |
| | 178 | |
|
| 6 | 179 | | if (values.Length != 2) |
| | 180 | | { |
| 3 | 181 | | throw new InvalidDataException("A sharpness statement has too many values."); |
| | 182 | | } |
| | 183 | |
|
| 3 | 184 | | currentMaterial.Sharpness = int.Parse(values[1], CultureInfo.InvariantCulture); |
| 3 | 185 | | break; |
| | 186 | |
|
| | 187 | | case "ni": |
| 12 | 188 | | if (currentMaterial == null) |
| | 189 | | { |
| 3 | 190 | | throw new InvalidDataException("The material name is not specified."); |
| | 191 | | } |
| | 192 | |
|
| 9 | 193 | | if (values.Length < 2) |
| | 194 | | { |
| 3 | 195 | | throw new InvalidDataException("A Ni statement must specify an optical density."); |
| | 196 | | } |
| | 197 | |
|
| 6 | 198 | | if (values.Length != 2) |
| | 199 | | { |
| 3 | 200 | | throw new InvalidDataException("A Ni statement has too many values."); |
| | 201 | | } |
| | 202 | |
|
| 3 | 203 | | currentMaterial.OpticalDensity = float.Parse(values[1], CultureInfo.InvariantCulture); |
| 3 | 204 | | break; |
| | 205 | |
|
| | 206 | | case "map_aat": |
| 18 | 207 | | if (currentMaterial == null) |
| | 208 | | { |
| 3 | 209 | | throw new InvalidDataException("The material name is not specified."); |
| | 210 | | } |
| | 211 | |
|
| 15 | 212 | | if (values.Length < 2) |
| | 213 | | { |
| 3 | 214 | | throw new InvalidDataException("A map_aat statement must specify a value."); |
| | 215 | | } |
| | 216 | |
|
| 12 | 217 | | if (values.Length != 2) |
| | 218 | | { |
| 3 | 219 | | throw new InvalidDataException("A map_aat statement has too many values."); |
| | 220 | | } |
| | 221 | |
|
| 9 | 222 | | if (string.Equals(values[1], "on", StringComparison.OrdinalIgnoreCase)) |
| | 223 | | { |
| 3 | 224 | | currentMaterial.IsAntiAliasingEnabled = true; |
| | 225 | | } |
| 6 | 226 | | else if (string.Equals(values[1], "off", StringComparison.OrdinalIgnoreCase)) |
| | 227 | | { |
| 3 | 228 | | currentMaterial.IsAntiAliasingEnabled = false; |
| | 229 | | } |
| | 230 | | else |
| | 231 | | { |
| 3 | 232 | | throw new InvalidDataException("A map_aat statement must specify on or off."); |
| | 233 | | } |
| | 234 | |
|
| | 235 | | break; |
| | 236 | |
|
| | 237 | | case "map_ka": |
| 195 | 238 | | if (currentMaterial == null) |
| | 239 | | { |
| 3 | 240 | | throw new InvalidDataException("The material name is not specified."); |
| | 241 | | } |
| | 242 | |
|
| 192 | 243 | | currentMaterial.AmbientMap = ObjMaterialFileReader.ParseMaterialMap("map_Ka", values); |
| 84 | 244 | | break; |
| | 245 | |
|
| | 246 | | case "map_kd": |
| 15 | 247 | | if (currentMaterial == null) |
| | 248 | | { |
| 3 | 249 | | throw new InvalidDataException("The material name is not specified."); |
| | 250 | | } |
| | 251 | |
|
| 12 | 252 | | currentMaterial.DiffuseMap = ObjMaterialFileReader.ParseMaterialMap("map_Kd", values); |
| 3 | 253 | | break; |
| | 254 | |
|
| | 255 | | case "map_ke": |
| 15 | 256 | | if (currentMaterial == null) |
| | 257 | | { |
| 3 | 258 | | throw new InvalidDataException("The material name is not specified."); |
| | 259 | | } |
| | 260 | |
|
| 12 | 261 | | currentMaterial.EmissiveMap = ObjMaterialFileReader.ParseMaterialMap("map_Ke", values); |
| 3 | 262 | | break; |
| | 263 | |
|
| | 264 | | case "map_ks": |
| 15 | 265 | | if (currentMaterial == null) |
| | 266 | | { |
| 3 | 267 | | throw new InvalidDataException("The material name is not specified."); |
| | 268 | | } |
| | 269 | |
|
| 12 | 270 | | currentMaterial.SpecularMap = ObjMaterialFileReader.ParseMaterialMap("map_Ks", values); |
| 3 | 271 | | break; |
| | 272 | |
|
| | 273 | | case "map_ns": |
| 15 | 274 | | if (currentMaterial == null) |
| | 275 | | { |
| 3 | 276 | | throw new InvalidDataException("The material name is not specified."); |
| | 277 | | } |
| | 278 | |
|
| 12 | 279 | | currentMaterial.SpecularExponentMap = ObjMaterialFileReader.ParseMaterialMap("map_Ns", values); |
| 3 | 280 | | break; |
| | 281 | |
|
| | 282 | | case "map_d": |
| | 283 | | case "map_tr": |
| 15 | 284 | | if (currentMaterial == null) |
| | 285 | | { |
| 3 | 286 | | throw new InvalidDataException("The material name is not specified."); |
| | 287 | | } |
| | 288 | |
|
| 12 | 289 | | currentMaterial.DissolveMap = ObjMaterialFileReader.ParseMaterialMap("map_d", values); |
| 3 | 290 | | break; |
| | 291 | |
|
| | 292 | | case "decal": |
| | 293 | | case "map_decal": |
| 15 | 294 | | if (currentMaterial == null) |
| | 295 | | { |
| 3 | 296 | | throw new InvalidDataException("The material name is not specified."); |
| | 297 | | } |
| | 298 | |
|
| 12 | 299 | | currentMaterial.DecalMap = ObjMaterialFileReader.ParseMaterialMap("decal", values); |
| 3 | 300 | | break; |
| | 301 | |
|
| | 302 | | case "disp": |
| | 303 | | case "map_disp": |
| 15 | 304 | | if (currentMaterial == null) |
| | 305 | | { |
| 3 | 306 | | throw new InvalidDataException("The material name is not specified."); |
| | 307 | | } |
| | 308 | |
|
| 12 | 309 | | currentMaterial.DispMap = ObjMaterialFileReader.ParseMaterialMap("disp", values); |
| 3 | 310 | | break; |
| | 311 | |
|
| | 312 | | case "bump": |
| | 313 | | case "map_bump": |
| 15 | 314 | | if (currentMaterial == null) |
| | 315 | | { |
| 3 | 316 | | throw new InvalidDataException("The material name is not specified."); |
| | 317 | | } |
| | 318 | |
|
| 12 | 319 | | currentMaterial.BumpMap = ObjMaterialFileReader.ParseMaterialMap("bump", values); |
| 3 | 320 | | break; |
| | 321 | |
|
| | 322 | | case "refl": |
| | 323 | | case "map_refl": |
| 45 | 324 | | if (currentMaterial == null) |
| | 325 | | { |
| 3 | 326 | | throw new InvalidDataException("The material name is not specified."); |
| | 327 | | } |
| | 328 | |
|
| 42 | 329 | | if (values.Length < 4) |
| | 330 | | { |
| 9 | 331 | | throw new InvalidDataException("A refl statement must specify a type and a file name."); |
| | 332 | | } |
| | 333 | |
|
| 33 | 334 | | if (!string.Equals(values[1], "-type", StringComparison.OrdinalIgnoreCase)) |
| | 335 | | { |
| 3 | 336 | | throw new InvalidDataException("A refl statement must specify a type."); |
| | 337 | | } |
| | 338 | |
|
| 30 | 339 | | switch (values[2].ToLowerInvariant()) |
| | 340 | | { |
| | 341 | | case "sphere": |
| 9 | 342 | | currentMaterial.ReflectionMap.Sphere = ObjMaterialFileReader.ParseMaterialMap("refl", va |
| 3 | 343 | | break; |
| | 344 | |
|
| | 345 | | case "cube_top": |
| 3 | 346 | | currentMaterial.ReflectionMap.CubeTop = ObjMaterialFileReader.ParseMaterialMap("refl", v |
| 3 | 347 | | break; |
| | 348 | |
|
| | 349 | | case "cube_bottom": |
| 3 | 350 | | currentMaterial.ReflectionMap.CubeBottom = ObjMaterialFileReader.ParseMaterialMap("refl" |
| 3 | 351 | | break; |
| | 352 | |
|
| | 353 | | case "cube_front": |
| 3 | 354 | | currentMaterial.ReflectionMap.CubeFront = ObjMaterialFileReader.ParseMaterialMap("refl", |
| 3 | 355 | | break; |
| | 356 | |
|
| | 357 | | case "cube_back": |
| 3 | 358 | | currentMaterial.ReflectionMap.CubeBack = ObjMaterialFileReader.ParseMaterialMap("refl", |
| 3 | 359 | | break; |
| | 360 | |
|
| | 361 | | case "cube_left": |
| 3 | 362 | | currentMaterial.ReflectionMap.CubeLeft = ObjMaterialFileReader.ParseMaterialMap("refl", |
| 3 | 363 | | break; |
| | 364 | |
|
| | 365 | | case "cube_right": |
| 3 | 366 | | currentMaterial.ReflectionMap.CubeRight = ObjMaterialFileReader.ParseMaterialMap("refl", |
| | 367 | | break; |
| | 368 | | } |
| | 369 | |
|
| | 370 | | break; |
| | 371 | | } |
| | 372 | | } |
| | 373 | |
|
| 261 | 374 | | mtl.HeaderText = string.Join("\n", lineReader.HeaderTextLines.ToArray()); |
| | 375 | |
|
| 261 | 376 | | return mtl; |
| | 377 | | } |
| | 378 | |
|
| | 379 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | 380 | | private static ObjMaterialColor ParseMaterialColor(string statement, string[] values) |
| | 381 | | { |
| 195 | 382 | | if (values.Length < 2) |
| | 383 | | { |
| 15 | 384 | | throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a color.")); |
| | 385 | | } |
| | 386 | |
|
| 180 | 387 | | var color = new ObjMaterialColor(); |
| | 388 | |
|
| 180 | 389 | | int index = 1; |
| | 390 | |
|
| 180 | 391 | | switch (values[1].ToLowerInvariant()) |
| | 392 | | { |
| | 393 | | case "spectral": |
| 60 | 394 | | index++; |
| | 395 | |
|
| 60 | 396 | | if (values.Length - index < 1) |
| | 397 | | { |
| 15 | 398 | | throw new InvalidDataException(string.Concat("A ", statement, " spectral statement must specify |
| | 399 | | } |
| | 400 | |
|
| 45 | 401 | | if (!Path.HasExtension(values[index])) |
| | 402 | | { |
| 15 | 403 | | throw new InvalidDataException("A filename must have an extension."); |
| | 404 | | } |
| | 405 | |
|
| 30 | 406 | | color.SpectralFileName = values[index]; |
| 30 | 407 | | index++; |
| | 408 | |
|
| 30 | 409 | | if (values.Length > index) |
| | 410 | | { |
| 15 | 411 | | color.SpectralFactor = float.Parse(values[index], CultureInfo.InvariantCulture); |
| 15 | 412 | | index++; |
| | 413 | | } |
| | 414 | |
|
| 15 | 415 | | break; |
| | 416 | |
|
| | 417 | | case "xyz": |
| 60 | 418 | | index++; |
| | 419 | |
|
| 60 | 420 | | if (values.Length - index < 1) |
| | 421 | | { |
| 15 | 422 | | throw new InvalidDataException(string.Concat("A ", statement, " xyz statement must specify a col |
| | 423 | | } |
| | 424 | |
|
| 45 | 425 | | color.UseXYZColorSpace = true; |
| | 426 | |
|
| 45 | 427 | | var xyz = new ObjVector3(); |
| | 428 | |
|
| 45 | 429 | | xyz.X = float.Parse(values[index], CultureInfo.InvariantCulture); |
| 45 | 430 | | index++; |
| | 431 | |
|
| 45 | 432 | | if (values.Length > index) |
| | 433 | | { |
| 30 | 434 | | if (values.Length - index < 2) |
| | 435 | | { |
| 15 | 436 | | throw new InvalidDataException(string.Concat("A ", statement, " xyz statement must specify a |
| | 437 | | } |
| | 438 | |
|
| 15 | 439 | | xyz.Y = float.Parse(values[index], CultureInfo.InvariantCulture); |
| 15 | 440 | | xyz.Z = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| 15 | 441 | | index += 2; |
| | 442 | | } |
| | 443 | | else |
| | 444 | | { |
| 15 | 445 | | xyz.Y = xyz.X; |
| 15 | 446 | | xyz.Z = xyz.X; |
| | 447 | | } |
| | 448 | |
|
| 30 | 449 | | color.Color = xyz; |
| 30 | 450 | | break; |
| | 451 | |
|
| | 452 | | default: |
| 60 | 453 | | var rgb = new ObjVector3(); |
| | 454 | |
|
| 60 | 455 | | rgb.X = float.Parse(values[index], CultureInfo.InvariantCulture); |
| 60 | 456 | | index++; |
| | 457 | |
|
| 60 | 458 | | if (values.Length > index) |
| | 459 | | { |
| 45 | 460 | | if (values.Length - index < 2) |
| | 461 | | { |
| 15 | 462 | | throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a RGB |
| | 463 | | } |
| | 464 | |
|
| 30 | 465 | | rgb.Y = float.Parse(values[index], CultureInfo.InvariantCulture); |
| 30 | 466 | | rgb.Z = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| 30 | 467 | | index += 2; |
| | 468 | | } |
| | 469 | | else |
| | 470 | | { |
| 15 | 471 | | rgb.Y = rgb.X; |
| 15 | 472 | | rgb.Z = rgb.X; |
| | 473 | | } |
| | 474 | |
|
| 45 | 475 | | color.Color = rgb; |
| | 476 | | break; |
| | 477 | | } |
| | 478 | |
|
| 105 | 479 | | if (index != values.Length) |
| | 480 | | { |
| 15 | 481 | | throw new InvalidDataException(string.Concat("A ", statement, " statement has too many values.")); |
| | 482 | | } |
| | 483 | |
|
| 90 | 484 | | return color; |
| | 485 | | } |
| | 486 | |
|
| | 487 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | 488 | | private static ObjMaterialMap ParseMaterialMap(string statement, string[] values) |
| | 489 | | { |
| 315 | 490 | | var map = new ObjMaterialMap(); |
| | 491 | |
|
| 891 | 492 | | for (int index = 0; index < values.Length;) |
| | 493 | | { |
| 447 | 494 | | index++; |
| | 495 | |
|
| 447 | 496 | | if (values.Length - index < 1) |
| | 497 | | { |
| 54 | 498 | | throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a filename.") |
| | 499 | | } |
| | 500 | |
|
| 393 | 501 | | switch (values[index].ToLowerInvariant()) |
| | 502 | | { |
| | 503 | | case "-type": |
| 33 | 504 | | if (values.Length - index < 2) |
| | 505 | | { |
| 3 | 506 | | throw new InvalidDataException(string.Concat("A ", statement, " -type option must specify a |
| | 507 | | } |
| | 508 | |
|
| 30 | 509 | | index++; |
| 30 | 510 | | break; |
| | 511 | |
|
| | 512 | | case "-blenu": |
| 15 | 513 | | if (values.Length - index < 2) |
| | 514 | | { |
| 3 | 515 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenu option must specify a |
| | 516 | | } |
| | 517 | |
|
| 12 | 518 | | if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase)) |
| | 519 | | { |
| 3 | 520 | | map.IsHorizontalBlendingEnabled = true; |
| | 521 | | } |
| 9 | 522 | | else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase)) |
| | 523 | | { |
| 3 | 524 | | map.IsHorizontalBlendingEnabled = false; |
| | 525 | | } |
| | 526 | | else |
| | 527 | | { |
| 6 | 528 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenu option must specify o |
| | 529 | | } |
| | 530 | |
|
| 6 | 531 | | index++; |
| 6 | 532 | | break; |
| | 533 | |
|
| | 534 | | case "-blenv": |
| 15 | 535 | | if (values.Length - index < 2) |
| | 536 | | { |
| 3 | 537 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenv option must specify a |
| | 538 | | } |
| | 539 | |
|
| 12 | 540 | | if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase)) |
| | 541 | | { |
| 3 | 542 | | map.IsVerticalBlendingEnabled = true; |
| | 543 | | } |
| 9 | 544 | | else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase)) |
| | 545 | | { |
| 3 | 546 | | map.IsVerticalBlendingEnabled = false; |
| | 547 | | } |
| | 548 | | else |
| | 549 | | { |
| 6 | 550 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenv option must specify o |
| | 551 | | } |
| | 552 | |
|
| 6 | 553 | | index++; |
| 6 | 554 | | break; |
| | 555 | |
|
| | 556 | | case "-bm": |
| 9 | 557 | | if (values.Length - index < 2) |
| | 558 | | { |
| 3 | 559 | | throw new InvalidDataException(string.Concat("A ", statement, " -bm option must specify a va |
| | 560 | | } |
| | 561 | |
|
| 6 | 562 | | map.BumpMultiplier = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 563 | |
|
| 6 | 564 | | index++; |
| 6 | 565 | | break; |
| | 566 | |
|
| | 567 | | case "-boost": |
| 9 | 568 | | if (values.Length - index < 2) |
| | 569 | | { |
| 3 | 570 | | throw new InvalidDataException(string.Concat("A ", statement, " -boost option must specify a |
| | 571 | | } |
| | 572 | |
|
| 6 | 573 | | map.Boost = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 574 | |
|
| 6 | 575 | | index++; |
| 6 | 576 | | break; |
| | 577 | |
|
| | 578 | | case "-cc": |
| 15 | 579 | | if (values.Length - index < 2) |
| | 580 | | { |
| 3 | 581 | | throw new InvalidDataException(string.Concat("A ", statement, " -cc option must specify a va |
| | 582 | | } |
| | 583 | |
|
| 12 | 584 | | if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase)) |
| | 585 | | { |
| 3 | 586 | | map.IsColorCorrectionEnabled = true; |
| | 587 | | } |
| 9 | 588 | | else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase)) |
| | 589 | | { |
| 3 | 590 | | map.IsColorCorrectionEnabled = false; |
| | 591 | | } |
| | 592 | | else |
| | 593 | | { |
| 6 | 594 | | throw new InvalidDataException(string.Concat("A ", statement, " -cc option must specify on o |
| | 595 | | } |
| | 596 | |
|
| 6 | 597 | | index++; |
| 6 | 598 | | break; |
| | 599 | |
|
| | 600 | | case "-clamp": |
| 15 | 601 | | if (values.Length - index < 2) |
| | 602 | | { |
| 3 | 603 | | throw new InvalidDataException(string.Concat("A ", statement, " -clamp option must specify a |
| | 604 | | } |
| | 605 | |
|
| 12 | 606 | | if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase)) |
| | 607 | | { |
| 3 | 608 | | map.IsClampingEnabled = true; |
| | 609 | | } |
| 9 | 610 | | else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase)) |
| | 611 | | { |
| 3 | 612 | | map.IsClampingEnabled = false; |
| | 613 | | } |
| | 614 | | else |
| | 615 | | { |
| 6 | 616 | | throw new InvalidDataException(string.Concat("A ", statement, " -clamp option must specify o |
| | 617 | | } |
| | 618 | |
|
| 6 | 619 | | index++; |
| 6 | 620 | | break; |
| | 621 | |
|
| | 622 | | case "-imfchan": |
| 27 | 623 | | if (values.Length - index < 2) |
| | 624 | | { |
| 3 | 625 | | throw new InvalidDataException(string.Concat("A ", statement, " -imfchan option must specify |
| | 626 | | } |
| | 627 | |
|
| 24 | 628 | | switch (values[index + 1].ToLowerInvariant()) |
| | 629 | | { |
| | 630 | | case "r": |
| 3 | 631 | | map.ScalarChannel = ObjMapChannel.Red; |
| 3 | 632 | | break; |
| | 633 | |
|
| | 634 | | case "g": |
| 3 | 635 | | map.ScalarChannel = ObjMapChannel.Green; |
| 3 | 636 | | break; |
| | 637 | |
|
| | 638 | | case "b": |
| 3 | 639 | | map.ScalarChannel = ObjMapChannel.Blue; |
| 3 | 640 | | break; |
| | 641 | |
|
| | 642 | | case "m": |
| 3 | 643 | | map.ScalarChannel = ObjMapChannel.Matte; |
| 3 | 644 | | break; |
| | 645 | |
|
| | 646 | | case "l": |
| 3 | 647 | | map.ScalarChannel = ObjMapChannel.Luminance; |
| 3 | 648 | | break; |
| | 649 | |
|
| | 650 | | case "z": |
| 3 | 651 | | map.ScalarChannel = ObjMapChannel.Depth; |
| 3 | 652 | | break; |
| | 653 | |
|
| | 654 | | default: |
| 6 | 655 | | throw new InvalidDataException(string.Concat("A ", statement, " -imfchan option must spe |
| | 656 | | } |
| | 657 | |
|
| 18 | 658 | | index++; |
| 18 | 659 | | break; |
| | 660 | |
|
| | 661 | | case "-mm": |
| 12 | 662 | | if (values.Length - index < 3) |
| | 663 | | { |
| 6 | 664 | | throw new InvalidDataException(string.Concat("A ", statement, " -mm option must specify a ba |
| | 665 | | } |
| | 666 | |
|
| 6 | 667 | | map.ModifierBase = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| 6 | 668 | | map.ModifierGain = float.Parse(values[index + 2], CultureInfo.InvariantCulture); |
| | 669 | |
|
| 6 | 670 | | index += 2; |
| 6 | 671 | | break; |
| | 672 | |
|
| | 673 | | case "-o": |
| 15 | 674 | | if (values.Length - index < 2) |
| | 675 | | { |
| 3 | 676 | | throw new InvalidDataException(string.Concat("A ", statement, " -o option must specify at le |
| | 677 | | } |
| | 678 | |
|
| 12 | 679 | | var offset = new ObjVector3(); |
| | 680 | |
|
| 12 | 681 | | offset.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 682 | |
|
| 12 | 683 | | if (values.Length - index > 3) |
| | 684 | | { |
| 6 | 685 | | offset.Y = float.Parse(values[index + 2], CultureInfo.InvariantCulture); |
| | 686 | |
|
| 6 | 687 | | if (values.Length - index > 4) |
| | 688 | | { |
| 3 | 689 | | offset.Z = float.Parse(values[index + 3], CultureInfo.InvariantCulture); |
| 3 | 690 | | index++; |
| | 691 | | } |
| | 692 | |
|
| 6 | 693 | | index++; |
| | 694 | | } |
| | 695 | |
|
| 12 | 696 | | index++; |
| | 697 | |
|
| 12 | 698 | | map.Offset = offset; |
| 12 | 699 | | break; |
| | 700 | |
|
| | 701 | | case "-s": |
| 15 | 702 | | if (values.Length - index < 2) |
| | 703 | | { |
| 3 | 704 | | throw new InvalidDataException(string.Concat("A ", statement, " -s option must specify at le |
| | 705 | | } |
| | 706 | |
|
| 12 | 707 | | var scale = new ObjVector3(1.0f, 1.0f, 1.0f); |
| | 708 | |
|
| 12 | 709 | | scale.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 710 | |
|
| 12 | 711 | | if (values.Length - index > 3) |
| | 712 | | { |
| 6 | 713 | | scale.Y = float.Parse(values[index + 2], CultureInfo.InvariantCulture); |
| | 714 | |
|
| 6 | 715 | | if (values.Length - index > 4) |
| | 716 | | { |
| 3 | 717 | | scale.Z = float.Parse(values[index + 3], CultureInfo.InvariantCulture); |
| 3 | 718 | | index++; |
| | 719 | | } |
| | 720 | |
|
| 6 | 721 | | index++; |
| | 722 | | } |
| | 723 | |
|
| 12 | 724 | | index++; |
| | 725 | |
|
| 12 | 726 | | map.Scale = scale; |
| 12 | 727 | | break; |
| | 728 | |
|
| | 729 | | case "-t": |
| 15 | 730 | | if (values.Length - index < 2) |
| | 731 | | { |
| 3 | 732 | | throw new InvalidDataException(string.Concat("A ", statement, " -t option must specify at le |
| | 733 | | } |
| | 734 | |
|
| 12 | 735 | | var turbulence = new ObjVector3(); |
| | 736 | |
|
| 12 | 737 | | turbulence.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 738 | |
|
| 12 | 739 | | if (values.Length - index > 3) |
| | 740 | | { |
| 6 | 741 | | turbulence.Y = float.Parse(values[index + 2], CultureInfo.InvariantCulture); |
| | 742 | |
|
| 6 | 743 | | if (values.Length - index > 4) |
| | 744 | | { |
| 3 | 745 | | turbulence.Z = float.Parse(values[index + 3], CultureInfo.InvariantCulture); |
| 3 | 746 | | index++; |
| | 747 | | } |
| | 748 | |
|
| 6 | 749 | | index++; |
| | 750 | | } |
| | 751 | |
|
| 12 | 752 | | index++; |
| | 753 | |
|
| 12 | 754 | | map.Turbulence = turbulence; |
| 12 | 755 | | break; |
| | 756 | |
|
| | 757 | | case "-texres": |
| 9 | 758 | | if (values.Length - index < 2) |
| | 759 | | { |
| 3 | 760 | | throw new InvalidDataException(string.Concat("A ", statement, " -texres option must specify |
| | 761 | | } |
| | 762 | |
|
| 6 | 763 | | map.TextureResolution = int.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 764 | |
|
| 6 | 765 | | index++; |
| 6 | 766 | | break; |
| | 767 | |
|
| | 768 | | default: |
| 189 | 769 | | string filename = string.Join(" ", values, index, values.Length - index); |
| | 770 | |
|
| 189 | 771 | | if (!Path.HasExtension(filename)) |
| | 772 | | { |
| 30 | 773 | | throw new InvalidDataException("A filename must have an extension."); |
| | 774 | | } |
| 159 | 775 | | if (Path.GetExtension(filename).Contains(" ")) |
| | 776 | | { |
| 30 | 777 | | throw new InvalidDataException("A filename extension must not have whitespace."); |
| | 778 | | } |
| | 779 | |
|
| 129 | 780 | | map.FileName = filename; |
| 129 | 781 | | index = values.Length; |
| | 782 | |
|
| | 783 | | break; |
| | 784 | | } |
| | 785 | | } |
| | 786 | |
|
| 129 | 787 | | return map; |
| | 788 | | } |
| | 789 | | } |
| | 790 | | } |
| | 791 | |
|