Accessing pixel values of Texture2D

来源:互联网 发布:用java做幸运抽奖 编辑:程序博客网 时间:2024/06/05 11:54

I was wondering if there is a way to read/write pixel values on a UTexture2D object (e.g. read values from a heightmap or draw something on a texture).

I've seen that there are FTexture2DMipMaps stored on the Texture that contain BulkData, which might be the pixel data of the texture. My idea was to get the first mip map and access its data somehow.

Is this even possible? Maybe somebody could give me some pointers in which direction to go.

Thanks!

Product Version: Not Selected
Tags:texture2dpixelmodifyread write
more ▼

asked Apr 07 '14 at 4:38 AM

TaurusI76 gravatar image

TaurusI76 
41  1  4  6


1 answer:sort voted first 
19

First you need to understand that a texture is normally, a sum of multiple images called MipMaps. MipMaps are down-scaled versions of your images, always in steps of power of 2, so the original image, is, say, 512x512 - this would be the MipMap "0", then the engine generates the MipMap "1" which is 256x256 and then MipMap "2" which is 128x128, this continues on down to 16x16 I think. The farther away the texture is rendered, the smaller version of the image is used. This means that you need to access the mipmap 0 of your texture.


First we need to access the mipmap 0 of the texture, it is important to only access the data we need through refrences or, like I did here, pointers, either will do. We do this to directly access the original data in the memory instead of copying that data into a local variable. This saves memory and speed:

  1. FTexture2DMipMap* MyMipMap = &MyTexture2D->PlatformData->Mips[0];
  2.  

With this, we can access the BulkData which contains the raw image data:

  1. FByteBulkData* RawImageData = &MyMipMap->BulkData;
  2.  

Considering this texture data is used in the rendering thread, we need to lock it to make sure nothing else can access it while we are using it, if we didn't, we could cause memory corruptions or crashes. So now we lock the image data which will also return a pointer to it - which we can then go ahead and cast the unformated data into an array of FColor's:

  1. FColor* FormatedImageData = static_cast<FColor*>( RawImageData->Lock( LOCK_READ_ONLY ) );
  2.  

The FColor array is ofc 1 dimensional. So to access certain parts of the image, we need to use the width and height of the texture to calculate the position of the pixel we are wanting to lookup. Here's a small statement that does just that:

  1. uint8 PixelX = 5, PixelY = 10;
  2. uint32 TextureWidth = MyMipMap->SizeX, TextureHeight = MyMipMap->SizeY;
  3. FColor PixelColor;
  4. if( PixelX >= 0 && PixelX < TextureWidth && PixelY >= 0 && PixelY < TextureHeight )
  5. {
  6. PixelColor = FormatedImageData[ PixelY * TextureWidth + PixelX ];
  7. }
  8.  

Now, for demonstration purposes, I broke down each step. We could easily access and cast this in one line:

  1. FColor* FormatedImageData = static_cast<FColor*>( MyTexture2D->PlatformData->Mips[0].BulkData.Lock( LOCK_READ_ONLY ) );
  2.  

And now that we are done with everything, we need to unlock the memory again:

  1. RawImageData->Unlock();

or

  1. MyTexture2D->PlatformData->Mips[0].BulkData.Unlock();
  2.  

Hope this helps.

more ▼

answered Jul 23 '14 at 3:16 PM

xC0DEB10C gravatar image

xC0DEB10C 
168  8  74  32

Grogger gravatar image Grogger Jan 23 '15 at 12:35 AM

I've tried this without success. A completely black image (32x32 DXT1) is returning values like:

  1. (R=0,G=0,B=0,A=0)
  2. (R=170,G=170,B=170,A=170)
  3. (R=0,G=0,B=0,A=0)
  4. (R=170,G=170,B=170,A=170)
  5. etc...
  6.  

Does the mip map raw data already account for texture pitch?

Edit: I just realized this post was old, hopefully you (or anyone) is still around to answer the question though.

TalkingGoose gravatar image TalkingGoose Jan 29 '15 at 3:05 AM

The given solution seems to work fine if the following settings are used for the texture;

  • Mip Gen Settings: NoMipmaps

  • sRGB: false

  • Compression Settings: TC Vector Displacementmap

This article might also be of use.

PowerDesign gravatar image PowerDesign Feb 17 '16 at 2:03 PM

It works for me. Is it possible to sample the texture with bilinear filter? I don't want the exact pixel value, I need the interpolated value.

Does locking/unlocking frequently affects performance?

Thanks!

BálintYYY gravatar image BálintYYY May 14 '16 at 1:08 AM

Can you answer this question in bluepprints, Thanks!

Ragethunder gravatar image Ragethunder Jul 30 '16 at 8:55 PM

Hey guys! I've done the exact same thing and my editor keeps crashing when I hit play. What I did was make a public Texture2D reference variable in my blueprint class created from my c++ class, asigned a image with NoMipmaps, sRGB: false and VectorDisplacementmap (RGBA8). I've then called this code via ReadPixel function I've exposed to ue4, but it crashes. What I've managed to do is locate that PlatformData->Mips[0] crashes the editor for some reason. Any ideas?

MaxPower42 gravatar image MaxPower42 Aug 06 '16 at 1:45 AM

Hi. I used this code to read layer-info from landscape weight map textures.

But what if I want to modify the texture? I changed the lock mode to read/write and wrote colors to the pixel-array, but I didn't see any effect afterwards :(

MaxPower42 gravatar image MaxPower42 6 days ago Newest

Help please!!

I was able to dynamically change textures by procedurally editing their bulk-data, but the next time I restart my project/editor all the changes are reverted.

So how do I save the modified bulkdata to disk? I saved the texture asset after modifying it, but that's not enough.

edit: actually, nevermind. I found the "persistent" data under UTexture2D::Source.


0 0
原创粉丝点击