-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPixelatedCamera.cs
More file actions
84 lines (66 loc) · 2.51 KB
/
Copy pathPixelatedCamera.cs
File metadata and controls
84 lines (66 loc) · 2.51 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PixelatedCamera : MonoBehaviour
{
public enum PixelScreenMode { Resize, Scale }
[System.Serializable]
public struct ScreenSize
{
// Basically an integer Vector2 to store screen size information
public int width;
public int height;
}
public static PixelatedCamera main;
private Camera renderCamera;
private RenderTexture renderTexture;
private int screenWidth, screenHeight;
[Header("Screen scaling settings")]
public PixelScreenMode mode;
public ScreenSize targetScreenSize = new ScreenSize { width = 256, height = 144 }; // Only used with PixelScreenMode.Resize
public uint screenScaleFactor = 1; // Only used with PixelScreenMode.Scale
[Header("Display")]
public RawImage display;
private void Awake()
{
// Try to set as main pixel camera
if (main == null) main = this;
}
private void Start()
{
// Initialize the system
Init();
}
private void Update()
{
// Re initialize system if the screen has been resized
if (CheckScreenResize()) Init();
}
public void Init() {
// Initialize the camera and get screen size values
if (!renderCamera) renderCamera = GetComponent<Camera>();
screenWidth = Screen.width;
screenHeight = Screen.height;
// Prevent any error
if (screenScaleFactor < 1) screenScaleFactor = 1;
if (targetScreenSize.width < 1) targetScreenSize.width = 1;
if (targetScreenSize.height < 1) targetScreenSize.height = 1;
// Calculate the render texture size
int width = mode == PixelScreenMode.Resize ? (int)targetScreenSize.width : screenWidth / (int)screenScaleFactor;
int height = mode == PixelScreenMode.Resize ? (int)targetScreenSize.height : screenHeight / (int)screenScaleFactor;
// Initialize the render texture
renderTexture = new RenderTexture(width, height, 24) {
filterMode = FilterMode.Point,
antiAliasing = 1
};
// Set the render texture as the camera's output
renderCamera.targetTexture = renderTexture;
// Attaching texture to the display UI RawImage
display.texture = renderTexture;
}
public bool CheckScreenResize() {
// Check whether the screen has been resized
return Screen.width != screenWidth || Screen.height != screenHeight;
}
}