Search Unity

Too blurry when using Gui.DrawTexture with Texture2D

Discussion in 'UGUI & TextMesh Pro' started by longvk1989, Mar 10, 2016.

  1. longvk1989

    longvk1989

    Joined:
    Apr 10, 2015
    Posts:
    17
    I take screenshot with RenderTexture like below :

    int width = (int)(Screen.width * multiplyResolution);
    int height = (int)(Screen.height * multiplyResolution);

    RenderTexture rt = new RenderTexture(width, height, 32);
    rt.antiAliasing = 1;
    rt.filterMode = FilterMode.Bilinear;
    rt.wrapMode = TextureWrapMode.Clamp;

    Camera.main.targetTexture = rt;
    Camera.main.Render();
    RenderTexture.active = rt;

    Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
    texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    texture.Apply();

    Camera.main.targetTexture = null;
    RenderTexture.active = null;
    Destroy(rt);

    Then i take a portion of that texture (for ex: crop everything except top left of that texture)

    Texture2D result = new Texture2D((int)rect.width, (int)rect.height);
    result.SetPixels(texture.GetPixels(0, 0, 200, 200));
    result.Apply();

    After that i use GUI.DrawTexture to draw it on screen but the quatity is bad, it's blurry. Why is it so blurry? please help
     
  2. longvk1989

    longvk1989

    Joined:
    Apr 10, 2015
    Posts:
    17
    I change from GUI.DrawTexture to Image in a Canvas Overlay but the result's still same. If Anybody faces this prob plz help ?
     
  3. Garbilicious

    Garbilicious

    Joined:
    Apr 1, 2020
    Posts:
    2
    Hello! I see it has been over 7 years since the original post, but running into this problem and not finding a solution has been disheartening. I do have the solution, tough. Make sure when you make a Texture2D, you set its filterMode to FilterMode.Point.

    For example:
    Code (CSharp):
    1. Texture2D texture = new Texture2D(64, 64);
    2.  
    3. texture.SetPixels(colorArr);
    4. texture.Apply();
    5.  
    6. // This is what should make your textures scalable.
    7. // You can add this before or after Apply().
    8. texture.filterMode = FilterMode.Point;
    I suppose that when you first make a Texture2D, it makes the filter mode bilinear or trilinear by default. So, change that and it should be good to go!
     
    eniemann likes this.