Hey guys, I need to set a specific pixel in a texture to a custom color, for doing that I made a Bitmap and I always set the pixel in the bitmap, then I recreate the texture from the bitmap. Doing that is very time consuming, is there any way to modify the pixel directly in the texture?
A:
Yeah you should be able to do it by using the Texture.LockRectangle function. You then need to seek to the correct pixel which will be addressable by jumping to ((y * pitch) + (x * bytesPerPixel)) in the stream. The you write your new pixel and call Texture.UnlockRectangle.
Goz
2009-10-14 14:46:19
In what stream?
Tamir
2009-10-14 15:16:58
The Stream returned by LockRectangle ...
Goz
2009-10-14 15:18:40
And how do I found the bytes per pixel and the pitch?
Tamir
2009-10-14 17:03:34
The pitch is returned by the LockRectangle call and the number of bytes per pixel is defined by the texture format. A8R8G8B8 is 4 bytes and A1R5G5B5 is 2 bytes, for example. Add up the bits and divide by 8 to get teh number of bytes. ie A8R8G8B8 = 8 + 8 + 8 + 8 = 32 bits / 8 = 4 bytes. Of course if the texture is compressed you are going to have a lot of problems. Its not a simple process modifying a compressed texel and, tbh, a texture that you need to update in this way ought not to be compressed.
Goz
2009-10-14 17:18:05