Sign in to follow this  
SelethD

xna pixel manipulation?

Recommended Posts

SelethD    456
Is there a way in Xna to create a 2dtexture and then be able to manipulate individual pixels on it, then show it to the screen as you would any other 2dtexture?

Share this post


Link to post
Share on other sites
Flimflam    665
// Create the texture object. Args are Width, Height, Mipmap count, TextureUsage (usually set to None) and SurfaceFormat (usually set to Color)
Texture2D someTexture = new Texture2D(256, 256, 1, TextureUsage.None, SurfaceFormat.Color);

// Copy the texture out into an array
uint[] textureData = new uint[someTexture.Width * someTexture.Height];
someTexture.GetData<uint>(textureData);

// Modify pixel at 50,50 to be white
textureData[50 + 50 * someTexture.Width] = 0xffffffff;

// Copy the array back into the texture
someTexture.SetData<uint>(textureData)

Alternatively, you can use Color instead of a uint in the example above if you prefer to work with a color structure instead of a uint.

After you do that, you basically use the texture as you normally would. I would not recommend editing textures multiple times every frame or something though, as copying the data to and from the texture can be pretty costly, not to mention the allocations in C# will go through the roof.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this