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!
asked Apr 07 '14 at 4:38 AM
TaurusI76
41 ● 1 ● 4 ● 6
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:
- FTexture2DMipMap* MyMipMap = &MyTexture2D->PlatformData->Mips[0];
With this, we can access the BulkData which contains the raw image data:
- FByteBulkData* RawImageData = &MyMipMap->BulkData;
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:
- FColor* FormatedImageData = static_cast<FColor*>( RawImageData->Lock( LOCK_READ_ONLY ) );
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:
- uint8 PixelX = 5, PixelY = 10;
- uint32 TextureWidth = MyMipMap->SizeX, TextureHeight = MyMipMap->SizeY;
- FColor PixelColor;
- if( PixelX >= 0 && PixelX < TextureWidth && PixelY >= 0 && PixelY < TextureHeight )
- {
- PixelColor = FormatedImageData[ PixelY * TextureWidth + PixelX ];
- }
Now, for demonstration purposes, I broke down each step. We could easily access and cast this in one line:
- FColor* FormatedImageData = static_cast<FColor*>( MyTexture2D->PlatformData->Mips[0].BulkData.Lock( LOCK_READ_ONLY ) );
And now that we are done with everything, we need to unlock the memory again:
- RawImageData->Unlock();
or
- MyTexture2D->PlatformData->Mips[0].BulkData.Unlock();
Hope this helps.
answered Jul 23 '14 at 3:16 PM
xC0DEB10C
168 ● 8 ● 74 ● 32
- Accessing pixel values of Texture2D
- OpenCV.2.Computer.Vision.Application.Programming.Cookbook--Accessing pixel values
- Accessing Domain Fixed Values
- How to get access to edit/get pixel values of Mat in OpenCV
- Get RGBA pixel values from UIImage.
- 访问数组中的值(Accessing Values in an Array)
- List of Values (LOV)
- Accessing of Rows in Silverlight DataGrid
- Accessing Members of an Enclosing Class
- Unity--A simple way of fading a Texture2D
- pixel
- Pixel
- Understanding NUll values of Oracle
- Heptavintimal Encoding of Ternary Values
- Theory of Basic Human Values
- Tow different ways of accessing Datasource in Alphablox
- Applied Reflection: Dynamically accessing properties of a class at runtime
- Name lookup, templates, and accessing members of base classes
- clock、GetTickCount的计时
- 解决连续枪声对上一次的覆盖问题【unity3D】
- android 打造真正的无限循环viewpager
- 关于设置ActionMode 的背景
- sybase数据库优化
- Accessing pixel values of Texture2D
- 何凯明的深度残差网络 ICML2016 tutorial
- java中的反射机制应用
- 安卓Studio实用快捷键
- 这个开源项目展示了如何实现一个横跨各种Android平台的音乐播放器,包括手机,平板,汽车,手表,电视等。Google官方推出,跨平台开发必看项目。
- php traits初学者的初步理解
- nginx服务器ob_flush和flush不起作用
- HtmlParser的开发实例
- React Native 学习笔记四(关于state的使用)
I've tried this without success. A completely black image (32x32 DXT1) is returning values like:
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.
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.
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!
Can you answer this question in bluepprints, Thanks!
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?
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 :(
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.