Phase O Task 4: extract the WB mesh pipeline (ObjectMeshManager + 7 support files) from references/WorldBuilder into src/AcDream.App/Rendering/Wb/ and bridge dat I/O through our DatCollection via a thin DatCollectionAdapter. O-D7 adapter path taken: ObjectMeshManager has 26 _dats.X call sites (threshold 20), so a DatCollectionAdapter : IDatReaderWriter is introduced rather than refactoring ObjectMeshManager's internal dat access directly. Files added (verbatim copies, namespace-only changes): - ObjectMeshManager.cs — mesh pipeline hub; IDatReaderWriter field satisfied by adapter - GlobalMeshBuffer.cs — single global VAO/VBO/IBO manager - EdgeLineBuilder.cs — wireframe edge geometry from CellStruct polygons - ModernRenderData.cs — ModernBatchData + LandblockMdiCommand structs - TextureAtlasManager.cs — texture array grouping by (Width, Height, Format) - ParticleBatcher.cs — GPU particle batching; T4 interim uses BaseObjectRenderManager static fields from Chorizite.OpenGLSDLBackend.Lib (stays until T7) - ParticleEmitterRenderer.cs — per-emitter particle lifecycle + rendering - ActiveParticleEmitter.cs — wrapper holding renderer + part index + local offset - DatCollectionAdapter.cs — NEW: bridges DatCollection → IDatReaderWriter; implements ResolveId() via DatDatabase.TypeFromId + Tree.TryGetFile in HighRes→Portal→Language→Cell order matching DefaultDatReaderWriter; DatDatabaseWrapper wraps DatDatabase as IDatDatabase WbMeshAdapter.cs changes (T4 Step 6): - _graphicsDevice switched from Chorizite.OpenGLSDLBackend.OpenGLGraphicsDevice to extracted AcDream.App.Rendering.Wb.OpenGLGraphicsDevice - ParticleBatcher = new ParticleBatcher(_graphicsDevice) restored (T3 had null! placeholder) - ObjectMeshManager now constructed with new DatCollectionAdapter(dats) instead of _wbDats - _wbDats field + its construction + disposal + [indoor-upload] NULL_RESULT diagnostic block left intact — T7 cleanup removes these once WorldBuilder project ref is dropped EmbeddedResourceReader.cs: replaced assembly manifest lookup (wrong prefix for our assembly) with disk-based lookup mapping "Shaders.Particle.vert" → Rendering/Shaders/wb_particle.vert; consistent with all other acdream shaders. wb_particle.vert / wb_particle.frag: WB particle shaders copied verbatim with wb_ prefix to distinguish from acdream's own particle.vert. OpenGLGraphicsDevice.cs: ParticleBatcher property type updated to extracted ParticleBatcher; setter changed from private to internal so WbMeshAdapter (same assembly) can assign post-ctor. Build: green (0 errors, 0 warnings in AcDream.App). Tests: 1147+8 baseline maintained (8 pre-existing failures unchanged). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
134 lines
4.5 KiB
C#
134 lines
4.5 KiB
C#
using System.Numerics;
|
|
using DatReaderWriter.Types;
|
|
|
|
namespace AcDream.App.Rendering.Wb {
|
|
public static class EdgeLineBuilder {
|
|
public static List<Vector3> BuildEdgeLines(CellStruct cellStruct) {
|
|
var edgeMap = new Dictionary<EdgeKey, List<Edge>>();
|
|
|
|
foreach (var kvp in cellStruct.Polygons) {
|
|
var polyIdx = kvp.Key;
|
|
var vertexIds = kvp.Value.VertexIds;
|
|
|
|
var v0 = cellStruct.VertexArray.Vertices[(ushort)vertexIds[0]].Origin;
|
|
|
|
// AC polys can either be triangles or triangle fans
|
|
for (var i = 1; i < vertexIds.Count - 1; i++) {
|
|
var v1 = cellStruct.VertexArray.Vertices[(ushort)vertexIds[i]].Origin;
|
|
var v2 = cellStruct.VertexArray.Vertices[(ushort)vertexIds[i + 1]].Origin;
|
|
|
|
AddEdge(edgeMap, polyIdx, v0, v1);
|
|
AddEdge(edgeMap, polyIdx, v1, v2);
|
|
AddEdge(edgeMap, polyIdx, v2, v0);
|
|
}
|
|
}
|
|
|
|
var output = new List<Vector3>();
|
|
var processedEdges = new HashSet<EdgeKey>();
|
|
|
|
foreach (var kvp in edgeMap) {
|
|
var edgeKey = kvp.Key;
|
|
var edgeList = kvp.Value;
|
|
|
|
if (processedEdges.Contains(edgeKey)) continue;
|
|
|
|
processedEdges.Add(edgeKey);
|
|
|
|
if (edgeList.Count == 2) {
|
|
var poly1 = cellStruct.Polygons[edgeList[0].PolyIdx];
|
|
var poly2 = cellStruct.Polygons[edgeList[1].PolyIdx];
|
|
|
|
if (HaveSameTexture(poly1, poly2) && IsCoplanar(poly1, poly2, cellStruct))
|
|
continue;
|
|
}
|
|
|
|
output.Add(edgeList[0].P0);
|
|
output.Add(edgeList[0].P1);
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
private static void AddEdge(Dictionary<EdgeKey, List<Edge>> edgeMap, ushort polyIdx, Vector3 p0, Vector3 p1) {
|
|
var key = new EdgeKey(p0, p1);
|
|
var edge = new Edge(polyIdx, p0, p1);
|
|
|
|
if (!edgeMap.ContainsKey(key))
|
|
edgeMap[key] = new List<Edge>();
|
|
|
|
edgeMap[key].Add(edge);
|
|
}
|
|
|
|
private static bool HaveSameTexture(Polygon a, Polygon b) {
|
|
return a.PosSurface == b.PosSurface;
|
|
}
|
|
|
|
private static Vector3 CalculateNormal(Polygon poly, CellStruct cellStruct) {
|
|
var vertexIds = poly.VertexIds;
|
|
var verts = cellStruct.VertexArray.Vertices;
|
|
|
|
var v0 = verts[(ushort)vertexIds[0]];
|
|
var v1 = verts[(ushort)vertexIds[1]];
|
|
var v2 = verts[(ushort)vertexIds[2]];
|
|
|
|
var edge1 = v1.Origin - v0.Origin;
|
|
var edge2 = v2.Origin - v0.Origin;
|
|
return Vector3.Normalize(Vector3.Cross(edge1, edge2));
|
|
}
|
|
|
|
private static bool IsCoplanar(Polygon a, Polygon b, CellStruct cellStruct) {
|
|
var normA = CalculateNormal(a, cellStruct);
|
|
var normB = CalculateNormal(b, cellStruct);
|
|
|
|
var dp = Vector3.Dot(normA, normB);
|
|
|
|
// If dot product is 1 or -1, normals are parallel (coplanar)
|
|
// Allow for both same and opposite facing normals
|
|
const float tolerance = 0.01f;
|
|
return Math.Abs(Math.Abs(dp) - 1) < tolerance;
|
|
}
|
|
|
|
private class Edge {
|
|
public ushort PolyIdx { get; }
|
|
public Vector3 P0 { get; }
|
|
public Vector3 P1 { get; }
|
|
|
|
public Edge(ushort polyIdx, Vector3 p0, Vector3 p1) {
|
|
PolyIdx = polyIdx;
|
|
P0 = p0;
|
|
P1 = p1;
|
|
}
|
|
}
|
|
|
|
private class EdgeKey : IEquatable<EdgeKey> {
|
|
private readonly Vector3 _p0;
|
|
private readonly Vector3 _p1;
|
|
|
|
public EdgeKey(Vector3 p0, Vector3 p1) {
|
|
if (CompareVector3(p0, p1) > 0) {
|
|
_p0 = p1;
|
|
_p1 = p0;
|
|
}
|
|
else {
|
|
_p0 = p0;
|
|
_p1 = p1;
|
|
}
|
|
}
|
|
|
|
public bool Equals(EdgeKey? e) {
|
|
if (e == null) return false;
|
|
return _p0 == e._p0 && _p1 == e._p1;
|
|
}
|
|
|
|
public override int GetHashCode() {
|
|
return HashCode.Combine(_p0, _p1);
|
|
}
|
|
|
|
private static int CompareVector3(Vector3 a, Vector3 b) {
|
|
if (a.X != b.X) return a.X.CompareTo(b.X);
|
|
if (a.Y != b.Y) return a.Y.CompareTo(b.Y);
|
|
return a.Z.CompareTo(b.Z);
|
|
}
|
|
}
|
|
}
|
|
}
|