/
UPAImage.cs
226 lines (166 loc) · 5.56 KB
/
UPAImage.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
//-----------------------------------------------------------------
// This class stores all information about the image.
// It has a full pixel map, width & height properties and some private project data.
// It also hosts functions for calculating how the pixels should be visualized in the editor.
//-----------------------------------------------------------------
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
[System.Serializable]
public class UPAImage : ScriptableObject {
// HELPER GETTERS
private Rect window {
get { return UPAEditorWindow.window.position; }
}
// IMAGE DATA
public int width;
public int height;
public List<UPALayer> layers;
public int layerCount
{
get { return layers.Count; }
}
public Texture2D finalImg;
// VIEW & NAVIGATION SETTINGS
[SerializeField]
private float _gridSpacing = 20f;
public float gridSpacing {
get { return _gridSpacing + 1f; }
set { _gridSpacing = Mathf.Clamp (value, 0, 140f); }
}
public float gridOffsetY = 0;
public float gridOffsetX = 0;
//Make sure we always get a valid layer
private int _selectedLayer = 0;
public int selectedLayer {
get {
return Mathf.Clamp(_selectedLayer, 0, layerCount);
}
set { _selectedLayer = value; }
}
// PAINTING SETTINGS
public Color selectedColor = new Color (1,0,0,1);
public UPATool tool = UPATool.PaintBrush;
public int gridBGIndex = 0;
//MISC VARIABLES
public bool dirty = false; // Used for determining if data has changed
// Class constructor
public UPAImage () {
// do nothing so far
}
// This is not called in constructor to have more control
public void Init (int w, int h) {
width = w;
height = h;
layers = new List<UPALayer>();
UPALayer newLayer = new UPALayer (this);
layers.Add ( newLayer );
EditorUtility.SetDirty (this);
dirty = true;
}
// Color a certain pixel by position in window in a certain layer
public void SetPixelByPos (Color color, Vector2 pos, int layer) {
Vector2 pixelCoordinate = GetPixelCoordinate (pos);
if (pixelCoordinate == new Vector2 (-1, -1))
return;
Undo.RecordObject (layers[layer].tex, "ColorPixel");
layers[layer].SetPixel ((int)pixelCoordinate.x, (int)pixelCoordinate.y, color);
EditorUtility.SetDirty (this);
dirty = true;
}
// Return a certain pixel by position in window
public Color GetPixelByPos (Vector2 pos, int layer) {
Vector2 pixelCoordinate = GetPixelCoordinate (pos);
if (pixelCoordinate == new Vector2 (-1, -1)) {
return Color.clear;
} else {
return layers[layer].GetPixel ((int)pixelCoordinate.x, (int)pixelCoordinate.y);
}
}
public Color GetBlendedPixel (int x, int y) {
Color color = Color.clear;
for (int i = 0; i < layers.Count; i++) {
if (!layers[i].enabled)
continue;
Color pixel = layers[i].tex.GetPixel(x,y);
// This is a blend between two methods of calculating color blending; Alpha blending and premultiplied alpha blending
// I have no clue why this actually works but it's very accurate :D
float newR = Mathf.Lerp (1f * pixel.r + (1f - pixel.a) * color.r, pixel.a * pixel.r + (1f - pixel.a) * color.r, color.a);
float newG = Mathf.Lerp (1f * pixel.g + (1f - pixel.a) * color.g, pixel.a * pixel.g + (1f - pixel.a) * color.g, color.a);
float newB = Mathf.Lerp (1f * pixel.b + (1f - pixel.a) * color.b, pixel.a * pixel.b + (1f - pixel.a) * color.b, color.a);
float newA = pixel.a + color.a * (1 - pixel.a);
color = new Color (newR, newG, newB, newA);
}
return color;
}
public void ChangeLayerPosition (int from, int to) {
if (from >= layers.Count || to >= layers.Count || from < 0 || to < 0) {
Debug.LogError ("Cannot ChangeLayerPosition, out of range.");
return;
}
UPALayer layer = layers[from];
layers.RemoveAt(from);
layers.Insert(to, layer);
dirty = true;
}
// Get the rect of the image as displayed in the editor
public Rect GetImgRect () {
float ratio = (float)height / (float)width;
float w = gridSpacing * 30;
float h = ratio * gridSpacing * 30;
float xPos = window.width / 2f - w/2f + gridOffsetX;
float yPos = window.height / 2f - h/2f + 20 + gridOffsetY;
return new Rect (xPos,yPos, w, h);
}
public Vector2 GetPixelCoordinate (Vector2 pos) {
Rect texPos = GetImgRect();
if (!texPos.Contains (pos)) {
return new Vector2(-1f,-1f);
}
float relX = (pos.x - texPos.x) / texPos.width;
float relY = (texPos.y - pos.y) / texPos.height;
int pixelX = (int)( width * relX );
int pixelY = (int)( height * relY ) - 1;
return new Vector2(pixelX, pixelY);
}
public Vector2 GetReadablePixelCoordinate (Vector2 pos) {
Vector2 coord = GetPixelCoordinate (pos);
if (coord.x == -1)
return coord;
coord.x += 1;
coord.y *= -1;
return coord;
}
public Texture2D GetFinalImage (bool update) {
if (!dirty && finalImg != null || !update && finalImg != null)
return finalImg;
finalImg = UPADrawer.CalculateBlendedTex(layers);
finalImg.filterMode = FilterMode.Point;
finalImg.Apply();
dirty = false;
return finalImg;
}
public void LoadAllTexsFromMaps () {
for (int i = 0; i < layers.Count; i++) {
if (layers[i].tex == null)
layers[i].LoadTexFromMap();
}
}
public void AddLayer () {
Undo.RecordObject (this, "AddLayer");
EditorUtility.SetDirty (this);
this.dirty = true;
UPALayer newLayer = new UPALayer (this);
layers.Add(newLayer);
}
public void RemoveLayerAt (int index) {
Undo.RecordObject (this, "RemoveLayer");
EditorUtility.SetDirty (this);
this.dirty = true;
layers.RemoveAt (index);
if (selectedLayer == index) {
selectedLayer = index - 1;
}
}
}