-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathPixelCamera.cs
448 lines (383 loc) · 12 KB
/
PixelCamera.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
using System;
using UnityEngine;
namespace SubjectNerd.Utilities
{
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class PixelCamera : MonoBehaviour
{
[Serializable]
protected class AdvancedSettings
{
[Tooltip("Material to draw output render with")]
public Material cameraMaterial;
[Tooltip("Stretches output display, for non square pixels")]
public Vector2 aspectStretch = Vector2.one;
[Tooltip("Scales down camera render size")]
public float downSample = 1;
[Tooltip("Z distance to draw as pixel perfect for perspective camera.")]
public float perspectiveZ = 10;
}
protected struct CamSettings
{
public int[] screenSize;
public Vector2 aspect;
public float zoomLevel;
public float pixelsPerUnit;
public float zDistance;
public float downsample;
public float fieldOfView;
public float farPlane;
public bool isOrtho;
public CamSettings(PixelCamera pixelCam, Camera cam)
{
screenSize = new[] {Screen.width, Screen.height};
this.aspect = pixelCam.AspectStretch;
this.zoomLevel = pixelCam.ZoomLevel;
this.pixelsPerUnit = pixelCam.pixelsPerUnit;
this.zDistance = pixelCam.PerspectiveZ;
this.downsample = pixelCam.DownSample;
this.fieldOfView = cam.fieldOfView;
this.isOrtho = cam.orthographic;
this.farPlane = cam.farClipPlane;
}
public bool Equals(CamSettings other)
{
bool equalScreen = other.screenSize[0] == screenSize[0] &&
other.screenSize[1] == screenSize[1];
bool equalAspect = other.aspect == aspect;
bool isEqual = other.isOrtho == isOrtho &&
equalScreen &&
equalAspect &&
Mathf.Approximately(other.zoomLevel, zoomLevel) &&
Mathf.Approximately(other.pixelsPerUnit, pixelsPerUnit) &&
Mathf.Approximately(other.downsample, downsample);
if (isEqual && isOrtho == false)
{
isEqual &= Mathf.Approximately(other.zDistance, zDistance) &&
Mathf.Approximately(other.fieldOfView, fieldOfView) &&
Mathf.Approximately(other.farPlane, farPlane);
}
return isEqual;
}
}
[SerializeField] protected Camera cam;
[SerializeField] protected float pixelsPerUnit = 100;
[SerializeField] protected float zoomLevel = 1f;
[Space]
[SerializeField] protected AdvancedSettings advancedSettings;
protected RenderTexture renderTexture;
protected GameObject falseCamGO;
protected Camera falseCam;
protected PixelCamDrawer camDraw;
protected Shader fallbackShader;
protected Material fallbackMaterial;
protected CamSettings lastSettings;
protected Vector2 quadOffset;
public Vector2 QuadMin { get; protected set; }
public Vector2 QuadMax { get; protected set; }
/// <summary>
/// Material to draw output render with
/// </summary>
public Material CameraMaterial
{
get
{
Material useMaterial = fallbackMaterial;
if (advancedSettings != null && advancedSettings.cameraMaterial != null)
useMaterial = advancedSettings.cameraMaterial;
return useMaterial;
}
set
{
if (advancedSettings.cameraMaterial != null)
advancedSettings.cameraMaterial.SetTexture("_MainTex", null);
advancedSettings.cameraMaterial = value;
if (advancedSettings.cameraMaterial == null) return;
if (renderTexture != null)
advancedSettings.cameraMaterial.SetTexture("_MainTex", renderTexture);
}
}
public float ZoomLevel
{
get { return zoomLevel; }
set { zoomLevel = value; }
}
public float PixelsPerUnit
{
get { return pixelsPerUnit; }
set { pixelsPerUnit = value; }
}
/// <summary>
/// For perspective cameras. Z distance between near and far plane to scale as pixel perfect.
/// </summary>
public float PerspectiveZ
{
get
{
if (advancedSettings == null)
return cam.farClipPlane*0.5f;
advancedSettings.perspectiveZ = Mathf.Clamp(advancedSettings.perspectiveZ, cam.nearClipPlane, cam.farClipPlane);
return advancedSettings.perspectiveZ;
}
set
{
if (advancedSettings == null)
return;
advancedSettings.perspectiveZ = Mathf.Clamp(value, cam.nearClipPlane, cam.farClipPlane); ;
}
}
/// <summary>
/// Stretches output display, for non square pixels
/// </summary>
public Vector2 AspectStretch
{
get
{
if (advancedSettings == null)
return Vector2.one;
return advancedSettings.aspectStretch;
}
set
{
if (advancedSettings == null)
return;
advancedSettings.aspectStretch = value;
}
}
/// <summary>
/// Scales down camera render size. Clamped at minimum value of 1.
/// </summary>
public float DownSample
{
get
{
if (advancedSettings == null)
return 1f;
advancedSettings.downSample = Mathf.Max(1f, advancedSettings.downSample);
return advancedSettings.downSample;
}
set
{
if (advancedSettings == null)
return;
advancedSettings.downSample = Mathf.Max(1f, value);
}
}
/// <summary>
/// The render texture camera is being drawn into
/// </summary>
public RenderTexture RenderTexture { get { return renderTexture; } }
/// <summary>
/// Pixel size of the camera
/// </summary>
public int[] CameraSize
{
get
{
if (renderTexture == null)
return new[] {0, 0};
return new[] {renderTexture.width, renderTexture.height};
}
}
private void Reset()
{
cam = GetComponent<Camera>();
float cameraPixelHeight = Mathf.FloorToInt(cam.aspect*2*pixelsPerUnit);
zoomLevel = Screen.height/cameraPixelHeight;
}
private void Start()
{
// Disable if we don't support image effects
if (!SystemInfo.supportsImageEffects)
enabled = false;
if (cam == null)
cam = GetComponent<Camera>();
if (cam == null)
enabled = false;
OnDisable(); // Force cleanup
if (enabled)
OnEnable();
}
protected virtual void OnEnable()
{
lastSettings = new CamSettings(this, cam)
{
screenSize = new []{0, 0}
};
ForceRefresh();
falseCamGO = new GameObject("False Camera") {hideFlags = HideFlags.HideAndDontSave};
falseCam = falseCamGO.AddComponent<Camera>();
falseCam.cullingMask = LayerMask.GetMask();
camDraw = falseCamGO.AddComponent<PixelCamDrawer>();
camDraw.SourceCamera = this;
fallbackShader = Shader.Find("Hidden/SubjectNerd/PixelCamFallback");
if (fallbackShader != null)
{
fallbackMaterial = new Material(fallbackShader)
{
hideFlags = HideFlags.DontSave
};
}
else
{
Debug.Log("Couldn't find fall back shader, material not created");
enabled = false;
}
}
protected virtual void OnDisable()
{
if (fallbackMaterial != null)
DestroyImmediate(fallbackMaterial);
fallbackShader = null;
cam.targetTexture = null;
cam.ResetAspect();
if (renderTexture != null)
DestroyImmediate(renderTexture);
if (falseCamGO != null)
DestroyImmediate(falseCamGO);
falseCam = null;
}
private void SetupCamera(CamSettings settings)
{
var aspect = settings.aspect;
zoomLevel = Mathf.Max(0.05f, Mathf.Abs(zoomLevel))*Math.Sign(zoomLevel);
// "Physical" pixel render size
Vector2 screenRenderSize = GetScreenRenderSize();
// Pixel render size
int[] pixelRenderSize = GetRenderTextureSize(screenRenderSize, settings.aspect);
float targetAspect = (float)pixelRenderSize[0] / (float)pixelRenderSize[1];
cam.aspect = targetAspect;
if (cam.orthographic)
{
// Orthographic camera needs to use screen size when calculating quad offset
screenRenderSize = new Vector2(Screen.width, Screen.height);
// Set the camera's size, according to pixel size
float targetHeight = pixelRenderSize[1];
// Use pixel density to convert to world units
targetHeight /= pixelsPerUnit;
targetHeight /= 2f;
// Change orthographic size so camera is sized to world unit
cam.orthographicSize = targetHeight;
}
// Find the settings to be used for drawing the GL quad
CalculateQuad(screenRenderSize, pixelRenderSize);
// Important to release current render texture
cam.targetTexture = null;
fallbackMaterial.SetTexture("_MainTex", null);
if (advancedSettings != null && advancedSettings.cameraMaterial != null)
advancedSettings.cameraMaterial.SetTexture("_MainTex", null);
if (renderTexture != null)
renderTexture.Release();
// Create new render texture
Vector2 renderSize = new Vector2(pixelRenderSize[0], pixelRenderSize[1]) / settings.downsample;
int[] actualRenderSize = GetRenderTextureSize(renderSize, Vector2.one);
renderTexture = new RenderTexture(actualRenderSize[0], actualRenderSize[1], 0)
{
useMipMap = true,
filterMode = FilterMode.Point,
wrapMode = TextureWrapMode.Clamp
};
// Make main camera render into it
cam.targetTexture = renderTexture;
// Set render texture as _MainTex on materials
fallbackMaterial.SetTexture("_MainTex", renderTexture);
if (advancedSettings != null)
CameraMaterial = advancedSettings.cameraMaterial;
lastSettings = settings;
cam.Render();
camDraw.DrawQuad();
}
private float GetPerspectiveHeight(float z)
{
var frustumHeight = 2.0f * z * Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
return frustumHeight;
}
protected Vector2 GetScreenRenderSize()
{
// For orthographic camera, physical render size is based on screen pixels
Vector2 screenRenderSize = new Vector2(Screen.width, Screen.height);
screenRenderSize /= zoomLevel;
// For perspective camera, physical render is based on world unit height
// in terms of fustrum distance, converted to pixels
if (cam.orthographic == false)
{
cam.aspect = (float) Screen.width / Screen.height;
float scale = Mathf.InverseLerp(cam.nearClipPlane, cam.farClipPlane, PerspectiveZ);
float maxHeight = GetPerspectiveHeight(cam.farClipPlane);
float minHeight = GetPerspectiveHeight(cam.nearClipPlane);
float height = Mathf.Lerp(minHeight, maxHeight, scale);
float width = height*cam.aspect;
screenRenderSize.x = width;
screenRenderSize.y = height;
screenRenderSize *= pixelsPerUnit;
}
return screenRenderSize;
}
/// <summary>
/// The integer width and height of the texture to render to
/// </summary>
/// <returns></returns>
protected int[] GetRenderTextureSize(Vector2 size, Vector2 aspect)
{
int width = Mathf.FloorToInt(Mathf.Abs(size.x / aspect.x));
int height = Mathf.FloorToInt(Mathf.Abs(size.y / aspect.y));
// Size is not integer, add padding
if (Math.Abs(size.x - width) > float.Epsilon)
width += 2;
if (Mathf.Abs(size.y - height) > float.Epsilon)
height += 2;
// Make sure this isn't an odd number
if (width%2 > 0)
width += 1;
if (height%2 > 0)
height += 1;
// Just in case
width = Mathf.Clamp(width, 2, 4096);
height = Mathf.Clamp(height, 2, 4096);
return new[] {width, height};
}
private void CalculateQuad(Vector2 screenRenderSize, int[] pixelRenderSize)
{
Vector2 pixelSize = new Vector2(pixelRenderSize[0], pixelRenderSize[1]) * zoomLevel;
quadOffset = pixelSize - screenRenderSize;
quadOffset /= 2;
quadOffset.x /= Screen.width;
quadOffset.y /= Screen.height;
Vector2 min = Vector2.zero - quadOffset;
Vector2 max = Vector2.one + quadOffset;
if (advancedSettings == null)
{
QuadMin = min;
QuadMax = max;
return;
}
Vector2 aspectStretch = advancedSettings.aspectStretch;
if (aspectStretch.x < float.Epsilon || aspectStretch.y < float.Epsilon)
return;
Vector2 center = (min + max) / 2;
min -= center;
max -= center;
min.x *= aspectStretch.x;
max.x *= aspectStretch.x;
min.y *= aspectStretch.y;
max.y *= aspectStretch.y;
min += center;
max += center;
QuadMin = min;
QuadMax = max;
}
public void ForceRefresh()
{
lastSettings.screenSize = new [] {0, 0};
}
public bool CheckCamera()
{
var currentSettings = new CamSettings(this, cam);
bool didChange = currentSettings.Equals(lastSettings) == false;
if (didChange)
SetupCamera(currentSettings);
return didChange;
}
}
}