Search Unity

Rexture2D.ReadPixels slow

Discussion in 'Editor & General Support' started by DJVDJV, Sep 7, 2019.

  1. DJVDJV

    DJVDJV

    Joined:
    Sep 11, 2014
    Posts:
    70
    Hi I try make some texture stuff like:

    Code (CSharp):
    1. private void GenerateTexture(Color32[] pixels)
    2.         {
    3.             RenderTexture.active = renderTexture;
    4.             texture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    5.             texture2D.SetPixels32(pixels);
    6.             texture2D.Apply();
    7.             RenderTexture.active = null;
    8.         }
    9.  
    Actually all works good BUT it is extremely slow.

    I would like to do this with:
    Code (CSharp):
    1. Graphics.CopyTexture
    Not sure how?
    Any professonal here in texsture stuff who can hel with Graphics.CopyTexture? Or is there even other way to dot his?
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    You're calling both ReadPixels and SetPixels at the same time. The former will copy from "renderTexture", while the latter will copy from "pixels" and overwrite the first operation. Copying from a RenderTexture will always be slow if you require CPU access, but you shouldn't need to do both.
     
    DJVDJV likes this.
  3. DJVDJV

    DJVDJV

    Joined:
    Sep 11, 2014
    Posts:
    70
    Heh thats true. Went already better if not perfect yet