This commit is contained in:
Oscar
2025-06-08 23:43:22 +03:00
parent a6f2e43c49
commit 160429eae6
34 changed files with 5219 additions and 1905 deletions

View File

@@ -9,8 +9,8 @@ public sealed class DSPReverb : Component, Component.ITriggerListener
{
[Property] public MixerHandle TargetMixer { get; set; }
[Property] public DspPresetHandle Preset { get; set; }
[Property] [Range(0f, 10f, 0.1f)] public float FadeDuration { get; set; } = 1f;
[Property] public BBox Bounds { get; set; } = new BBox(Vector3.One * -100f, Vector3.One * 100f);
[Property] [Range( 0f, 10f, 0.1f )] public float FadeDuration { get; set; } = 1f;
[Property] public BBox Bounds { get; set; } = new BBox( Vector3.One * -100f, Vector3.One * 100f );
private DspProcessor _processor;
private BoxCollider _triggerCollider;
@@ -28,63 +28,79 @@ public sealed class DSPReverb : Component, Component.ITriggerListener
_triggerCollider.Center = Bounds.Center;
}
public void OnTriggerEnter(Collider other)
public void OnTriggerEnter( Collider other )
{
_cts?.Cancel();
_cts = new CancellationTokenSource();
if(_processor != null) { TargetMixer.Get().RemoveProcessor(_processor);}
_processor = new DspProcessor(Preset.Name);
_processor.Mix = 0f;
TargetMixer.Get().AddProcessor(_processor);
_ = UpdateMixAsync(1f);
if ( other.Components.TryGet<Dedugan>( out Dedugan dedugan ) )
{
if ( dedugan.Connection == Dedugan.Local.Connection )
{
_cts?.Cancel();
_cts = new CancellationTokenSource();
if ( _processor != null ) { TargetMixer.Get().RemoveProcessor( _processor ); }
_processor = new DspProcessor( Preset.Name );
_processor.Mix = 0f;
TargetMixer.Get().AddProcessor( _processor );
_processor.Mix = 1f;
// _ = UpdateMixAsync(1f);
}
}
}
private async Task UpdateMixAsync(float targetMix)
private async Task UpdateMixAsync( float targetMix )
{
float startMix = _processor.Mix;
float elapsed = FadeDuration * ((targetMix == 0f || startMix == 0f) ? 0f : Math.Min(startMix / targetMix, 1f));
float elapsed = FadeDuration *
((targetMix == 0f || startMix == 0f) ? 0f : Math.Min( startMix / targetMix, 1f ));
float lastTime = Time.Now;
while (elapsed < FadeDuration && !_cts.IsCancellationRequested)
while ( elapsed < FadeDuration && !_cts.IsCancellationRequested )
{
await Task.FixedUpdate();
float delta = Time.Now - lastTime;
elapsed += delta;
float t = Math.Clamp(elapsed / FadeDuration, 0f, 1f);
_processor.Mix = Math.Clamp(startMix + (targetMix - startMix) * t, 0f, 1f);
float t = Math.Clamp( elapsed / FadeDuration, 0f, 1f );
_processor.Mix = Math.Clamp( startMix + (targetMix - startMix) * t, 0f, 1f );
lastTime = Time.Now;
}
if (!_cts.IsCancellationRequested)
if ( !_cts.IsCancellationRequested )
{
_processor.Mix = targetMix;
}
}
public void OnTriggerExit(Collider other)
public void OnTriggerExit( Collider other )
{
_cts?.Cancel();
_cts = new CancellationTokenSource();
_ = UpdateMixAsync(0f).ContinueWith( (_) =>
if ( other.Components.TryGet<Dedugan>( out Dedugan dedugan ) )
{
if (_processor == null) return;
TargetMixer.Get().RemoveProcessor(_processor);
_processor = null;
} );
if ( dedugan.Connection == Dedugan.Local.Connection )
{
_cts?.Cancel();
_cts = new CancellationTokenSource();
_processor.Mix = 0f;
}
}
// _ = UpdateMixAsync(0f).ContinueWith( (_) =>
// {
// if (_processor == null) return;
//
// TargetMixer.Get().RemoveProcessor(_processor);
// _processor = null;
// } );
}
protected override void DrawGizmos()
{
base.DrawGizmos();
Gizmo.Draw.Color = Color.Green;
Gizmo.Draw.LineBBox(Bounds);
Gizmo.Draw.LineBBox( Bounds );
}
}