Copying a small region of a RenderTexture

I’m trying to copy a small region (300x300) of a larger RenderTexture (say 1920x1080.)

At the moment I’m using the following code.

smallTexture->setKeepMatrix(true);

smallerTexture->setVirtualViewport(Vec2(cornerPos.x, cornerPos.y),
                              Rect(0,0, smallerTextureWidth , smallerTextureHeight),
                              Rect(0,0, largerTextureWidth, largerTextureHeight));
smallerTexture->beginWithClear(color);
largerRenderTexturesSprite->visit();
smallerTexture->end;

I’d expect the following result (sorry for shitty paint.)

However this isn’t the result. I’ve messed around with it a good bit and can’t seem to get a handle on what the arguments are truly affecting.

I was hoping someone who has some experience with RenderTextures could explain why what I’m expecting is incorrect.

ClippingNode?

I looked at using a clipping node.
I’m running shaders on the smaller texture so I wanted to get a literal second RenderTexture object.

In 2.x, I would move largerRenderTexturesSprite by (-500, -300), then visit it, then move it back, but I’m not sure if this works with 3.x’s async rendering…

Unfortunately, nothing that intuitive has worked.

I hate to bump this but I’m sure someone has successfully used RenderTextures in version 3.3. I’d really appreciate any help.

Also, does Node::visit() add the node being visited as a child or something? Because calling visit always results in the node being visited moving.

Edit: This topic answers the Node::visit vs Node::draw() thing for anyone who is trying to figure that out.

Ok, I’ve narrowed down what the issue is after reading the link above. What grim fate said should work, but only if you don’t have to move the largerRenderTexturesSprite back afterward since the renderer doesn’t record position. So the solution is to have two copies of the sprite you need to have in two positions, but with a RenderTexture that’s the full size of the screen I’m trying to avoid that.

I tried forcing the renderer and it worked for me. Use the method I recommended - moving the nodes you visit and then moving them back afterwards - and try calling Director::getInstance()->getRenderer()->render(); after you call end() on your render texture, but before you move your nodes back.

E.g.

Point pos = largerRenderTexture->getPosition();
largerRenderTexture->setPosition(Point(0, 0));
smallerTexture->begin();
largerRenderTexture->visit();
smallerTexture->end();
Director::getInstance()->getRenderer()->render();
largerRenderTexture->setPosition(pos);
1 Like