ManiaMap.Unity
Procedural generation of metroidvania style maps for Unity.
TextureUtility.cs
1using System;
2using System.Runtime.CompilerServices;
3using UnityEngine;
4
6{
10 public static class TextureUtility
11 {
18 [MethodImpl(MethodImplOptions.AggressiveInlining)]
19 private static int Index(int row, int column, int columns)
20 {
21 return row * columns + column;
22 }
23
28 public static void FillBorder(Texture2D texture)
29 {
30 if (!TextureIsNullOrEmpty(texture) && texture.width > 2 && texture.height > 2)
31 {
32 var width = texture.width;
33 var height = texture.height;
34 var pixels = texture.GetRawTextureData<Color32>();
35
36 for (int i = 1; i < height - 1; i++)
37 {
38 pixels[Index(i, width - 1, width)] = pixels[Index(i, width - 2, width)];
39 pixels[Index(i, 0, width)] = pixels[Index(i, 1, width)];
40 }
41
42 for (int j = 0; j < width; j++)
43 {
44 pixels[Index(height - 1, j, width)] = pixels[Index(height - 2, j, width)];
45 pixels[Index(0, j, width)] = pixels[Index(1, j, width)];
46 }
47 }
48 }
49
55 public static void Fill(Texture2D texture, Color32 color)
56 {
57 if (!TextureIsNullOrEmpty(texture))
58 {
59 var pixels = texture.GetRawTextureData<Color32>();
60
61 for (int i = 0; i < pixels.Length; i++)
62 {
63 pixels[i] = color;
64 }
65 }
66 }
67
74 public static void Fill(Texture2D texture, Color32 color, RectInt area)
75 {
76 if (!TextureIsNullOrEmpty(texture))
77 {
78 var pixels = texture.GetRawTextureData<Color32>();
79 var textureWidth = texture.width;
80
81 for (int i = 0; i < area.height; i++)
82 {
83 var row = i + area.y;
84
85 for (int j = 0; j < area.width; j++)
86 {
87 var column = j + area.x;
88 pixels[Index(row, column, textureWidth)] = color;
89 }
90 }
91 }
92 }
93
99 public static void CompositeFill(Texture2D texture, Color color)
100 {
101 if (!TextureIsNullOrEmpty(texture))
102 {
103 var pixels = texture.GetRawTextureData<Color32>();
104
105 for (int i = 0; i < pixels.Length; i++)
106 {
107 pixels[i] = ColorUtility.CompositeColors(color, pixels[i]);
108 }
109 }
110 }
111
118 public static void CompositeFill(Texture2D texture, Color color, RectInt area)
119 {
120 if (!TextureIsNullOrEmpty(texture))
121 {
122 var pixels = texture.GetRawTextureData<Color32>();
123 var textureWidth = texture.width;
124
125 for (int i = 0; i < area.height; i++)
126 {
127 var row = i + area.y;
128
129 for (int j = 0; j < area.width; j++)
130 {
131 var column = j + area.x;
132 var index = Index(row, column, textureWidth);
133 pixels[index] = ColorUtility.CompositeColors(color, pixels[index]);
134 }
135 }
136 }
137 }
138
144 public static void TileImage(Texture2D texture, Texture2D brush)
145 {
146 if (!TextureIsNullOrEmpty(texture) && !TextureIsNullOrEmpty(brush))
147 {
148 var row = 0;
149 var brushPixels = brush.GetRawTextureData<Color32>();
150 var pixels = texture.GetRawTextureData<Color32>();
151 var textureHeight = texture.height;
152 var textureWidth = texture.width;
153 var brushHeight = brush.height;
154 var brushWidth = brush.width;
155
156 for (int i = 0; i < textureHeight; i++)
157 {
158 var column = 0;
159
160 for (int j = 0; j < textureWidth; j++)
161 {
162 var index = Index(i, j, textureWidth);
163 var color = brushPixels[Index(row, column, brushWidth)];
164 pixels[index] = ColorUtility.CompositeColors(color, pixels[index]);
165
166 if (++column >= brushWidth)
167 column = 0;
168 }
169
170 if (++row >= brushHeight)
171 row = 0;
172 }
173 }
174 }
175
182 public static void DrawImage(Texture2D texture, Texture2D brush, Vector2Int point)
183 {
184 if (!TextureIsNullOrEmpty(texture) && !TextureIsNullOrEmpty(brush))
185 {
186 var pixels = texture.GetRawTextureData<Color32>();
187 var brushPixels = brush.GetRawTextureData<Color32>();
188 var textureWidth = texture.width;
189 var brushWidth = brush.width;
190 var brushHeight = brush.height;
191
192 for (int i = 0; i < brushHeight; i++)
193 {
194 var row = i + point.y;
195
196 for (int j = 0; j < brushWidth; j++)
197 {
198 var column = j + point.x;
199 var index = Index(row, column, textureWidth);
200 var color = brushPixels[Index(i, j, brushWidth)];
201 pixels[index] = ColorUtility.CompositeColors(color, pixels[index]);
202 }
203 }
204 }
205 }
206
213 public static byte[] EncodeToBytes(Texture2D texture, string extension)
214 {
215 switch (extension.TrimStart('.').ToLower())
216 {
217 case "png":
218 return texture.EncodeToPNG();
219 case "jpg":
220 case "jpeg":
221 return texture.EncodeToJPG();
222 default:
223 throw new ArgumentException($"Unhandled file extension: {extension}");
224 }
225 }
226
231 public static bool TextureIsNullOrEmpty(Texture2D texture)
232 {
233 return texture == null || texture.width == 0 || texture.height == 0;
234 }
235 }
236}
Contains methods for manipulating colors.
Definition: ColorUtility.cs:14
static Color CompositeColors(Color colorA, Color colorB)
Calculates the composite of top color A onto bottom color B.
Definition: ColorUtility.cs:38
Contains methods for manipulating textures.
static int Index(int row, int column, int columns)
Returns the flat array index corresponding to the row-column index.
static void Fill(Texture2D texture, Color32 color)
Fills the texture with the specified color.
static void TileImage(Texture2D texture, Texture2D brush)
Tiles the image across the texture.
static void FillBorder(Texture2D texture)
Fills the 1 pixel border around the texture with the colors at a 1 pixel inset.
static void CompositeFill(Texture2D texture, Color color, RectInt area)
Composites the color onto the pixels in the specified texture area.
static bool TextureIsNullOrEmpty(Texture2D texture)
Returns true if the texture is null or an empty array.
static byte[] EncodeToBytes(Texture2D texture, string extension)
Returns the image format bytes for the texture corresponding to the file extension (....
static void Fill(Texture2D texture, Color32 color, RectInt area)
Fills the specified texture area with the specified color.
static void DrawImage(Texture2D texture, Texture2D brush, Vector2Int point)
Draws the brush texture at the specified point.
static void CompositeFill(Texture2D texture, Color color)
Composites the color onto all pixels of the specified texture.