[SOLVED] Graphics.Blit(RT, RT) in linear mode cause wrong colors

I’m working on a new demo for Unity and get some issues on Graphics.Blit(RT, RT) in linear mode.

  • Create a new project with Project Settings > Player > Color Space [Linear]
  • New Scene, create Cube, attach the following script.
  • Press Play, click on Inspector RT_MainSrc followed by RT_MainDst.

Graphics.Blit() change the colors, but it shouldn’t. Would be great if someone can confirm this.

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

   public RenderTexture RT_MainSrc;
   public RenderTexture RT_MainDst;

   // Use this for initialization
   void Start () {

     RT_MainSrc = new RenderTexture(256, 256, 0);
     RT_MainSrc.wrapMode = TextureWrapMode.Repeat;
     RT_MainSrc.generateMips = true;
     RT_MainSrc.Create();

     RT_MainDst = new RenderTexture(256, 256, 0);
     RT_MainDst.wrapMode = TextureWrapMode.Repeat;
     RT_MainDst.generateMips = true;
     RT_MainDst.Create();
   
     RenderTexture.active = RT_MainSrc;
     GL.Begin(GL.TRIANGLES);
     Vector4 col;
     col.x = 0.5f;
     col.y = 0.5f;
     col.z = 1f;
     col.w = 1f;
     GL.Clear(false, true, col);
     GL.End();
   

     Graphics.Blit(RT_MainSrc, RT_MainDst);
   }
}

You should probably use the longer constructor that takes an argument whether to use linear or sRGB sampling. See Unity - Scripting API: RenderTexture.RenderTexture

2 Likes

Thanks! The script now works with the additional changes.

using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {

   public RenderTexture RT_MainSrc;
   public RenderTexture RT_MainDst;

   // Use this for initialization
   void Start () {
     RT_MainSrc = new RenderTexture(256, 256, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
     RT_MainSrc.wrapMode = TextureWrapMode.Repeat;
     RT_MainSrc.generateMips = true;
     RT_MainSrc.Create();
     RT_MainDst = new RenderTexture(256, 256, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
     RT_MainDst.wrapMode = TextureWrapMode.Repeat;
     RT_MainDst.generateMips = true;
     RT_MainDst.Create();
 
     RenderTexture.active = RT_MainSrc;
     GL.Begin(GL.TRIANGLES);
     Vector4 col;
     col.x = 0.5f;
     col.y = 0.5f;
     col.z = 1f;
     col.w = 1f;
     GL.Clear(false, true, col);
     GL.End();
 
     Graphics.Blit(RT_MainSrc, RT_MainDst);
   }
}
1 Like