- Home /
Translate a pixel using SetPixel on a Texture2D?
Hey guys, I been scratching my head about something with editing 2D textures in Unity and am finding it hard to describe my problem really, but bear with me - is there a simple way to do a simple transform of pixel(s) through SetPixel/SetPixels32 methods then applying to a texture - so that you do not leave behind the "old" pixels that have since moved to a new location in the texture? (these "pixels" are dynamic, they are affected by gravity and explosions which send them bouncing around the 2D scene)
A visual aid to help describe what I need to figure out:
1) first we have a pixel in some location on a texture2D:
2) then we transition the pixel (in this case only a single one - other times multiple) to another position:
3) and finally we get rid of the old pixels position (here is the part I have trouble with):
So yeah I feel like its a pretty simple thing I am going for, but it escapes me how to properly get rid of the old position, especially when in a few frames, the pixels can shift dramatically (skipping several pixel lengths at high speed, before a collision detects) and I need an efficient way to determine where to "erase" the old pixel from... has anybody tried doing something like using SetPixel(x, y) and had to go back to erase the old pixel before Apply()'ing the changes to the texture? Also the position of the "pixel" is stored in an x, and y value format in an array of pixels that are in motion, so I can always check where a pixel is currently - how can I keep track accurately the last position the pixel was in, to "clean up" the positions that would be a dotted trail of where that pixel has traveled? How do you go about tracking individual pixels, and remembering where they came from/if they have moved at all since last frame (maybe they floated at the peak of a movement between frames and didnt actually move enough to go down a full pixel) and successfully cleaning up the old positions of the pixel?
Other possibly useful info:
keep track of where a pixel is supposed to be, based on a pile of clunky physics decisions to keep velocity looking realistic and bounce off the normal of local pixels after a collision...
-The texture which contains these dynamic pixels ALSO has a 2D pixel terrain, which has been through having all purple "sky" pixels iterated through and deleted from the texture (replaced by alpha 0 pixels) so thats what collisions are hitting.
-In doing this I must be sure not to erase "new" pixels that have just been drawn to their new position (which could be an "old" position of another pixel)
am not really asking you to write me some code, more like just your theory with an idea in pseudocode, but a simple example would be appreciated!
-One idea I had was to erase, then redraw every pixel that is dynamic (leaving terrain unchanged) but I felt like the overhead would be higher - maybe I am wrong about that? Wouldn't I just create a new problem of combining separate images (the terrain texture, then the dynamic pixels texture after erase/movement calculation) into one image. Plus that creates a problem with dynamic pixels colliding with other dynamic pixels, which I don't want...
-This is what a level looks like (before iterating through purple pixels and erasing them)
AND EVEN MORE info to show the problem (player is represented by red X of pixels) :
See that dang leftover pixel problem? How will I make that go away????
Answer by nesis · Feb 01, 2014 at 11:43 PM
Store a copy of your original texture, then write that to your other Texture2D along with the pixel.
I appreciate your answer. I came up with a sort of over complicated answer while waiting, which was to take the players position, and iterate through two loops, one for x and one for y (so like "for(...for(...))" inside each other - err nested?) and found I could write the new ones, and then apply, then erase the now "old" ones, and write the new ones. I am probably super over-doing it and could make a blank texture each frame, then just redraw where they are updated at.... I will probably try something like your suggesting though again, and see if I can avoid doing this "double for" strategy I have working right now. If it works I'll select your one sentence answer to my wall of text as correct :P
Okay so tried again to create a "clear texture, redraw" way to do it, had to separate the static pixels texture/mat and the dynamic pixels ones, so I could draw to them separately and compare, and for now I will experiment more to find what works best, so thanks for your suggestion!