summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/PostEffect/FastBloom.cs
blob: dc1fc7455ec675efebd43241308ee52f474c7f78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class FastBloom : PostEffectsBase
{
    public enum Resolution {
		Low = 0,
		High = 1,
	}

	public enum BlurType {
		Standard = 0,
		Sgx = 1,
	}

	[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)]
	public int blurIterations = 1;

	public BlurType blurType = BlurType.Standard;

	public Shader fastBloomShader;
	private Material fastBloomMaterial = null;
	
	new bool CheckResources ()
    {	
		CheckSupport (false);	
	
		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;

		var rtW = source.width/divider;
		var 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));

			// vertical blur
			RenderTexture rt2 = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
			rt2.filterMode = FilterMode.Bilinear;
			Graphics.Blit (rt, rt2, fastBloomMaterial, 2 + passOffs);
			RenderTexture.ReleaseTemporary (rt);
			rt = rt2;

			// 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);
	}	
}