137 lines
3.5 KiB
C#
137 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BF
|
|
{
|
|
[RequireComponent(typeof(Camera))]
|
|
[ExecuteInEditMode]
|
|
[DisallowMultipleComponent]
|
|
public class PostEffectBehaviour : MonoBehaviour
|
|
{
|
|
PostEffectType effectType = PostEffectType.None;
|
|
public PostEffectType EffectType
|
|
{
|
|
get { return effectType; }
|
|
private set { effectType = value; SetDirty(); }
|
|
}
|
|
|
|
public PostEffectVividBloom VividBloomEffect { get; private set; }
|
|
public PostEffectFxaa FxaaEffect { get; private set; }
|
|
public PostEffectRadialBlur RadialBlurEffetc { get; private set; }
|
|
|
|
bool hasEffect;
|
|
bool inited;
|
|
bool support;
|
|
PostEffectContext context = new PostEffectContext();
|
|
List<AbsPostEffect> curActiveEffects = new List<AbsPostEffect>();
|
|
|
|
void Awake()
|
|
{
|
|
if (!CheckSupport())
|
|
{
|
|
enabled = false;
|
|
support = false;
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
support = true;
|
|
Init();
|
|
SetDirty();
|
|
}
|
|
}
|
|
|
|
public bool CheckSupport()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
if (!inited)
|
|
{
|
|
VividBloomEffect = new PostEffectVividBloom();
|
|
FxaaEffect = new PostEffectFxaa();
|
|
RadialBlurEffetc = new PostEffectRadialBlur();
|
|
inited = true;
|
|
}
|
|
}
|
|
|
|
public void SetPostEffectType(PostEffectType type)
|
|
{
|
|
EffectType = type;
|
|
}
|
|
|
|
public void OpenPostEffect(PostEffectType effectType)
|
|
{
|
|
EffectType |= effectType;
|
|
}
|
|
|
|
public void ClosePostEffect(PostEffectType effectType)
|
|
{
|
|
if ((this.effectType & effectType) != 0)
|
|
{
|
|
EffectType ^= effectType;
|
|
}
|
|
}
|
|
|
|
void SetDirty()
|
|
{
|
|
if (!support)
|
|
{
|
|
return;
|
|
}
|
|
if (!inited)
|
|
{
|
|
Init();
|
|
}
|
|
|
|
curActiveEffects.Clear();
|
|
hasEffect = enabled = EffectType != PostEffectType.None;
|
|
|
|
VividBloomEffect.active = (EffectType & PostEffectType.VividBloom) != 0;
|
|
if (VividBloomEffect.active)
|
|
{
|
|
curActiveEffects.Add(VividBloomEffect);
|
|
}
|
|
|
|
RadialBlurEffetc.active = (EffectType & PostEffectType.RadialBlur) != 0;
|
|
if (RadialBlurEffetc.active)
|
|
{
|
|
curActiveEffects.Add(RadialBlurEffetc);
|
|
}
|
|
|
|
|
|
FxaaEffect.active = (EffectType & PostEffectType.Fxaa) != 0;
|
|
if (FxaaEffect.active)
|
|
{
|
|
curActiveEffects.Add(FxaaEffect);
|
|
}
|
|
}
|
|
|
|
void OnRenderImage(RenderTexture src, RenderTexture dest)
|
|
{
|
|
if (hasEffect)
|
|
{
|
|
var count = curActiveEffects.Count;
|
|
context.src = src;
|
|
context.dest = dest;
|
|
for (var i = 0; i < count - 1; i++)
|
|
{
|
|
context = curActiveEffects[i].OnRenderImage(context);
|
|
Graphics.Blit(dest, src);
|
|
}
|
|
curActiveEffects[count - 1].OnRenderImage(context);
|
|
}
|
|
else
|
|
{
|
|
Graphics.Blit(src, dest);
|
|
}
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
}
|
|
}
|
|
}
|