259 lines
9.5 KiB
C#
259 lines
9.5 KiB
C#
using UnityEngine;
|
|
|
|
namespace UnityStandardAssets.ImageEffects
|
|
{
|
|
[ExecuteInEditMode]
|
|
[RequireComponent(typeof(Camera))]
|
|
[AddComponentMenu("Image Effects/Bloom and Glow/BloomAndFlares (3.5, Deprecated)")]
|
|
public class BloomAndFlares : PostEffectsBase
|
|
{
|
|
public TweakMode34 tweakMode;
|
|
|
|
public BloomScreenBlendMode screenBlendMode = BloomScreenBlendMode.Add;
|
|
|
|
public HDRBloomMode hdr;
|
|
|
|
private bool doHdr;
|
|
|
|
public float sepBlurSpread = 1.5f;
|
|
|
|
public float useSrcAlphaAsMask = 0.5f;
|
|
|
|
public float bloomIntensity = 1f;
|
|
|
|
public float bloomThreshold = 0.5f;
|
|
|
|
public int bloomBlurIterations = 2;
|
|
|
|
public bool lensflares;
|
|
|
|
public int hollywoodFlareBlurIterations = 2;
|
|
|
|
public LensflareStyle34 lensflareMode = LensflareStyle34.Anamorphic;
|
|
|
|
public float hollyStretchWidth = 3.5f;
|
|
|
|
public float lensflareIntensity = 1f;
|
|
|
|
public float lensflareThreshold = 0.3f;
|
|
|
|
public Color flareColorA = new Color(0.4f, 0.4f, 0.8f, 0.75f);
|
|
|
|
public Color flareColorB = new Color(0.4f, 0.8f, 0.8f, 0.75f);
|
|
|
|
public Color flareColorC = new Color(0.8f, 0.4f, 0.8f, 0.75f);
|
|
|
|
public Color flareColorD = new Color(0.8f, 0.4f, 0f, 0.75f);
|
|
|
|
public Texture2D lensFlareVignetteMask;
|
|
|
|
public Shader lensFlareShader;
|
|
|
|
private Material lensFlareMaterial;
|
|
|
|
public Shader vignetteShader;
|
|
|
|
private Material vignetteMaterial;
|
|
|
|
public Shader separableBlurShader;
|
|
|
|
private Material separableBlurMaterial;
|
|
|
|
public Shader addBrightStuffOneOneShader;
|
|
|
|
private Material addBrightStuffBlendOneOneMaterial;
|
|
|
|
public Shader screenBlendShader;
|
|
|
|
private Material screenBlend;
|
|
|
|
public Shader hollywoodFlaresShader;
|
|
|
|
private Material hollywoodFlaresMaterial;
|
|
|
|
public Shader brightPassFilterShader;
|
|
|
|
private Material brightPassFilterMaterial;
|
|
|
|
public override bool CheckResources()
|
|
{
|
|
CheckSupport(false);
|
|
screenBlend = CheckShaderAndCreateMaterial(screenBlendShader, screenBlend);
|
|
lensFlareMaterial = CheckShaderAndCreateMaterial(lensFlareShader, lensFlareMaterial);
|
|
vignetteMaterial = CheckShaderAndCreateMaterial(vignetteShader, vignetteMaterial);
|
|
separableBlurMaterial = CheckShaderAndCreateMaterial(separableBlurShader, separableBlurMaterial);
|
|
addBrightStuffBlendOneOneMaterial = CheckShaderAndCreateMaterial(addBrightStuffOneOneShader, addBrightStuffBlendOneOneMaterial);
|
|
hollywoodFlaresMaterial = CheckShaderAndCreateMaterial(hollywoodFlaresShader, hollywoodFlaresMaterial);
|
|
brightPassFilterMaterial = CheckShaderAndCreateMaterial(brightPassFilterShader, brightPassFilterMaterial);
|
|
if (!isSupported)
|
|
{
|
|
ReportAutoDisable();
|
|
}
|
|
return isSupported;
|
|
}
|
|
|
|
private void OnRenderImage(RenderTexture source, RenderTexture destination)
|
|
{
|
|
if (!CheckResources())
|
|
{
|
|
Graphics.Blit(source, destination);
|
|
return;
|
|
}
|
|
doHdr = false;
|
|
if (hdr == HDRBloomMode.Auto)
|
|
{
|
|
doHdr = source.format == RenderTextureFormat.ARGBHalf && GetComponent<Camera>().hdr;
|
|
}
|
|
else
|
|
{
|
|
doHdr = hdr == HDRBloomMode.On;
|
|
}
|
|
doHdr = doHdr && supportHDRTextures;
|
|
BloomScreenBlendMode pass = screenBlendMode;
|
|
if (doHdr)
|
|
{
|
|
pass = BloomScreenBlendMode.Add;
|
|
}
|
|
RenderTextureFormat format = ((!doHdr) ? RenderTextureFormat.Default : RenderTextureFormat.ARGBHalf);
|
|
RenderTexture temporary = RenderTexture.GetTemporary(source.width / 2, source.height / 2, 0, format);
|
|
RenderTexture temporary2 = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, format);
|
|
RenderTexture temporary3 = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, format);
|
|
RenderTexture temporary4 = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, format);
|
|
float num = 1f * (float)source.width / (1f * (float)source.height);
|
|
float num2 = 0.001953125f;
|
|
Graphics.Blit(source, temporary, screenBlend, 2);
|
|
Graphics.Blit(temporary, temporary2, screenBlend, 2);
|
|
RenderTexture.ReleaseTemporary(temporary);
|
|
BrightFilter(bloomThreshold, useSrcAlphaAsMask, temporary2, temporary3);
|
|
temporary2.DiscardContents();
|
|
if (bloomBlurIterations < 1)
|
|
{
|
|
bloomBlurIterations = 1;
|
|
}
|
|
for (int i = 0; i < bloomBlurIterations; i++)
|
|
{
|
|
float num3 = (1f + (float)i * 0.5f) * sepBlurSpread;
|
|
separableBlurMaterial.SetVector("offsets", new Vector4(0f, num3 * num2, 0f, 0f));
|
|
RenderTexture renderTexture = ((i != 0) ? temporary2 : temporary3);
|
|
Graphics.Blit(renderTexture, temporary4, separableBlurMaterial);
|
|
renderTexture.DiscardContents();
|
|
separableBlurMaterial.SetVector("offsets", new Vector4(num3 / num * num2, 0f, 0f, 0f));
|
|
Graphics.Blit(temporary4, temporary2, separableBlurMaterial);
|
|
temporary4.DiscardContents();
|
|
}
|
|
if (lensflares)
|
|
{
|
|
if (lensflareMode == LensflareStyle34.Ghosting)
|
|
{
|
|
BrightFilter(lensflareThreshold, 0f, temporary2, temporary4);
|
|
temporary2.DiscardContents();
|
|
Vignette(0.975f, temporary4, temporary3);
|
|
temporary4.DiscardContents();
|
|
BlendFlares(temporary3, temporary2);
|
|
temporary3.DiscardContents();
|
|
}
|
|
else
|
|
{
|
|
hollywoodFlaresMaterial.SetVector("_threshold", new Vector4(lensflareThreshold, 1f / (1f - lensflareThreshold), 0f, 0f));
|
|
hollywoodFlaresMaterial.SetVector("tintColor", new Vector4(flareColorA.r, flareColorA.g, flareColorA.b, flareColorA.a) * flareColorA.a * lensflareIntensity);
|
|
Graphics.Blit(temporary4, temporary3, hollywoodFlaresMaterial, 2);
|
|
temporary4.DiscardContents();
|
|
Graphics.Blit(temporary3, temporary4, hollywoodFlaresMaterial, 3);
|
|
temporary3.DiscardContents();
|
|
hollywoodFlaresMaterial.SetVector("offsets", new Vector4(sepBlurSpread * 1f / num * num2, 0f, 0f, 0f));
|
|
hollywoodFlaresMaterial.SetFloat("stretchWidth", hollyStretchWidth);
|
|
Graphics.Blit(temporary4, temporary3, hollywoodFlaresMaterial, 1);
|
|
temporary4.DiscardContents();
|
|
hollywoodFlaresMaterial.SetFloat("stretchWidth", hollyStretchWidth * 2f);
|
|
Graphics.Blit(temporary3, temporary4, hollywoodFlaresMaterial, 1);
|
|
temporary3.DiscardContents();
|
|
hollywoodFlaresMaterial.SetFloat("stretchWidth", hollyStretchWidth * 4f);
|
|
Graphics.Blit(temporary4, temporary3, hollywoodFlaresMaterial, 1);
|
|
temporary4.DiscardContents();
|
|
if (lensflareMode == LensflareStyle34.Anamorphic)
|
|
{
|
|
for (int j = 0; j < hollywoodFlareBlurIterations; j++)
|
|
{
|
|
separableBlurMaterial.SetVector("offsets", new Vector4(hollyStretchWidth * 2f / num * num2, 0f, 0f, 0f));
|
|
Graphics.Blit(temporary3, temporary4, separableBlurMaterial);
|
|
temporary3.DiscardContents();
|
|
separableBlurMaterial.SetVector("offsets", new Vector4(hollyStretchWidth * 2f / num * num2, 0f, 0f, 0f));
|
|
Graphics.Blit(temporary4, temporary3, separableBlurMaterial);
|
|
temporary4.DiscardContents();
|
|
}
|
|
AddTo(1f, temporary3, temporary2);
|
|
temporary3.DiscardContents();
|
|
}
|
|
else
|
|
{
|
|
for (int k = 0; k < hollywoodFlareBlurIterations; k++)
|
|
{
|
|
separableBlurMaterial.SetVector("offsets", new Vector4(hollyStretchWidth * 2f / num * num2, 0f, 0f, 0f));
|
|
Graphics.Blit(temporary3, temporary4, separableBlurMaterial);
|
|
temporary3.DiscardContents();
|
|
separableBlurMaterial.SetVector("offsets", new Vector4(hollyStretchWidth * 2f / num * num2, 0f, 0f, 0f));
|
|
Graphics.Blit(temporary4, temporary3, separableBlurMaterial);
|
|
temporary4.DiscardContents();
|
|
}
|
|
Vignette(1f, temporary3, temporary4);
|
|
temporary3.DiscardContents();
|
|
BlendFlares(temporary4, temporary3);
|
|
temporary4.DiscardContents();
|
|
AddTo(1f, temporary3, temporary2);
|
|
temporary3.DiscardContents();
|
|
}
|
|
}
|
|
}
|
|
screenBlend.SetFloat("_Intensity", bloomIntensity);
|
|
screenBlend.SetTexture("_ColorBuffer", source);
|
|
Graphics.Blit(temporary2, destination, screenBlend, (int)pass);
|
|
RenderTexture.ReleaseTemporary(temporary2);
|
|
RenderTexture.ReleaseTemporary(temporary3);
|
|
RenderTexture.ReleaseTemporary(temporary4);
|
|
}
|
|
|
|
private void AddTo(float intensity_, RenderTexture from, RenderTexture to)
|
|
{
|
|
addBrightStuffBlendOneOneMaterial.SetFloat("_Intensity", intensity_);
|
|
Graphics.Blit(from, to, addBrightStuffBlendOneOneMaterial);
|
|
}
|
|
|
|
private void BlendFlares(RenderTexture from, RenderTexture to)
|
|
{
|
|
lensFlareMaterial.SetVector("colorA", new Vector4(flareColorA.r, flareColorA.g, flareColorA.b, flareColorA.a) * lensflareIntensity);
|
|
lensFlareMaterial.SetVector("colorB", new Vector4(flareColorB.r, flareColorB.g, flareColorB.b, flareColorB.a) * lensflareIntensity);
|
|
lensFlareMaterial.SetVector("colorC", new Vector4(flareColorC.r, flareColorC.g, flareColorC.b, flareColorC.a) * lensflareIntensity);
|
|
lensFlareMaterial.SetVector("colorD", new Vector4(flareColorD.r, flareColorD.g, flareColorD.b, flareColorD.a) * lensflareIntensity);
|
|
Graphics.Blit(from, to, lensFlareMaterial);
|
|
}
|
|
|
|
private void BrightFilter(float thresh, float useAlphaAsMask, RenderTexture from, RenderTexture to)
|
|
{
|
|
if (doHdr)
|
|
{
|
|
brightPassFilterMaterial.SetVector("threshold", new Vector4(thresh, 1f, 0f, 0f));
|
|
}
|
|
else
|
|
{
|
|
brightPassFilterMaterial.SetVector("threshold", new Vector4(thresh, 1f / (1f - thresh), 0f, 0f));
|
|
}
|
|
brightPassFilterMaterial.SetFloat("useSrcAlphaAsMask", useAlphaAsMask);
|
|
Graphics.Blit(from, to, brightPassFilterMaterial);
|
|
}
|
|
|
|
private void Vignette(float amount, RenderTexture from, RenderTexture to)
|
|
{
|
|
if ((bool)lensFlareVignetteMask)
|
|
{
|
|
screenBlend.SetTexture("_ColorBuffer", lensFlareVignetteMask);
|
|
Graphics.Blit(from, to, screenBlend, 3);
|
|
}
|
|
else
|
|
{
|
|
vignetteMaterial.SetFloat("vignetteIntensity", amount);
|
|
Graphics.Blit(from, to, vignetteMaterial);
|
|
}
|
|
}
|
|
}
|
|
}
|