Files
Cielonos/Assets/Scripts/MainGame/Effects/PostProcessingManager.cs

86 lines
3.0 KiB
C#
Raw Normal View History

2025-11-25 08:19:33 -05:00
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 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);
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);
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;
}
}
}