Setting TextureRect’s texture to ViewportTexture in code

Godot Version

4.6.2

Question

2D game.

I’m instantiating TextureRects at runtime to create a parallax effect without having TileMapLayers be children of Parallax2Ds: I found that when the camera moves, process time spikes with extremely large TileMapLayers.

To pull this off, every child of each Parallax2D is reassigned to a corresponding SubViewport; this is in turn supposed to be fed into a TextureRect.

How do I assign a ViewportTexture to a TextureRect at runtime?

$TextureRect.texture = $SubViewport.get_texture()
1 Like

Thank you!

1 Like