82 lines
2.6 KiB
C#
82 lines
2.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace BF
|
|
{
|
|
/// <summary>
|
|
/// bloom effect
|
|
/// </summary>
|
|
public class PostEffectBloom : AbsPostEffect
|
|
{
|
|
public static int BlurSizeID = Shader.PropertyToID("_BlurSize");
|
|
public static int BloomID = Shader.PropertyToID("_Bloom");
|
|
|
|
Material bloomMaterial;
|
|
|
|
[Range(1, 8)]
|
|
int iterations = 1;
|
|
[Range(1, 16)]
|
|
int downSample = 8;
|
|
[Range(0.0f, 2.0f)]
|
|
float luminanceThreshold = 0.6f;
|
|
|
|
public int Iterations { get { return iterations; } }
|
|
public int DownSample { get { return downSample; } }
|
|
public float LuminanceThreshold { get { return luminanceThreshold; } }
|
|
|
|
public void SetIteration(int iterations)
|
|
{
|
|
this.iterations = iterations;
|
|
}
|
|
|
|
public void SetDownSample(int downSample)
|
|
{
|
|
this.downSample = downSample;
|
|
}
|
|
|
|
public void SetLuminance(float luminanceThreshold)
|
|
{
|
|
this.luminanceThreshold = luminanceThreshold;
|
|
}
|
|
|
|
public PostEffectBloom()
|
|
{
|
|
bloomMaterial = BFMain.Instance.RenderMgr.BloomMat;
|
|
}
|
|
|
|
public override PostEffectContext OnRenderImage(PostEffectContext context)
|
|
{
|
|
var src = context.src;
|
|
var dest = context.dest;
|
|
|
|
bloomMaterial.SetFloat(LuminanceThresholdID, luminanceThreshold);
|
|
var width = src.width / downSample;
|
|
var height = src.height / downSample;
|
|
var buffer0 = RenderTexture.GetTemporary(width, height, 0);
|
|
buffer0.filterMode = FilterMode.Bilinear;
|
|
Graphics.Blit(src, buffer0, bloomMaterial, 0);
|
|
|
|
for (int i = 0; i < iterations; i++)
|
|
{
|
|
bloomMaterial.SetFloat(BlurSizeID, 1.0f + i);
|
|
var buffer1 = RenderTexture.GetTemporary(width, height, 0);
|
|
Graphics.Blit(buffer0, buffer1, bloomMaterial, 1);
|
|
buffer0.DiscardContents();
|
|
RenderTexture.ReleaseTemporary(buffer0);
|
|
buffer0 = buffer1;
|
|
buffer1 = RenderTexture.GetTemporary(width, height, 0);
|
|
Graphics.Blit(buffer0, buffer1, bloomMaterial, 2);
|
|
buffer0.DiscardContents();
|
|
RenderTexture.ReleaseTemporary(buffer0);
|
|
buffer0 = buffer1;
|
|
}
|
|
|
|
bloomMaterial.SetTexture(BloomID, buffer0);
|
|
Graphics.Blit(src, dest, bloomMaterial, 3);
|
|
buffer0.DiscardContents();
|
|
RenderTexture.ReleaseTemporary(buffer0);
|
|
|
|
return context;
|
|
}
|
|
}
|
|
} |