refactor(pipeline): MP1a - MeshExtractor extracted to AcDream.Content (verbatim move)

This commit is contained in:
Erik 2026-07-05 20:19:52 +02:00
parent e3376d4734
commit 30cc1e282e
6 changed files with 1260 additions and 1113 deletions

View file

@ -1,3 +1,4 @@
using AcDream.Content;
using DatReaderWriter;
using DatReaderWriter.Enums;
using DatReaderWriter.Lib.IO;

View file

@ -1,134 +0,0 @@
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);
}
}
}
}

View file

@ -1,116 +0,0 @@
using DatReaderWriter;
using DatReaderWriter.Enums;
using DatReaderWriter.Lib.IO;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
// Phase O-T7: verbatim copy of WorldBuilder.Shared.Services.IDatReaderWriter +
// IDatDatabase into the AcDream.App.Rendering.Wb namespace so the
// WorldBuilder.Shared project reference can be dropped.
// The only consumer of IDatReaderWriter in acdream is DatCollectionAdapter +
// ObjectMeshManager, both already in this namespace.
namespace AcDream.App.Rendering.Wb;
/// <summary>
/// Interface for the dat reader/writer
/// </summary>
public interface IDatReaderWriter : IDisposable {
/// <summary>
/// Gets the source directory of the DAT files.
/// </summary>
string SourceDirectory { get; }
/// <summary>
/// Tries to get the raw bytes of a file from a specific region database.
/// </summary>
bool TryGetFileBytes(uint regionId, uint fileId, ref byte[] bytes, out int bytesRead);
/// <summary>
/// The portal database
/// </summary>
IDatDatabase Portal { get; }
/// <summary>
/// The cell region databases. Each key is a cell region ID
/// </summary>
ReadOnlyDictionary<uint, IDatDatabase> CellRegions { get; }
/// <summary>
/// The high res database
/// </summary>
IDatDatabase HighRes { get; }
/// <summary>
/// The language database
/// </summary>
IDatDatabase Language { get; }
/// <summary>
/// A mapping of region ids to region dat file entry ids. key: region id, value: region dat file entry
/// </summary>
ReadOnlyDictionary<uint, uint> RegionFileMap { get; }
/// <summary>
/// Gets the current portal iteration.
/// </summary>
int PortalIteration { get; }
/// <summary>
/// Gets the current cell iteration (from the first cell region).
/// </summary>
int CellIteration { get; }
/// <summary>
/// Gets the current high res iteration.
/// </summary>
int HighResIteration { get; }
/// <summary>
/// Gets the current language iteration.
/// </summary>
int LanguageIteration { get; }
// Write-path methods — preserved from WB's interface for verbatim
// compatibility but not exercised by ObjectMeshManager in acdream.
/// <summary>Attempts to save a database object to the appropriate DAT.</summary>
bool TrySave<T>(T obj, int iteration = 0) where T : IDBObj;
/// <summary>Attempts to save a database object to the appropriate DAT for a specific region.</summary>
bool TrySave<T>(uint regionId, T obj, int iteration = 0) where T : IDBObj;
/// <summary>
/// Resolution of a data ID to a database and type
/// </summary>
public record IdResolution(IDatDatabase Database, DBObjType Type);
/// <summary>
/// Resolves a data ID to all possible databases and types.
/// </summary>
public IEnumerable<IdResolution> ResolveId(uint id);
}
/// <summary>
/// Interface for a dat database, providing methods to retrieve files and objects.
/// </summary>
public interface IDatDatabase : IDisposable {
DatDatabase Db { get; }
/// <summary>Retrieves the current iteration of the database.</summary>
int Iteration { get; }
/// <summary>Retrieves all file IDs of a specific type.</summary>
public IEnumerable<uint> GetAllIdsOfType<T>() where T : IDBObj;
/// <summary>Attempts to retrieve a database object by its file ID.</summary>
public bool TryGet<T>(uint fileId, [MaybeNullWhen(false)] out T value) where T : IDBObj;
/// <summary>Attempts to retrieve the raw bytes of a file by its ID.</summary>
bool TryGetFileBytes(uint fileId, [MaybeNullWhen(false)] out byte[] value);
/// <summary>Attempts to retrieve the raw bytes of a file by its ID into a provided buffer.</summary>
bool TryGetFileBytes(uint fileId, ref byte[] bytes, out int bytesRead);
/// <summary>Attempts to save a database object.</summary>
bool TrySave<T>(T obj, int iteration = 0) where T : IDBObj;
}

File diff suppressed because it is too large Load diff