Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved OnPostRender changes

Discussion in 'High Definition Render Pipeline' started by andyz, Oct 1, 2020.

  1. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,150
    OnPostRender no longer works in the new render pipelines though it does not mention that in the documents (https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.OnPostRender.html) which would be helpful but anyway...

    The example in the above page does not work and my previous code using GL functions doesn't work either, if adjusted to use RenderPipelineManager.endCameraRendering, which is a bit of an issue.
    Should the GL functions still work within the new 'post render'?

    I was using the post render to render into a render texture with a dummy camera using GL calls
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,087
  3. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,150
    Yes it is awkward with the docs - SRPs are like a hack-on.
    Anyway the thing is I am not wanting post effects I was previously using GL calls to fast copy textures into a render texture to generate a new image at runtime but it is unclear how to do that now, previously you could do something like this:
    Code (CSharp):
    1. //cam is a dummy camera only used for offscreen stuff
    2. cam.targetTexture = renderTex;
    3. cam.Render();//calls OnPostRender with GL. calls or Graphics.DrawTexture
    4. cam.targetTexture = null;
    5.  
    6. //copy to Texture2D
    7. RenderTexture.active = renderTex;
    8. target.ReadPixels(new Rect(0, 0, 1024, 1024), 0, 0);
    9. target.Apply();
    10. RenderTexture.active = null;
     
  4. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,150
    Demo of gl commands writing to render texture in built-in
    How on earth in HDRP/SRP is a bit of a mystery, I can "RenderPipelineManager.endCameraRendering += OnHDPostRender" to do similar but then I get a ScriptableRenderContext and Camera which do not work without some additional context calls?!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PostRenderTest : MonoBehaviour
    7. {
    8.     public RawImage rawImage;//target UI texture for test
    9.  
    10.     Camera cam;
    11.     RenderTexture renderTex;
    12.  
    13.     bool textureDraw = false;
    14.  
    15.     void Awake()
    16.     {
    17.         cam = GetComponent<Camera>();
    18.         if (cam == null)
    19.         {
    20.             cam = gameObject.AddComponent<Camera>();
    21.             cam.clearFlags = CameraClearFlags.Color;
    22.             cam.backgroundColor = new Color32( 0, 0, 255, 255 );
    23.             cam.cullingMask = 0;
    24.         }
    25.         cam.enabled = false;
    26.     }
    27.  
    28.     private void Start()
    29.     {
    30.         Test();
    31.     }
    32.  
    33.     void OnPostRender()
    34.     {
    35.         if (textureDraw)
    36.         {
    37.             var shader = Shader.Find( "Hidden/Internal-Colored" );
    38.             Material mat = new Material( shader );
    39.             mat.SetInt( "_Cull", (int)UnityEngine.Rendering.CullMode.Off );
    40.             mat.SetInt( "_ZWrite", 0 );
    41.             mat.SetInt( "_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always );
    42.  
    43.             GL.PushMatrix();
    44.             GL.LoadPixelMatrix( 0, 1024, 0, 1024 );
    45.  
    46.             mat.SetPass( 0 );
    47.  
    48.             GL.Begin( GL.TRIANGLES );
    49.             GL.Color( Color.red );
    50.             GL.Vertex3(0,0,0);
    51.             GL.Vertex3(0,1000,0);
    52.             GL.Vertex3(1000,800,0);
    53.             GL.End();
    54.  
    55.             GL.PopMatrix();
    56.  
    57.             textureDraw = false;
    58.         }
    59.     }
    60.  
    61.     void Test()
    62.     {
    63.         if (renderTex == null) renderTex = new RenderTexture( 1024, 1024, 0 );
    64.  
    65.         cam.targetTexture = renderTex;
    66.         textureDraw = true;
    67.         cam.Render();
    68.         textureDraw = false;
    69.         cam.targetTexture = null;
    70.  
    71.         rawImage.texture = renderTex;
    72.     }
    73. }
    74.  
     
  5. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,150
    CustomRenderTextures it seems might be better...
    or Graphics.Blit for simple stuff actually
     
    Last edited: Oct 13, 2020