forked from Wacky-Mole/WackysDatabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpriteTools.cs
199 lines (175 loc) · 5.63 KB
/
SpriteTools.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
using System.IO;
using System.Reflection;
using UnityEngine;
namespace wackydatabase
{
public class SpriteToolsCombined
{
private Sprite outcome;
private Texture2D texture;
private Texture2D rawTexture;
private Texture2D secondaryLayer;
private Sprite source;
private Color tint;
public float ratio = 0.5f;
public void setTint(Color tint)
{
this.tint = tint;
}
public void setSecondaryLayer(Texture2D secondaryLayer)
{
this.secondaryLayer = secondaryLayer;
}
public void setRatio(int ratio)
{
this.ratio = ((float)ratio / 100);
}
public void setRatio(float ratio)
{
this.ratio = ratio;
}
private byte[] ReadEmbeddedFileBytes(string name)
{
var myType = typeof(SpriteToolsCombined);
var n = myType.Namespace;
using MemoryStream stream = new();
Assembly.GetExecutingAssembly().GetManifestResourceStream(n + "." + name)?.CopyTo(stream);
return stream.ToArray();
}
public Texture2D loadTexture(string name)
{
Texture2D texture = new(64, 64);
texture.LoadImage(ReadEmbeddedFileBytes(name));
return texture;
}
public Texture2D CreateTexture2D(Texture2D rawTexture, bool useTint)
{
this.rawTexture = rawTexture;
makeTexture(rawTexture.width, rawTexture.height);
getTexturePixels();
if (useTint && tint != null)
{
BlendTexture();
}
return texture;
}
public Texture2D CreateTexture2D(Sprite source, bool useTint)
{
this.source = source;
makeTexture((int)source.rect.width, (int)source.rect.height);
getSpritePixels();
if (useTint && tint != null)
{
BlendTexture();
}
return texture;
}
private void MakeGrayscale()
{
for (int x = 0; x < texture.width; x++)
{
for (int y = 0; y < texture.height; y++)
{
Color pixel = texture.GetPixel(x, y);
if (pixel.a > 0)
{
var gray = pixel.grayscale;
Color grayish = new Color(gray, gray, gray, pixel.a);
texture.SetPixel(x, y, grayish);
}
}
}
texture.Apply();
}
public Sprite CreateSprite(Sprite source, bool useTint, bool grayscale = false)
{
this.source = source;
makeTexture((int)source.rect.width, (int)source.rect.height);
getSpritePixels();
if (grayscale)
{
MakeGrayscale();
}
if (useTint && tint != null)
{
BlendTexture();
}
MakeSprite();
return outcome;
}
public Sprite CreateSprite(Texture2D rawTexture, bool useTint, bool grayscale = false)
{
this.rawTexture = rawTexture;
makeTexture(rawTexture.width, rawTexture.height);
getTexturePixels();
if (grayscale)
{
MakeGrayscale();
}
if (useTint && tint != null)
{
BlendTexture();
}
MakeSprite();
return outcome;
}
public void makeTexture(int width, int height)
{
texture = new Texture2D(width, height);
}
private void getTexturePixels()
{
var pixels = rawTexture.GetPixels();
texture.SetPixels(pixels);
texture.Apply();
}
private void getSpritePixels()
{
var pixels = source.texture.GetPixels((int)source.textureRect.x,
(int)source.textureRect.y,
(int)source.textureRect.width,
(int)source.textureRect.height);
texture.SetPixels(pixels);
texture.Apply();
}
private void BlendTexture()
{
for (int x = 0; x < texture.width; x++)
{
for (int y = 0; y < texture.height; y++)
{
Color pixel = texture.GetPixel(x, y);
if (pixel.a > 0)
{
texture.SetPixel(x, y, Color.Lerp(pixel, tint, ratio));
if (secondaryLayer != null)
{
Color secPixel = secondaryLayer.GetPixel(x, y);
if (secPixel.a > 0)
{
texture.SetPixel(x, y, secPixel);
}
}
}
}
}
texture.Apply();
}
public Sprite convertTextureToSprite(Texture2D tex)
{
return Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
}
private void MakeSprite()
{
outcome = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
}
public Texture2D GetTexture2D()
{
return texture;
}
public Sprite getSprite()
{
return outcome;
}
}
}