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