Search Unity

Question Issues with Camera.Render() and Post Processing

Discussion in 'Universal Render Pipeline' started by aveakrause, May 4, 2023.

  1. aveakrause

    aveakrause

    Joined:
    Oct 3, 2018
    Posts:
    70
    I'm working on a tool that captures an image to a render texture for everything on a specific layer. The background must be transparent, which I set with clear flags using Color.clear. The render texture is then converted into a sprite and used in the game or saved out to a png. The issue I'm having is that I can't seem to get post processing to apply to the result of the Camera.Render() call. I get all the opaques and transparents just fine. I've flipped every lever I can think of in the player settings, on the renderers, on the render texture, and on the camera. I tried ensuring the post processing was on the same layer. I've tried manually adding a Volume to the camera's object and copying the settings from the existing global volume. I've tried creating a new Volume manually with the post processing I want. I want it to render out the full camera stack, as if it was doing a screenshot, but only a certain layer. So if there's a bloom pass, it should get done, if there's tone mapping applied, it should be applied, etc.

    My post processing does work in my scene just fine for normal gameplay. Using the screenshot tools, the post processing is captured just fine.

    Here's some snippets of some important parts of the system, which may help.

    Code (CSharp):
    1.  
    2. public static bool CaptureToRenderTexture(Camera sourceCamera, Transform anchor, RenderTexture rTex) {
    3.     if (sourceCamera == null) return false;
    4.  
    5.    // Clear any stale data
    6.    rTex.Clear();
    7.  
    8.    // Configure camera
    9.    var glamourCam = new GameObject(
    10.       name: "GlamourCam",
    11.       components: new System.Type[1] { typeof(Camera) }
    12.    ).GetComponent<Camera>();
    13.  
    14.    glamourCam.CopyFrom(sourceCamera);
    15.    glamourCam.transform.SetPositionAndRotation(anchor.position, anchor.rotation);
    16.    glamourCam.tag = "GlamourCam";
    17.    glamourCam.cullingMask = 1 << LayerMask.NameToLayer("Glamour");
    18.    glamourCam.clearFlags = CameraClearFlags.SolidColor;
    19.    glamourCam.backgroundColor = Color.clear;
    20.    glamourCam.targetTexture = rTex;
    21.  
    22.    // Work
    23.    glamourCam.Render();
    24.  
    25.    // Cleanup & Return
    26.    GameObject.Destroy(glamourCam.gameObject);
    27.    return true;
    28. }
    29.  
    30. public static Texture2D CaptureToTexture2D(Camera sourceCamera, (int width, int height) resolution, Transform anchor) {
    31.    RenderTexture rTex = new(resolution.width, resolution.height, 24);
    32.    rTex.Create();
    33.  
    34.    if (!CaptureToRenderTexture(sourceCamera, anchor, rTex)) {
    35.       rTex.Release();
    36.       return null;
    37.    }
    38.  
    39.    var tex = rTex.ConvertToTexture2D();
    40.  
    41.    rTex.Release();
    42.    return tex;
    43. }
    44.  
    45. public static void Clear(this RenderTexture rt) {
    46.    RenderTexture.active = rt;
    47.    GL.Clear(true, true, Color.clear);
    48.    RenderTexture.active = null;
    49. }
    50.  
    51. public static Texture2D ConvertToTexture2D(this RenderTexture rTex) {
    52.    Texture2D tex = new(rTex.width, rTex.height, TextureFormat.RGBA32, false);
    53.    RenderTexture.active = rTex;
    54.    tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
    55.    tex.Apply();
    56.    RenderTexture.active = null;
    57.    return tex;
    58. }
    59.  
    60. public static Sprite ConvertToSprite(this Texture2D tex) =>
    61.    Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
    62.  
    Here's the global volume I'd like to use.
    upload_2023-5-3_20-28-40.png

    This is the camera that the settings are copied from.
    upload_2023-5-3_20-28-51.png

    Here's the renderer used.
    upload_2023-5-3_20-28-58.png

    And here's the pipeline asset. HDR is on.
    upload_2023-5-3_20-29-3.png
     
  2. aveakrause

    aveakrause

    Joined:
    Oct 3, 2018
    Posts:
    70
    Bump. Still having issues with this, could really use some help!
     
  3. aveakrause

    aveakrause

    Joined:
    Oct 3, 2018
    Posts:
    70
    Bump, still seeing this issue. I've not been able to repro it in another project. Not sure what's going on, could really use some assistance!
     
  4. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    Be sure to also add a UniversalAdditionalCameraData component to your glamourCam, as this actually holds URP-specific properties, such as post processing being enabled or not.

    It may get added automatically, but post processing would be disabled by default.
     
  5. aveakrause

    aveakrause

    Joined:
    Oct 3, 2018
    Posts:
    70
    Thanks for the reply!
    With the inclusion of UniversalAdditionalCameraData, we still see the same behavior. We stopped creating a new camera and are relying on an existing scene camera which has the UACD component attached.