Is it possible to somehow render only into part of a render texture?
Unity docu says that viewport rect is not supported when rendering into a rendertexture. I still tried it, didn’t work / behave properly.
I know I can just remap UVs in a shader and then blit, but this will stil make a whole pass over the entire render texture → bad performance, especially since I need to draw 4x4=16 separate areas into the render texture.
Is there any other way to do this? Maybe with the OpenGL API?
Unfortunately, the most definitive answer that you’ll likely find is: it depends.
The version of Unity is the biggest factor here so if do even find a way to get it working you have to be comfortable with the fact that your code will effectively be locked to that version of Unity perhaps indefinitely. It also appears to vary from platform-to-platform and between release and editor builds.
It’s been a couple of years since I looked into this myself so I can’t really say much more than that. If you search around you’ll certainly come across old Unity forum posts about it. Everything I said above is pretty much a summary of that. I never managed to get it working for any version of Unity I’ve been using so I can’t really say more from experience. My final takeaway was that it’s too unreliable and too risky to jeapordize a project by relying on it even if you did manage to get it working.
Since this is a long standing issue, maybe someone from the Unity team could chime in.
I refuse to believe, that there is no way to somehow render into only a part of a render texture.
Considering there are posts going back to 2016, I wouldn’t hold out looking for alternatives. Though I have to admit it surely would be a nice feature to have and I can’t imagine it would be particularly difficult to add.
In my own case, since I was just rendering individual objects to assigned portions of the render texture I was able to use some shader magic to simply translate and rotate them to appear in their assigned grid positions.
For future visiting devs that are as desperate as me:
I managed to create a workaround by creating a simple compute shader that writes a source texture into a destination texture, given an offset.
I think you misunderstood my case. It’s a single camera with a single render texture. Essentially just lining up all of the objects in front of the camera and taking a snapshot.
Glad to hear you got something working though. Probably easier to use than what I have anyway.
Not my area but aren’t there SRP techniques that do subdivide the screen somehow as a cost saving method? I have some vague memory of someone explaining doing this though perhaps it wasn’t Unity. edit: ah ya not in Unity
You can do it with camera.pixelRect, something like this will work
using UnityEngine;
public class RenderToTexture : MonoBehaviour
{
public RenderTexture _targetTexture;
public Camera _renderToTextureCamera;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
_renderToTextureCamera.targetTexture = _targetTexture;
_renderToTextureCamera.pixelRect = new Rect(0, 0, _targetTexture.width / 2, _targetTexture.height / 2);
}
// Update is called once per frame
void Update()
{
}
}
I just tested it to make sure it still works, looks like it does. Rendering into a sub section is a critical feature of render textures and will unlock a lot of possibilities for you.
This is what @Sluggy1 mentioned. On some machines/projects it works and for some it doesn’t. Also depends on the Unity version. For me, cam.pixelRect has the same strange behavior as cam.rect.
Can you post some code? I’ve been using this method since probably unity 5.x and I just tested it in 6 and it worked fine. It’s a pretty fundamental feature of the camera.
I just went back to my project and created a new scene and script to prove you wrong,but when I pressed play, everything worked exactly as it is supposed to!
I then went back to my game scene and tried it there. Didn’t work.
I then started to investigate what’s going on.
TURNS OUT the viewport rect functionality breaks when post processing is enabled on the camera!