| | | 1 | | // <copyright file="ObjMaterialFileReader9.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 | | using System.Text; |
| | | 13 | | |
| | | 14 | | #if NET9_0_OR_GREATER |
| | | 15 | | using SpanSplitEnumerator = System.MemoryExtensions.SpanSplitEnumerator<char>; |
| | | 16 | | #else |
| | | 17 | | using SpanSplitEnumerator = Polyfills.Polyfill.SpanSplitEnumerator<char>; |
| | | 18 | | #endif |
| | | 19 | | |
| | | 20 | | namespace JeremyAnsel.Media.WavefrontObj |
| | | 21 | | { |
| | | 22 | | internal static class ObjMaterialFileReader9 |
| | | 23 | | { |
| | | 24 | | private static void MoveNextSkipEmpty(ref SpanSplitEnumerator values) |
| | | 25 | | { |
| | 6396 | 26 | | while (values.MoveNext()) |
| | | 27 | | { |
| | 6375 | 28 | | if (values.Current.Start.Value != values.Current.End.Value) |
| | | 29 | | { |
| | 6051 | 30 | | return; |
| | | 31 | | } |
| | | 32 | | } |
| | 21 | 33 | | } |
| | | 34 | | |
| | | 35 | | private static ReadOnlySpan<char> GetNextValue(ref ReadOnlySpan<char> currentLine, ref SpanSplitEnumerator value |
| | | 36 | | { |
| | 3606 | 37 | | MoveNextSkipEmpty(ref values); |
| | 3606 | 38 | | return currentLine[values.Current]; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | private static bool TryFloatParse(ReadOnlySpan<char> s, out float value) |
| | | 42 | | { |
| | 624 | 43 | | return float.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out value); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | private static bool TryIntParse(ReadOnlySpan<char> s, out int value) |
| | | 47 | | { |
| | 21 | 48 | | return int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out value); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | | 52 | | public static ObjMaterialFile FromStream(Stream? stream, ObjMaterialFileReaderSettings settings) |
| | | 53 | | { |
| | 1299 | 54 | | if (stream == null) |
| | | 55 | | { |
| | 3 | 56 | | throw new ArgumentNullException(nameof(stream)); |
| | | 57 | | } |
| | | 58 | | |
| | 1296 | 59 | | var mtl = new ObjMaterialFile(); |
| | 1296 | 60 | | var lineReader = new LineReader9(); |
| | | 61 | | |
| | 1296 | 62 | | ObjMaterial? currentMaterial = null; |
| | | 63 | | |
| | 1296 | 64 | | int valueBufferSize = 16; |
| | 1296 | 65 | | Span<char> valueBuffer = stackalloc char[valueBufferSize]; |
| | | 66 | | |
| | 7095 | 67 | | foreach (var currentLineString in lineReader.Read9(stream)) |
| | | 68 | | { |
| | 2466 | 69 | | ReadOnlySpan<char> currentLine = currentLineString.Buffer.AsSpan()[..currentLineString.Length]; |
| | | 70 | | |
| | 2466 | 71 | | int valuesCount = 0; |
| | | 72 | | |
| | 18858 | 73 | | foreach (Range range in currentLine.SplitAny(LineReader9.LineSeparators)) |
| | | 74 | | { |
| | 6963 | 75 | | if (currentLine[range].Length == 0) |
| | | 76 | | { |
| | | 77 | | continue; |
| | | 78 | | } |
| | | 79 | | |
| | 6387 | 80 | | valuesCount++; |
| | | 81 | | } |
| | | 82 | | |
| | 2466 | 83 | | SpanSplitEnumerator values = currentLine.SplitAny(LineReader9.LineSeparators); |
| | | 84 | | |
| | 2466 | 85 | | MoveNextSkipEmpty(ref values); |
| | | 86 | | |
| | 2466 | 87 | | if (values.Current.End.Value - values.Current.Start.Value > valueBufferSize) |
| | | 88 | | { |
| | | 89 | | continue; |
| | | 90 | | } |
| | | 91 | | |
| | 2463 | 92 | | ReadOnlySpan<char> value0 = currentLine[values.Current]; |
| | 2463 | 93 | | int value0Length = value0.ToLowerInvariant(valueBuffer); |
| | | 94 | | |
| | | 95 | | //if (value0Length == -1) |
| | | 96 | | //{ |
| | | 97 | | // throw new InvalidDataException("the buffer is too small"); |
| | | 98 | | //} |
| | | 99 | | |
| | 2463 | 100 | | switch (valueBuffer[..value0Length]) |
| | | 101 | | { |
| | | 102 | | case "newmtl": |
| | | 103 | | { |
| | 1191 | 104 | | if (valuesCount < 2) |
| | | 105 | | { |
| | 3 | 106 | | throw new InvalidDataException("A newmtl statement must specify a name."); |
| | | 107 | | } |
| | | 108 | | |
| | 1188 | 109 | | var sb = new StringBuilder(); |
| | | 110 | | |
| | 1188 | 111 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 112 | | |
| | 2394 | 113 | | for (int i = 2; i < valuesCount; i++) |
| | | 114 | | { |
| | 9 | 115 | | sb.Append(' '); |
| | 9 | 116 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 117 | | } |
| | | 118 | | |
| | 1188 | 119 | | currentMaterial = new ObjMaterial |
| | 1188 | 120 | | { |
| | 1188 | 121 | | Name = sb.ToString() |
| | 1188 | 122 | | }; |
| | | 123 | | |
| | 1188 | 124 | | mtl.Materials.Add(currentMaterial); |
| | | 125 | | |
| | 1188 | 126 | | break; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | case "ka": |
| | 45 | 130 | | if (currentMaterial == null) |
| | | 131 | | { |
| | 3 | 132 | | throw new InvalidDataException("The material name is not specified."); |
| | | 133 | | } |
| | | 134 | | |
| | 42 | 135 | | currentMaterial.AmbientColor = ParseMaterialColor("Ka", value0, ref currentLine, ref values, val |
| | 24 | 136 | | break; |
| | | 137 | | |
| | | 138 | | case "kd": |
| | 45 | 139 | | if (currentMaterial == null) |
| | | 140 | | { |
| | 3 | 141 | | throw new InvalidDataException("The material name is not specified."); |
| | | 142 | | } |
| | | 143 | | |
| | 42 | 144 | | currentMaterial.DiffuseColor = ParseMaterialColor("Kd", value0, ref currentLine, ref values, val |
| | 24 | 145 | | break; |
| | | 146 | | |
| | | 147 | | case "ke": |
| | 45 | 148 | | if (currentMaterial == null) |
| | | 149 | | { |
| | 3 | 150 | | throw new InvalidDataException("The material name is not specified."); |
| | | 151 | | } |
| | | 152 | | |
| | 42 | 153 | | currentMaterial.EmissiveColor = ParseMaterialColor("Ke", value0, ref currentLine, ref values, va |
| | 24 | 154 | | break; |
| | | 155 | | |
| | | 156 | | case "ks": |
| | 45 | 157 | | if (currentMaterial == null) |
| | | 158 | | { |
| | 3 | 159 | | throw new InvalidDataException("The material name is not specified."); |
| | | 160 | | } |
| | | 161 | | |
| | 42 | 162 | | currentMaterial.SpecularColor = ParseMaterialColor("Ks", value0, ref currentLine, ref values, va |
| | 24 | 163 | | break; |
| | | 164 | | |
| | | 165 | | case "tf": |
| | 45 | 166 | | if (currentMaterial == null) |
| | | 167 | | { |
| | 3 | 168 | | throw new InvalidDataException("The material name is not specified."); |
| | | 169 | | } |
| | | 170 | | |
| | 42 | 171 | | currentMaterial.TransmissionColor = ParseMaterialColor("Tf", value0, ref currentLine, ref values |
| | 24 | 172 | | break; |
| | | 173 | | |
| | | 174 | | case "illum": |
| | | 175 | | { |
| | 12 | 176 | | if (currentMaterial == null) |
| | | 177 | | { |
| | 3 | 178 | | throw new InvalidDataException("The material name is not specified."); |
| | | 179 | | } |
| | | 180 | | |
| | 9 | 181 | | if (valuesCount < 2) |
| | | 182 | | { |
| | 3 | 183 | | throw new InvalidDataException("An illum statement must specify an illumination model."); |
| | | 184 | | } |
| | | 185 | | |
| | 6 | 186 | | if (valuesCount != 2) |
| | | 187 | | { |
| | 3 | 188 | | throw new InvalidDataException("An illum statement has too many values."); |
| | | 189 | | } |
| | | 190 | | |
| | 3 | 191 | | if (TryIntParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 192 | | { |
| | 3 | 193 | | currentMaterial.IlluminationModel = value; |
| | | 194 | | } |
| | | 195 | | |
| | 3 | 196 | | break; |
| | | 197 | | } |
| | | 198 | | case "d": |
| | | 199 | | { |
| | 21 | 200 | | if (currentMaterial == null) |
| | | 201 | | { |
| | 3 | 202 | | throw new InvalidDataException("The material name is not specified."); |
| | | 203 | | } |
| | | 204 | | |
| | 18 | 205 | | if (valuesCount < 2) |
| | | 206 | | { |
| | 3 | 207 | | throw new InvalidDataException("A d statement must specify a factor."); |
| | | 208 | | } |
| | | 209 | | |
| | 15 | 210 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 211 | | |
| | 15 | 212 | | if (value1.Equals("-halo", StringComparison.OrdinalIgnoreCase)) |
| | | 213 | | { |
| | 9 | 214 | | if (valuesCount < 3) |
| | | 215 | | { |
| | 3 | 216 | | throw new InvalidDataException("A d statement must specify a factor."); |
| | | 217 | | } |
| | | 218 | | |
| | 6 | 219 | | if (valuesCount != 3) |
| | | 220 | | { |
| | 3 | 221 | | throw new InvalidDataException("A d statement has too many values."); |
| | | 222 | | } |
| | | 223 | | |
| | 3 | 224 | | currentMaterial.IsHaloDissolve = true; |
| | 3 | 225 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 226 | | { |
| | 3 | 227 | | currentMaterial.DissolveFactor = value; |
| | | 228 | | } |
| | | 229 | | } |
| | | 230 | | else |
| | | 231 | | { |
| | 6 | 232 | | if (valuesCount != 2) |
| | | 233 | | { |
| | 3 | 234 | | throw new InvalidDataException("A d statement has too many values."); |
| | | 235 | | } |
| | | 236 | | |
| | 3 | 237 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 238 | | { |
| | 3 | 239 | | currentMaterial.DissolveFactor = value; |
| | | 240 | | } |
| | | 241 | | } |
| | | 242 | | |
| | 3 | 243 | | break; |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | case "ns": |
| | | 247 | | { |
| | 12 | 248 | | if (currentMaterial == null) |
| | | 249 | | { |
| | 3 | 250 | | throw new InvalidDataException("The material name is not specified."); |
| | | 251 | | } |
| | | 252 | | |
| | 9 | 253 | | if (valuesCount < 2) |
| | | 254 | | { |
| | 3 | 255 | | throw new InvalidDataException("A Ns statement must specify a specular exponent."); |
| | | 256 | | } |
| | | 257 | | |
| | 6 | 258 | | if (valuesCount != 2) |
| | | 259 | | { |
| | 3 | 260 | | throw new InvalidDataException("A Ns statement has too many values."); |
| | | 261 | | } |
| | | 262 | | |
| | 3 | 263 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 264 | | { |
| | 3 | 265 | | currentMaterial.SpecularExponent = value; |
| | | 266 | | } |
| | 3 | 267 | | break; |
| | | 268 | | } |
| | | 269 | | case "sharpness": |
| | | 270 | | { |
| | 12 | 271 | | if (currentMaterial == null) |
| | | 272 | | { |
| | 3 | 273 | | throw new InvalidDataException("The material name is not specified."); |
| | | 274 | | } |
| | | 275 | | |
| | 9 | 276 | | if (valuesCount < 2) |
| | | 277 | | { |
| | 3 | 278 | | throw new InvalidDataException("A sharpness statement must specify a sharpness value."); |
| | | 279 | | } |
| | | 280 | | |
| | 6 | 281 | | if (valuesCount != 2) |
| | | 282 | | { |
| | 3 | 283 | | throw new InvalidDataException("A sharpness statement has too many values."); |
| | | 284 | | } |
| | 3 | 285 | | if (TryIntParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 286 | | { |
| | 3 | 287 | | currentMaterial.Sharpness = value; |
| | | 288 | | } |
| | 3 | 289 | | break; |
| | | 290 | | } |
| | | 291 | | case "ni": |
| | | 292 | | { |
| | 12 | 293 | | if (currentMaterial == null) |
| | | 294 | | { |
| | 3 | 295 | | throw new InvalidDataException("The material name is not specified."); |
| | | 296 | | } |
| | | 297 | | |
| | 9 | 298 | | if (valuesCount < 2) |
| | | 299 | | { |
| | 3 | 300 | | throw new InvalidDataException("A Ni statement must specify an optical density."); |
| | | 301 | | } |
| | | 302 | | |
| | 6 | 303 | | if (valuesCount != 2) |
| | | 304 | | { |
| | 3 | 305 | | throw new InvalidDataException("A Ni statement has too many values."); |
| | | 306 | | } |
| | | 307 | | |
| | 3 | 308 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 309 | | { |
| | 3 | 310 | | currentMaterial.OpticalDensity = value; |
| | | 311 | | } |
| | | 312 | | |
| | 3 | 313 | | break; |
| | | 314 | | } |
| | | 315 | | case "map_aat": |
| | | 316 | | { |
| | 18 | 317 | | if (currentMaterial == null) |
| | | 318 | | { |
| | 3 | 319 | | throw new InvalidDataException("The material name is not specified."); |
| | | 320 | | } |
| | | 321 | | |
| | 15 | 322 | | if (valuesCount < 2) |
| | | 323 | | { |
| | 3 | 324 | | throw new InvalidDataException("A map_aat statement must specify a value."); |
| | | 325 | | } |
| | | 326 | | |
| | 12 | 327 | | if (valuesCount != 2) |
| | | 328 | | { |
| | 3 | 329 | | throw new InvalidDataException("A map_aat statement has too many values."); |
| | | 330 | | } |
| | | 331 | | |
| | 9 | 332 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 333 | | |
| | 9 | 334 | | if (value1.Equals("on", StringComparison.OrdinalIgnoreCase)) |
| | | 335 | | { |
| | 3 | 336 | | currentMaterial.IsAntiAliasingEnabled = true; |
| | | 337 | | } |
| | 6 | 338 | | else if (value1.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 339 | | { |
| | 3 | 340 | | currentMaterial.IsAntiAliasingEnabled = false; |
| | | 341 | | } |
| | | 342 | | else |
| | | 343 | | { |
| | 3 | 344 | | throw new InvalidDataException("A map_aat statement must specify on or off."); |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | break; |
| | | 348 | | } |
| | | 349 | | |
| | | 350 | | case "map_ka": |
| | 396 | 351 | | if (currentMaterial == null) |
| | | 352 | | { |
| | 3 | 353 | | throw new InvalidDataException("The material name is not specified."); |
| | | 354 | | } |
| | | 355 | | |
| | 393 | 356 | | currentMaterial.AmbientMap = ParseMaterialMap("map_Ka", value0, ref currentLine, ref values, val |
| | 291 | 357 | | break; |
| | | 358 | | |
| | | 359 | | case "map_kd": |
| | 39 | 360 | | if (currentMaterial == null) |
| | | 361 | | { |
| | 3 | 362 | | throw new InvalidDataException("The material name is not specified."); |
| | | 363 | | } |
| | | 364 | | |
| | 36 | 365 | | currentMaterial.DiffuseMap = ParseMaterialMap("map_Kd", value0, ref currentLine, ref values, val |
| | 33 | 366 | | break; |
| | | 367 | | |
| | | 368 | | case "map_ke": |
| | 39 | 369 | | if (currentMaterial == null) |
| | | 370 | | { |
| | 3 | 371 | | throw new InvalidDataException("The material name is not specified."); |
| | | 372 | | } |
| | | 373 | | |
| | 36 | 374 | | currentMaterial.EmissiveMap = ParseMaterialMap("map_Ke", value0, ref currentLine, ref values, va |
| | 33 | 375 | | break; |
| | | 376 | | |
| | | 377 | | case "map_ks": |
| | 39 | 378 | | if (currentMaterial == null) |
| | | 379 | | { |
| | 3 | 380 | | throw new InvalidDataException("The material name is not specified."); |
| | | 381 | | } |
| | | 382 | | |
| | 36 | 383 | | currentMaterial.SpecularMap = ParseMaterialMap("map_Ks", value0, ref currentLine, ref values, va |
| | 33 | 384 | | break; |
| | | 385 | | |
| | | 386 | | case "map_ns": |
| | 39 | 387 | | if (currentMaterial == null) |
| | | 388 | | { |
| | 3 | 389 | | throw new InvalidDataException("The material name is not specified."); |
| | | 390 | | } |
| | | 391 | | |
| | 36 | 392 | | currentMaterial.SpecularExponentMap = ParseMaterialMap("map_Ns", value0, ref currentLine, ref va |
| | 33 | 393 | | break; |
| | | 394 | | |
| | | 395 | | case "map_d": |
| | | 396 | | case "map_tr": |
| | 39 | 397 | | if (currentMaterial == null) |
| | | 398 | | { |
| | 3 | 399 | | throw new InvalidDataException("The material name is not specified."); |
| | | 400 | | } |
| | | 401 | | |
| | 36 | 402 | | currentMaterial.DissolveMap = ParseMaterialMap("map_d", value0, ref currentLine, ref values, val |
| | 33 | 403 | | break; |
| | | 404 | | |
| | | 405 | | case "decal": |
| | | 406 | | case "map_decal": |
| | 15 | 407 | | if (currentMaterial == null) |
| | | 408 | | { |
| | 3 | 409 | | throw new InvalidDataException("The material name is not specified."); |
| | | 410 | | } |
| | | 411 | | |
| | 12 | 412 | | currentMaterial.DecalMap = ParseMaterialMap("decal", value0, ref currentLine, ref values, values |
| | 9 | 413 | | break; |
| | | 414 | | |
| | | 415 | | case "disp": |
| | | 416 | | case "map_disp": |
| | 15 | 417 | | if (currentMaterial == null) |
| | | 418 | | { |
| | 3 | 419 | | throw new InvalidDataException("The material name is not specified."); |
| | | 420 | | } |
| | | 421 | | |
| | 12 | 422 | | currentMaterial.DispMap = ParseMaterialMap("disp", value0, ref currentLine, ref values, valuesCo |
| | 9 | 423 | | break; |
| | | 424 | | |
| | | 425 | | case "bump": |
| | | 426 | | case "map_bump": |
| | 9 | 427 | | if (currentMaterial == null) |
| | | 428 | | { |
| | 3 | 429 | | throw new InvalidDataException("The material name is not specified."); |
| | | 430 | | } |
| | | 431 | | |
| | 6 | 432 | | currentMaterial.BumpMap = ParseMaterialMap("bump", value0, ref currentLine, ref values, valuesCo |
| | 3 | 433 | | break; |
| | | 434 | | |
| | | 435 | | case "refl": |
| | | 436 | | case "map_refl": |
| | 84 | 437 | | if (currentMaterial == null) |
| | | 438 | | { |
| | 3 | 439 | | throw new InvalidDataException("The material name is not specified."); |
| | | 440 | | } |
| | | 441 | | |
| | 81 | 442 | | if (valuesCount < 4) |
| | | 443 | | { |
| | 12 | 444 | | throw new InvalidDataException("A refl statement must specify a type and a file name."); |
| | | 445 | | } |
| | | 446 | | |
| | 69 | 447 | | ObjMaterialMap materialMap = ParseMaterialMap("refl", value0, ref currentLine, ref values, value |
| | | 448 | | |
| | | 449 | | switch (materialMapType) |
| | | 450 | | { |
| | | 451 | | case MaterialMapType.Sphere: |
| | 9 | 452 | | currentMaterial.ReflectionMap.Sphere = materialMap; |
| | 9 | 453 | | break; |
| | | 454 | | |
| | | 455 | | case MaterialMapType.CubeTop: |
| | 9 | 456 | | currentMaterial.ReflectionMap.CubeTop = materialMap; |
| | 9 | 457 | | break; |
| | | 458 | | |
| | | 459 | | case MaterialMapType.CubeBottom: |
| | 9 | 460 | | currentMaterial.ReflectionMap.CubeBottom = materialMap; |
| | 9 | 461 | | break; |
| | | 462 | | |
| | | 463 | | case MaterialMapType.CubeFront: |
| | 9 | 464 | | currentMaterial.ReflectionMap.CubeFront = materialMap; |
| | 9 | 465 | | break; |
| | | 466 | | |
| | | 467 | | case MaterialMapType.CubeBack: |
| | 9 | 468 | | currentMaterial.ReflectionMap.CubeBack = materialMap; |
| | 9 | 469 | | break; |
| | | 470 | | |
| | | 471 | | case MaterialMapType.CubeLeft: |
| | 9 | 472 | | currentMaterial.ReflectionMap.CubeLeft = materialMap; |
| | 9 | 473 | | break; |
| | | 474 | | |
| | | 475 | | case MaterialMapType.CubeRight: |
| | 9 | 476 | | currentMaterial.ReflectionMap.CubeRight = materialMap; |
| | 9 | 477 | | break; |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | break; |
| | | 481 | | case "pr": |
| | | 482 | | { |
| | 12 | 483 | | if (currentMaterial == null) |
| | | 484 | | { |
| | 3 | 485 | | throw new InvalidDataException("The material name is not specified."); |
| | | 486 | | } |
| | | 487 | | |
| | 9 | 488 | | if (valuesCount < 2) |
| | | 489 | | { |
| | 3 | 490 | | throw new InvalidDataException("A Pr statement must specify an optical density."); |
| | | 491 | | } |
| | | 492 | | |
| | 6 | 493 | | if (valuesCount != 2) |
| | | 494 | | { |
| | 3 | 495 | | throw new InvalidDataException("A Pr statement has too many values."); |
| | | 496 | | } |
| | | 497 | | |
| | 3 | 498 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 499 | | { |
| | 3 | 500 | | currentMaterial.Roughness = value; |
| | | 501 | | } |
| | | 502 | | |
| | 3 | 503 | | break; |
| | | 504 | | } |
| | | 505 | | case "map_pr": |
| | 39 | 506 | | if (currentMaterial == null) |
| | | 507 | | { |
| | 3 | 508 | | throw new InvalidDataException("The material name is not specified."); |
| | | 509 | | } |
| | | 510 | | |
| | 36 | 511 | | currentMaterial.RoughnessMap = ParseMaterialMap("map_Pr", value0, ref currentLine, ref values, v |
| | 33 | 512 | | break; |
| | | 513 | | case "pm": |
| | | 514 | | { |
| | 12 | 515 | | if (currentMaterial == null) |
| | | 516 | | { |
| | 3 | 517 | | throw new InvalidDataException("The material name is not specified."); |
| | | 518 | | } |
| | | 519 | | |
| | 9 | 520 | | if (valuesCount < 2) |
| | | 521 | | { |
| | 3 | 522 | | throw new InvalidDataException("A Pm statement must specify an optical density."); |
| | | 523 | | } |
| | | 524 | | |
| | 6 | 525 | | if (valuesCount != 2) |
| | | 526 | | { |
| | 3 | 527 | | throw new InvalidDataException("A Pm statement has too many values."); |
| | | 528 | | } |
| | | 529 | | |
| | 3 | 530 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 531 | | { |
| | 3 | 532 | | currentMaterial.Metallic = value; |
| | | 533 | | } |
| | 3 | 534 | | break; |
| | | 535 | | } |
| | | 536 | | case "map_pm": |
| | 39 | 537 | | if (currentMaterial == null) |
| | | 538 | | { |
| | 3 | 539 | | throw new InvalidDataException("The material name is not specified."); |
| | | 540 | | } |
| | | 541 | | |
| | 36 | 542 | | currentMaterial.MetallicMap = ParseMaterialMap("map_Pm", value0, ref currentLine, ref values, va |
| | 33 | 543 | | break; |
| | | 544 | | case "ps": |
| | | 545 | | { |
| | 12 | 546 | | if (currentMaterial == null) |
| | | 547 | | { |
| | 3 | 548 | | throw new InvalidDataException("The material name is not specified."); |
| | | 549 | | } |
| | | 550 | | |
| | 9 | 551 | | if (valuesCount < 2) |
| | | 552 | | { |
| | 3 | 553 | | throw new InvalidDataException("A Ps statement must specify an optical density."); |
| | | 554 | | } |
| | | 555 | | |
| | 6 | 556 | | if (valuesCount != 2) |
| | | 557 | | { |
| | 3 | 558 | | throw new InvalidDataException("A Ps statement has too many values."); |
| | | 559 | | } |
| | | 560 | | |
| | 3 | 561 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 562 | | { |
| | 3 | 563 | | currentMaterial.Sheen = value; |
| | | 564 | | } |
| | 3 | 565 | | break; |
| | | 566 | | } |
| | | 567 | | case "map_ps": |
| | 39 | 568 | | if (currentMaterial == null) |
| | | 569 | | { |
| | 3 | 570 | | throw new InvalidDataException("The material name is not specified."); |
| | | 571 | | } |
| | | 572 | | |
| | 36 | 573 | | currentMaterial.SheenMap = ParseMaterialMap("map_Ps", value0, ref currentLine, ref values, value |
| | 33 | 574 | | break; |
| | | 575 | | case "pc": |
| | | 576 | | { |
| | 12 | 577 | | if (currentMaterial == null) |
| | | 578 | | { |
| | 3 | 579 | | throw new InvalidDataException("The material name is not specified."); |
| | | 580 | | } |
| | | 581 | | |
| | 9 | 582 | | if (valuesCount < 2) |
| | | 583 | | { |
| | 3 | 584 | | throw new InvalidDataException("A Pc statement must specify an optical density."); |
| | | 585 | | } |
| | | 586 | | |
| | 6 | 587 | | if (valuesCount != 2) |
| | | 588 | | { |
| | 3 | 589 | | throw new InvalidDataException("A Pc statement has too many values."); |
| | | 590 | | } |
| | | 591 | | |
| | 3 | 592 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 593 | | { |
| | 3 | 594 | | currentMaterial.ClearCoatThickness = value; |
| | | 595 | | } |
| | 3 | 596 | | break; |
| | | 597 | | } |
| | | 598 | | case "pcr": |
| | | 599 | | { |
| | 12 | 600 | | if (currentMaterial == null) |
| | | 601 | | { |
| | 3 | 602 | | throw new InvalidDataException("The material name is not specified."); |
| | | 603 | | } |
| | | 604 | | |
| | 9 | 605 | | if (valuesCount < 2) |
| | | 606 | | { |
| | 3 | 607 | | throw new InvalidDataException("A Pcr statement must specify an optical density."); |
| | | 608 | | } |
| | | 609 | | |
| | 6 | 610 | | if (valuesCount != 2) |
| | | 611 | | { |
| | 3 | 612 | | throw new InvalidDataException("A Pcr statement has too many values."); |
| | | 613 | | } |
| | | 614 | | |
| | 3 | 615 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 616 | | { |
| | 3 | 617 | | currentMaterial.ClearCoatRoughness = value; |
| | | 618 | | } |
| | 3 | 619 | | break; |
| | | 620 | | } |
| | | 621 | | case "aniso": |
| | | 622 | | { |
| | 12 | 623 | | if (currentMaterial == null) |
| | | 624 | | { |
| | 3 | 625 | | throw new InvalidDataException("The material name is not specified."); |
| | | 626 | | } |
| | | 627 | | |
| | 9 | 628 | | if (valuesCount < 2) |
| | | 629 | | { |
| | 3 | 630 | | throw new InvalidDataException("A aniso statement must specify an optical density."); |
| | | 631 | | } |
| | | 632 | | |
| | 6 | 633 | | if (valuesCount != 2) |
| | | 634 | | { |
| | 3 | 635 | | throw new InvalidDataException("A aniso statement has too many values."); |
| | | 636 | | } |
| | | 637 | | |
| | 3 | 638 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 639 | | { |
| | 3 | 640 | | currentMaterial.Anisotropy = value; |
| | | 641 | | } |
| | | 642 | | |
| | 3 | 643 | | break; |
| | | 644 | | } |
| | | 645 | | case "anisor": |
| | | 646 | | { |
| | 12 | 647 | | if (currentMaterial == null) |
| | | 648 | | { |
| | 3 | 649 | | throw new InvalidDataException("The material name is not specified."); |
| | | 650 | | } |
| | | 651 | | |
| | 9 | 652 | | if (valuesCount < 2) |
| | | 653 | | { |
| | 3 | 654 | | throw new InvalidDataException("A anisor statement must specify an optical density."); |
| | | 655 | | } |
| | | 656 | | |
| | 6 | 657 | | if (valuesCount != 2) |
| | | 658 | | { |
| | 3 | 659 | | throw new InvalidDataException("A anisor statement has too many values."); |
| | | 660 | | } |
| | | 661 | | |
| | 3 | 662 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 663 | | { |
| | 3 | 664 | | currentMaterial.AnisotropyRotation = value; |
| | | 665 | | } |
| | 3 | 666 | | break; |
| | | 667 | | } |
| | | 668 | | case "norm": |
| | 39 | 669 | | if (currentMaterial == null) |
| | | 670 | | { |
| | 3 | 671 | | throw new InvalidDataException("The material name is not specified."); |
| | | 672 | | } |
| | | 673 | | |
| | 36 | 674 | | currentMaterial.Norm = ParseMaterialMap("norm", value0, ref currentLine, ref values, valuesCount |
| | | 675 | | break; |
| | | 676 | | } |
| | | 677 | | } |
| | | 678 | | |
| | 867 | 679 | | mtl.HeaderText = string.Join("\n", lineReader.HeaderTextLines.ToArray()); |
| | | 680 | | |
| | 867 | 681 | | return mtl; |
| | | 682 | | } |
| | | 683 | | |
| | | 684 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | | 685 | | private static ObjMaterialColor ParseMaterialColor(ReadOnlySpan<char> statement, ReadOnlySpan<char> value0, ref |
| | | 686 | | { |
| | 210 | 687 | | if (valuesCount < 2) |
| | | 688 | | { |
| | 15 | 689 | | throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a color.")); |
| | | 690 | | } |
| | | 691 | | |
| | 195 | 692 | | var color = new ObjMaterialColor(); |
| | | 693 | | |
| | 195 | 694 | | ReadOnlySpan<char> value1 = GetNextValue(ref currentLine, ref values); |
| | 195 | 695 | | Span<char> valueBuffer = stackalloc char[value1.Length]; |
| | 195 | 696 | | int value1Length = value1.ToLowerInvariant(valueBuffer); |
| | | 697 | | |
| | | 698 | | //if (value1Length == -1) |
| | | 699 | | //{ |
| | | 700 | | // throw new InvalidDataException("the buffer is too small"); |
| | | 701 | | //} |
| | | 702 | | |
| | 195 | 703 | | int index = 1; |
| | | 704 | | |
| | 195 | 705 | | switch (valueBuffer[..value1Length]) |
| | | 706 | | { |
| | | 707 | | case "spectral": |
| | | 708 | | { |
| | 60 | 709 | | index++; |
| | | 710 | | |
| | 60 | 711 | | if (valuesCount - index < 1) |
| | | 712 | | { |
| | 15 | 713 | | throw new InvalidDataException(string.Concat("A ", statement, |
| | 15 | 714 | | " spectral statement must specify a file name.")); |
| | | 715 | | } |
| | | 716 | | |
| | 45 | 717 | | color.SpectralFileName = GetNextValue(ref currentLine, ref values).ToString(); |
| | 45 | 718 | | index++; |
| | | 719 | | |
| | 45 | 720 | | if (valuesCount > index) |
| | | 721 | | { |
| | 30 | 722 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 723 | | { |
| | 30 | 724 | | color.SpectralFactor = value; |
| | | 725 | | } |
| | | 726 | | |
| | 30 | 727 | | index++; |
| | | 728 | | } |
| | | 729 | | |
| | 30 | 730 | | break; |
| | | 731 | | } |
| | | 732 | | case "xyz": |
| | | 733 | | { |
| | 60 | 734 | | index++; |
| | | 735 | | |
| | 60 | 736 | | if (valuesCount - index < 1) |
| | | 737 | | { |
| | 15 | 738 | | throw new InvalidDataException(string.Concat("A ", statement, |
| | 15 | 739 | | " xyz statement must specify a color.")); |
| | | 740 | | } |
| | | 741 | | |
| | 45 | 742 | | color.UseXYZColorSpace = true; |
| | | 743 | | |
| | 45 | 744 | | var xyz = new ObjVector3(); |
| | 45 | 745 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out var value)) |
| | | 746 | | { |
| | 45 | 747 | | xyz.X = value; |
| | | 748 | | } |
| | | 749 | | |
| | 45 | 750 | | index++; |
| | | 751 | | |
| | 45 | 752 | | if (valuesCount > index) |
| | | 753 | | { |
| | 30 | 754 | | if (valuesCount - index < 2) |
| | | 755 | | { |
| | 15 | 756 | | throw new InvalidDataException(string.Concat("A ", statement, |
| | 15 | 757 | | " xyz statement must specify a XYZ color.")); |
| | | 758 | | } |
| | | 759 | | |
| | 15 | 760 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out value)) |
| | | 761 | | { |
| | 15 | 762 | | xyz.Y = value; |
| | | 763 | | } |
| | | 764 | | |
| | 15 | 765 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out value)) |
| | | 766 | | { |
| | 15 | 767 | | xyz.Z = value; |
| | | 768 | | } |
| | | 769 | | |
| | 15 | 770 | | index += 2; |
| | | 771 | | } |
| | | 772 | | else |
| | | 773 | | { |
| | 15 | 774 | | xyz.Y = xyz.X; |
| | 15 | 775 | | xyz.Z = xyz.X; |
| | | 776 | | } |
| | | 777 | | |
| | 30 | 778 | | color.Color = xyz; |
| | 30 | 779 | | break; |
| | | 780 | | } |
| | | 781 | | default: |
| | | 782 | | { |
| | 75 | 783 | | var rgb = new ObjVector3(); |
| | 75 | 784 | | if (TryFloatParse(value1, out var value)) |
| | | 785 | | { |
| | 75 | 786 | | rgb.X = value; |
| | | 787 | | } |
| | | 788 | | |
| | 75 | 789 | | index++; |
| | | 790 | | |
| | 75 | 791 | | if (valuesCount > index) |
| | | 792 | | { |
| | 60 | 793 | | if (valuesCount - index < 2) |
| | | 794 | | { |
| | 15 | 795 | | throw new InvalidDataException(string.Concat("A ", statement, |
| | 15 | 796 | | " statement must specify a RGB color.")); |
| | | 797 | | } |
| | | 798 | | |
| | 45 | 799 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out value)) |
| | | 800 | | { |
| | 45 | 801 | | rgb.Y = value; |
| | | 802 | | } |
| | | 803 | | |
| | 45 | 804 | | if (TryFloatParse(GetNextValue(ref currentLine, ref values), out value)) |
| | | 805 | | { |
| | 45 | 806 | | rgb.Z = value; |
| | | 807 | | } |
| | | 808 | | |
| | 45 | 809 | | index += 2; |
| | | 810 | | } |
| | | 811 | | else |
| | | 812 | | { |
| | 15 | 813 | | rgb.Y = rgb.X; |
| | 15 | 814 | | rgb.Z = rgb.X; |
| | | 815 | | } |
| | | 816 | | |
| | 60 | 817 | | color.Color = rgb; |
| | | 818 | | break; |
| | | 819 | | } |
| | | 820 | | } |
| | | 821 | | |
| | 135 | 822 | | if (index != valuesCount) |
| | | 823 | | { |
| | 15 | 824 | | throw new InvalidDataException(string.Concat("A ", statement, " statement has too many values.")); |
| | | 825 | | } |
| | | 826 | | |
| | 120 | 827 | | return color; |
| | | 828 | | } |
| | | 829 | | |
| | | 830 | | private enum MaterialMapType |
| | | 831 | | { |
| | | 832 | | None, |
| | | 833 | | Sphere, |
| | | 834 | | CubeTop, |
| | | 835 | | CubeBottom, |
| | | 836 | | CubeFront, |
| | | 837 | | CubeBack, |
| | | 838 | | CubeLeft, |
| | | 839 | | CubeRight |
| | | 840 | | } |
| | | 841 | | |
| | | 842 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | | 843 | | private static ObjMaterialMap ParseMaterialMap(ReadOnlySpan<char> statement, ReadOnlySpan<char> value0, ref Read |
| | | 844 | | { |
| | 747 | 845 | | return ParseMaterialMap(statement, value0, ref currentLine, ref values, valuesCount, settings, out _); |
| | | 846 | | } |
| | | 847 | | |
| | | 848 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | | 849 | | private static ObjMaterialMap ParseMaterialMap(ReadOnlySpan<char> statement, ReadOnlySpan<char> value0, ref Read |
| | | 850 | | { |
| | 816 | 851 | | materialMapType = MaterialMapType.None; |
| | | 852 | | |
| | 816 | 853 | | var map = new ObjMaterialMap(); |
| | 816 | 854 | | var charsRead = 0; |
| | | 855 | | |
| | 816 | 856 | | int valueBufferSize = 16; |
| | 816 | 857 | | Span<char> valueBuffer = stackalloc char[valueBufferSize]; |
| | | 858 | | |
| | 2535 | 859 | | for (int index = 0; index < valuesCount;) |
| | | 860 | | { |
| | 1164 | 861 | | index++; |
| | | 862 | | |
| | 1164 | 863 | | if (valuesCount - index < 1) |
| | | 864 | | { |
| | 66 | 865 | | throw new InvalidDataException(string.Concat("A ", statement, " statement must specify a filename.") |
| | | 866 | | } |
| | | 867 | | |
| | 1098 | 868 | | ReadOnlySpan<char> value1 = GetNextValue(ref currentLine, ref values); |
| | 1098 | 869 | | int value1Length = value1.ToLowerInvariant(valueBuffer); |
| | | 870 | | |
| | | 871 | | // Value1Length is -1 when the buffer is too small. |
| | | 872 | | // This should only happen if the value is a file name and not an option |
| | 1098 | 873 | | if (value1Length == -1) |
| | | 874 | | { |
| | 120 | 875 | | if (index == 1) |
| | | 876 | | { |
| | 120 | 877 | | if (statement.Equals("refl", StringComparison.OrdinalIgnoreCase)) |
| | | 878 | | { |
| | 0 | 879 | | throw new InvalidDataException("A refl statement must specify a type."); |
| | | 880 | | } |
| | | 881 | | } |
| | 120 | 882 | | if (settings.KeepWhitespacesOfMapFileReferences) |
| | | 883 | | { |
| | 30 | 884 | | map.FileName = new string(currentLine[(statement.Length + charsRead + 1)..]); |
| | | 885 | | } |
| | | 886 | | else |
| | | 887 | | { |
| | 90 | 888 | | var sb = new StringBuilder(); |
| | | 889 | | |
| | 90 | 890 | | sb.Append(value1); |
| | | 891 | | |
| | 300 | 892 | | for (int i = index + 1; i < valuesCount; i++) |
| | | 893 | | { |
| | 60 | 894 | | sb.Append(' '); |
| | 60 | 895 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 896 | | } |
| | | 897 | | |
| | 90 | 898 | | string filename = sb.ToString(); |
| | | 899 | | |
| | 90 | 900 | | map.FileName = filename; |
| | | 901 | | } |
| | | 902 | | |
| | 120 | 903 | | return map; |
| | | 904 | | } |
| | | 905 | | |
| | 978 | 906 | | charsRead += value1Length; |
| | | 907 | | |
| | 978 | 908 | | if (statement.Equals("refl", StringComparison.OrdinalIgnoreCase)) |
| | | 909 | | { |
| | 135 | 910 | | if (index == 1) |
| | | 911 | | { |
| | 69 | 912 | | if (!MemoryExtensions.Equals(valueBuffer[..value1Length], "-type", StringComparison.OrdinalIgnor |
| | | 913 | | { |
| | 3 | 914 | | throw new InvalidDataException("A refl statement must specify a type."); |
| | | 915 | | } |
| | | 916 | | } |
| | | 917 | | } |
| | | 918 | | |
| | 975 | 919 | | switch (valueBuffer[..value1Length]) |
| | | 920 | | { |
| | | 921 | | case "-type": |
| | 72 | 922 | | if (valuesCount - index < 2) |
| | | 923 | | { |
| | 3 | 924 | | throw new InvalidDataException(string.Concat("A ", statement, " -type option must specify a |
| | | 925 | | } |
| | | 926 | | |
| | 69 | 927 | | if (statement.Equals("refl", StringComparison.OrdinalIgnoreCase)) |
| | | 928 | | { |
| | 66 | 929 | | ReadOnlySpan<char> value2 = GetNextValue(ref currentLine, ref values); |
| | 66 | 930 | | int value2Length = value2.ToLowerInvariant(valueBuffer); |
| | 66 | 931 | | charsRead += value2Length; |
| | | 932 | | |
| | | 933 | | //if (value2Length == -1) |
| | | 934 | | //{ |
| | | 935 | | // throw new InvalidDataException("the buffer is too small"); |
| | | 936 | | //} |
| | | 937 | | |
| | 66 | 938 | | switch (valueBuffer[..value2Length]) |
| | | 939 | | { |
| | | 940 | | case "sphere": |
| | 9 | 941 | | materialMapType = MaterialMapType.Sphere; |
| | 9 | 942 | | break; |
| | | 943 | | |
| | | 944 | | case "cube_top": |
| | 9 | 945 | | materialMapType = MaterialMapType.CubeTop; |
| | 9 | 946 | | break; |
| | | 947 | | |
| | | 948 | | case "cube_bottom": |
| | 9 | 949 | | materialMapType = MaterialMapType.CubeBottom; |
| | 9 | 950 | | break; |
| | | 951 | | |
| | | 952 | | case "cube_front": |
| | 9 | 953 | | materialMapType = MaterialMapType.CubeFront; |
| | 9 | 954 | | break; |
| | | 955 | | |
| | | 956 | | case "cube_back": |
| | 9 | 957 | | materialMapType = MaterialMapType.CubeBack; |
| | 9 | 958 | | break; |
| | | 959 | | |
| | | 960 | | case "cube_left": |
| | 9 | 961 | | materialMapType = MaterialMapType.CubeLeft; |
| | 9 | 962 | | break; |
| | | 963 | | |
| | | 964 | | case "cube_right": |
| | 9 | 965 | | materialMapType = MaterialMapType.CubeRight; |
| | 9 | 966 | | break; |
| | | 967 | | } |
| | | 968 | | } |
| | | 969 | | else |
| | | 970 | | { |
| | 3 | 971 | | GetNextValue(ref currentLine, ref values); |
| | | 972 | | } |
| | | 973 | | |
| | 69 | 974 | | index++; |
| | 69 | 975 | | break; |
| | | 976 | | |
| | | 977 | | case "-blenu": |
| | | 978 | | { |
| | 21 | 979 | | if (valuesCount - index < 2) |
| | | 980 | | { |
| | 3 | 981 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenu option must speci |
| | | 982 | | } |
| | | 983 | | |
| | 18 | 984 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | 18 | 985 | | charsRead += value2.Length; |
| | | 986 | | |
| | 18 | 987 | | if (value2.Equals("on", StringComparison.OrdinalIgnoreCase)) |
| | | 988 | | { |
| | 6 | 989 | | map.IsHorizontalBlendingEnabled = true; |
| | | 990 | | } |
| | 12 | 991 | | else if (value2.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 992 | | { |
| | 6 | 993 | | map.IsHorizontalBlendingEnabled = false; |
| | | 994 | | } |
| | | 995 | | else |
| | | 996 | | { |
| | 6 | 997 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenu option must speci |
| | | 998 | | } |
| | | 999 | | |
| | 12 | 1000 | | index++; |
| | 12 | 1001 | | break; |
| | | 1002 | | } |
| | | 1003 | | |
| | | 1004 | | case "-blenv": |
| | | 1005 | | { |
| | 21 | 1006 | | if (valuesCount - index < 2) |
| | | 1007 | | { |
| | 3 | 1008 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenv option must speci |
| | | 1009 | | } |
| | | 1010 | | |
| | 18 | 1011 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | 18 | 1012 | | charsRead += value2.Length; |
| | | 1013 | | |
| | 18 | 1014 | | if (value2.Equals("on", StringComparison.OrdinalIgnoreCase)) |
| | | 1015 | | { |
| | 6 | 1016 | | map.IsVerticalBlendingEnabled = true; |
| | | 1017 | | } |
| | 12 | 1018 | | else if (value2.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 1019 | | { |
| | 6 | 1020 | | map.IsVerticalBlendingEnabled = false; |
| | | 1021 | | } |
| | | 1022 | | else |
| | | 1023 | | { |
| | 6 | 1024 | | throw new InvalidDataException(string.Concat("A ", statement, " -blenv option must speci |
| | | 1025 | | } |
| | | 1026 | | |
| | 12 | 1027 | | index++; |
| | 12 | 1028 | | break; |
| | | 1029 | | } |
| | | 1030 | | |
| | | 1031 | | case "-bm": |
| | | 1032 | | { |
| | 18 | 1033 | | if (valuesCount - index < 2) |
| | | 1034 | | { |
| | 3 | 1035 | | throw new InvalidDataException(string.Concat("A ", statement, " -bm option must specify a va |
| | | 1036 | | } |
| | | 1037 | | |
| | 15 | 1038 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | 15 | 1039 | | charsRead += value2.Length; |
| | | 1040 | | |
| | 15 | 1041 | | if (TryFloatParse(value2, out var value)) |
| | | 1042 | | { |
| | 15 | 1043 | | map.BumpMultiplier = value; |
| | | 1044 | | } |
| | | 1045 | | |
| | 15 | 1046 | | index++; |
| | 15 | 1047 | | break; |
| | | 1048 | | } |
| | | 1049 | | case "-boost": |
| | | 1050 | | { |
| | 18 | 1051 | | if (valuesCount - index < 2) |
| | | 1052 | | { |
| | 3 | 1053 | | throw new InvalidDataException(string.Concat("A ", statement, |
| | 3 | 1054 | | " -boost option must specify a value.")); |
| | | 1055 | | } |
| | | 1056 | | |
| | 15 | 1057 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | 15 | 1058 | | charsRead += value2.Length; |
| | | 1059 | | |
| | 15 | 1060 | | if (TryFloatParse(value2, out var value)) |
| | | 1061 | | { |
| | 15 | 1062 | | map.Boost = value; |
| | | 1063 | | } |
| | | 1064 | | |
| | 15 | 1065 | | index++; |
| | 15 | 1066 | | break; |
| | | 1067 | | } |
| | | 1068 | | case "-cc": |
| | | 1069 | | { |
| | 21 | 1070 | | if (valuesCount - index < 2) |
| | | 1071 | | { |
| | 3 | 1072 | | throw new InvalidDataException(string.Concat("A ", statement, " -cc option must specify |
| | | 1073 | | } |
| | | 1074 | | |
| | 18 | 1075 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | 18 | 1076 | | charsRead += value2.Length; |
| | | 1077 | | |
| | 18 | 1078 | | if (value2.Equals("on", StringComparison.OrdinalIgnoreCase)) |
| | | 1079 | | { |
| | 6 | 1080 | | map.IsColorCorrectionEnabled = true; |
| | | 1081 | | } |
| | 12 | 1082 | | else if (value2.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 1083 | | { |
| | 6 | 1084 | | map.IsColorCorrectionEnabled = false; |
| | | 1085 | | } |
| | | 1086 | | else |
| | | 1087 | | { |
| | 6 | 1088 | | throw new InvalidDataException(string.Concat("A ", statement, " -cc option must specify |
| | | 1089 | | } |
| | | 1090 | | |
| | 12 | 1091 | | index++; |
| | 12 | 1092 | | break; |
| | | 1093 | | } |
| | | 1094 | | |
| | | 1095 | | case "-clamp": |
| | | 1096 | | { |
| | 21 | 1097 | | if (valuesCount - index < 2) |
| | | 1098 | | { |
| | 3 | 1099 | | throw new InvalidDataException(string.Concat("A ", statement, " -clamp option must speci |
| | | 1100 | | } |
| | | 1101 | | |
| | 18 | 1102 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | 18 | 1103 | | charsRead += value2.Length; |
| | | 1104 | | |
| | 18 | 1105 | | if (value2.Equals("on", StringComparison.OrdinalIgnoreCase)) |
| | | 1106 | | { |
| | 6 | 1107 | | map.IsClampingEnabled = true; |
| | | 1108 | | } |
| | 12 | 1109 | | else if (value2.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 1110 | | { |
| | 6 | 1111 | | map.IsClampingEnabled = false; |
| | | 1112 | | } |
| | | 1113 | | else |
| | | 1114 | | { |
| | 6 | 1115 | | throw new InvalidDataException(string.Concat("A ", statement, " -clamp option must speci |
| | | 1116 | | } |
| | | 1117 | | |
| | 12 | 1118 | | index++; |
| | 12 | 1119 | | break; |
| | | 1120 | | } |
| | | 1121 | | |
| | | 1122 | | case "-imfchan": |
| | | 1123 | | { |
| | 63 | 1124 | | if (valuesCount - index < 2) |
| | | 1125 | | { |
| | 3 | 1126 | | throw new InvalidDataException(string.Concat("A ", statement, " -imfchan option must spe |
| | | 1127 | | } |
| | | 1128 | | |
| | 60 | 1129 | | ReadOnlySpan<char> value2 = GetNextValue(ref currentLine, ref values); |
| | 60 | 1130 | | int value2Length = value2.ToLowerInvariant(valueBuffer); |
| | 60 | 1131 | | charsRead += value2Length; |
| | | 1132 | | |
| | | 1133 | | //if (value2Length == -1) |
| | | 1134 | | //{ |
| | | 1135 | | // throw new InvalidDataException("the buffer is too small"); |
| | | 1136 | | //} |
| | | 1137 | | |
| | 60 | 1138 | | switch (valueBuffer[..value2Length]) |
| | | 1139 | | { |
| | | 1140 | | case "r": |
| | 9 | 1141 | | map.ScalarChannel = ObjMapChannel.Red; |
| | 9 | 1142 | | break; |
| | | 1143 | | |
| | | 1144 | | case "g": |
| | 9 | 1145 | | map.ScalarChannel = ObjMapChannel.Green; |
| | 9 | 1146 | | break; |
| | | 1147 | | |
| | | 1148 | | case "b": |
| | 9 | 1149 | | map.ScalarChannel = ObjMapChannel.Blue; |
| | 9 | 1150 | | break; |
| | | 1151 | | |
| | | 1152 | | case "m": |
| | 9 | 1153 | | map.ScalarChannel = ObjMapChannel.Matte; |
| | 9 | 1154 | | break; |
| | | 1155 | | |
| | | 1156 | | case "l": |
| | 9 | 1157 | | map.ScalarChannel = ObjMapChannel.Luminance; |
| | 9 | 1158 | | break; |
| | | 1159 | | |
| | | 1160 | | case "z": |
| | 9 | 1161 | | map.ScalarChannel = ObjMapChannel.Depth; |
| | 9 | 1162 | | break; |
| | | 1163 | | |
| | | 1164 | | default: |
| | 6 | 1165 | | throw new InvalidDataException(string.Concat("A ", statement, " -imfchan option must |
| | | 1166 | | } |
| | | 1167 | | |
| | 54 | 1168 | | index++; |
| | 54 | 1169 | | break; |
| | | 1170 | | } |
| | | 1171 | | |
| | | 1172 | | case "-mm": |
| | | 1173 | | { |
| | 21 | 1174 | | if (valuesCount - index < 3) |
| | | 1175 | | { |
| | 6 | 1176 | | throw new InvalidDataException(string.Concat("A ", statement, " -mm option must specify a ba |
| | | 1177 | | } |
| | | 1178 | | |
| | 15 | 1179 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | 15 | 1180 | | charsRead += value2.Length; |
| | | 1181 | | |
| | 15 | 1182 | | if (TryFloatParse(value2, out var value)) |
| | | 1183 | | { |
| | 15 | 1184 | | map.ModifierBase = value; |
| | | 1185 | | } |
| | 15 | 1186 | | value2 = GetNextValue(ref currentLine, ref values); |
| | 15 | 1187 | | charsRead += value2.Length; |
| | | 1188 | | |
| | 15 | 1189 | | if (TryFloatParse(value2, out value)) |
| | | 1190 | | { |
| | 15 | 1191 | | map.ModifierGain = value; |
| | | 1192 | | } |
| | | 1193 | | |
| | 15 | 1194 | | index += 2; |
| | 15 | 1195 | | break; |
| | | 1196 | | } |
| | | 1197 | | |
| | | 1198 | | |
| | | 1199 | | case "-o": |
| | | 1200 | | { |
| | 42 | 1201 | | if (valuesCount - index < 2) |
| | | 1202 | | { |
| | 3 | 1203 | | throw new InvalidDataException(string.Concat("A ", statement, |
| | 3 | 1204 | | " -o option must specify at least 2 values.")); |
| | | 1205 | | } |
| | | 1206 | | |
| | 39 | 1207 | | var offset = new ObjVector3(); |
| | 39 | 1208 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | | 1209 | | |
| | 39 | 1210 | | if (TryFloatParse(value2, out var value)) |
| | | 1211 | | { |
| | 39 | 1212 | | offset.X = value; |
| | 39 | 1213 | | index++; |
| | 39 | 1214 | | charsRead += value2.Length; |
| | | 1215 | | } |
| | | 1216 | | else |
| | | 1217 | | { |
| | 0 | 1218 | | map.Offset = offset; |
| | 0 | 1219 | | break; |
| | | 1220 | | } |
| | | 1221 | | |
| | 39 | 1222 | | if (valuesCount - index > 2) |
| | | 1223 | | { |
| | 30 | 1224 | | value2 = GetNextValue(ref currentLine, ref values); |
| | 30 | 1225 | | if (TryFloatParse(value2, out value)) |
| | | 1226 | | { |
| | 24 | 1227 | | offset.Y = value; |
| | 24 | 1228 | | index++; |
| | 24 | 1229 | | charsRead += value2.Length; |
| | | 1230 | | } |
| | | 1231 | | else |
| | | 1232 | | { |
| | 6 | 1233 | | map.Offset = offset; |
| | 6 | 1234 | | break; |
| | | 1235 | | } |
| | | 1236 | | |
| | 24 | 1237 | | if (valuesCount - index > 2) |
| | | 1238 | | { |
| | 18 | 1239 | | value2 = GetNextValue(ref currentLine, ref values); |
| | 18 | 1240 | | if (TryFloatParse(value2, out value)) |
| | | 1241 | | { |
| | 12 | 1242 | | offset.Z = value; |
| | 12 | 1243 | | index++; |
| | 12 | 1244 | | charsRead += value2.Length; |
| | | 1245 | | } |
| | | 1246 | | else |
| | | 1247 | | { |
| | 6 | 1248 | | map.Offset = offset; |
| | 6 | 1249 | | break; |
| | | 1250 | | } |
| | | 1251 | | } |
| | | 1252 | | } |
| | | 1253 | | |
| | 27 | 1254 | | map.Offset = offset; |
| | 27 | 1255 | | break; |
| | | 1256 | | } |
| | | 1257 | | case "-s": |
| | | 1258 | | { |
| | 42 | 1259 | | if (valuesCount - index < 2) |
| | | 1260 | | { |
| | 3 | 1261 | | throw new InvalidDataException(string.Concat("A ", statement, |
| | 3 | 1262 | | " -s option must specify at least 2 values.")); |
| | | 1263 | | } |
| | | 1264 | | |
| | 39 | 1265 | | var scale = new ObjVector3(1.0f, 1.0f, 1.0f); |
| | | 1266 | | |
| | 39 | 1267 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | | 1268 | | |
| | 39 | 1269 | | if (TryFloatParse(value2, out var value)) |
| | | 1270 | | { |
| | 39 | 1271 | | scale.X = value; |
| | 39 | 1272 | | index++; |
| | 39 | 1273 | | charsRead += value2.Length; |
| | | 1274 | | } |
| | | 1275 | | else |
| | | 1276 | | { |
| | 0 | 1277 | | map.Scale = scale; |
| | 0 | 1278 | | break; |
| | | 1279 | | } |
| | | 1280 | | |
| | 39 | 1281 | | if (valuesCount - index > 2) |
| | | 1282 | | { |
| | 30 | 1283 | | value2 = GetNextValue(ref currentLine, ref values); |
| | 30 | 1284 | | if (TryFloatParse(value2, out value)) |
| | | 1285 | | { |
| | 24 | 1286 | | scale.Y = value; |
| | 24 | 1287 | | index++; |
| | 24 | 1288 | | charsRead += value2.Length; |
| | | 1289 | | } |
| | | 1290 | | else |
| | | 1291 | | { |
| | 6 | 1292 | | map.Scale = scale; |
| | 6 | 1293 | | break; |
| | | 1294 | | } |
| | | 1295 | | |
| | 24 | 1296 | | if (valuesCount - index > 2) |
| | | 1297 | | { |
| | 18 | 1298 | | value2 = GetNextValue(ref currentLine, ref values); |
| | 18 | 1299 | | if (TryFloatParse(value2, out value)) |
| | | 1300 | | { |
| | 12 | 1301 | | scale.Z = value; |
| | 12 | 1302 | | index++; |
| | 12 | 1303 | | charsRead += value2.Length; |
| | | 1304 | | } |
| | | 1305 | | else |
| | | 1306 | | { |
| | 6 | 1307 | | map.Scale = scale; |
| | 6 | 1308 | | break; |
| | | 1309 | | } |
| | | 1310 | | } |
| | | 1311 | | } |
| | | 1312 | | |
| | 27 | 1313 | | map.Scale = scale; |
| | 27 | 1314 | | break; |
| | | 1315 | | } |
| | | 1316 | | case "-t": |
| | | 1317 | | { |
| | 42 | 1318 | | if (valuesCount - index < 2) |
| | | 1319 | | { |
| | 3 | 1320 | | throw new InvalidDataException(string.Concat("A ", statement, |
| | 3 | 1321 | | " -t option must specify at least 2 values.")); |
| | | 1322 | | } |
| | | 1323 | | |
| | 39 | 1324 | | var turbulence = new ObjVector3(); |
| | | 1325 | | |
| | 39 | 1326 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | | 1327 | | |
| | 39 | 1328 | | if (TryFloatParse(value2, out var value)) |
| | | 1329 | | { |
| | 39 | 1330 | | turbulence.X = value; |
| | 39 | 1331 | | index++; |
| | 39 | 1332 | | charsRead += value2.Length; |
| | | 1333 | | } |
| | | 1334 | | else |
| | | 1335 | | { |
| | 0 | 1336 | | map.Turbulence = turbulence; |
| | 0 | 1337 | | break; |
| | | 1338 | | } |
| | | 1339 | | |
| | 39 | 1340 | | if (valuesCount - index > 2) |
| | | 1341 | | { |
| | 30 | 1342 | | value2 = GetNextValue(ref currentLine, ref values); |
| | 30 | 1343 | | if (TryFloatParse(value2, out value)) |
| | | 1344 | | { |
| | 24 | 1345 | | turbulence.Y = value; |
| | 24 | 1346 | | index++; |
| | 24 | 1347 | | charsRead += value2.Length; |
| | | 1348 | | } |
| | | 1349 | | else |
| | | 1350 | | { |
| | 6 | 1351 | | map.Turbulence = turbulence; |
| | 6 | 1352 | | break; |
| | | 1353 | | } |
| | | 1354 | | |
| | 24 | 1355 | | if (valuesCount - index > 2) |
| | | 1356 | | { |
| | 18 | 1357 | | value2 = GetNextValue(ref currentLine, ref values); |
| | 18 | 1358 | | if (TryFloatParse(value2, out value)) |
| | | 1359 | | { |
| | 12 | 1360 | | turbulence.Z = value; |
| | 12 | 1361 | | index++; |
| | 12 | 1362 | | charsRead += value2.Length; |
| | | 1363 | | } |
| | | 1364 | | else |
| | | 1365 | | { |
| | 6 | 1366 | | map.Turbulence = turbulence; |
| | 6 | 1367 | | break; |
| | | 1368 | | } |
| | | 1369 | | } |
| | | 1370 | | } |
| | | 1371 | | |
| | 27 | 1372 | | map.Turbulence = turbulence; |
| | 27 | 1373 | | break; |
| | | 1374 | | } |
| | | 1375 | | case "-texres": |
| | | 1376 | | { |
| | 18 | 1377 | | if (valuesCount - index < 2) |
| | | 1378 | | { |
| | 3 | 1379 | | throw new InvalidDataException(string.Concat("A ", statement, |
| | 3 | 1380 | | " -texres option must specify a value.")); |
| | | 1381 | | } |
| | 15 | 1382 | | var value2 = GetNextValue(ref currentLine, ref values); |
| | 15 | 1383 | | charsRead += value2.Length; |
| | 15 | 1384 | | if (TryIntParse(value2, out var value)) |
| | | 1385 | | { |
| | 15 | 1386 | | map.TextureResolution = value; |
| | | 1387 | | } |
| | | 1388 | | |
| | 15 | 1389 | | index++; |
| | 15 | 1390 | | break; |
| | | 1391 | | } |
| | | 1392 | | default: |
| | | 1393 | | { |
| | 555 | 1394 | | if (settings.KeepWhitespacesOfMapFileReferences) |
| | | 1395 | | { |
| | 180 | 1396 | | map.FileName = new string(currentLine[(statement.Length + index + charsRead - value1.Len |
| | | 1397 | | } |
| | | 1398 | | else |
| | | 1399 | | { |
| | 375 | 1400 | | var sb = new StringBuilder(); |
| | | 1401 | | |
| | 375 | 1402 | | sb.Append(value1); |
| | | 1403 | | |
| | 1182 | 1404 | | for (int i = index + 1; i < valuesCount; i++) |
| | | 1405 | | { |
| | 216 | 1406 | | sb.Append(' '); |
| | 216 | 1407 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 1408 | | } |
| | | 1409 | | |
| | 375 | 1410 | | string filename = sb.ToString(); |
| | | 1411 | | |
| | 375 | 1412 | | map.FileName = filename; |
| | | 1413 | | } |
| | 555 | 1414 | | index = valuesCount; |
| | | 1415 | | |
| | | 1416 | | break; |
| | | 1417 | | } |
| | | 1418 | | } |
| | | 1419 | | } |
| | | 1420 | | |
| | 555 | 1421 | | return map; |
| | | 1422 | | } |
| | | 1423 | | } |
| | | 1424 | | } |
| | | 1425 | | |
| | | 1426 | | #endif |
| | | 1427 | | |