71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using Ichni.RhythmGame.Beatmap;
|
||
using UnityEngine;
|
||
using UnityEngine.Rendering.Universal; // 需要引入 URP 库
|
||
|
||
namespace Ichni.RhythmGame
|
||
{
|
||
public class ChromaticAberrationEffect : EffectBase
|
||
{
|
||
#region [效果参数] Effect Parameters
|
||
public float peak;
|
||
public AnimationCurve intensityCurve;
|
||
|
||
// 缓存后处理组件,避免运行时每帧由于反序列化而产生 GC
|
||
private ChromaticAberration _chromaticAberration;
|
||
#endregion
|
||
|
||
#region [初始化] Initialization
|
||
public ChromaticAberrationEffect(float effectTime, float peak, AnimationCurve intensityCurve)
|
||
: base(effectTime) // 【修改1】调用基类含时构造,将 effectTime 赋为 duration,激活时长逻辑
|
||
{
|
||
this.effectTime = effectTime;
|
||
this.peak = peak;
|
||
this.intensityCurve = intensityCurve;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 私有方法:安全获取 URP 色差后处理句柄
|
||
/// </summary>
|
||
private void PrepareEffectHandle()
|
||
{
|
||
if (_chromaticAberration == null && PostProcessingManager.GlobalVolume != null)
|
||
{
|
||
// .profile 会自动实例化一个副本,防止修改了Editor原有的资产
|
||
PostProcessingManager.GlobalVolume.profile.TryGet(out _chromaticAberration);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region [效果逻辑覆盖] Effect Pattern Overrides
|
||
public override void PreExecute()
|
||
{
|
||
PrepareEffectHandle();
|
||
}
|
||
|
||
public override void Execute()
|
||
{
|
||
if (_chromaticAberration != null)
|
||
{
|
||
// 【修改2】直接借助基类已算出的 effectProgressPercent 来拉取曲线
|
||
// 不再需要外部插件,每一帧的强度与音乐进程强绑定!哪怕音乐倒退也会原路倒退。
|
||
float currentIntensity = intensityCurve.Evaluate(effectProgressPercent) * peak;
|
||
_chromaticAberration.intensity.value = currentIntensity;
|
||
}
|
||
}
|
||
|
||
public override void Adjust() { ResetEffect(); }
|
||
public override void Recover() { ResetEffect(); }
|
||
public override void Disrupt() { ResetEffect(); }
|
||
|
||
private void ResetEffect()
|
||
{
|
||
if (_chromaticAberration != null) _chromaticAberration.intensity.value = 0f;
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|