Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change pixels on a QUAD texture

Discussion in '2D' started by amiga4K, Oct 15, 2021.

  1. amiga4K

    amiga4K

    Joined:
    Jan 30, 2018
    Posts:
    71
    Hi,
    I'm trying to do a simple thing: take an original texture put on a Quad and then draw new pixels at certain positions.

    The problem is that when I do this the texture becomes all grey and the pixels drawn seem to have a strange blurring effect.

    The code is the following:

    Code (CSharp):
    1. MeshRenderer meshRenderer = quad.GetComponent<MeshRenderer>();
    2. texture = new Texture2D(meshRenderer.material.mainTexture.width, meshRenderer.material.mainTexture.height);
    3. meshRenderer.material.mainTexture = texture;
    4.  
    5. texture.SetPixel(0, 0, Color.red);
    6. texture.SetPixel(1, 0, Color.red);
    7. texture.SetPixel(2, 0, Color.red);
    8.  
    9. texture.Apply();
    Look at images:

    Original 800x600

    Unity_JwNeOZI4OM.png

    After set pixels:

    Unity_Qg5OoD7FJ5.png


    I don't understand the problem! Please help.
    Thanks.
     
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    you need to change the settings on the texture, set it to filtermode point, no compression ettc
     
  3. amiga4K

    amiga4K

    Joined:
    Jan 30, 2018
    Posts:
    71
    Yes, this is the problem but I also missed getting the original pixels.. :oops:

    Below the code:

    Code (CSharp):
    1.  MeshRenderer meshRenderer = quad.GetComponent<MeshRenderer>();
    2.  
    3.         texture = new Texture2D(meshRenderer.material.mainTexture.width, meshRenderer.material.mainTexture.height, TextureFormat.RGBA32, false);
    4.         texture.filterMode = FilterMode.Point;
    5.         texture.wrapMode = TextureWrapMode.Clamp;
    6.         texture.SetPixels(((Texture2D)meshRenderer.material.mainTexture).GetPixels());
    7.         texture.Apply();
    8.  
    9.         meshRenderer.material.mainTexture = texture;
    10.  
    11.         for (int y = 0; y < 200; y++)
    12.         {
    13.             for (int x = 0; x < 800; x++)
    14.             {
    15.                 texture.SetPixel(x, y, Color.clear);
    16.             }
    17.         }
    18.  
    19.         texture.SetPixel(0, 0, Color.red);
    20.         texture.SetPixel(1, 0, Color.red);
    21.         texture.SetPixel(2, 0, Color.red);
    22.  
    23.         texture.Apply();
    And below the results:

    Unity_Qd0x7B3nLW.png


    Many thanks for your help!!!