Files
Cielonos/Assets/Scripts/MainGame/Effects/PostProcessingManager.cs
SoulliesOfficial f7af60351b 阶段性完成
2025-12-08 05:27:53 -05:00

89 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using Cielonos.MainGame;
using Cielonos.MainGame.Effects;
using NBShader;
using SLSFramework.General;
using UnityEngine;
using UnityEngine.Rendering;
namespace Cielonos
{
public partial class PostProcessingManager : Singleton<PostProcessingManager>
{
public PostProcessingController postProcessingController;
public RadialBlurSubmodule radialBlurSm;
public SpeedLinesSubmodule speedLinesSm;
public ChromaticAberrationSubmodule chromaticAberrationSm;
public RGBSplitGlitchSubmodule rgbSplitGlitchSm;
public TonemappingSubmodule tonemappingSm;
[Tooltip("主要的后处理 Volume")]
[SerializeField]
private Volume volume;
private VolumeProfile profile;
private Dictionary<Type, VolumeComponent> componentCache;
protected override void Awake()
{
base.Awake();
radialBlurSm = new RadialBlurSubmodule(this);
speedLinesSm = new SpeedLinesSubmodule(this);
chromaticAberrationSm = new ChromaticAberrationSubmodule(this);
rgbSplitGlitchSm = new RGBSplitGlitchSubmodule(this);
tonemappingSm = new TonemappingSubmodule(this);
if (volume != null)
{
profile = volume.profile;
componentCache = new Dictionary<Type, VolumeComponent>();
}
else
{
Debug.LogError("PostProcessingManager: 未分配 Volume 组件。请在 Inspector 中设置。");
}
}
private void Update()
{
radialBlurSm.Update(MainGameManager.PlayerTimeScale);
speedLinesSm.Update(MainGameManager.PlayerTimeScale);
rgbSplitGlitchSm.Update(MainGameManager.PlayerTimeScale);
chromaticAberrationSm.Update(MainGameManager.PlayerTimeScale);
tonemappingSm.Update(MainGameManager.PlayerTimeScale);
}
/// <summary>
/// 从 Volume Profile 中安全地获取一个后处理组件(如 SpeedLines, RadialBlur
/// </summary>
/// <typeparam name="T">要获取的组件类型 (必须继承自 VolumeComponent)</typeparam>
/// <param name="component">获取到的组件实例</param>
/// <returns>如果成功找到,返回 true</returns>
public bool GetVolumeComponent<T>(out T component) where T : VolumeComponent
{
Type type = typeof(T);
// 1. 尝试从缓存中获取
if (componentCache.TryGetValue(type, out var cachedComponent))
{
component = (T)cachedComponent;
return component != null;
}
// 2. 如果缓存中没有,从 Profile 中获取
if (profile.TryGet(out T foundComponent))
{
componentCache[type] = foundComponent; // 存入缓存
component = foundComponent;
return true;
}
// 3. 未找到
Debug.LogWarning($"PostProcessManager: 在 Volume Profile 中未找到类型为 {type.Name} 的组件。");
component = null;
return false;
}
}
}