- Home /
Getting inverted Y axis from Texture2D.GetPixel()
This questions complete source code/project is located in this thread:
http://forum.unity3d.com/threads/198919-Destructible-Pixel-Terrain-Open-Source
and my question is this:
I try to do a method to get whether or not the alpha of a certain pixel is 0 or 1. when I do this thought I check for pixels that are from top left of the level. This ends up being around the place it spawns in the player (100, 100 if top left is 0, 0) and when I run those x y through texture2d.getpixel(x, y) it ends up giving me the pixels in the bottom left corner of the texture, however I need the pixels from top left...
It must be something simple like a line of some math to get it working, how could I get my Y to be on my player (which gives its location apparently from top left rather than bottom right... so the very top left would be 0, 0...) and remember that x works its directly below the player (who is near top left corner at spawn, see that thread)
anybody had this problem? I feel like its stupid simple... lol
Here is an image to describe my problem better:
Answer by PAEvenson · Sep 10, 2013 at 05:39 PM
Yea, I think texture2d.GetPixel's (0,0) is bottom left of the texture. To get the pixel from top left you could use:
texture2d.GetPixel(x, texture2d.height - y);
Maybe?
Hmm maybe, but the code provides so many other problems its hard to tell haha, I think this is right though! I knew it would be a one-liner :D
I'll mess with it more and if this is right I will accept your answer. EDIT: yes this works for me, thank alot!
Answer by Benjames · Jul 14, 2014 at 01:44 AM
This should be in the coding reference or at least more well known. It killed me for hours spread out over a couple days, while I was trying to retrieve coordinates for full red pixels.
It was pretty ridiculous for something that can be so simply relayed.
Yes it could use mention on the docs. Had me busy a couple days anyway. Your answer should be converted to a comment though :p
Your answer
Follow this Question
Related Questions
GetPixel optimisation 2 Answers
Find a colour in a Texture2D 1 Answer
Get set of Pixels from PolygonCollider2d Position 0 Answers
GetPixel in c# 1 Answer
Steps in Texture2D after writing values to an array 1 Answer