Hi All,
I’ve seen several posts on combining textures and have some code that works although it does not work when I try to place a texture at a specific location on the background texture. What I’m trying to achieve is shown in the following image where the green rectangle (layer1) is centered on top of the black rectangle (layer0) and results in the image shown. What I get when I run this code is the green rectangle positioned against the left wall of the black rectangle. Code is below and any help would be very much appreciated.
public Texture2D MergeTextures(Texture2D background, Texture2D layer1)
{
int startX = 0;
int startY = 0;
Texture2D newTex = new Texture2D(background.width, background.height, background.format, false);
for (int x = 0; x < background.width; x++)
{
for (int y = 0; y < background.height; y++)
{
if (x >= startX && y >= startY && x < layer1.width && y < layer1.height)
{
Color bgColor = background.GetPixel(x, y);
Color wmColor = layer1.GetPixel(x - startX, y - startY);
Color final_color = Color.Lerp(bgColor, wmColor, wmColor.a / 1.0f);
newTex.SetPixel(x, y, final_color);
}
else
newTex.SetPixel(x, y, background.GetPixel(x, y));
}
}
newTex.Apply();
return newTex;
}