Using viewport with RenderTarget

I need to render a scene on a specific location inside a render target (for creating texture atlas).
So I memorize the current viewport then switch targets, render and put the old viewport back, like so:

const renderRTVP = (rend, scene, camera, outputRT, vp, clear = true) => {
	const bool = !clear && rend.autoClear;
	if (bool) rend.autoClear = false;
	const vp_old = new THREE.Vector4();
	rend.render(scene, camera);
	if(bool) rend.autoClear = true;

is this the right code?

it works under Windows 10 but doesn’t work on Android mobile and probably other OS.

This is what I see when I press both buttons:

  1. What’s the difference between getCurrentViewport and getViewport, I see different sizes on mobile?

  2. Does the order of calling setRenderTarget and setViewport matter?

Not sure if you’ve looked at code for portal example. It doesn’t seem to need to use viewport

The stereo example uses two view ports. Looks like the code for Stereo Effect doesn’t attempt to restore the viewport.

Thanks, but, unfortunately, it doesn’t help me much.
In the portal example, each portal uses its own texture that is rendered in full size, so there is no need to use a viewport inside it, in the second example the viewport is not restored because stereo effect is run on each frame and it resets scissors and viewport halves from scratch.

Ok, for the record, currentViewport is Viewport multiplied by window.devicePixelRatio which will break your code if you apply that to a render texture that is sized in “true” pixels as it will be out of bounds.