GraphicsUtils.cs
4.16 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
namespace UnityEngine.PostProcessing
{
using UnityObject = Object;
public static class GraphicsUtils
{
public static bool isLinearColorSpace
{
get { return QualitySettings.activeColorSpace == ColorSpace.Linear; }
}
public static bool supportsDX11
{
#if UNITY_WEBGL
get { return false; }
#else
get { return SystemInfo.graphicsShaderLevel >= 50 && SystemInfo.supportsComputeShaders; }
#endif
}
static Texture2D s_WhiteTexture;
public static Texture2D whiteTexture
{
get
{
if (s_WhiteTexture != null)
return s_WhiteTexture;
s_WhiteTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
s_WhiteTexture.SetPixel(0, 0, new Color(1f, 1f, 1f, 1f));
s_WhiteTexture.Apply();
return s_WhiteTexture;
}
}
static Mesh s_Quad;
public static Mesh quad
{
get
{
if (s_Quad != null)
return s_Quad;
var vertices = new[]
{
new Vector3(-1f, -1f, 0f),
new Vector3( 1f, 1f, 0f),
new Vector3( 1f, -1f, 0f),
new Vector3(-1f, 1f, 0f)
};
var uvs = new[]
{
new Vector2(0f, 0f),
new Vector2(1f, 1f),
new Vector2(1f, 0f),
new Vector2(0f, 1f)
};
var indices = new[] { 0, 1, 2, 1, 0, 3 };
s_Quad = new Mesh
{
vertices = vertices,
uv = uvs,
triangles = indices
};
s_Quad.RecalculateNormals();
s_Quad.RecalculateBounds();
return s_Quad;
}
}
// Useful when rendering to MRT
public static void Blit(Material material, int pass)
{
GL.PushMatrix();
{
GL.LoadOrtho();
material.SetPass(pass);
GL.Begin(GL.TRIANGLE_STRIP);
{
GL.TexCoord2(0f, 0f); GL.Vertex3(0f, 0f, 0.1f);
GL.TexCoord2(1f, 0f); GL.Vertex3(1f, 0f, 0.1f);
GL.TexCoord2(0f, 1f); GL.Vertex3(0f, 1f, 0.1f);
GL.TexCoord2(1f, 1f); GL.Vertex3(1f, 1f, 0.1f);
}
GL.End();
}
GL.PopMatrix();
}
public static void ClearAndBlit(Texture source, RenderTexture destination, Material material, int pass, bool clearColor = true, bool clearDepth = false)
{
var oldRT = RenderTexture.active;
RenderTexture.active = destination;
GL.Clear(false, clearColor, Color.clear);
GL.PushMatrix();
{
GL.LoadOrtho();
material.SetTexture("_MainTex", source);
material.SetPass(pass);
GL.Begin(GL.TRIANGLE_STRIP);
{
GL.TexCoord2(0f, 0f); GL.Vertex3(0f, 0f, 0.1f);
GL.TexCoord2(1f, 0f); GL.Vertex3(1f, 0f, 0.1f);
GL.TexCoord2(0f, 1f); GL.Vertex3(0f, 1f, 0.1f);
GL.TexCoord2(1f, 1f); GL.Vertex3(1f, 1f, 0.1f);
}
GL.End();
}
GL.PopMatrix();
RenderTexture.active = oldRT;
}
public static void Destroy(UnityObject obj)
{
if (obj != null)
{
#if UNITY_EDITOR
if (Application.isPlaying)
UnityObject.Destroy(obj);
else
UnityObject.DestroyImmediate(obj);
#else
UnityObject.Destroy(obj);
#endif
}
}
public static void Dispose()
{
Destroy(s_Quad);
}
}
}