How does Graphics.SetRenderTarget work?

Hi,

so i’ve got no clue whatsoever how Graphics.SetRenderTarget works and how to use it properly. So here’s the stuff that i’ve attached to the main camera:

    public RenderTexture tex1;
    public RenderTexture tex2;

	void Start () {
        tex1 = new RenderTexture(Screen.width,Screen.height,24,RenderTextureFormat.ARGB32);
        tex2 = new RenderTexture(Screen.width,Screen.height,24,RenderTextureFormat.ARGB32);
        tex1.Create();
        tex2.Create();
	}

    void OnPreRender()
    {
        RenderBuffer[] colorBuffers = { tex1.colorBuffer, tex2.colorBuffer };
        Graphics.SetRenderTarget(colorBuffers, tex1.depthBuffer);
    }

First of all, this camera doesn’t render anything useful anymore at this point, but it’s still possible to display what’s inside tex1 and tex2.

The weird thing now is this:

  • If i only set tex1 as a rendertarget, it’s filled with black.
  • If i set the colorBuffer and depthBuffer of tex1, it’s black.
  • If i do the above thing with two colorBuffer in dx9, tex1 get’s cleared but nothing is drawn. Using dx11 it’s black again.

Not quite sure if this even works with cameras or if it only works inside post processing effects, but documentation for this is a one liner so… yeah, i’ve got no clue.

So if anyone could tell me what on earth i’m doing wrong it would be much appreciated.

Greetz,
Chicken

1 Like

Do you mean to use Camera.targetTexture instead?