Master Kenth

Personal portfolio site

GameDev Xmas Challenge – Hour 6-8: Git and procedural texture generation in C++

Back from the dead to continue on my #OneHourGameDev contribution. Been busy working on another title and fatigued from working on said title, but now I’m ready and I plan to catch up with the lost hours over the course of the next couple of days.

So what have I been up to then? First I configured and pushed my entire project to Github so that I have “checkpoints” along the way in case something goes horribly wrong. I plan to make a git commit every session unless stuff is broken.

The big thing I’ve been working on however is a system for dynamic/procedural texture generation! My primary use-case for this function is that I want end-users to be able to draw their own tic tac toe markers, instead of using the dull and worn X’s and O’s. Except for the ghastly copy-paste errors I made that caused debugging to take an extra 30-40 minutes the dynamic texture generation actually works!

2015-12-11 12_13_15-TTTProject Game Preview Standalone (64-bit_PCD3D_SM5)
A dynamically generated piece of texture

The image might not say much but programmers might get a tingle from knowing that the red pixels and the blue dot is made entirely by directly manipulating the pixel data and updating it into the texture in runtime.

So how is this done? Surprisingly easy actually. The core function, which takes a texture reference, some pixel data and some extra stuff is readily available as  void UTexture2D::UpdateTextureRegions(...), however this function is compiled with an #if WITH_EDITOR directive. Not sure why it’s only available in the editor however the function still works correctly if lifted out and put into your own class, with a few minor tweaks.

I followed this very well written UE4 wiki article about procedural materials (which also mentioned this wiki article that outlines the function mentioned above) and I took the code listed apart and worked out which parts did what and what minimum code was needed to get the texture to update dynamically in runtime. It was a bit tricky though, as the article made a class based on class AStaticMeshActor and I wanted something more generic. The primary difference was how the dynamic material instance was created, but after some searching a found a class-agnostic method.

I finally ended up with the code below. Note to UE4 C++ veterans that it might not be pretty, or follow all conventions and what not, but with my limited UE4 C++ knowledge I’m glad it’s working at least. Plan to clean up later 🙂 .

Sorry I don’t have a lot of expressive comments in the code yet, but basically the main meat is the  uint8* mDynamicColors array which holds all the pixel data, where every 4th element is a new pixel, and the elements in between are the color channels, in the BGRA format. So for example  mDynamicColors[0] is the value of the blue channel (in the range 0-255) in the first pixel (top-left). It is then passed into the  void UpdateTextureRegions(...) function, which is an almost exact copy of the UTexture2D function with the same name. Apparently this should be performant enough to do every tick, however further investigation (especially for mobile devices) is needed.

As for the editor setup needed, the variable  UMaterialInterface* DynamicMaterialBase; must be set to a material following the outline in the wiki article, but basically it needs a TextureSampleParameter2D  node set with Sampler Type to “Color” (as opposed to the default “Linear Color”). Its parameter name should match the  FName MaterialTextureParameter; parameter on my class. I also setup my material to be translucent and unlit for its intended usage.

2015-12-11 12_36_59-TTTProject - Unreal Editor
Material setup

Then just create a blueprint actor based on class ProceduralTextureActor and it should be ready to go! The instanced material is publically available as  UMaterialInstanceDynamic* DynamicMaterialInstance; and can be used as the material for any supported component, such as a PaperSpriteComponent. Here is how the dot was done in blueprint (the red fill is the default I used for testing):

2015-12-11 12_39_56-TTTProject - Unreal Editor

So that’s it for now. I’m still 3 hours behind total and have to make it up so expect a few heavier updates the coming days. My next step is to get a better brush drawing algorithm going as well as mapping touch input to drawing actions.

As always, here are the time-lapses, though I still decided to split them up into 1-hour segments:

 

 

2015-12-11 14:02 ( mode_edit 2015-12-1216:05)

Comments

No comments yet. Why not leave one yourself?

Leave a Reply

Your email address will not be published. Required fields are marked *


*