- Home /
Graphics.DrawTexture in OnPostprocessTexture
I'm trying to manipulate a texture during import. For that, I'm creating a RenderTexture which I clear with GL.Clear
and then want to draw the loaded Texture onto. Then read back the pixels into the original texture asset.
While GL.Clear
works and the image does indeed gets a yellow background. Graphics.DrawTexture
does not, so the image will just remain completely yellow.
I already tried using a different texture that I know exists through AssetDatabase.LoadAssetAtPath
and with that texture Graphics.DrawTexture
works as intended. Only the texture that was just processed and gets passed by OnPostprocessTexture(Texture2D texture)
seems to not get drawn by Graphics.DrawTexture
.
Does anyone know what the problem is, and a way around it?
Here is a minimal reproducible example:
public class SpriteImporterPostProcessor : AssetPostprocessor
{
private void OnPostprocessTexture(Texture2D texture)
{
var drawRect = new Rect(0,0, texture.width, texture.height);
var active = RenderTexture.active;
var temp = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
RenderTexture.active = temp;
GL.LoadPixelMatrix(0, texture.width, texture.height,0);
GL.Clear(true, true, Color.yellow);
Graphics.DrawTexture(drawRect, texture);
texture.ReadPixels(drawRect , 0, 0, false);
texture.Apply(false);
RenderTexture.active = active;
RenderTexture.ReleaseTemporary(temp);
}
}
Answer by Fuzzel_ · Aug 01, 2020 at 12:33 AM
For anyone stumbling upon this:
I found the solution, after a lot of trial and error. As far as I understood it, the texture that was just imported is not yet pushed to the GPU, so Graphics.DrawTexture
can not draw it (it does not throw an error, but other functions like Graphics.CopyTexture
do, and thats where I got the hint).
So to make the texture useable in Graphics.DrawTexture
call texture.Apply()
first, which pushes the texture to the GPU.