Clone/duplicate Texture2D?

How can I make a data duplicate of a Texture2D so that it isn’t just a pointer to the another texture?

the scenario is:
I am using a RenderTexture to making a drawing app with a basic “undo” functionality. This is accomplished by copying the RenderTexture’s texture [ _renderTexture->getSprite()->getTexture() ] when a new action is taken, and replace it with the previous one when "undo"ing.

My current solution is to save the RenderTexture to a PNG, then “undo” by reading that PNG in to a sprite and visiting it to overwrite the existing RenderTexture’s texture. The problem with this method is the quality loss during the write/read/visit process. If instead of visiting, I directly set the texture [ _renderTexture->getSprite()->setTexture( tex ) ] , there is less quality loss (but still some), but the image is flipped. Also, when a lot the image quality would degrade extensively (think of repeatedly opening, saving, and closing a JPG).

The best solution would be to make an exact binary duplicate (clone?) of the texture and store that in memory, then directly set the RenderTexture’s texture during undo. This would incur no image quality loss during the “undo” process, but I cannot find a way to make this duplicate, thus the initial question.

Thank you.

3 Likes