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
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:
//cam is a dummy camera only used for offscreen stuff
cam.targetTexture = renderTex;
cam.Render();//calls OnPostRender with GL. calls or Graphics.DrawTexture
cam.targetTexture = null;
//copy to Texture2D
RenderTexture.active = renderTex;
target.ReadPixels(new Rect(0, 0, 1024, 1024), 0, 0);
target.Apply();
RenderTexture.active = null;
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?!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PostRenderTest : MonoBehaviour
{
public RawImage rawImage;//target UI texture for test
Camera cam;
RenderTexture renderTex;
bool textureDraw = false;
void Awake()
{
cam = GetComponent<Camera>();
if (cam == null)
{
cam = gameObject.AddComponent<Camera>();
cam.clearFlags = CameraClearFlags.Color;
cam.backgroundColor = new Color32( 0, 0, 255, 255 );
cam.cullingMask = 0;
}
cam.enabled = false;
}
private void Start()
{
Test();
}
void OnPostRender()
{
if (textureDraw)
{
var shader = Shader.Find( "Hidden/Internal-Colored" );
Material mat = new Material( shader );
mat.SetInt( "_Cull", (int)UnityEngine.Rendering.CullMode.Off );
mat.SetInt( "_ZWrite", 0 );
mat.SetInt( "_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always );
GL.PushMatrix();
GL.LoadPixelMatrix( 0, 1024, 0, 1024 );
mat.SetPass( 0 );
GL.Begin( GL.TRIANGLES );
GL.Color( Color.red );
GL.Vertex3(0,0,0);
GL.Vertex3(0,1000,0);
GL.Vertex3(1000,800,0);
GL.End();
GL.PopMatrix();
textureDraw = false;
}
}
void Test()
{
if (renderTex == null) renderTex = new RenderTexture( 1024, 1024, 0 );
cam.targetTexture = renderTex;
textureDraw = true;
cam.Render();
textureDraw = false;
cam.targetTexture = null;
rawImage.texture = renderTex;
}
}