Reading the pixels from a UTexture2D is not particularly difficult, indeed this post on Unreal AnswerHub resume almost perfectly how to do it. However, there are some points missing and one could go in the case where a call to RawImageData->Lock(LOCK_READ_ONLY) will return nullptr. When it happens, it prevents us from reading the pixels, and moreover, potentially causes a huge crash if the situation wasn’t anticipated.
We will review quickly how to read the pixels from a UTexture2D, and how to avoid the situation where nullptr is return when locking the image.
Reading the pixels.
RetroStyleGames - 2D 3D Art Outsourcing Studio and Indie Game Developer. Download Free Textures, 3D Models, Isometric Sprites, Spine Animated Characters. Elevate your workflow with the Pixel Art Textures asset from Lost Algorithms. Find this & more Textures & Materials on the Unity Asset Store. In the case of texture, OpenGL also needs an RGB color for each pixel, and instead of combining vertex colors, it read it in the texture. Consequently, the question is: which pixel of the texture OpenGL should read? Here come UV maps: it is the set of 2D coordinates in the texture that defines the pixels to read.
As stated in the AnswerHub’s post, reading the pixels of a UTexture2D can be separated in the following steps:
- Accessing the mipmap 0 of the texture (i the one with the same resolution as the original texture)
- Accessing the BulkData of this mipmap
- Locking it to access to the pixels
- Unlocking it
In terms of code, it can be written like this:
In this code sample, we can note two interresting points.
First, the result of LockReadOnly(), which returns an array to the bytes of the texture is casted to an array of FColor. It could also be casted to an array of uint8, if so, FormatedImageData[0] would represent the red channel of the first pixel, FormatedImageData[1] would the green channel of the first pixel and so on. By casting in FColor*, we have FormatedImageData[0] containing all the color channels of the first pixel.
Second, the returned array is a 1D array representing a 2D texture, we have to convert our (X, Y) 2D coordinates into 1D coordinates. Cryptey 1 8 0 download free. This is made by using the following formula: Y * MyTexture2D->SizeX + X which is used to access to the pixel (X, Y) in the FormatedImageData array.
Preventing LockReadOnly from returning nullptr.
When calling LockReadOnly we may get a nullptr in return if the texture is not in a specific format.
Indeed, and as far as I know, the texture must have the following parameters:
- CompressionSettings set to VectorDisplacementmap
- MipGenSettings to NoMipmaps
- SRGB to false
In a general case, we will assume that these conditions are not met. So, what we will do is, saving the current texture settings, applying the settings above, reading the pixels, restoring the previous settings.
Here’s the code sample to save and apply the new settings:
Pixel Textures 2d Texture
The call to UpdateResource() applies the new settings. Then, we can read the pixels using the code presented in the first part of this article.
Once the pixels are read, and the texture is unlocked, we can reapply the old settings:
Conclusion.
Pixel Textures 2d Shapes
When we integrate all the code samples detailed in this article, we get the following code:
This code have been tested and we hope it will be working for you. However, there may have some case not managed, and eventually you may find another mandatory setting when reading the pixels (Unreal documentation is not really exhaustive about it). If so, feel free to contact us so we can improve this article.
Related
2d Texture Pack
27-07-2019, 22:14/Просмотров: 94/Комментариев: 0
The Pixel Lab Material Pack For Element 3D V2 Shader | textures | 1.5 GB I’m extremely excited to introduce The Pixel Lab “Material Pack” has been converted to Videocopilot Element3D for After Effects! Over 500 C4D materials/textures to help you speed up your workflow and focus on being creative! The textures are organized into the following categories: Alpha Overlays, Everyday Color, Fabric/Leather, Glass/Transparent/Ice, Luminance, Metal, Misc., Paper/Cardboard, Patterns/Tiles,
Скачать