Trying to combine 2 black and white images so the white parts overlay. I’ve tried just simply adding the pixel colors together which SHOULD keep the black parts black (0+0=0) and the white parts white (0+1=1) but for some reason the background turns into a light gray?
(first pic is 1/2 images being combined, second is the result, I couldn’t fit all 3 images, but both of them have black backgrounds)
public Texture2D CombineTex(List<Texture2D> input)
int width = input.width;
int height = input.height;
Texture2D newTex = new Texture2D(width, height, TextureFormat.RGB24, false);
foreach (Texture2D tex in input)
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
Color c = newTex.GetPixel(x, y) + tex.GetPixel(x, y);
newTex.SetPixel(x, y, c);
newTex.name += tex.name;
byte bytes = newTex.EncodeToPNG();
File.WriteAllBytes("Assets/Textures/CatTex/WhiteMarks/" + newTex.name + ".png", bytes);
Well, there are a gazillion ways how to combine two textures. Just adding them is possible, but could result in some strange results depending on how the result is treated. Keep in mind that adding full brightness to another full brightness would result in double that value so with normalized colors a 1 and a 1 results in a 2 which probably would simply clip back to 1. If that’s what you want, great, that’s what your code does. However there may be an issue if your color also have an alpha channel as this will be combined the same way. By looking at your provided image, the top image seems to have an actual background color of black (0,0,0) and the features are actually white (1,1,1). However your resulting image has an 80% white background.
I can see several possible issues here. First of all you should clip all your rgba values and keep them in the range of 0 to 1, especially the alpha value. Second, Unity always was a bit dodgy when iit comes to gamma correction with png files. So we don’t know what colorspace your target image lives in. You should check that. I’ve once created those generic PNGTools to read and disect a png file into it’s “chunks” in order to inspect them or manipulate them. I originally created them for this answer but it was about the dpi size of the image (specifically the pHYs chunk). Though I used them in this question which also was about color issues due to the gamma information stored in the png file.
When creating a Texture2D object in Unity we can specify if we want a linear color space or the gamma color space. So if your original image (which seems to be your target image) uses a certain color space, that would be the resulting colorspace as well. You may want to create a completely new Texture as the target texture so you can specify the color space. AFAIK there’s no way to change the colorspace of an existing Texture2D.
nvm I’m dumb. I forgot to set the new texture to be equal to the first texture to add together. I guess the “texture base color” was light gray or something so even if I added onto it the bg was gray ^^;