Hi!
I have a camera that renders into a render texture.
Then, I want to save the resulting texture in the assets folder as a texture2d (target texture).
So first I use Graphics.CopyTexture
to copy render texture into my target texture (asset).
It works until you save the scene or apply texture, because on the CPU side the texture still contains old data.
My bruteforce solution is to read the texture back from GPU, write data on the CPU side and apply it.
void CopyRenderTexToTex()
{
// ..we copy rendertexture to targettexture inside gpu...
Graphics.CopyTexture(renderTexture, targetTexture);
// ..then we read targettexture from gpu..
var req = AsyncGPUReadback.Request(targetTexture, 0);
req.WaitForCompletion();
// ..and we write it on the cpu
targetTexture.LoadRawTextureData(req.GetData<byte>());
EditorUtility.SetDirty(targetTexture);
// apply to save
targetTexture.Apply();
}
It works, but it’s kinda ineffective, because we unnecessarily write texture from the CPU back to GPU during targetTexture.Apply();
So I was wondering if there’s a more effective way to do this?