Search Unity

Commandbuffer.Blit flips the image until switching to VR

Discussion in 'Image Effects' started by tspk91, Aug 21, 2018.

  1. tspk91

    tspk91

    Joined:
    Nov 19, 2014
    Posts:
    131
    Hi, I am using a commandbuffer for a simple grab of the camera buffer into a temp rendertexture that is used later in a material (a helmet visor). The problem is that the blit flips the Y coordinate of the uvs. I could correct for this by changing the helmet shader (1-uv.y), but when switching into VR it stops flipping the uvs, so I can't rely on a constant solution. After having switched to VR once, the uvs stop being flipped even after returning to screen mode, which is disconcerting. I am using single pass stereo (NOT instanced). Here is some of the code:

    Code (CSharp):
    1. RenderTextureDescriptor desc;
    2.                 if (VR.VRController.Instance.VREnabled) {
    3.                     desc = UnityEngine.XR.XRSettings.eyeTextureDesc;
    4.                 } else {
    5.                     desc = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.DefaultHDR);
    6.                 }
    7.                 desc.depthBufferBits = 0;
    8.                 holoRenderTex = RenderTexture.GetTemporary(desc);
    Code (CSharp):
    1.                 blitCommand = new CommandBuffer();
    2.                 blitCommand.name = "HUDBlit";
    3.                 blitCommand.Blit(BuiltinRenderTextureType.CurrentActive, holoRenderTex);
    4.                 holoCamera.AddCommandBuffer(CameraEvent.AfterEverything, blitCommand);
    5.                 glassMat.SetTexture("_HoloTex", holoRenderTex);
     
  2. tspk91

    tspk91

    Joined:
    Nov 19, 2014
    Posts:
    131
    I found a workaround: specifying a blit material using the hidden blit shader fixes the uv flip.

    Code (CSharp):
    1. blitMat = new Material(Shader.Find("Hidden/BlitCopy"));
    2. blitCommand.Blit(BuiltinRenderTextureType.CurrentActive, holoRenderTex, blitMat);
     
  3. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    I have been trying to duplicate your answer but with succese. When I use the 2 parameter version of blitCommand.Blit(src, dest) my result is upside down. If I use the 3 parameter version blitCommand.Blit(src, dest, material) my "_MainTex" is not populated with a texture. Any idea of why this could be the case?