using UnityEngine; using MoreMountains.Feedbacks; using MoreMountains.Tools; using UnityEngine.Rendering; // 引入你的 PostProcess 命名空间 using SLSFramework.Rendering.PostProcessing; namespace MoreMountains.FeedbacksForThirdParty { /// /// 事件定义:携带所有必要的参数 /// public struct MMAdvancedChromaticAberrationShakeEvent { public static void Register(Delegate callback) { OnEvent += callback; } public static void Unregister(Delegate callback) { OnEvent -= callback; } public delegate void Delegate(MMF_Feedback source, AnimationCurve intensityCurve, float duration, float remapMin, float remapMax, bool relativeIntensity = false, float feedbacksIntensity = 1.0f, MMChannelData channelData = null, bool resetShakerValuesAfterShake = true, bool resetTargetValuesAfterShake = true, bool forwardDirection = true, TimescaleModes timescaleMode = TimescaleModes.Scaled, bool stop = false, bool restore = false, // Center bool modifyCenter = false, Vector2 center = default, // Extras bool modifyExtra = false, Vector3 channelSplit = default, float dispersionStrength = 0f, float jitterIntensity = 0f, float maskRadius = 0.2f, float maskHardness = 0.2f); public static event Delegate OnEvent; public static void Trigger(MMF_Feedback source, AnimationCurve intensityCurve, float duration, float remapMin, float remapMax, bool relativeIntensity = false, float feedbacksIntensity = 1.0f, MMChannelData channelData = null, bool resetShakerValuesAfterShake = true, bool resetTargetValuesAfterShake = true, bool forwardDirection = true, TimescaleModes timescaleMode = TimescaleModes.Scaled, bool stop = false, bool restore = false, bool modifyCenter = false, Vector2 center = default, bool modifyExtra = false, Vector3 channelSplit = default, float dispersionStrength = 0f, float jitterIntensity = 0f, float maskRadius = 0.2f, float maskHardness = 0.2f) { OnEvent?.Invoke(source, intensityCurve, duration, remapMin, remapMax, relativeIntensity, feedbacksIntensity, channelData, resetShakerValuesAfterShake, resetTargetValuesAfterShake, forwardDirection, timescaleMode, stop, restore, modifyCenter, center, modifyExtra, channelSplit, dispersionStrength, jitterIntensity, maskRadius, maskHardness); } } /// /// Shaker: 控制 Advanced Chromatic Aberration /// [AddComponentMenu("More Mountains/Feedbacks/Shakers/PostProcessing/MM Advanced Chromatic Aberration Shaker")] [RequireComponent(typeof(Volume))] public class MMAdvancedChromaticAberrationShaker : MMShaker { [MMInspectorGroup("Intensity", true, 53)] [Tooltip("是否在现有 Intensity 基础上叠加")] public bool RelativeIntensity = false; [Tooltip("Intensity 的震动曲线")] public AnimationCurve ShakeIntensity = new AnimationCurve(new Keyframe(0, 0), new Keyframe(0.5f, 1), new Keyframe(1, 0)); [Tooltip("曲线 0 对应的值")] public float RemapIntensityZero = 0f; [Tooltip("曲线 1 对应的值")] public float RemapIntensityOne = 1f; protected Volume _volume; protected AdvancedChromaticAberration _aca; // 初始值存储 protected float _initialIntensity; protected Vector2 _initialCenter; protected Vector3 _initialSplit; protected float _initialDispersion; protected float _initialJitter; protected float _initialMaskRadius; protected float _initialMaskHardness; // 运行时目标值存储 protected bool _modifyCenter; protected Vector2 _targetCenter; protected bool _modifyExtra; protected Vector3 _targetSplit; protected float _targetDispersion; protected float _targetJitter; protected float _targetMaskRadius; protected float _targetMaskHardness; // Shaker 复位备份 protected float _originalShakeDuration; protected bool _originalRelativeIntensity; protected AnimationCurve _originalShakeIntensity; protected float _originalRemapIntensityZero; protected float _originalRemapIntensityOne; protected override void Initialization() { base.Initialization(); _volume = GetComponent(); if (!_volume.profile.TryGet(out _aca)) { Debug.LogWarning("MMAdvancedChromaticAberrationShaker : No AdvancedChromaticAberration found in Volume on " + name); } } protected override void GrabInitialValues() { if (_aca != null) { _initialIntensity = _aca.intensity.value; _initialCenter = _aca.center.value; _initialSplit = _aca.channelSplit.value; _initialDispersion = _aca.dispersionStrength.value; _initialJitter = _aca.jitterIntensity.value; _initialMaskRadius = _aca.maskRadius.value; _initialMaskHardness = _aca.maskHardness.value; } } protected override void Shake() { if (_aca == null) return; // 1. 驱动 Intensity (带有 Curve 动画) float newIntensity = ShakeFloat(ShakeIntensity, RemapIntensityZero, RemapIntensityOne, RelativeIntensity, _initialIntensity); _aca.intensity.value = newIntensity; // 2. 驱动 Center (如果启用,强制设为目标值) if (_modifyCenter) { _aca.center.value = _targetCenter; } // 3. 驱动 Extras (如果启用,强制设为目标值) if (_modifyExtra) { _aca.channelSplit.value = _targetSplit; _aca.dispersionStrength.value = _targetDispersion; _aca.jitterIntensity.value = _targetJitter; _aca.maskRadius.value = _targetMaskRadius; _aca.maskHardness.value = _targetMaskHardness; } } protected override void ResetTargetValues() { base.ResetTargetValues(); if (_aca != null) { _aca.intensity.value = _initialIntensity; _aca.center.value = _initialCenter; _aca.channelSplit.value = _initialSplit; _aca.dispersionStrength.value = _initialDispersion; _aca.jitterIntensity.value = _initialJitter; _aca.maskRadius.value = _initialMaskRadius; _aca.maskHardness.value = _initialMaskHardness; } } protected override void ResetShakerValues() { base.ResetShakerValues(); ShakeDuration = _originalShakeDuration; ShakeIntensity = _originalShakeIntensity; RemapIntensityZero = _originalRemapIntensityZero; RemapIntensityOne = _originalRemapIntensityOne; RelativeIntensity = _originalRelativeIntensity; _modifyCenter = false; _modifyExtra = false; } public virtual void OnEvent(MMF_Feedback source, AnimationCurve intensityCurve, float duration, float remapMin, float remapMax, bool relativeIntensity = false, float feedbacksIntensity = 1.0f, MMChannelData channelData = null, bool resetShakerValuesAfterShake = true, bool resetTargetValuesAfterShake = true, bool forwardDirection = true, TimescaleModes timescaleMode = TimescaleModes.Scaled, bool stop = false, bool restore = false, bool modifyCenter = false, Vector2 center = default, bool modifyExtra = false, Vector3 channelSplit = default, float dispersionStrength = 0f, float jitterIntensity = 0f, float maskRadius = 0.2f, float maskHardness = 0.2f) { if (!CheckEventAllowed(channelData)) return; if (stop) { Stop(); return; } if (restore) { ResetTargetValues(); return; } if (resetShakerValuesAfterShake) { _originalShakeDuration = ShakeDuration; _originalShakeIntensity = ShakeIntensity; _originalRemapIntensityZero = RemapIntensityZero; _originalRemapIntensityOne = RemapIntensityOne; _originalRelativeIntensity = RelativeIntensity; } if (!OnlyUseShakerValues) { TimescaleMode = timescaleMode; ShakeDuration = duration; ShakeIntensity = intensityCurve; RemapIntensityZero = remapMin * feedbacksIntensity; RemapIntensityOne = remapMax * feedbacksIntensity; RelativeIntensity = relativeIntensity; ForwardDirection = forwardDirection; // 接收额外参数 _modifyCenter = modifyCenter; _targetCenter = center; _modifyExtra = modifyExtra; _targetSplit = channelSplit; _targetDispersion = dispersionStrength; _targetJitter = jitterIntensity; _targetMaskRadius = maskRadius; _targetMaskHardness = maskHardness; } Play(source); } public override void StartListening() { base.StartListening(); MMAdvancedChromaticAberrationShakeEvent.Register(OnEvent); } public override void StopListening() { base.StopListening(); MMAdvancedChromaticAberrationShakeEvent.Unregister(OnEvent); } } }