From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- Client/Assets/Scripts/PostEffect/MobileBloom.cs | 118 ++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 Client/Assets/Scripts/PostEffect/MobileBloom.cs (limited to 'Client/Assets/Scripts/PostEffect/MobileBloom.cs') diff --git a/Client/Assets/Scripts/PostEffect/MobileBloom.cs b/Client/Assets/Scripts/PostEffect/MobileBloom.cs new file mode 100644 index 00000000..64681b87 --- /dev/null +++ b/Client/Assets/Scripts/PostEffect/MobileBloom.cs @@ -0,0 +1,118 @@ +using UnityEngine; +using System.Collections; + +[RequireComponent(typeof(Camera))] +public class MobileBloom : MobilePostEffectsBase +{ + public enum Resolution { + Low = 0, + High = 1, + } + + public enum BlurType { + Standard = 0, + Sgx = 1, + } + public enum BlurDir + { + vertical = 0, + horizontal = 1, + both = 2 + } + [Range(0.0f, 1.5f)] + public float threshhold = 0.25f; + + [Range(0.0f, 2.5f)] + public float intensity = 0.75f; + + [Range(0.25f, 5.5f)] + public float blurSize = 1.0f; + + Resolution resolution = Resolution.Low; + [Range(1, 4)] + private int blurIterations = 1; + + private BlurType blurType = BlurType.Sgx; + + public BlurDir blurDir = BlurDir.vertical; + + public Shader fastBloomShader; + private Material fastBloomMaterial = null; + + new bool CheckResources () + { + CheckSupport (false); + + if(fastBloomShader==null) + fastBloomShader = Shader.Find("Hidden/FastBloom"); + if (fastBloomMaterial==null) + fastBloomMaterial = CheckShaderAndCreateMaterial (fastBloomShader, fastBloomMaterial); + + //if(!isSupported) + // ReportAutoDisable (); + return isSupported; + } + + void OnDisable() + { + if (fastBloomMaterial) + DestroyImmediate(fastBloomMaterial); + } + + void OnRenderImage (RenderTexture source,RenderTexture destination) + { + if(CheckResources() == false) + { + //Graphics.Blit (source, destination); + return; + } + + int divider = resolution == Resolution.Low ? 4 : 2; + float widthMod = resolution == Resolution.Low ? 0.5f : 1.0f; + + fastBloomMaterial.SetVector("_Parameter", new Vector4(blurSize * widthMod, 0.0f, threshhold, intensity)); + source.filterMode = FilterMode.Bilinear; + + int rtW = source.width / divider; + int rtH = source.height / divider; + + // downsample + RenderTexture rt = RenderTexture.GetTemporary (rtW, rtH, 0, source.format); + rt.filterMode = FilterMode.Bilinear; + Graphics.Blit (source, rt, fastBloomMaterial, 1); + + int passOffs = blurType == BlurType.Standard ? 0 : 2; + + for(int i = 0; i < blurIterations; i++) + { + fastBloomMaterial.SetVector ("_Parameter", new Vector4 (blurSize * widthMod + (i*1.0f), 0.0f, threshhold, intensity)); + RenderTexture rt2 = null; + if (blurDir == BlurDir.vertical || blurDir == BlurDir.both) + { + // vertical blur + rt2 = RenderTexture.GetTemporary(rtW, rtH, 0, source.format); + rt2.filterMode = FilterMode.Bilinear; + Graphics.Blit(rt, rt2, fastBloomMaterial, 2 + passOffs); + RenderTexture.ReleaseTemporary(rt); + rt = rt2; + } + + if (blurDir == BlurDir.horizontal || blurDir == BlurDir.both) + { + // horizontal blur + rt2 = RenderTexture.GetTemporary(rtW, rtH, 0, source.format); + rt2.filterMode = FilterMode.Bilinear; + Graphics.Blit(rt, rt2, fastBloomMaterial, 3 + passOffs); + RenderTexture.ReleaseTemporary(rt); + rt = rt2; + } + + } + + fastBloomMaterial.SetTexture("_Bloom", rt); + + Graphics.Blit(source, destination, fastBloomMaterial, 0); + + RenderTexture.ReleaseTemporary(rt); + } +} \ No newline at end of file -- cgit v1.1-26-g67d0