I made a “texture flattening” function which will lay one texture on top of another, with color incorporated. Put in a list of textures, another list of textures to determine what colors get written over, and a list of colors, corresponding to the textures.
The reason I separated the alphaSource is because normal maps need to let underlying textures’ normal maps be shown. Some people keep their “transparency” layer on their main texture, and some keep it on the normal map. Whichever you use, use that list of textures for alphaSource.
public static Texture2D Flatten (List<Texture2D> combines, List<Texture2D> alphaSource, List<Color> colorSource) {
//The new texture.
Texture2D newTexture = new Texture2D(0,0,TextureFormat.ARGB32,true);
//Resize the new texture to match the largest texture.
foreach(Texture2D resizeTex in combines) {
if(resizeTex != null) {
if(resizeTex.width > newTexture.width) {
newTexture.Resize(resizeTex.width,newTexture.height);
}
if(resizeTex.height > newTexture.height) {
newTexture.Resize(newTexture.width,resizeTex.height);
}
}
}
//Resize each layer to match the new texture, and draw the layer onto the new texture.
int i = 0;
int l = combines.Count;
while(i < l) {
int xx = newTexture.width;
int yy = newTexture.height;
if(combines[i] != null alphaSource[i] != null) {
Texture2D addTex = new Texture2D(xx,yy,TextureFormat.ARGB32,true);
addTex.SetPixels(combines[i].GetPixels(0),0);
addTex.Resize(xx,yy,TextureFormat.ARGB32,true);
int y = 0;
while(y < yy) {
int x = 0;
while(x < xx) {
Color mainPixel = newTexture.GetPixel(x,y);
Color newPixel = addTex.GetPixel(x,y);
Color alphaPixel = alphaSource[i].GetPixel(x,y);
Color colorChange = colorSource[i];
mainPixel = Color.Lerp(mainPixel,newPixel*colorChange,alphaPixel.a);
newTexture.SetPixel(x,y,mainPixel);
++x;
}
++y;
}
}
++i;
}
newTexture.Apply();
return newTexture;
}
Addendum: This is good for keeping all your textures together, without having to rely on a specialized shader or extra polygons or what-have-you. Also, all textures used must be read/write enabled. Go to that texture’s import settings, set it to an Advanced image, and click that little read/write thing.