Ports retail's ParticleEmitterInfo / Particle::Init / Particle::Update
(0x005170d0..0x0051d400) and PhysicsScript runtime to a C# data-layer
plus a Silk.NET billboard renderer. Sky-PES path is debug-only behind
ACDREAM_ENABLE_SKY_PES because named-retail decomp confirms GameSky
copies SkyObject.pes_id but never reads it (CreateDeletePhysicsObjects
0x005073c0, MakeObject 0x00506ee0, UseTime 0x005075b0).
Post-review fixes folded into this commit:
H1: AttachLocal (is_parent_local=1) follows live parent each frame.
ParticleSystem.UpdateEmitterAnchor + ParticleHookSink.UpdateEntityAnchor
let the owning subsystem refresh AnchorPos every tick — matches
ParticleEmitter::UpdateParticles 0x0051d2d4 which re-reads the live
parent frame when is_parent_local != 0. Drops the renderer-side
cameraOffset hack that only worked when the parent was the camera.
H3: Strip the long stale comment in GfxObjMesh.cs that contradicted the
retail-faithful (1 - translucency) opacity formula. The code was
right; the comment was a leftover from an earlier hypothesis and
would have invited a wrong "fix".
M1: SkyRenderer tracks textures whose wrap mode it set to ClampToEdge
and restores them to Repeat at end-of-pass, so non-sky renderers
that share the GL handle can't silently inherit clamped wrap state.
M2: Post-scene Z-offset (-120m) only fires when the SkyObject is
weather-flagged AND bit 0x08 is clear, matching retail
GameSky::UpdatePosition 0x00506dd0. The old code applied it to
every post-scene object — a no-op today (every Dereth post-scene
entry happens to be weather-flagged) but a future post-scene-only
sun rim would have been pushed below the camera.
M4: ParticleSystem.EmitterDied event lets ParticleHookSink prune dead
handles from the per-entity tracking dictionaries, fixing a slow
leak where naturally-expired emitters' handles stayed in the
ConcurrentBag forever during long sessions.
M5: SkyPesEntityId moves the post-scene flag bit to 0x08000000 so it
can't ever overlap the object-index range. Synthetic IDs stay in
the reserved 0xFxxxxxxx space.
New tests (ParticleSystemTests + ParticleHookSinkTests):
- UpdateEmitterAnchor_AttachLocal_ParticlePositionFollowsLiveAnchor
- UpdateEmitterAnchor_AttachLocalCleared_ParticleFrozenAtSpawnOrigin
- EmitterDied_FiresOncePerHandle_AfterAllParticlesExpire
- Birthrate_PerSec_EmitsOnePerTickWhenIntervalElapsed (retail-faithful
single-emit-per-frame behavior)
- UpdateEntityAnchor_WithAttachLocal_MovesParticleToLiveAnchor
- EmitterDied_PrunesPerEntityHandleTracking
dotnet build green, dotnet test green: 695 / 393 / 243 = 1331 passed
(up from 1325).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
263 lines
12 KiB
C#
263 lines
12 KiB
C#
using System.Numerics;
|
|
using AcDream.Core.Terrain;
|
|
using DatReaderWriter;
|
|
using DatReaderWriter.DBObjs;
|
|
using DatReaderWriter.Enums;
|
|
|
|
namespace AcDream.Core.Meshing;
|
|
|
|
public static class GfxObjMesh
|
|
{
|
|
/// <summary>
|
|
/// Walk a GfxObj's polygons and produce one <see cref="GfxObjSubMesh"/>
|
|
/// per referenced Surface, emitting positive-side and negative-side
|
|
/// triangles separately when the polygon specifies both.
|
|
/// </summary>
|
|
/// <param name="gfxObj">The GfxObj to build sub-meshes from.</param>
|
|
/// <param name="dats">
|
|
/// Optional dat collection used to read Surface.Type flags and set
|
|
/// <see cref="GfxObjSubMesh.Translucency"/>. When null (e.g. offline tests)
|
|
/// all sub-meshes default to <see cref="TranslucencyKind.Opaque"/>.
|
|
/// </param>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// Ported from
|
|
/// <c>references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/ObjectMeshManager.cs</c>
|
|
/// (BuildPolygonIndices + the pos/neg emission loop around line 955).
|
|
/// The rule for emitting a polygon side:
|
|
/// </para>
|
|
/// <list type="bullet">
|
|
/// <item><b>Pos side:</b> emit whenever <c>!Stippling.NoPos</c> and
|
|
/// <c>PosSurface</c> is a valid index.</item>
|
|
/// <item><b>Neg side:</b> emit when
|
|
/// <c>Stippling.Negative</c>, <c>Stippling.Both</c>, or
|
|
/// <c>(!Stippling.NoNeg && SidesType == CullMode.Clockwise)</c>.
|
|
/// The last condition is AC's non-obvious convention for "this
|
|
/// polygon has a back face even though nothing in Stippling
|
|
/// declares it" — you cannot drop it without punching holes
|
|
/// through closed meshes like the lifestone and any other
|
|
/// weenie that relies on double-sided polygons.</item>
|
|
/// </list>
|
|
/// <para>
|
|
/// Neg-side triangles get the reversed winding and a negated vertex
|
|
/// normal so lighting stays correct if we ever enable face culling;
|
|
/// acdream currently renders with culling disabled, but we still emit
|
|
/// reversed indices to keep the semantics right.
|
|
/// </para>
|
|
/// <para>
|
|
/// The dedup cache is keyed by <c>(posIdx, uvIdx, isNeg)</c> because
|
|
/// the same vertex position on the pos and neg sides needs different
|
|
/// normals (and potentially different UVs via <c>NegUVIndices</c>).
|
|
/// </para>
|
|
/// </remarks>
|
|
public static IReadOnlyList<GfxObjSubMesh> Build(GfxObj gfxObj, DatCollection? dats = null)
|
|
{
|
|
// One bucket per (surface-index, isNeg) pair. Negative-side triangles
|
|
// always land in a different bucket than their positive counterparts
|
|
// because their normals and winding differ; the renderer doesn't care
|
|
// about the distinction once sub-meshes are emitted, but the build
|
|
// loop has to keep them separate to produce correct vertex data.
|
|
var perBucket = new Dictionary<(int surfaceIdx, bool isNeg),
|
|
(List<Vertex> Vertices, List<uint> Indices,
|
|
Dictionary<(int pos, int uv, bool neg), uint> Dedupe)>();
|
|
|
|
foreach (var kvp in gfxObj.Polygons)
|
|
{
|
|
var poly = kvp.Value;
|
|
if (poly.VertexIds.Count < 3)
|
|
continue; // degenerate — can't form a triangle
|
|
|
|
// --- Positive side ---
|
|
bool hasPos = !poly.Stippling.HasFlag(StipplingType.NoPos);
|
|
if (hasPos)
|
|
EmitSide(poly, poly.PosSurface, isNeg: false);
|
|
|
|
// --- Negative side ---
|
|
// Three ways AC flags a polygon as double-sided:
|
|
// 1. Stippling.Negative or Stippling.Both — explicit.
|
|
// 2. Stippling.NoNeg is NOT set AND SidesType == Clockwise —
|
|
// AC's "Clockwise CullMode means there are NegUVIndices
|
|
// on the wire" convention. See
|
|
// DatReaderWriter/.../Generated/Types/Polygon.generated.cs
|
|
// — NegUVIndices are only read when SidesType == Clockwise,
|
|
// and WorldBuilder uses the same rule to decide whether to
|
|
// emit the neg side at build time.
|
|
bool hasNeg =
|
|
poly.Stippling.HasFlag(StipplingType.Negative) ||
|
|
poly.Stippling.HasFlag(StipplingType.Both) ||
|
|
(!poly.Stippling.HasFlag(StipplingType.NoNeg) && poly.SidesType == CullMode.Clockwise);
|
|
|
|
if (hasNeg)
|
|
EmitSide(poly, poly.NegSurface, isNeg: true);
|
|
|
|
void EmitSide(DatReaderWriter.Types.Polygon p, short surfaceIdx, bool isNeg)
|
|
{
|
|
if (surfaceIdx < 0 || surfaceIdx >= gfxObj.Surfaces.Count)
|
|
return;
|
|
|
|
var bucketKey = ((int)surfaceIdx, isNeg);
|
|
if (!perBucket.TryGetValue(bucketKey, out var bucket))
|
|
{
|
|
bucket = (new List<Vertex>(), new List<uint>(),
|
|
new Dictionary<(int, int, bool), uint>());
|
|
perBucket[bucketKey] = bucket;
|
|
}
|
|
|
|
// Collect one output index per polygon corner. If we fail to
|
|
// resolve a vertex we abort the whole polygon rather than
|
|
// emitting a degenerate triangle (matches the behavior of
|
|
// the previous builder).
|
|
var polyOut = new List<uint>(p.VertexIds.Count);
|
|
bool skipPoly = false;
|
|
for (int i = 0; i < p.VertexIds.Count; i++)
|
|
{
|
|
int posIdx = p.VertexIds[i];
|
|
|
|
// UV index selection: neg side uses NegUVIndices when
|
|
// present; otherwise fall back to PosUVIndices; otherwise
|
|
// zero. Matches WorldBuilder/ObjectMeshManager.cs:1521-1524.
|
|
int uvIdx = 0;
|
|
if (isNeg && p.NegUVIndices.Count > 0 && i < p.NegUVIndices.Count)
|
|
uvIdx = p.NegUVIndices[i];
|
|
else if (!isNeg && i < p.PosUVIndices.Count)
|
|
uvIdx = p.PosUVIndices[i];
|
|
else if (i < p.PosUVIndices.Count)
|
|
uvIdx = p.PosUVIndices[i]; // neg side with no NegUVIndices — borrow pos
|
|
|
|
if (!gfxObj.VertexArray.Vertices.TryGetValue((ushort)posIdx, out var sw))
|
|
{
|
|
skipPoly = true;
|
|
break;
|
|
}
|
|
|
|
var texcoord = uvIdx >= 0 && uvIdx < sw.UVs.Count
|
|
? new Vector2(sw.UVs[uvIdx].U, sw.UVs[uvIdx].V)
|
|
: Vector2.Zero;
|
|
|
|
// Negate the vertex normal for the neg side so lighting
|
|
// stays correct if we ever enable face culling. With
|
|
// culling disabled the shader still samples this normal
|
|
// for the diffuse term so getting it right matters
|
|
// regardless of backface state.
|
|
var normal = System.Numerics.Vector3.Normalize(isNeg ? -sw.Normal : sw.Normal);
|
|
|
|
var key = (posIdx, uvIdx, isNeg);
|
|
if (!bucket.Dedupe.TryGetValue(key, out var outIdx))
|
|
{
|
|
outIdx = (uint)bucket.Vertices.Count;
|
|
bucket.Vertices.Add(new Vertex(sw.Origin, normal, texcoord, TerrainLayer: 0));
|
|
bucket.Dedupe[key] = outIdx;
|
|
}
|
|
polyOut.Add(outIdx);
|
|
}
|
|
|
|
if (skipPoly || polyOut.Count < 3)
|
|
return;
|
|
|
|
// Fan triangulation. Pos side keeps the original
|
|
// (0, i, i+1) winding the earlier builder used so existing
|
|
// tests and render behavior are preserved. Neg side emits
|
|
// the opposite winding so the two faces point away from
|
|
// each other — matches WorldBuilder/ObjectMeshManager.cs:
|
|
// 1564-1577 once you account for the reversed pos order.
|
|
if (isNeg)
|
|
{
|
|
for (int i = 1; i < polyOut.Count - 1; i++)
|
|
{
|
|
bucket.Indices.Add(polyOut[i + 1]);
|
|
bucket.Indices.Add(polyOut[i]);
|
|
bucket.Indices.Add(polyOut[0]);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = 1; i < polyOut.Count - 1; i++)
|
|
{
|
|
bucket.Indices.Add(polyOut[0]);
|
|
bucket.Indices.Add(polyOut[i]);
|
|
bucket.Indices.Add(polyOut[i + 1]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Emit one sub-mesh per (surface, side) bucket. The sub-mesh API
|
|
// doesn't care whether a surface came from the pos or neg side —
|
|
// both go through the same texture cache path.
|
|
var result = new List<GfxObjSubMesh>(perBucket.Count);
|
|
foreach (var kvp in perBucket)
|
|
{
|
|
var (surfaceIdx, _) = kvp.Key;
|
|
var surfaceId = (uint)gfxObj.Surfaces[surfaceIdx];
|
|
|
|
// Resolve Surface.Type flags when a DatCollection is available
|
|
// so the renderer can split the draw into opaque and translucent
|
|
// passes. Also capture Surface.Luminosity (self-illumination
|
|
// coefficient — the FLOAT field, NOT the SurfaceType.Luminous
|
|
// flag bit). This is the retail signal used to make the sky
|
|
// dome / sun / moon texture-passthrough while clouds pick up
|
|
// the time-of-day ambient tint (see
|
|
// docs/research/2026-04-23-sky-retail-verbatim.md §6).
|
|
var translucency = TranslucencyKind.Opaque;
|
|
var luminosity = 0f;
|
|
// SurfOpacity = (1 - Surface.Translucency) for Translucent
|
|
// surfaces, 1.0 otherwise. See
|
|
// TranslucencyKindExtensions.OpacityFromSurfaceTranslucency for
|
|
// the decomp citation (CMaterial::SetTranslucencySimple at
|
|
// 0x005396f0 writes material alpha as 1 - translucency).
|
|
var diffuse = 1f;
|
|
var surfOpacity = 1f;
|
|
var disableFog = false;
|
|
if (dats is not null)
|
|
{
|
|
var surface = dats.Get<Surface>(surfaceId);
|
|
if (surface is not null)
|
|
{
|
|
translucency = TranslucencyKindExtensions.FromSurfaceType(surface.Type);
|
|
luminosity = surface.Luminosity;
|
|
diffuse = surface.Diffuse;
|
|
// Apply the dat's Translucency value as opacity ONLY
|
|
// when the Translucent flag (0x10) is set on the
|
|
// Surface. Without this gate, surfaces with
|
|
// Translucency=0 (non-Translucent default) would
|
|
// render fully transparent.
|
|
surfOpacity = TranslucencyKindExtensions.OpacityFromSurfaceTranslucency(
|
|
surface.Type,
|
|
surface.Translucency);
|
|
disableFog = TranslucencyKindExtensions.DisablesFixedFunctionFog(surface.Type);
|
|
}
|
|
}
|
|
|
|
// Authored UV range determines the wrap-mode choice in the
|
|
// sky pass. A mesh whose UVs are strictly in [0,1] (e.g. the
|
|
// outer dome 0x010015EE) wants CLAMP_TO_EDGE to avoid
|
|
// bilinear-filter bleed at the wall-seam edges; a mesh whose
|
|
// UVs deliberately tile (e.g. 0x010015EF, ~0.4..4.6) wants
|
|
// REPEAT so the texture tiles across the geometry. We make
|
|
// the call data-driven here rather than guessing from
|
|
// TexVelocity at draw time. See
|
|
// docs/research/2026-04-26-sky-investigation-handoff.md (Bug B).
|
|
bool needsUvRepeat = false;
|
|
foreach (var v in kvp.Value.Vertices)
|
|
{
|
|
if (v.TexCoord.X < 0f || v.TexCoord.X > 1f
|
|
|| v.TexCoord.Y < 0f || v.TexCoord.Y > 1f)
|
|
{ needsUvRepeat = true; break; }
|
|
}
|
|
|
|
result.Add(new GfxObjSubMesh(
|
|
SurfaceId: surfaceId,
|
|
Vertices: kvp.Value.Vertices.ToArray(),
|
|
Indices: kvp.Value.Indices.ToArray())
|
|
{
|
|
Translucency = translucency,
|
|
Luminosity = luminosity,
|
|
Diffuse = diffuse,
|
|
NeedsUvRepeat = needsUvRepeat,
|
|
SurfOpacity = surfOpacity,
|
|
DisableFog = disableFog,
|
|
});
|
|
}
|
|
return result;
|
|
}
|
|
}
|