Skip to content
0.2.0-dev Download ZIP
148 lines ·5.7 KB Raw
1using System.Numerics;
2using EvilEngine.Formats;
3using EvilEngine.Formats.Models;
4
5namespace EvilEngine.Adapters.Overgrowth.Models;
6
7public static class OvergrowthMesh
8{
9 public const string Bitangent = "_OVERGROWTH_BITANGENT";
10
11 public static readonly IReadOnlyDictionary<string, string> Attributes = new Dictionary<string, string>
12 {
13 [Bitangent] =
14 "Each vertex's bitangent as Overgrowth works it out, toward the OBJ's own +v before its v is turned to the format's, which its tangent-space normal maps read with green inverted."
15 };
16
17 public static Vector3 Centre(ObjFile obj)
18 {
19 if (obj.Triangles.Count == 0)
20 return Vector3.Zero;
21 var min = new Vector3(float.PositiveInfinity);
22 var max = new Vector3(float.NegativeInfinity);
23 foreach (var (a, b, c) in obj.Triangles)
24 foreach (var corner in new[] { a, b, c })
25 {
26 min = Vector3.Min(min, obj.Positions[corner.Position]);
27 max = Vector3.Max(max, obj.Positions[corner.Position]);
28 }
29
30 return (min + max) / 2;
31 }
32
33 public static Vector3 Size(ObjFile obj)
34 {
35 var min = new Vector3(float.PositiveInfinity);
36 var max = new Vector3(float.NegativeInfinity);
37 foreach (var (a, b, c) in obj.Triangles)
38 foreach (var corner in new[] { a, b, c })
39 {
40 min = Vector3.Min(min, obj.Positions[corner.Position]);
41 max = Vector3.Max(max, obj.Positions[corner.Position]);
42 }
43
44 return obj.Triangles.Count == 0 ? Vector3.Zero : max - min;
45 }
46
47 public static ModelPrimitive Primitive(ObjFile obj, Vector3 centre, int? material)
48 {
49 var smooth = obj.Normals.Count > 0;
50 var index = new Dictionary<ObjCorner, uint>();
51 var positions = new List<Vector3>();
52 var normals = new List<Vector3>();
53 var uvs = new List<Vector2>();
54 var raw = new List<Vector2>();
55 var indices = new List<uint>();
56
57 uint Add(ObjCorner corner, Vector3 flat)
58 {
59 if (smooth && index.TryGetValue(corner, out var known))
60 return known;
61 var at = (uint)positions.Count;
62 positions.Add(obj.Positions[corner.Position] - centre);
63 var normal = smooth && corner.Normal >= 0 ? obj.Normals[corner.Normal] : flat;
64 normals.Add(normal.LengthSquared() > 0 ? Vector3.Normalize(normal) : Vector3.UnitY);
65 var uv = corner.Uv >= 0 ? obj.Uvs[corner.Uv] : Vector2.Zero;
66 raw.Add(uv);
67 uvs.Add(new Vector2(uv.X, 1 - uv.Y));
68 if (smooth)
69 index[corner] = at;
70 return at;
71 }
72
73 foreach (var (a, b, c) in obj.Triangles)
74 {
75 var (pa, pb, pc) = (obj.Positions[a.Position], obj.Positions[b.Position], obj.Positions[c.Position]);
76 var flat = Vector3.Cross(pb - pa, pc - pa);
77 indices.Add(Add(a, flat));
78 indices.Add(Add(b, flat));
79 indices.Add(Add(c, flat));
80 }
81
82 var (tangents, bitangents) = Tangents(positions, normals, raw, indices);
83 return new ModelPrimitive(material, [.. indices],
84 new ModelVertices([.. positions], [.. normals], tangents, [.. uvs],
85 Custom: [new ModelAttribute(Bitangent, 3, bitangents)]), [], []);
86 }
87
88 public static (Vector4[] Tangents, float[] Bitangents) Tangents(IReadOnlyList<Vector3> positions,
89 IReadOnlyList<Vector3> normals, IReadOnlyList<Vector2> uvs, IReadOnlyList<uint> indices)
90 {
91 var tan = new Vector3[positions.Count];
92 var bit = new Vector3[positions.Count];
93 for (var i = 0; i + 2 < indices.Count; i += 3)
94 {
95 var (a, b, c) = ((int)indices[i], (int)indices[i + 1], (int)indices[i + 2]);
96 var e1 = positions[b] - positions[a];
97 var e2 = positions[c] - positions[a];
98 var d1 = uvs[b] - uvs[a];
99 var d2 = uvs[c] - uvs[a];
100 var r = d1.X * d2.Y - d2.X * d1.Y;
101 if (Math.Abs(r) < 1e-12f)
102 continue;
103 r = 1 / r;
104 var s = (e1 * d2.Y - e2 * d1.Y) * r;
105 var t = (e2 * d1.X - e1 * d2.X) * r;
106 foreach (var v in new[] { a, b, c })
107 {
108 tan[v] += s;
109 bit[v] += t;
110 }
111 }
112
113 var tangents = new Vector4[positions.Count];
114 var bitangents = new float[positions.Count * 3];
115 for (var v = 0; v < positions.Count; v++)
116 {
117 var n = normals[v];
118 var t = Orthogonal(tan[v], n, Perpendicular(n));
119 var b = Orthogonal(bit[v], n, Vector3.Cross(n, t));
120 var w = Vector3.Dot(Vector3.Cross(n, t), b) < 0 ? -1f : 1f;
121 tangents[v] = new Vector4(t, w);
122 bitangents[v * 3] = b.X;
123 bitangents[v * 3 + 1] = b.Y;
124 bitangents[v * 3 + 2] = b.Z;
125 }
126
127 return (tangents, bitangents);
128 }
129
130 private static Vector3 Orthogonal(Vector3 v, Vector3 n, Vector3 fallback)
131 {
132 var o = v - n * Vector3.Dot(n, v);
133 return o.LengthSquared() > 1e-20f ? Vector3.Normalize(o) : fallback;
134 }
135
136 private static Vector3 Perpendicular(Vector3 n)
137 {
138 return Vector3.Normalize(Vector3.Cross(n, Math.Abs(n.Y) < 0.9f ? Vector3.UnitY : Vector3.UnitX));
139 }
140
141 public static Model Single(ModelPrimitive primitive, ContentId? material, string? name)
142 {
143 return new Model([new ModelNode(name, [], Vector3.Zero, Quaternion.Identity, Vector3.One, 0)], [0],
144 [new ModelMesh(name, [primitive], [], [])], [],
145 material is { } id ? [new ModelMaterial(id.Name, id, ModelPbr.Default)] : [], [], [], [], [],
146 ModelExtras.Empty with { Attributes = Attributes });
147 }
148}