I thought this would be easy , but It was almost impossible for me to find out how to do it. I want to do two things
1.change the stencilformat of screen render target
2.change the depthbits of screen render target
I tried to change them in scriptable renderer pass, but unity told me it is invalid to change these after rt being created. So I wonder if I can change them earlier before the creation of screen fbo.
I tried lots of things.
1.I tried to bind a render texture asset to camera . I succeeded , but Iâm not satisfied with this . Renderdoc works only for frame if Iâm correct . I canât look into details how that render texture is generated .
2.I thought the name âDisplay1â has something to do with the screen . But a Display class didnât help
3.I tried to access camera.activeTexture and camera.targetTexture. But theyâre null. I know a targetTexture which is set to null controls camera to render to screen . I have no idea what does it mean that activeTexture is null .
4.I set render target to my custom RTHandle in one scriptable render pass before rendering opaque. But it didnât help . When rendering opaque , unity no longer used my custom RTHandle as target. The change is local.
5.I tried ReenderPipelineManager . But the only things I can access there were ScriptableRenderContext and Camera. What I need is ScriptableRenderer or RenderingData. I need a way to access RenderTexture because stencilFormat is its field .
6.I tried to find an explicit setting in inspectors of camera, urp renderer data , and urp asset . I looked into project setting and preference as well . I didnât see an option to control the color format used to render screen .
I need help pls . Can someone tell me it is impossible to directly configure the screen rt ?
Solved . Add this code in scriptable renderer feature :
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderingData.cameraData.cameraTargetDescriptor.stencilFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.R8_UInt;
//renderingData.cameraData.cameraTargetDescriptor.colorFormat = RenderTextureFormat.ARGB1555;
//renderingData.cameraData.cameraTargetDescriptor.width = 400;
//renderingData.cameraData.cameraTargetDescriptor.height = 400;
renderer.EnqueuePass(m_ScriptablePass);
}
Why ? Because URP executes codes of
.........
renderer.AddRenderPasses(ref renderingData);
if (useRenderGraph)
{
RecordAndExecuteRenderGraph(s_RenderGraph, context, ref renderingData);
renderer.FinishRenderGraphRendering(ref renderingData);
}
else
{
using (new ProfilingScope(Profiling.Pipeline.Renderer.setup))
renderer.Setup(context, ref renderingData);
// Timing scope inside
renderer.Execute(context, ref renderingData);
}
RT is created during renderer.Setup which calls the implementation of scriptable renderer (e.g. renderer2D) . URP calls Renderer2D to allocate new rt here.
As we know scriptable renderer feature owns a method called SetupRenderPasses . This is called by scriptable renderer class in its SetupRenderPasses method (the same name) . SetupRenderPasses is called in the same class (scriptable renderer) by Execute method . Execute is called by URP : ârenderer.Execute(context, ref renderingData);â .
Before calling feature.SetupRenderPasses , URP calls renderer.Setup (equals to Renderer2D.Setup) which allocates actual rt as following :
CreateRenderTextures(ref cameraData, ppcUsesOffscreenRT, colorTextureFilterMode, cmd,
out colorTargetHandle, out depthTargetHandle);
You canât change the format of a created rt . You must change it before its creation . i.e. you should change them in renderer.AddRenderPasses(ref renderingData);. Go take a look of the code. renderer.AddRenderPasses(ref renderingData); is before renderer.Setup(context, ref renderingData); .
I successfully changed width , height , and color format . I changed color format to R5G5B5, and I got a posterized visual effect. But you canât change the stencil . Because if you call ReAllocateIfNeeded without Vector2 scaleFactor, youâre not able to go into the overloading which sets stencilformat. I think this is a bug of unity .