Drawing on textures in real-time

I want to be able to draw on a texture and update it in-game, so it is shown in-game.
We attempted following this tutorial: A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums
However we got stuck at the “Dynamic Texture & Dynamic Material” title.
This was because we could not find out what types were used and what the variables represented.

Any help with this problem would be highly appreciated.

Okay, I’ll run you over what I can tell just by looking at that section.
All of the code in that section is run within an overriden PostInitializeComponents from a class derived from AActor.

UTexture2D::CreateTrasient(int32 InSizeX, int32 InSizeY, EPixelFormat InFormat) returns a pointer to a empty UTexture2D.
mDynamicTexture is a pointer member variable in which they’re storing the empty texture into.
The “CompressionSettings” is a public variable which they’re setting to TC_VectorDisplacementMap to disable compression.
The “SRGB” is a public variable they’re setting to 0 to disable gamma correction which skews data.
The “AddToRoot” function is called to make the texture a root object and prevents it from being GCed.

The textureToReadFrom variable seems to be a pointer to the texture they created at the start of the article. I don not know how they’re supplying the value to the function, but I assume it is a pointer member variable.
The “GetSizeX” and GetSizeY" functions are really self explanatory.

“FTexture2DMipMap& readMip = textureToReadFrom->PlatformData->Mips[0]” This seems a little confusing, but all they’re doing is referencing the first mipmap inside of the texture.

mDataSize and mDataSqrtSize are used by UpdateTextureRegions.
However, it openly assumes that width and height are equal and powers of 2.
mDataSize is a member variable of type int32 (should probably be uint32) which they’re calculating from the width * height * 4 of the textureToReadFrom.
mDataSqrtSize is another member variable of type int32 (should probably be uin32), but its formula is wrong. It should be (width * 4) / 2 to actually be the square root.

Now, the “readMip.BulkData.GetCopy((void**)&mTextureColors)” has a lot of terrible dark magic, but all it does is copy the mip map’s data to a member variable called mTextureColors.
mTextureColors has to be an array and it is probably an array of uint8 since mDynamicColors is.
mTextureColors is probably set through more dark magic in the actor’s constructor.
mDynamicColors IS an array of uint8 which is equal to mDataSize.

FMemory::Memcpy(mDynamicColors, mTextureColors, mDataSize) copies all of the color information in mTextureColors to mDynamicColors. So, mTextureColors definitely has to be an uint8 array.

mUpdateTextureRegion is a pointer member variable which contains the update region of the texture.

Last, but not least, the "mDynamicMaterials[0]->SetTextureParamaterValue(“DynamicTextureParam”, mDynamicTexture) updates the material’s “DynamicTextureParam” to the newly generated dynamic texture.

I am also trying to get it to work and i am not getting any result.
Thank you zacharymwade for this explaination, but would it be possible to have a working exemple of that for non-programming people ?
the wiki page got a sample straight out of another project that won’t work unless we have the whole thing :frowning:

Hey, i put together a simple example
hope this helps.

just to make it interesting, this example runs Conway’s Game of Life and renders it on a 256x256 texture


Ah ! thank you very much, i will take a look at what you did !