-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSMAA.cs
102 lines (83 loc) · 2.75 KB
/
SMAA.cs
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
99
100
101
102
using UnityEngine;
using System.Collections;
public class SMAA : MonoBehaviour
{
public bool ApplyEffect;
public int State = 1;
public int Passes = 1;
private Texture2D black;
private Shader shader;
private Material mat;
/// <summary>
///
/// </summary>
void Start()
{
shader = Shader.Find("Custom/SMAAshader");
mat = new Material(shader);
black = new Texture2D(1,1);
black.SetPixel(0,0,new Color(0,0,0,0));
black.Apply();
//create texture generator
GameObject obj = new GameObject();
obj.name = "TextureGenerator";
obj.AddComponent<AreaTexture>();
obj.AddComponent<SearchTexture>();
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(black, destination);
Vector4 metrics = new Vector4(1 / (float)Screen.width, 1 / (float)Screen.height, Screen.width, Screen.height);
if (this.ApplyEffect)
{
if (State == 1)
{
Graphics.Blit(source, destination, mat, 0);
}
else if (State == 2)
{
mat.SetTexture("areaTex", GameObject.Find("TextureGenerator").GetComponent<AreaTexture>().alphaTex);
mat.SetTexture("luminTex", GameObject.Find("TextureGenerator").GetComponent<AreaTexture>().luminTex);
mat.SetTexture("searchTex", GameObject.Find("TextureGenerator").GetComponent<SearchTexture>().alphaTex);
mat.SetVector("SMAA_RT_METRICS", metrics);
var rt = RenderTexture.GetTemporary(Screen.width, Screen.height, 0);
Graphics.Blit(source, rt, mat, 0);
Graphics.Blit(rt, destination, mat, 1);
RenderTexture.ReleaseTemporary(rt);
}
else if (State == 3)
{
mat.SetTexture("areaTex", GameObject.Find("TextureGenerator").GetComponent<AreaTexture>().alphaTex);
mat.SetTexture("luminTex", GameObject.Find("TextureGenerator").GetComponent<AreaTexture>().luminTex);
mat.SetTexture("searchTex", GameObject.Find("TextureGenerator").GetComponent<SearchTexture>().alphaTex);
mat.SetTexture("_SrcTex", source);
mat.SetVector("SMAA_RT_METRICS", metrics);
var rt = RenderTexture.GetTemporary(Screen.width, Screen.height, 0);
var rt2 = RenderTexture.GetTemporary(Screen.width, Screen.height, 0);
var rt3 = RenderTexture.GetTemporary(Screen.width, Screen.height, 0);
Graphics.Blit(source, rt3);
for (var i = 0; i < Passes; i++)
{
Graphics.Blit(black, rt);
Graphics.Blit(black, rt2);
Graphics.Blit(rt3, rt, mat, 0);
Graphics.Blit(rt, rt2, mat, 1);
Graphics.Blit(rt2, rt3, mat, 2);
}
Graphics.Blit(rt3, destination);
RenderTexture.ReleaseTemporary(rt);
RenderTexture.ReleaseTemporary(rt2);
RenderTexture.ReleaseTemporary(rt3);
}
}
else
{
Graphics.Blit(source, destination);
}
}
}