How to Implement dynamic texture in c++ plugin?

I want to implement custom texture which is updated in c++ plugin, How do I expose it as normal texture for game project?

Is there any way that I can implement custom c++ texture in plugin and expose it like UMG widget?

There are several ways to do this

  • The easiest way would be to just create a RenderTarget. With this you have access to the new multi-pass rendering thing from withing the material editor and blueprints (as well as c++)
  • The next way would be to actually write a UTexture2D bit by bit and pass it to whereever you need it. You can read up on how to do that here
  • Another way would be to implement your own texture asset type. This way you can write it however you want and can just reference it using the editor like you would with any other texture. You can look into how Paper2D and other asset importer handle this to get a grip on it. You probably need to do some custom work if you want to hook it up with things that expect a UTexture2D.

Actually I’ve created UTexture2D and it is getting updated with some data in c++ plugin, currently I’ve attached with UserWidget. But I want to expose it as texture so that I can put it on any shape object like sphere or cube.
Can you pls guide me how Do I create texture asset or RenderTarget in c++ plugin?

Hey,

If you want to add it to an object, you need to use it from within a material. So you can either use a render target or a UTexture2D for that.

You can actually create MaterialInstances from c++ and set TextureParameters from there.
You can create a the MaterialInstance using this function.

If you want to go the asset route (so you can assign your textures from the content browser and the editors), it is a lot more convoluted. If you have access to the engine code, there are a lot of files to look into. A good place to start is UFactory. That pretty much let’s you create your own asset. You probably need to do some extra work to make it work with the material editor for example.

Thanks DennyR for providing useful pointers.
I’ve created asset usign UFactory and it is visible in editor under Misc but still I;m notable to figure out how I can make it Texture2D type?
I’ve tried to create My object derived from UTexture but it made asset disappear from editor.

I have never really done this, but I would look into how MediaTextures work. The MediaPlayerPlugin is EditorOnly, so there you can see the factory and the asset stuff.
The rest of it is in the Engine/Runtime/Media and MediaAssets.

They are pretty much what you are trying to do, they render movies into textures that can be used in materials.

I’m following MediaTexure implementation but still wondering how come I’m not able see my texture in editor.

Actually, it does not disappear but name of texture does not take class name. it appear as BaseTexture.

Now I’m able to render my texture after setting it in material and then on static mesh. But I could not figure out how I can access functions of these texture in blueprint which I defined in c++ class? I could not get any method which gives texture instance from static mesh.

You should be able to expose every function using FunctionSpecifiers

just mark them as BlueprintCallable and you should be able to call C++ code from BP.

And to get a texture from a mesh, I think the easiest way is to use MaterialInstances and just set your DynamicTexture via TextureParameters.

Yes, I could able to get texture instance by DynamicTexture via TextureParameters.

I’ve now different issue with Texture. I’ve created texture and made it asset using UFactory method, texture update happen in plugin. Now I want to use it in multiple places in game. means I want to have multiple instances of textures in game and each instance updated with different content. Currently when I set content to one texture, other instances of texture also get updated with same content. Although these textures corresponds to different static mesh and Actor. I guess this behavior is due to asset and it physically asset is one static instance. So my question is how Do I create different instances of textures or texture asset dynamically?