If you've ever wanted to generate a texture from C++ in UE5, here's one way to do it.
I've never packed up an Unreal plugin before, so maybe the plugin in the src
folder just works for you if you copy it into your project folder, š¤·
- Add the
BP_DynamicTexture
actor to your scene (it should be a cube with theMaterial_Dynamic
already set on it, andDynamicTexture
component already added) - Run the project and you should see a cube with a pattern like shown above
- Modify the
UDynamicTextureComponent::TickComponent()
function to modify the texture in whatever way you want - Go back to Unreal, hit
CTRL-ALT-F11
to trigger a hot rebuild (do NOT use build from within Visual Studio) - Repeat from 3 until you have what you want
If the plugin fails for whatever reason, here's how I went about doing this.
- navigate to the
Tools
menu - select
New C++ Class
- click
Show all classes
- type
ActorComponent
- name it whatever you like, I called mine
DynamicTexture
Unreal will do its thing, then should show you your files within Visual Studio
- you should see a file ending with
.Build.cs
near your new source files (mine isDynamicTexturePlugin.Build.cs
) - you should see existing dependencies such as
"CoreUObject", "Engine", "Slate", "SlateCore"
- add
"RHI", "RenderCore"
to the end, otherwise you'll get linker error
- copy the source from the repo into whatever files you've created
- DynamicTextureComponent.cpp
- DynamicTextureComponent.h
- pre-empting the next step, we need a blank texture
- create a texture the same size of what you're going to use via code, for me it's
1024x1024
- save it as a PNG and import it into Unreal (I called mine
Texture_Blank1024
) - double-click it to open it
- change
Mip Gen Settings
toNoMipmaps
- change
Compression Settings
toVectorDisplacementMap (RGBA8)
(which ironically results in aB8G8R8A8
texture for us to use) - uncheck the
sRGB
checkbox
- right click on your content browser and click
New Material
, I named mineMaterial_Dynamic
- double-click it to open it
- left click the
Base Color
pin and drag it off to the side, then release it - in the popup that appears, type
TextureSampleParameter2D
to create that node - it will be asking for the name of it, make sure to name it
DynamicTextureParam
- with the node still selected, note the
Material Expression Texture Base
section of theDetails
tab that should be visible somewhere - change the texture from
DefaultTexture
to the texture you setup in the previous step (Texture_Blank1024
for me) - change the
Sampler Type
toLinearColor
(this would fail with the default texture, which is why we created our own first)
- right click on your content browser and click
Blueprint Class
- show all classed
- selected
StaticMeshActor
- I named mine
BP_DynamicTexture
- double-click it to open it
- select the
StaticMeshComponent
component and select a mesh, any will do, a plane works - change the material it's using to be the material we created above (
Material_Dynamic
for me) - now add a new component, the component we created in the first step (
DynamicTextureComponent
for me)
Now you should finally be able to run your project and see a changing texture!