Files
ichni_Official/Assets/Scripts/Game/GameElements/GeneralEffects/ChromaticAberrationEffect.cs

71 lines
2.4 KiB
C#
Raw Normal View History

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