Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GetPixel, problem to get correct color

Discussion in 'General Graphics' started by amiga4K, Jun 18, 2021.

  1. amiga4K

    amiga4K

    Joined:
    Jan 30, 2018
    Posts:
    68
    Hi I have the following image; a PNG 800x800

    level.png

    when I try to get the colour at the following position 100, 560 of the texture (texture coordinates)
    GetPixel
    returns the colour 0.477, 0.477, 0.788.

    However, this is not the correct colour. In fact if, for example, I open the same image in Photoshop at the same coordinates (100, 240 starting from the top left corner) it gives me 0.000, 0.745, 0.443 (or as byte values 0, 190, 113) as colour.

    Photoshop_PoREG0OIeL.png

    In fact, if I place a sprite at the coordinates (-300, 160 World Position) in Unity for example, you can clearly see that that pixel is on the green colour (0.000, 0.745, 0.443) and not on the blue colour ( 0.477, 0.477, 0.788) of the background.

    Unity_Z5t0B5MhXv.png

    I imported the image with:
    • wrap mode -> clamp
    • fliter mode -> point no filter
    • format -> rgba32 no compression
    Any help? Is this a bug or is this something else?

    Thanks!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    have you disabled scaling? (set to none)
    upload_2021-6-18_14-18-36.png

    and try using SetPixel() to verify you are in the right pixel coordinate?
     
  3. amiga4K

    amiga4K

    Joined:
    Jan 30, 2018
    Posts:
    68
    I imported the image as Sprite (2D and UI) so I haven't that option.
     
  4. amiga4K

    amiga4K

    Joined:
    Jan 30, 2018
    Posts:
    68
    I have used SetPixel to draw a black pixel at same coordinates (100,560 in texture coordinate).

    Unity_gl6N7b9mUN.png

    As you can see from the image below the pixel has been rendered at the position (100,560 in texture coordinates) but this position does not correspond to where it should have been rendered... is there some strange offset? If this is the problem, I don't understand....

    I explain better World Coordinate -> -300, 160 are equals to Photoshop Screen Coordinates (100, 240) that are equals to Unity Texture Coordinates (100, 560). However as we can see, the color is different.
    So it seems as the pixel is get and set at wrong coordinates...
     
  5. Gameccino

    Gameccino

    Joined:
    Aug 1, 2012
    Posts:
    40
    any solution for this?
    I'm experiencing the same issue on unity 2021.3.4f
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    I'm going to move this thread to the General Graphics forum. Texture2D is not a 2D team feature, it's obviously the standard texture used throughout all of Unity.

    The 2D team deal with Sprites yes, but GetPixel/SetPixel is not part of that.