Search Unity

Bug UI Camera overriding shader effects

Discussion in 'General Graphics' started by unity_AE93C15A5FC65F14E331, May 15, 2023.

  1. unity_AE93C15A5FC65F14E331

    unity_AE93C15A5FC65F14E331

    Joined:
    Mar 7, 2023
    Posts:
    5
    EDIT: Solved it by using Graphics.Blit with OnRenderImage... For some reason I thought Graphics.Blit was deprecated but it looks like I was wrong. Still don't understand why the following code doesn't work and would appreciate insight


    I have two cameras, "vrCamera" and "UI_Camera"

    I'm using a CommandBuffer.Blit() with the vrCamera to apply shaders. It works fine:
    upload_2023-5-15_15-40-45.png

    As soon as I turn on UI_Camera, it undoes the shader:
    upload_2023-5-15_15-41-16.png

    The UI_Camera is not overriding the vr_camera, I know this because the UI_Camera is pointed at 0,0,0 and the horizon would be in the center of the screen. It's still displaying the vrCamera view, just without the shader.

    The only work around I've been able to find is to render the vrCamera to a render texture and then display that as a raw image on the UI. This is problematic as it removes the stereoscopic rendering.

    Has anyone else dealt with this? The following is my code to activate shaders:
    Code (CSharp):
    1.  public void ActivateShaders(List<int> activePositions)
    2.         {
    3.             if (commandBuffer != null)
    4.             {
    5.                 commandBuffer.ReleaseTemporaryRT(tempRT);
    6.                 Camera.main.RemoveCommandBuffer(CameraEvent.AfterEverything, commandBuffer);
    7.             }
    8.  
    9.             Debug.Log("Activate shaders by position: " + activePositions.ToArray().ToCommaSeparatedString());
    10.             this.activePositions = activePositions;
    11.             activeNames.Clear();
    12.             currentActiveNames.Clear();
    13.             currentActivePositions.Clear();
    14.          
    15.             // Create a new command buffer
    16.             commandBuffer = new CommandBuffer();
    17.  
    18.                 // Create a temporary render texture to hold the intermediate results
    19.                 tempRT = Shader.PropertyToID("_TempRT");
    20.                 commandBuffer.GetTemporaryRT(tempRT, Screen.width, Screen.height, 0, FilterMode.Bilinear);
    21.  
    22.                 // Copy the source render target to the temporary render texture
    23.                 commandBuffer.Blit(BuiltinRenderTextureType.CurrentActive, tempRT);
    24.  
    25.                 // Apply each shader in turn
    26.                     foreach (int currShader in activePositions)
    27.                     {
    28.                         currentActivePositions.Add(currShader);
    29.                         activeNames.Add(shaderMaterials[currShader].name);
    30.                         currentActiveNames.Add(shaderMaterials[currShader].name);
    31.                         Material shaderMaterial = shaderMaterials[currShader];
    32.                         // Set the shader material properties here if necessary
    33.  
    34.                         // Apply the shader
    35.                         commandBuffer.Blit(tempRT, BuiltinRenderTextureType.CurrentActive, shaderMaterial);
    36.          
    37.                         // Copy the result back to the temporary render texture for the next shader
    38.                         commandBuffer.Blit(BuiltinRenderTextureType.CurrentActive, tempRT);
    39.                     }
    40.  
    41.                 // Copy the final result back to the source render target
    42.                 commandBuffer.Blit(tempRT, BuiltinRenderTextureType.CurrentActive);
    43.              
    44.                 // Add the command buffer to the camera
    45.                 sxrSettings.Instance.vrCamera.AddCommandBuffer(CameraEvent.AfterEverything, commandBuffer);
    46.             }

    I am 100% certain it's the UI_Camera (and specifically its camera component) as turning it off/on is the only thing that causes the shader to stop working
     
    Last edited: May 16, 2023