Graphics.Blit() results is too dark?

Hello everyone!

As the title suggest, I’m having some trouble when it comes to use Graphics.Blit() correctly.

After searching for a while, the closest thread I’ve found reporting this problem could be this one (bug?) Render the screen using a color renders the wrong color? - Unity Engine - Unity Discussions , unfortunately, it didn’t helped me.

Here’s my issue in a simple script you can attach to a gameObject in the editor.

using UnityEngine;
using System.Collections;

public class test2 : MonoBehaviour {
    public Texture t;
    int width = 256;
    public Material testMat;

    void Start () {

        RenderTexture rt = new RenderTexture(width, width, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
        rt.enableRandomWrite = true;
        rt.Create();

        Graphics.Blit(t, rt );

        Texture2D t2D = new Texture2D(width, width, TextureFormat.ARGB32, false);
        RenderTexture.active = rt;

       
        t2D.ReadPixels(new Rect(0, 0, width, width), 0,0);     
        t2D.Apply();

        testMat.mainTexture = t2D;
}
}

(I know I don’t need to use rendertextures here, but I’ll need it in my final code, to apply some materials properties.)

I expect it to copy the given texture, and apply it to the given material. However, the copied texture is darker than the original.

I was using this kind of script in 4.5.x, and it worked pretty fine. Something changed? Am I missing something?

Thanks in advance for any help.

Edit : Using Unity 5.2.0p1

1 Like

Why are you using RenderTextureReadWrite.Linear ?

1 Like

It was an attempt at fixing it, I tried sRGB and Default without success. I saw in the linked thread it could lead to this issue.
And it did, actually, I just solved it.
The output texture should’ve been initialized by

Texture2D t2D = new Texture2D(width, width, TextureFormat.ARGB32 ,false,true);

I was so focused on the renderTexture parameters I forgot about the texture itself…

Thanks for your time!

2 Likes