Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Compare two Textures2D by pixels - GetPixels() method

Discussion in 'Scripting' started by Hurts, Apr 16, 2018.

  1. Hurts


    Feb 28, 2018
    Hello there!

    Im trying to compare two Textures2D by pixels and I having some trubles with GetPixels() method. This is the code Im using but let me explain it a bit:

    Code (CSharp):
    2. // An auxiliar render camera that has a renderTexture. I want to compare the alpha of this renderTexture with
    3.     // other texture that I have in the spriteRenderer of a GameObject that is in the scene
    4.     public Camera renderCamera;
    6.     // The GameObject that contains the texture in the Scene
    7.     public GameObject sceneGameObject;
    9.     // Variable to save the renderTexture
    10.     private RenderTexture cameraRenderTexture;
    12.     // Variable to save the texture of the GameObject in the scene. I call it Texture1.
    13.     private Texture2D Texture1;
    15.     // Variable to save the texture of the RenderTexture of the camera. I call it Texture2.
    16.     private Texture2D Texture2;
    18.     // Array of texture 1 colors;
    19.     private Color[] texture1_ColorArray;
    21.     // Array of texture 2 colors;
    22.     private Color[] texture2_ColorArray;
    24.     // Counter of empty pixels in Texture 1
    25.     private int counterColoredPixelsInTexture1;
    27.     // Counter of common empty pixels between both textures
    28.     private int counterOfSimilarAlphaPixels = 0;
    30.     // Bool to check that the comparation works
    31.     private bool itWorks = false;
    33.     private void Start()
    34.     {
    35.         //Get the camera render texture
    36.         cameraRenderTexture = renderCamera.targetTexture;
    37.         //save the texture of the scene´s gameObject in a variable called Texture1
    38.         Texture1 = (Texture2D) sceneGameObject.GetComponent<SpriteRenderer>().material.mainTexture;
    39.         //Get and save the color pixels of texture1 using GetPixels()
    40.         texture1_ColorArray = Texture1.GetPixels();
    42.         //Set the value of the coloredpixels counter to the hole Texture2 489540, that is exacly the value in pixels of my Texture2D(796x615)
    43.         counterColoredPixelsInTexture1 = texture1_ColorArray.Length;
    45.         //Loop to check all the texture1 color array
    46.         for(int i = 0; i < texture1_ColorArray.Length; i++)
    47.         {
    48.             //If a pixel is empty
    49.             if(texture1_ColorArray[i] == Color.clear)
    50.             {
    51.                 //Reduce the coloredPixels counter.
    52.                 counterColoredPixelsInTexture1--;
    53.             }
    54.         }
    55.     }
    57.     private void CheckIfSimilar()
    58.     {
    59.         //Create a new Texture2 with the same values of the camera render texture
    60.         Texture2 = new Texture2D(cameraRenderTexture.width, cameraRenderTexture.height, TextureFormat.RGB24, false);
    61.         //Activate the renderTexture
    62. = cameraRenderTexture;
    63.         //Read pixels of texture2 and apply them
    64.         Texture2.ReadPixels(new Rect(0, 0, cameraRenderTexture.width, cameraRenderTexture.height), 0, 0);
    65.         Texture2.Apply();
    67.         //Get and sabe the color pixels of texture2 using GetPixels();
    68.         texture2_ColorArray = Texture2.GetPixels();
    70.         //Loop to check each pixel Color of texture 1
    71.         for (int i = 0; i < texture1_ColorArray.Length; i++)
    72.         {
    73.             //If an alpha pixel of texture1 is the same as the alpha pixel of texture2
    74.             if (texture1_ColorArray[i].a == texture2_ColorArray[i].a)
    75.             {
    76.                 //Increase the counter of same pixels
    77.                 counterOfSimilarAlphaPixels++;
    79.                 //if the similar alpha pixels between the textures is more or equal as the alpha pixels of texture1
    80.                 //(489540 is all the pixels of my texture)
    81.                 if (counterOfSimilarAlphaPixels >= (489540 - counterColoredPixelsInTexture1))
    82.                 {
    83.                     itWorks = true;
    84.                 }
    85.             }
    86.         }
    87.     }
    The problem is that with the debugger mode in the inspector, the value of counterClearPixel is 489540, that is exacly the value in pixels of my Texture2D (796x615). That means my Texture2D is full of colours, but is not, its a png with empty space.

    Anyone have an idea of what is happening? I have my texture in read/write enable

    Thanks in advance
    Last edited: Apr 16, 2018
  2. methos5k


    Aug 3, 2015
    I think it means that none are clear, since you begin with the full value and decrease it every time you match a Color.clear.
  3. Hurts


    Feb 28, 2018
    yes, I thought the same, but in my texture I have empty pixels for sure, it´s a png that has empty space at the sides... So the value should decrease.
  4. methos5k


    Aug 3, 2015
    Now that I think about this some more, I believe I've found a thread or more talking about this at some point.
    Sadly, I can't remember the good information that was included to explain it at this time :)

    Now, you want to find transparent pixels, or do they literally have to be clear?

    I tested this creating a tiny image and testing, and I never got a Color.clear either, though I know there are 127 of them ;)
    However, this worked:
    Code (csharp):
    1. if(array[i].a == 0) // whatever
    Last edited: Apr 16, 2018
  5. Hurts


    Feb 28, 2018
    I try to explain better my code, sorry for not being clear and thanks for your replys!

    I want to find the transparent pixels.
    The goal of this code is compare two Textures by the alpha channel, when both textures has the same alpha pixels, my boolean variable changes.

    "testing for array.a == 0 produced the correct result" that´s true! Thanks, let me check if adding this solve my problem!

    Sorry for this mess, I´m not the best coding :)
    Last edited: Apr 16, 2018
  6. methos5k


    Aug 3, 2015
    Well, since I could remember or find the explanation .. nor could I get the proper value of 0,0,0,0 for Color.clear and my transparent pixels, what I did was I only tested if the color's alpha value was zero.

    If you don't care if the transparent pixel is blue with alpha 0 or red with alpha 0, but all you care about is whether the alpha is zero or not, that will work for you.

    Does that make sense?
  7. Hurts


    Feb 28, 2018
    Yes, I´m testing if with the solution you give me, I can reach my goal. Thank you very much! I will post my code as soon as it works.

    By the way, it´s extrange that if there are empty pixels, the GetPixels function doesnt give you those pixels as empty. That makes the function Color.clear useless.

    Thanks again!
  8. methos5k


    Aug 3, 2015
    Ya.. I hear ya. I mean, I think there's a reason of sorts. ;)

    Anyways, I'm fairly confident this will work for you. Plus, it's arguably easier to check 1 float than 4, so it could be a win after all. :) lol