| | | 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, ObjMaterialFileReaderSettings settings) |
| | | 19 | | { |
| | 433 | 20 | | if (stream == null) |
| | | 21 | | { |
| | 1 | 22 | | throw new ArgumentNullException(nameof(stream)); |
| | | 23 | | } |
| | | 24 | | |
| | 432 | 25 | | var mtl = new ObjMaterialFile(); |
| | 432 | 26 | | var lineReader = new LineReader(); |
| | | 27 | | |
| | 432 | 28 | | ObjMaterial? currentMaterial = null; |
| | | 29 | | |
| | 2365 | 30 | | foreach (string currentLine in lineReader.Read(stream)) |
| | | 31 | | { |
| | 822 | 32 | | string[] values = currentLine.Split(LineReader.LineSeparators, StringSplitOptions.RemoveEmptyEntries); |
| | | 33 | | |
| | 822 | 34 | | switch (values[0].ToLowerInvariant()) |
| | | 35 | | { |
| | | 36 | | case "newmtl": |
| | 397 | 37 | | if (values.Length < 2) |
| | | 38 | | { |
| | 1 | 39 | | throw new InvalidDataException("A newmtl statement must specify a name."); |
| | | 40 | | } |
| | | 41 | | |
| | 396 | 42 | | currentMaterial = new ObjMaterial |
| | 396 | 43 | | { |
| | 396 | 44 | | Name = string.Join(" ", values, 1, values.Length - 1) |
| | 396 | 45 | | }; |
| | | 46 | | |
| | 396 | 47 | | mtl.Materials.Add(currentMaterial); |
| | | 48 | | |
| | 396 | 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": |
| | 132 | 242 | | if (currentMaterial == null) |
| | | 243 | | { |
| | 1 | 244 | | throw new InvalidDataException("The material name is not specified."); |
| | | 245 | | } |
| | | 246 | | |
| | 131 | 247 | | currentMaterial.AmbientMap = ObjMaterialFileReader.ParseMaterialMap("map_Ka", values, currentLin |
| | 97 | 248 | | break; |
| | | 249 | | |
| | | 250 | | case "map_kd": |
| | 13 | 251 | | if (currentMaterial == null) |
| | | 252 | | { |
| | 1 | 253 | | throw new InvalidDataException("The material name is not specified."); |
| | | 254 | | } |
| | | 255 | | |
| | 12 | 256 | | currentMaterial.DiffuseMap = ObjMaterialFileReader.ParseMaterialMap("map_Kd", values, currentLin |
| | 11 | 257 | | break; |
| | | 258 | | |
| | | 259 | | case "map_ke": |
| | 13 | 260 | | if (currentMaterial == null) |
| | | 261 | | { |
| | 1 | 262 | | throw new InvalidDataException("The material name is not specified."); |
| | | 263 | | } |
| | | 264 | | |
| | 12 | 265 | | currentMaterial.EmissiveMap = ObjMaterialFileReader.ParseMaterialMap("map_Ke", values, currentLi |
| | 11 | 266 | | break; |
| | | 267 | | |
| | | 268 | | case "map_ks": |
| | 13 | 269 | | if (currentMaterial == null) |
| | | 270 | | { |
| | 1 | 271 | | throw new InvalidDataException("The material name is not specified."); |
| | | 272 | | } |
| | | 273 | | |
| | 12 | 274 | | currentMaterial.SpecularMap = ObjMaterialFileReader.ParseMaterialMap("map_Ks", values, currentLi |
| | 11 | 275 | | break; |
| | | 276 | | |
| | | 277 | | case "map_ns": |
| | 13 | 278 | | if (currentMaterial == null) |
| | | 279 | | { |
| | 1 | 280 | | throw new InvalidDataException("The material name is not specified."); |
| | | 281 | | } |
| | | 282 | | |
| | 12 | 283 | | currentMaterial.SpecularExponentMap = ObjMaterialFileReader.ParseMaterialMap("map_Ns", values, c |
| | 11 | 284 | | break; |
| | | 285 | | |
| | | 286 | | case "map_d": |
| | | 287 | | case "map_tr": |
| | 13 | 288 | | if (currentMaterial == null) |
| | | 289 | | { |
| | 1 | 290 | | throw new InvalidDataException("The material name is not specified."); |
| | | 291 | | } |
| | | 292 | | |
| | 12 | 293 | | currentMaterial.DissolveMap = ObjMaterialFileReader.ParseMaterialMap("map_d", values, currentLin |
| | 11 | 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, currentLine, |
| | 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, currentLine, se |
| | 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, currentLine, se |
| | 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": |
| | 13 | 394 | | if (currentMaterial == null) |
| | | 395 | | { |
| | 1 | 396 | | throw new InvalidDataException("The material name is not specified."); |
| | | 397 | | } |
| | | 398 | | |
| | 12 | 399 | | currentMaterial.RoughnessMap = ObjMaterialFileReader.ParseMaterialMap("map_Pr", values, currentL |
| | 11 | 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": |
| | 13 | 420 | | if (currentMaterial == null) |
| | | 421 | | { |
| | 1 | 422 | | throw new InvalidDataException("The material name is not specified."); |
| | | 423 | | } |
| | | 424 | | |
| | 12 | 425 | | currentMaterial.MetallicMap = ObjMaterialFileReader.ParseMaterialMap("map_Pm", values, currentLi |
| | 11 | 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": |
| | 13 | 446 | | if (currentMaterial == null) |
| | | 447 | | { |
| | 1 | 448 | | throw new InvalidDataException("The material name is not specified."); |
| | | 449 | | } |
| | | 450 | | |
| | 12 | 451 | | currentMaterial.SheenMap = ObjMaterialFileReader.ParseMaterialMap("map_Ps", values, currentLine, |
| | 11 | 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": |
| | 13 | 526 | | if (currentMaterial == null) |
| | | 527 | | { |
| | 1 | 528 | | throw new InvalidDataException("The material name is not specified."); |
| | | 529 | | } |
| | | 530 | | |
| | 12 | 531 | | currentMaterial.Norm = ObjMaterialFileReader.ParseMaterialMap("norm", values, currentLine, setti |
| | | 532 | | break; |
| | | 533 | | } |
| | | 534 | | } |
| | | 535 | | |
| | 289 | 536 | | mtl.HeaderText = string.Join("\n", lineReader.HeaderTextLines.ToArray()); |
| | | 537 | | |
| | 289 | 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": |
| | 20 | 556 | | index++; |
| | | 557 | | |
| | 20 | 558 | | if (values.Length - index < 1) |
| | | 559 | | { |
| | 5 | 560 | | throw new InvalidDataException(string.Concat("A ", statement, " spectral statement must specify |
| | | 561 | | } |
| | | 562 | | |
| | 15 | 563 | | color.SpectralFileName = values[index]; |
| | 15 | 564 | | index++; |
| | | 565 | | |
| | 15 | 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 | | { |
| | 25 | 613 | | var rgb = new ObjVector3(); |
| | | 614 | | |
| | 25 | 615 | | rgb.X = float.Parse(values[index], CultureInfo.InvariantCulture); |
| | 25 | 616 | | index++; |
| | | 617 | | |
| | 25 | 618 | | if (values.Length > index) |
| | | 619 | | { |
| | 20 | 620 | | if (values.Length - index < 2) |
| | | 621 | | { |
| | 5 | 622 | | throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a |
| | | 623 | | } |
| | | 624 | | |
| | 15 | 625 | | rgb.Y = float.Parse(values[index], CultureInfo.InvariantCulture); |
| | 15 | 626 | | rgb.Z = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 15 | 627 | | index += 2; |
| | | 628 | | } |
| | | 629 | | else |
| | | 630 | | { |
| | 5 | 631 | | rgb.Y = rgb.X; |
| | 5 | 632 | | rgb.Z = rgb.X; |
| | | 633 | | } |
| | | 634 | | |
| | 20 | 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, string currentLine, ObjMateria |
| | | 650 | | { |
| | 270 | 651 | | var map = new ObjMaterialMap(); |
| | | 652 | | |
| | 879 | 653 | | for (int index = 0; index < values.Length;) |
| | | 654 | | { |
| | 385 | 655 | | index++; |
| | | 656 | | |
| | 385 | 657 | | if (values.Length - index < 1) |
| | | 658 | | { |
| | 22 | 659 | | throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a filename.") |
| | | 660 | | } |
| | | 661 | | |
| | 363 | 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": |
| | 7 | 674 | | if (values.Length - index < 2) |
| | | 675 | | { |
| | 1 | 676 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenu option must specify a |
| | | 677 | | } |
| | | 678 | | |
| | 6 | 679 | | if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase)) |
| | | 680 | | { |
| | 2 | 681 | | map.IsHorizontalBlendingEnabled = true; |
| | | 682 | | } |
| | 4 | 683 | | else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase)) |
| | | 684 | | { |
| | 2 | 685 | | map.IsHorizontalBlendingEnabled = false; |
| | | 686 | | } |
| | | 687 | | else |
| | | 688 | | { |
| | 2 | 689 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenu option must specify o |
| | | 690 | | } |
| | | 691 | | |
| | 4 | 692 | | index++; |
| | 4 | 693 | | break; |
| | | 694 | | |
| | | 695 | | case "-blenv": |
| | 7 | 696 | | if (values.Length - index < 2) |
| | | 697 | | { |
| | 1 | 698 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenv option must specify a |
| | | 699 | | } |
| | | 700 | | |
| | 6 | 701 | | if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase)) |
| | | 702 | | { |
| | 2 | 703 | | map.IsVerticalBlendingEnabled = true; |
| | | 704 | | } |
| | 4 | 705 | | else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase)) |
| | | 706 | | { |
| | 2 | 707 | | map.IsVerticalBlendingEnabled = false; |
| | | 708 | | } |
| | | 709 | | else |
| | | 710 | | { |
| | 2 | 711 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenv option must specify o |
| | | 712 | | } |
| | | 713 | | |
| | 4 | 714 | | index++; |
| | 4 | 715 | | break; |
| | | 716 | | |
| | | 717 | | case "-bm": |
| | 6 | 718 | | if (values.Length - index < 2) |
| | | 719 | | { |
| | 1 | 720 | | throw new InvalidDataException(string.Concat("A ", statement, " -bm option must specify a va |
| | | 721 | | } |
| | | 722 | | |
| | 5 | 723 | | map.BumpMultiplier = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | | 724 | | |
| | 5 | 725 | | index++; |
| | 5 | 726 | | break; |
| | | 727 | | |
| | | 728 | | case "-boost": |
| | 6 | 729 | | if (values.Length - index < 2) |
| | | 730 | | { |
| | 1 | 731 | | throw new InvalidDataException(string.Concat("A ", statement, " -boost option must specify a |
| | | 732 | | } |
| | | 733 | | |
| | 5 | 734 | | map.Boost = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | | 735 | | |
| | 5 | 736 | | index++; |
| | 5 | 737 | | break; |
| | | 738 | | |
| | | 739 | | case "-cc": |
| | 7 | 740 | | if (values.Length - index < 2) |
| | | 741 | | { |
| | 1 | 742 | | throw new InvalidDataException(string.Concat("A ", statement, " -cc option must specify a va |
| | | 743 | | } |
| | | 744 | | |
| | 6 | 745 | | if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase)) |
| | | 746 | | { |
| | 2 | 747 | | map.IsColorCorrectionEnabled = true; |
| | | 748 | | } |
| | 4 | 749 | | else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase)) |
| | | 750 | | { |
| | 2 | 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 | | |
| | 4 | 758 | | index++; |
| | 4 | 759 | | break; |
| | | 760 | | |
| | | 761 | | case "-clamp": |
| | 7 | 762 | | if (values.Length - index < 2) |
| | | 763 | | { |
| | 1 | 764 | | throw new InvalidDataException(string.Concat("A ", statement, " -clamp option must specify a |
| | | 765 | | } |
| | | 766 | | |
| | 6 | 767 | | if (string.Equals(values[index + 1], "on", StringComparison.OrdinalIgnoreCase)) |
| | | 768 | | { |
| | 2 | 769 | | map.IsClampingEnabled = true; |
| | | 770 | | } |
| | 4 | 771 | | else if (string.Equals(values[index + 1], "off", StringComparison.OrdinalIgnoreCase)) |
| | | 772 | | { |
| | 2 | 773 | | map.IsClampingEnabled = false; |
| | | 774 | | } |
| | | 775 | | else |
| | | 776 | | { |
| | 2 | 777 | | throw new InvalidDataException(string.Concat("A ", statement, " -clamp option must specify o |
| | | 778 | | } |
| | | 779 | | |
| | 4 | 780 | | index++; |
| | 4 | 781 | | break; |
| | | 782 | | |
| | | 783 | | case "-imfchan": |
| | 21 | 784 | | if (values.Length - index < 2) |
| | | 785 | | { |
| | 1 | 786 | | throw new InvalidDataException(string.Concat("A ", statement, " -imfchan option must specify |
| | | 787 | | } |
| | | 788 | | |
| | 20 | 789 | | switch (values[index + 1].ToLowerInvariant()) |
| | | 790 | | { |
| | | 791 | | case "r": |
| | 3 | 792 | | map.ScalarChannel = ObjMapChannel.Red; |
| | 3 | 793 | | break; |
| | | 794 | | |
| | | 795 | | case "g": |
| | 3 | 796 | | map.ScalarChannel = ObjMapChannel.Green; |
| | 3 | 797 | | break; |
| | | 798 | | |
| | | 799 | | case "b": |
| | 3 | 800 | | map.ScalarChannel = ObjMapChannel.Blue; |
| | 3 | 801 | | break; |
| | | 802 | | |
| | | 803 | | case "m": |
| | 3 | 804 | | map.ScalarChannel = ObjMapChannel.Matte; |
| | 3 | 805 | | break; |
| | | 806 | | |
| | | 807 | | case "l": |
| | 3 | 808 | | map.ScalarChannel = ObjMapChannel.Luminance; |
| | 3 | 809 | | break; |
| | | 810 | | |
| | | 811 | | case "z": |
| | 3 | 812 | | map.ScalarChannel = ObjMapChannel.Depth; |
| | 3 | 813 | | break; |
| | | 814 | | |
| | | 815 | | default: |
| | 2 | 816 | | throw new InvalidDataException(string.Concat("A ", statement, " -imfchan option must spe |
| | | 817 | | } |
| | | 818 | | |
| | 18 | 819 | | index++; |
| | 18 | 820 | | break; |
| | | 821 | | |
| | | 822 | | case "-mm": |
| | 7 | 823 | | if (values.Length - index < 3) |
| | | 824 | | { |
| | 2 | 825 | | throw new InvalidDataException(string.Concat("A ", statement, " -mm option must specify a ba |
| | | 826 | | } |
| | | 827 | | |
| | 5 | 828 | | map.ModifierBase = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 5 | 829 | | map.ModifierGain = float.Parse(values[index + 2], CultureInfo.InvariantCulture); |
| | | 830 | | |
| | 5 | 831 | | index += 2; |
| | 5 | 832 | | break; |
| | | 833 | | |
| | | 834 | | case "-o": |
| | 14 | 835 | | if (values.Length - index < 2) |
| | | 836 | | { |
| | 1 | 837 | | throw new InvalidDataException(string.Concat("A ", statement, " -o option must specify at le |
| | | 838 | | } |
| | | 839 | | |
| | 13 | 840 | | var offset = new ObjVector3(); |
| | | 841 | | |
| | 13 | 842 | | offset.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 13 | 843 | | index++; |
| | | 844 | | |
| | 13 | 845 | | if (values.Length - index > 2) |
| | | 846 | | { |
| | 10 | 847 | | if (float.TryParse(values[index + 1], NumberStyles.Float, CultureInfo.InvariantCulture, out |
| | | 848 | | { |
| | 8 | 849 | | offset.Y = v; |
| | 8 | 850 | | index++; |
| | | 851 | | } |
| | | 852 | | else |
| | | 853 | | { |
| | 2 | 854 | | map.Offset = offset; |
| | 2 | 855 | | break; |
| | | 856 | | } |
| | | 857 | | |
| | 8 | 858 | | if (values.Length - index > 2) |
| | | 859 | | { |
| | 6 | 860 | | if (float.TryParse(values[index + 1], NumberStyles.Float, CultureInfo.InvariantCulture, |
| | | 861 | | { |
| | 4 | 862 | | offset.Z = v; |
| | 4 | 863 | | index++; |
| | | 864 | | } |
| | | 865 | | else |
| | | 866 | | { |
| | 2 | 867 | | map.Offset = offset; |
| | 2 | 868 | | break; |
| | | 869 | | } |
| | | 870 | | } |
| | | 871 | | } |
| | | 872 | | |
| | 9 | 873 | | map.Offset = offset; |
| | 9 | 874 | | break; |
| | | 875 | | |
| | | 876 | | case "-s": |
| | | 877 | | { |
| | 14 | 878 | | if (values.Length - index < 2) |
| | | 879 | | { |
| | 1 | 880 | | throw new InvalidDataException(string.Concat("A ", statement, " -s option must specify a |
| | | 881 | | } |
| | | 882 | | |
| | 13 | 883 | | var scale = new ObjVector3(1.0f, 1.0f, 1.0f); |
| | | 884 | | |
| | 13 | 885 | | scale.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 13 | 886 | | index++; |
| | | 887 | | |
| | 13 | 888 | | if (values.Length - index > 2) |
| | | 889 | | { |
| | 10 | 890 | | if (float.TryParse(values[index + 1], NumberStyles.Float, CultureInfo.InvariantCulture, |
| | | 891 | | { |
| | 8 | 892 | | scale.Y = v; |
| | 8 | 893 | | index++; |
| | | 894 | | } |
| | | 895 | | else |
| | | 896 | | { |
| | 2 | 897 | | map.Scale = scale; |
| | 2 | 898 | | break; |
| | | 899 | | } |
| | | 900 | | |
| | 8 | 901 | | if (values.Length - index > 2) |
| | | 902 | | { |
| | 6 | 903 | | if (float.TryParse(values[index + 1], NumberStyles.Float, CultureInfo.InvariantCultu |
| | | 904 | | { |
| | 4 | 905 | | scale.Z = v; |
| | 4 | 906 | | index++; |
| | | 907 | | } |
| | | 908 | | else |
| | | 909 | | { |
| | 2 | 910 | | map.Scale = scale; |
| | 2 | 911 | | break; |
| | | 912 | | } |
| | | 913 | | } |
| | | 914 | | } |
| | | 915 | | |
| | 9 | 916 | | map.Scale = scale; |
| | 9 | 917 | | break; |
| | | 918 | | } |
| | | 919 | | |
| | | 920 | | case "-t": |
| | | 921 | | { |
| | 14 | 922 | | if (values.Length - index < 2) |
| | | 923 | | { |
| | 1 | 924 | | throw new InvalidDataException(string.Concat("A ", statement, " -t option must specify a |
| | | 925 | | } |
| | | 926 | | |
| | 13 | 927 | | var turbulence = new ObjVector3(); |
| | | 928 | | |
| | 13 | 929 | | turbulence.X = float.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | 13 | 930 | | index++; |
| | | 931 | | |
| | 13 | 932 | | if (values.Length - index > 2) |
| | | 933 | | { |
| | 10 | 934 | | if (float.TryParse(values[index + 1], NumberStyles.Float, CultureInfo.InvariantCulture, |
| | | 935 | | { |
| | 8 | 936 | | turbulence.Y = v; |
| | 8 | 937 | | index++; |
| | | 938 | | } |
| | | 939 | | else |
| | | 940 | | { |
| | 2 | 941 | | map.Turbulence = turbulence; |
| | 2 | 942 | | break; |
| | | 943 | | } |
| | | 944 | | |
| | 8 | 945 | | if (values.Length - index > 2) |
| | | 946 | | { |
| | 6 | 947 | | if (float.TryParse(values[index + 1], NumberStyles.Float, CultureInfo.InvariantCultu |
| | | 948 | | { |
| | 4 | 949 | | turbulence.Z = v; |
| | 4 | 950 | | index++; |
| | | 951 | | } |
| | | 952 | | else |
| | | 953 | | { |
| | 2 | 954 | | map.Turbulence = turbulence; |
| | 2 | 955 | | break; |
| | | 956 | | } |
| | | 957 | | } |
| | | 958 | | } |
| | | 959 | | |
| | 9 | 960 | | map.Turbulence = turbulence; |
| | 9 | 961 | | break; |
| | | 962 | | } |
| | | 963 | | |
| | | 964 | | case "-texres": |
| | 6 | 965 | | if (values.Length - index < 2) |
| | | 966 | | { |
| | 1 | 967 | | throw new InvalidDataException(string.Concat("A ", statement, " -texres option must specify |
| | | 968 | | } |
| | | 969 | | |
| | 5 | 970 | | map.TextureResolution = int.Parse(values[index + 1], CultureInfo.InvariantCulture); |
| | | 971 | | |
| | 5 | 972 | | index++; |
| | 5 | 973 | | break; |
| | | 974 | | |
| | | 975 | | default: |
| | | 976 | | { |
| | 224 | 977 | | if (settings.KeepWhitespacesOfMapFileReferences) |
| | | 978 | | { |
| | 70 | 979 | | var charsRead = 0; |
| | 340 | 980 | | for (var i = 1; i < index; i++) |
| | | 981 | | { |
| | 100 | 982 | | charsRead += values[i].Length; |
| | | 983 | | } |
| | 70 | 984 | | map.FileName = currentLine.Remove(0, statement.Length + charsRead + index); |
| | | 985 | | } |
| | | 986 | | else |
| | | 987 | | { |
| | 154 | 988 | | string filename = string.Join(" ", values, index, values.Length - index); |
| | | 989 | | |
| | 154 | 990 | | map.FileName = filename; |
| | | 991 | | } |
| | 224 | 992 | | index = values.Length; |
| | | 993 | | |
| | | 994 | | break; |
| | | 995 | | } |
| | | 996 | | } |
| | | 997 | | } |
| | | 998 | | |
| | 224 | 999 | | return map; |
| | | 1000 | | } |
| | | 1001 | | } |
| | | 1002 | | } |
| | | 1003 | | |
| | | 1004 | | #endif |
| | | 1005 | | |