-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTexturePacker.cs
More file actions
200 lines (186 loc) · 7.86 KB
/
Copy pathTexturePacker.cs
File metadata and controls
200 lines (186 loc) · 7.86 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
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
using System.Collections.Generic;
using UnityEngine;
namespace UnityTextureRgbPacker
{
public class TexturePacker
{
public static UnityEngine.Texture2D GetCompositeTextureRgb(
string compositeTextureName,
Texture2D textureRedChannel,
Texture2D textureGreenChannel,
Texture2D textureBlueChannel,
Texture2D textureAlphaChannel = null,
int width = 0,
int height = 0)
{
if (!textureRedChannel && !textureGreenChannel && !textureBlueChannel && !textureAlphaChannel)
{
throw new System.Exception("At least one texture input must be valid.");
}
// If the texture input was provided, add it to a list
var validTextureList = new List<Texture2D>();
if (textureRedChannel)
{
validTextureList.Add(textureRedChannel);
}
if (textureGreenChannel)
{
validTextureList.Add(textureGreenChannel);
}
if (textureBlueChannel)
{
validTextureList.Add(textureBlueChannel);
}
var hasAlphaInput = false;
if (textureAlphaChannel)
{
validTextureList.Add(textureAlphaChannel);
hasAlphaInput = true;
}
// Determine the smallest texture size or use the input if one is provided
int resizeWidth;
int resizeHeight;
if (width == 0 || height == 0)
{
var resizeTargetTexture = TextureUtilities.GetSmallestSizedTexture(validTextureList);
resizeWidth = resizeTargetTexture.width;
resizeHeight = resizeTargetTexture.height;
}
else
{
resizeWidth = width;
resizeHeight = height;
}
// Create "empty" black textures for the ones that are missing
if (!textureRedChannel)
{
textureRedChannel = CreateTextureOfColor(resizeWidth, resizeHeight, Color.black);
}
if (!textureGreenChannel)
{
textureGreenChannel = CreateTextureOfColor(resizeWidth, resizeHeight, Color.black);
}
if (!textureBlueChannel)
{
textureBlueChannel = CreateTextureOfColor(resizeWidth, resizeHeight, Color.black);
}
if (!textureAlphaChannel)
{
textureAlphaChannel = CreateTextureOfColor(resizeWidth, resizeHeight, Color.black);
}
// Obtaining the texture sizes
var textureRedChannelWidth = textureRedChannel.width;
var textureRedChannelHeight = textureRedChannel.height;
var textureGreenChannelWidth = textureGreenChannel.width;
var textureGreenChannelHeight = textureGreenChannel.height;
var textureBlueChannelWidth = textureBlueChannel.width;
var textureBlueChannelHeight = textureBlueChannel.height;
var textureAlphaChannelWidth = textureAlphaChannel.width;
var textureAlphaChannelHeight = textureAlphaChannel.height;
// Resize all the textures to the needed size
textureRedChannel = TextureUtilities.GetRwTextureCopy(textureRedChannel);
if (resizeWidth != textureRedChannelWidth && resizeHeight != textureRedChannelHeight)
{
textureRedChannel = TextureUtilities.ScaleTexture(
textureRedChannel, resizeWidth, resizeHeight);
}
textureGreenChannel = TextureUtilities.GetRwTextureCopy(textureGreenChannel);
if (resizeWidth != textureGreenChannelWidth && resizeHeight != textureGreenChannelHeight)
{
textureGreenChannel = TextureUtilities.ScaleTexture(
textureGreenChannel, resizeWidth, resizeHeight);
}
textureBlueChannel = TextureUtilities.GetRwTextureCopy(textureBlueChannel);
if (resizeWidth != textureBlueChannelWidth && resizeHeight != textureBlueChannelHeight)
{
textureBlueChannel = TextureUtilities.ScaleTexture(
textureBlueChannel, resizeWidth, resizeHeight);
}
textureAlphaChannel = TextureUtilities.GetRwTextureCopy(textureAlphaChannel);
if (resizeWidth != textureAlphaChannelWidth && resizeHeight != textureAlphaChannelHeight)
{
textureAlphaChannel = TextureUtilities.ScaleTexture(
textureAlphaChannel, resizeWidth, resizeHeight);
}
// If there was an alpha input, use it. If not, create a texture without it.
if (hasAlphaInput)
{
var packedTextureWithAlpha = CreatePackedTexture(
resizeWidth, resizeHeight,
compositeTextureName,
textureRedChannel,
textureGreenChannel,
textureBlueChannel,
textureAlphaChannel);
return packedTextureWithAlpha;
}
var packedTextureWithoutAlpha = CreatePackedTexture(
resizeWidth, resizeHeight,
compositeTextureName,
textureRedChannel,
textureGreenChannel,
textureBlueChannel,
null);
return packedTextureWithoutAlpha;
}
private static UnityEngine.Texture2D CreatePackedTexture(
int width, int height,
string compositeTextureName,
Texture2D textureRedChannel,
Texture2D textureGreenChannel,
Texture2D textureBlueChannel,
Texture2D textureAlphaChannel = null)
{
var packedTexture = new Texture2D(width, height, TextureFormat.RGB24, false);
packedTexture.name = compositeTextureName;
if (textureAlphaChannel)
{
packedTexture = new Texture2D(width, height, TextureFormat.RGBA32, false);
for (var i = 0; i < width; i++)
{
for (var j = 0; j < height; j++)
{
packedTexture.SetPixel(
i, j,
new Color(
textureRedChannel.GetPixel(i, j).grayscale,
textureGreenChannel.GetPixel(i, j).grayscale,
textureBlueChannel.GetPixel(i, j).grayscale,
textureAlphaChannel.GetPixel(i, j).grayscale)
);
}
}
}
else
{
for (var i = 0; i < width; i++)
{
for (var j = 0; j < height; j++)
{
packedTexture.SetPixel(
i, j,
new Color(
textureRedChannel.GetPixel(i, j).grayscale,
textureGreenChannel.GetPixel(i, j).grayscale,
textureBlueChannel.GetPixel(i, j).grayscale));
}
}
}
packedTexture.Apply();
return packedTexture;
}
private static UnityEngine.Texture2D CreateTextureOfColor(int width, int height, Color color)
{
var texture = new Texture2D(width, height);
for (var i = 0; i < width; i++)
{
for (var j = 0; j < height; j++)
{
texture.SetPixel(i, j, color);
}
}
texture.Apply();
return texture;
}
}
}