-
Notifications
You must be signed in to change notification settings - Fork 324
/
UserInterfaceRenderTarget.cs
245 lines (197 loc) · 6.99 KB
/
UserInterfaceRenderTarget.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors:
//
// Notes:
//
using UnityEngine;
namespace DaggerfallWorkshop.Game.UserInterface
{
/// <summary>
/// Manages the render target texture for UI systems and provides helpers for drawing UI components.
/// </summary>
public class UserInterfaceRenderTarget : MonoBehaviour
{
#region Fields
public UnityEngine.UI.RawImage OutputImage;
public bool EnableCustomStack = false;
int customWidth = 0;
int customHeight = 0;
Panel parentPanel;
int createCount = 0;
RenderTexture targetTexture = null;
Vector2 targetSize = new Vector2();
Color clearColor = Color.clear;
FilterMode filterMode;
#endregion
#region Properties
/// <summary>
/// Gets current render target texture.
/// </summary>
public RenderTexture TargetTexture
{
get { return targetTexture; }
}
/// <summary>
/// Gets size of target texture.
/// </summary>
public Vector2 TargetSize
{
get { return targetSize; }
}
/// <summary>
/// Gets or sets a custom width for target texture.
/// </summary>
public int CustomWidth
{
get { return customWidth; }
set { customWidth = value; }
}
/// <summary>
/// Gets or sets a custom height for target texture.
/// </summary>
public int CustomHeight
{
get { return customHeight; }
set { customHeight = value; }
}
/// <summary>
/// Gets custom parent panel for adding custom own UI controls.
/// This will be set to custom width/height dimensions.
/// </summary>
public Panel ParentPanel
{
get { return parentPanel; }
}
/// <summary>
/// Gets or sets custom clear colour.
/// </summary>
public Color ClearColor
{
get { return clearColor; }
set { clearColor = value; }
}
/// <summary>
/// Gets or sets render texture filter mode.
/// </summary>
public FilterMode FilterMode
{
get { return filterMode; }
set { filterMode = value; }
}
#endregion
#region Unity
private void Awake()
{
parentPanel = new Panel();
filterMode = DaggerfallUI.Instance.GlobalFilterMode;
}
private void Start()
{
CheckTargetTexture();
}
private void Update()
{
CheckTargetTexture();
// Update parent panel
parentPanel.Update();
}
private void OnGUI()
{
GUI.depth = 0;
if (Event.current.type != EventType.Repaint || !EnableCustomStack)
return;
RenderTexture oldRt = RenderTexture.active;
RenderTexture.active = targetTexture;
if (IsReady())
{
// Clear UI
Clear(clearColor);
// Draw parent panel
parentPanel.Draw();
}
RenderTexture.active = oldRt;
}
#endregion
#region Drawing Methods
public void Clear()
{
GL.Clear(true, true, Color.clear);
}
public void Clear(Color color)
{
GL.Clear(true, true, color);
}
public void DrawTexture(Rect position, Texture2D image)
{
if (!IsReady())
return;
DaggerfallUI.DrawTexture(position, image);
}
public void DrawTexture(Rect position, Texture2D image, ScaleMode scaleMode, bool alphaBlend = true, float imageAspect = 0)
{
if (!IsReady())
return;
DaggerfallUI.DrawTexture(position, image, scaleMode, alphaBlend);
}
public void DrawTextureWithTexCoords(Rect position, Texture image, Rect texCoords, bool alphaBlend = true)
{
if (!IsReady())
return;
DaggerfallUI.DrawTextureWithTexCoords(position, image, texCoords, alphaBlend);
}
#endregion
#region Render Texture Management
// Check render texture is non-null and created
bool IsReady()
{
return (targetTexture != null && targetTexture.IsCreated());
}
// Check render texture and recreate if not valid
void CheckTargetTexture()
{
// Use either screen or custom dimensions
int width = (customWidth == 0) ? Screen.width : customWidth;
int height = (customHeight == 0) ? Screen.height : customHeight;
targetSize = new Vector2(width, height);
// Set custom panel root size and scale
// Unity likes to scale drawing to render texture by current screen dimensions
// This is a hack to use panel scaling to compensate
float scaleX = (float)Screen.width / (float)CustomWidth;
float scaleY = (float)Screen.height / (float)CustomHeight;
parentPanel.RootSize = targetSize;
parentPanel.Scale = new Vector2(scaleX, scaleY);
parentPanel.AutoSize = AutoSizeModes.None;
// Just return same texture if still valid
if (!IsReady() || targetTexture.width != width || targetTexture.height != height)
{
// Create target texture matching screen dimensions
targetTexture = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32);
targetTexture.filterMode = filterMode;
targetTexture.name = string.Format("DaggerfallUI RenderTexture {0}", createCount++);
targetTexture.Create();
Debug.LogFormat("Created UI RenderTexture with dimensions {0}, {1}", width, height);
// Update output image to new render texture if one is set
if (OutputImage)
OutputImage.texture = targetTexture;
// Raise event to notify other systems target texture has changed
RaiseOnCreateTargetTexture();
}
}
#endregion
#region Events
// OnCreateTargetTexture
public delegate void OnCreateTargetTextureEventHandler();
public event OnCreateTargetTextureEventHandler OnCreateTargetTexture;
protected virtual void RaiseOnCreateTargetTexture()
{
if (OnCreateTargetTexture != null)
OnCreateTargetTexture();
}
#endregion
}
}