- Home /
What is difference between Camera.SetTargetBuffers and setting Camera.targetTexture?
If cam is the Camera component of a camera GameObject and rt is a RenderTexture created with:
RenderTexture rt = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32);
Then, are these three lines all exactly equivalent?
cam.targetTexture = rt;
cam.SetTargetBuffers(rt.colorBuffer, rt.depthBuffer);
cam.SetTargetBuffers(new RenderBuffer[] { rt.colorBuffer }, rt.depthBuffer);
If not, what is the difference?
My code works fine, but when I try to to change the first of these to use SetTargetBuffers my code stops working.
I'm just refactoring code that will shortly be needing MRT (multiple render targets) and I thought I'd test that it still works with one color buffer.
[Edit] In this case not working means that a later stage in the pipeline gets black instead of the expected output when I change from the first line to either of the other two. If there's no expected difference then I guess I need to go through and validate that that is all that's changing between tests and try to create a simpler sample.
Any suggestions as to why this would be?
Thanks
You answered your first question yourself, if the outcome is different they can't be equal.
What does not working mean?
Do you ever make it work? Im facing the same problem - SetTargetBuffers just returns black texture
Answer by Bunny83 · Aug 26, 2017 at 05:51 AM
The documentation states that at least the first two lines should do the same. However the internal declaraion looks, well, strange:
public void SetTargetBuffers(RenderBuffer colorBuffer, RenderBuffer depthBuffer)
{
this.SetTargetBuffersImpl(out colorBuffer, out depthBuffer);
}
public void SetTargetBuffers(RenderBuffer[] colorBuffer, RenderBuffer depthBuffer)
{
this.SetTargetBuffersMRTImpl(colorBuffer, out depthBuffer);
}
What's strange is that in the single RenderBuffer version both are "out" parameters. This might not be a problem as "SetTargetBuffersImpl" is a native method so the "out" restriction most likely do not apply. Though we can't be sure about that. From a pure managed point of view the implementation as it stands makes no sense. Since out and ref parameters are actual pointers to the variables (not managed references) it probably shouldn't matter.
That does look odd - the wrapper hides the out, so my RenderTarget can't be affected.