Suche Bibliothek für 3D Formate Wavefront <> Collada

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von Gonger96.

    Suche Bibliothek für 3D Formate Wavefront <> Collada

    Hallo,
    ​ich habe vor Kurzem auf meiner alten HDD diverse 3D Modelle aus Spyro 3 gefunden. Diese sind alle im Wavefront Format und beinhalten u.a. Skyboxen. Eine Skybox ist da eine Halbkugel aus doof vielen Vertices die den Himmel bilden und ein großer Würfel der das Ganze umschließt. Nun hat die Skybox aber keine Textur sondern nur Vertex Painting. Wavefront unterstützt allerdings kein Vertex Paint und so hab ich nur graue Himmel ^^
    Jetzt habe ich aber herausgefunden, dass in der Datei alle Vertex Colours als vt also Texturkoordinaten drin stehen. Hab eine Datei mal angehängt. Um die Vertex Colours in Blender zu importieren dachte ich, es wäre am einfachsten die Daten aus der Wavefront einzulesen und zu Collada oder Ähnlichem zu konvertieren inkl. der Vertex Colours. Kennt ihr irgendeine Bibliothek die sich für dieses Vorhaben eignet? Wavefront zu parsen und zu im-/exportieren ist kein Ding, aber ich wollte mich ungern in Collada einlesen :saint:

    Grüße
    Dateien
    • Sky.zip

      (33,53 kB, 102 mal heruntergeladen, zuletzt: )
    Problem gelöst. Das Ply-Format ist auch relativ einfach aufgebaut, also habe ich nen kleinen Importer für Wavefront und einen Exporter für Ply gebastelt. Der Import funktioniert allerdings nur für das Format, das ich auf'er Platte habe. Ggf. bau ich das irgendwann noch mal aus.
    Spoiler anzeigen

    C#-Quellcode

    1. public struct Vertex
    2. {
    3. public float X { get; set; }
    4. public float Y { get; set; }
    5. public float Z { get; set; }
    6. public Colour VertexColour { get; set; }
    7. }
    8. public struct Face
    9. {
    10. public List<uint> Indices { get; set; }
    11. public List<uint> ColourIndices { get; set; }
    12. }
    13. public struct Colour
    14. {
    15. public float R { get; set; }
    16. public float G { get; set; }
    17. public float B { get; set; }
    18. }
    19. public class Mesh
    20. {
    21. List<Vertex> Vertices { get; set; } = new List<Vertex>();
    22. List<Colour> VertexColours { get; set; } = new List<Colour>();
    23. List<Face> Faces { get; set; } = new List<Face>();
    24. public static Mesh FromObj(string filename)
    25. {
    26. // Gilt nur für Wavefront Dateien die in dem Format "f vertex/colour vertex/colour vertex/colour..." speichern
    27. Mesh mesh = new Mesh();
    28. string[] data = File.ReadAllLines(filename);
    29. foreach (string line in data)
    30. {
    31. if (line.StartsWith("vn"))
    32. {
    33. // Ignore
    34. }
    35. else if (line.StartsWith("vt")) // Vertex Colour nix Texturkoordinate!
    36. {
    37. string[] values = line.Remove(0, 2).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    38. Colour v = new Colour();
    39. v.R = float.Parse(values[0], System.Globalization.CultureInfo.InvariantCulture);
    40. v.G = float.Parse(values[1], System.Globalization.CultureInfo.InvariantCulture);
    41. v.B = float.Parse(values[2], System.Globalization.CultureInfo.InvariantCulture);
    42. mesh.VertexColours.Add(v);
    43. }
    44. else if (line.StartsWith("v")) // Vertex
    45. {
    46. string[] values = line.Remove(0, 1).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    47. Vertex v = new Vertex();
    48. v.X = float.Parse(values[0], System.Globalization.CultureInfo.InvariantCulture);
    49. v.Y = float.Parse(values[1], System.Globalization.CultureInfo.InvariantCulture);
    50. v.Z = float.Parse(values[2], System.Globalization.CultureInfo.InvariantCulture);
    51. mesh.Vertices.Add(v);
    52. }
    53. else if (line.StartsWith("f")) // Face
    54. {
    55. string[] values = line.Remove(0, 1).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    56. Face f = new Face();
    57. f.Indices = new List<uint>();
    58. f.ColourIndices = new List<uint>();
    59. foreach (var val in values)
    60. {
    61. var splitted = val.Split('/');
    62. f.Indices.Add(uint.Parse(splitted[0]) - 1); // Vorher war 1 basiert
    63. f.ColourIndices.Add(uint.Parse(splitted[1]) - 1);
    64. }
    65. mesh.Faces.Add(f);
    66. }
    67. }
    68. for (int i = 0; i < mesh.Vertices.Count; ++i)
    69. {
    70. var v = mesh.Vertices[i];
    71. Colour c = new Colour();
    72. foreach (Face f in mesh.Faces)
    73. {
    74. int idx = f.Indices.IndexOf((uint)i); // Wenn Vertex in diesem face vorkommt dann benutze diese Colour
    75. if (idx == -1)
    76. continue;
    77. c = mesh.VertexColours[(int)f.ColourIndices[idx]];
    78. }
    79. v.VertexColour = c;
    80. mesh.Vertices[i] = v;
    81. }
    82. return mesh;
    83. }
    84. public void SaveStanfortPly(string filename)
    85. {
    86. using (FileStream fs = new FileStream(@"C:\Users\user\Desktop\out.ply", FileMode.Create))
    87. {
    88. using (StreamWriter sw = new StreamWriter(fs))
    89. {
    90. // Header
    91. sw.WriteLine("ply");
    92. sw.WriteLine("format ascii 1.0");
    93. sw.WriteLine("element vertex " + Vertices.Count);
    94. sw.WriteLine("property float x");
    95. sw.WriteLine("property float y");
    96. sw.WriteLine("property float z");
    97. sw.WriteLine("property float red");
    98. sw.WriteLine("property float green");
    99. sw.WriteLine("property float blue");
    100. sw.WriteLine("element face " + Faces.Count);
    101. sw.WriteLine("property list uchar uint vertex_indices");
    102. sw.WriteLine("end_header");
    103. // Schreibe Vertices "x y z r g b"
    104. foreach (Vertex v in Vertices)
    105. {
    106. sw.WriteLine(v.X.ToString(CultureInfo.InvariantCulture) + " " + v.Y.ToString(CultureInfo.InvariantCulture) + " " + v.Z.ToString(CultureInfo.InvariantCulture) +
    107. " " + v.VertexColour.R.ToString(CultureInfo.InvariantCulture) + " " + v.VertexColour.G.ToString(CultureInfo.InvariantCulture) + " " + v.VertexColour.B.ToString(CultureInfo.InvariantCulture));
    108. }
    109. foreach (Face f in Faces)
    110. {
    111. sw.Write(f.Indices.Count);
    112. foreach (uint idx in f.Indices)
    113. {
    114. sw.Write(" " + idx.ToString()); // Schreibe Faces "VertexCount v1 v2 .. vn"
    115. }
    116. sw.WriteLine();
    117. }
    118. }
    119. }
    120. }
    121. }
    Bilder
    • sky.png

      330,93 kB, 1.340×759, 110 mal angesehen