Copy ViewportTexture

Godot Version

4.2

Question

There is a function to get a texture from the Viewport
There is a function to get a texture from the Viewport. In this function, I change the position of the camera inside the Viewport and get the texture. The texture corresponds to a point on the map. I return this texture and insert the record that is saved. When I create a new recording, I change the position of the camera in the Viewport again. The texture changes both in this entry and in the old one.

	public static Texture GetPhoto(in Vector2 cameraPosition, in Vector2 zoom = default)
	{
		var camera = MainAPINPCPhoto.GetCamera2D();
		
		var newZoom = zoom;
		if (newZoom == default)
			newZoom = new Vector2(45, 30);
		
		camera.Zoom = newZoom;
		camera.Position = cameraPosition;
		
		return MainAPINPCPhoto.GetTexture();
	}

I started googling my problem. I kind of found a solution by copying it to ImageTexture.

	public static Texture GetPhoto(in Vector2 cameraPosition, in Vector2 zoom = default)
	{
		var camera = MainAPINPCPhoto.GetCamera2D();
		
		var newZoom = zoom;
		if (newZoom == default)
			newZoom = new Vector2(45, 30);
		
		camera.Zoom = newZoom;
		camera.Position = cameraPosition;
		
		var texture = MainAPINPCPhoto.GetTexture() as Texture2D;
		var image = texture.GetImage();
		var imageTexture = ImageTexture.CreateFromImage(image);
		return imageTexture;
	}

However, Image corresponds to the previous point. Each time I change the position of the camera, the texture itself corresponds to a point on the map, but the Image I get from the same texture corresponds to the previous point.
How to get a non-changing ViewportTexture?

Solved

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.