feat(vfx): Phase E.3 particle system + hook wiring + registry
Full runtime particle pipeline consuming Phase E.1's animation hooks.
13 motion integrators, per-emitter particle pools with overwrite-oldest
eviction, colour / scale / alpha interpolation over life, and a
ParticleHookSink routing CreateParticle / DestroyParticle / StopParticle /
CreateBlockingParticle hooks from the animation-hook router.
Core layer:
- ParticleSystem: handle-based emitter pool, per-tick emission
accumulator (retail Birthrate = time-between-spawns → our emit rate
via 1/B), 13 integrators covering the full ParticleType enum:
Still, LocalVelocity, GlobalVelocity, 7 Parabolic variants (all
apply Gravity * dt to velocity), Swarm (orbital drift),
Explode (outward from anchor), Implode (inward to anchor, dies at
convergence).
- EmitterDescRegistry: id-keyed EmitterDesc cache with fallback-to-
default for unknown ids. Replaces the dat-loaded path until
Chorizite.DatReaderWriter exposes ParticleEmitterInfo (v2.1.7 does
not; upgraded from 2.1.4 anyway for future types).
- ParticleHookSink: wires the full hook family:
- CreateParticleHook → SpawnEmitterById at entity pose + hook offset
- CreateBlockingParticleHook → marker only (blocking semantics live
in the sequencer not here)
- DestroyParticleHook → StopEmitter(handle, fadeOut=false)
- StopParticleHook → StopEmitter(handle, fadeOut=true)
- (Default/CallPES deferred until PhysicsScript dat is loadable)
GameWindow integration:
- ParticleSystem created eagerly (no driver dep), sink registered with
hook router, Tick advanced per OnRender frame after animation tick so
hooks fired this frame get integrated.
Tests (11 new): spawn-handle, emit-over-time steady state, lifetime
death curve, LocalVelocity movement, Parabolic gravity arc, Explode
outward trajectory, StopEmitter instant kill vs fadeOut, MaxParticles
cap enforcement, registry default fallback, registry custom
registration.
Upgraded Chorizite.DatReaderWriter 2.1.4 → 2.1.7 across Core + Cli.
Build green, 508 tests pass (up from 497).
Ref: r04 §2 (CParticleManager), §3 (13 integrators), §6 (PhysicsScript).
Renderer (instanced billboarded quads in translucent pass) ships next
commit; this one covers the data / logic / wiring layer in full.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
351723928f
commit
d3165f99d7
7 changed files with 820 additions and 2 deletions
129
src/AcDream.Core/Vfx/ParticleHookSink.cs
Normal file
129
src/AcDream.Core/Vfx/ParticleHookSink.cs
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Numerics;
|
||||
using AcDream.Core.Physics;
|
||||
using DatReaderWriter.Types;
|
||||
|
||||
namespace AcDream.Core.Vfx;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="IAnimationHookSink"/> that translates particle-bearing
|
||||
/// animation hooks into <see cref="ParticleSystem"/> spawn / stop calls.
|
||||
///
|
||||
/// <para>
|
||||
/// Hook types handled (r04 §6):
|
||||
/// <list type="bullet">
|
||||
/// <item><description>
|
||||
/// <see cref="CreateParticleHook"/> — spawn an emitter from the
|
||||
/// hook's <c>EmitterInfoId</c> at the entity's world pose plus the
|
||||
/// hook offset. Retail attaches to a specific mesh part; we
|
||||
/// attach to the entity's root and will refine per-part when the
|
||||
/// renderer exposes per-part world transforms.
|
||||
/// </description></item>
|
||||
/// <item><description>
|
||||
/// <see cref="CreateBlockingParticleHook"/> — same as
|
||||
/// CreateParticleHook but the retail sequencer blocks animation
|
||||
/// progression until the particle is done. We spawn the emitter
|
||||
/// identically; the "blocking" semantics belong to the sequencer.
|
||||
/// </description></item>
|
||||
/// <item><description>
|
||||
/// <see cref="DestroyParticleHook"/> — stop the most-recent emitter
|
||||
/// matching the hook's <c>EmitterId</c>. Deferred to a future pass
|
||||
/// when we retain per-entity emitter-id → handle maps.
|
||||
/// </description></item>
|
||||
/// <item><description>
|
||||
/// <see cref="StopParticleHook"/> — pause all emitters on the
|
||||
/// entity (fade out).
|
||||
/// </description></item>
|
||||
/// <item><description>
|
||||
/// <see cref="DefaultScriptHook"/> / <see cref="DefaultScriptPartHook"/>
|
||||
/// — trigger the entity's <c>DefaultScriptId</c> PhysicsScript.
|
||||
/// Requires PhysicsScript table; deferred.
|
||||
/// </description></item>
|
||||
/// <item><description>
|
||||
/// <see cref="CallPESHook"/> — fire a PhysicsScript by id.
|
||||
/// Deferred until DRW exposes PhysicsScript dat.
|
||||
/// </description></item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// Per-entity emitter handle tracking is kept here so DestroyParticle /
|
||||
/// StopParticle can target the right emitter when a server-sent
|
||||
/// <c>PlayEffect</c> fires.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed class ParticleHookSink : IAnimationHookSink
|
||||
{
|
||||
private readonly ParticleSystem _system;
|
||||
|
||||
// entityId → most-recently-spawned emitter handle per emitterId.
|
||||
// DestroyParticleHook.EmitterId is effectively an application-layer
|
||||
// key ("the smoke trail I spawned 2 seconds ago"), so we track by
|
||||
// (entity, emitterId).
|
||||
private readonly ConcurrentDictionary<(uint EntityId, uint EmitterId), int> _handlesByKey = new();
|
||||
|
||||
public ParticleHookSink(ParticleSystem system)
|
||||
{
|
||||
_system = system ?? throw new ArgumentNullException(nameof(system));
|
||||
}
|
||||
|
||||
public void OnHook(uint entityId, Vector3 entityWorldPosition, AnimationHook hook)
|
||||
{
|
||||
switch (hook)
|
||||
{
|
||||
case CreateParticleHook cph:
|
||||
SpawnFromHook(entityId, entityWorldPosition,
|
||||
emitterInfoId: (uint)cph.EmitterInfoId,
|
||||
offset: cph.Offset.Origin,
|
||||
partIndex: (int)cph.PartIndex,
|
||||
logicalId: cph.EmitterId);
|
||||
break;
|
||||
|
||||
case CreateBlockingParticleHook:
|
||||
// Retail's CreateBlockingParticleHook is a sentinel (no
|
||||
// payload) — the sequencer pauses advancement until the
|
||||
// current particle emitter finishes. We surface this as
|
||||
// "no spawn needed, just keep the current particle alive";
|
||||
// blocking semantics live in the sequencer, not here.
|
||||
break;
|
||||
|
||||
case DestroyParticleHook dph:
|
||||
if (_handlesByKey.TryRemove((entityId, dph.EmitterId), out var handleToDestroy))
|
||||
_system.StopEmitter(handleToDestroy, fadeOut: false);
|
||||
break;
|
||||
|
||||
case StopParticleHook sph:
|
||||
if (_handlesByKey.TryGetValue((entityId, sph.EmitterId), out var handleToStop))
|
||||
_system.StopEmitter(handleToStop, fadeOut: true);
|
||||
break;
|
||||
|
||||
// DefaultScript / CallPES are wired when PhysicsScript loading
|
||||
// is available. They arrive at the sink but we can't act until
|
||||
// the script table returns a real emitter list.
|
||||
}
|
||||
}
|
||||
|
||||
private void SpawnFromHook(
|
||||
uint entityId,
|
||||
Vector3 worldPos,
|
||||
uint emitterInfoId,
|
||||
Vector3 offset,
|
||||
int partIndex,
|
||||
uint logicalId)
|
||||
{
|
||||
// Spawn position: entity pose + hook offset. PartIndex will be
|
||||
// used when the renderer passes per-part transforms through; for
|
||||
// now, fold it into the root pos.
|
||||
var anchor = worldPos + offset;
|
||||
|
||||
int handle = _system.SpawnEmitterById(
|
||||
emitterId: emitterInfoId,
|
||||
anchor: anchor,
|
||||
rot: Quaternion.Identity,
|
||||
attachedObjectId: entityId,
|
||||
attachedPartIndex: partIndex);
|
||||
|
||||
_handlesByKey[(entityId, logicalId)] = handle;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue