[C#]Pasting texture on top of another?(Fast)

Hey
I need a fast solution for pasting a texture on top of another.
I have this code:

public static Texture2D CombineTextures(Texture2D aBaseTexture, Texture2D aToCopyTexture, bool DestroyOriginal)
    {
        int aWidth = aBaseTexture.width;
        int aHeight = aBaseTexture.height;
        Texture2D aReturnTexture = new Texture2D(aWidth, aHeight, TextureFormat.RGBA32, false);

        Color[] aBaseTexturePixels = aBaseTexture.GetPixels();
        Color[] aCopyTexturePixels = aToCopyTexture.GetPixels();
        Color[] aColorList = new Color[aBaseTexturePixels.Length];
        int aPixelLength = aBaseTexturePixels.Length;

        for(int p = 0; p < aPixelLength; p++)
        {
            aColorList[p] = Color.Lerp(aBaseTexturePixels[p], aCopyTexturePixels[p], aCopyTexturePixels[p].a);
        }

        aReturnTexture.SetPixels(aColorList);
        aReturnTexture.Apply(false);
        if (DestroyOriginal == true)
            Texture2D.Destroy (aBaseTexture);
        return aReturnTexture;
    }

But it’s way too slow.
Are there any faster solutions?

1 Like

Graphics.Blit() Unity - Scripting API: Graphics.Blit

Let me know if you need help

Your current method goes through and copies 1 pixel at a time on the CPU. Graphics.Blit() will render the texture all at once using the GPU.

But this pastes it into a RenderTexture
Also it pastes a Material to the RenderTexture, not a Texture2D
Is it possible to create a RenderTexture in realtime, applying the image with Graphics.Blit and then converting it to a Texture2D? And pasting a Texture2D instead of a Material?

A RenderTexture can be used as any other kind of texture.

You can make a new RenderTexture at runtime like this:
“RenderTexture newTexture = new RenderTexture(pixelWidth, pixelHeight, depthBits)”

I’m not sure what you mean. Graphics.Blit() takes in 2 textures, and optionally a material. Texture 1 will be copied onto texture 2. If you give it a material, it will copy the texture using that material, otherwise it will copy as normal.

You can also leave texture2 blank to render the texture to the screen.

1 Like

Oh, sorry, my bad. I misunderstood the function.
Thank you!

Alternatively, could you create a shader with two materials?

Im not a shader guy, but I assume its possible to do something like what you are after.

Guess it depends if you need a static image of the combined textures or not