Recompile material at runtime

Hi, this may be an odd question but, can we ask UE to recompile a material at runtime ?
How could we do it using C++ ?
I don’t care that much about performance it’s not for a game.
Thanks!

Hello Gerry93,

Have you tried using a Material Instance instead of the base material? If that doesn’t work for you, could you give more context as to why you need to be able to recompile a material at runtime?

Because I’m using UE4 to render films, and I need to generate masks for compositing.
For opaque simple object I just need to set a new material with just some emissive color ands that works well.
But if the material has opacity, world offset, etc, I need to keep those information in my mask. So I want to recompile a material from the base material and just override the emissive color value to create a mask that keeps all those information.
I can’t go with an instance because I’m more creating a tool than doing a project, and we can’t add a parameter manually to every shader we will use in production. I’m currently playing with the source code, I tried to see if I could create a parameter that automatically override emissive for every material created, but I didn’t do well there, so now I’m trying to modify my materials at runtime when it comes to masks generation.

Hello Gerry93,

That is a very complicated and intensive thing to do at runtime and in a normal game it would most likely be impossible to do with acceptable framerates but for your purposes, I may be able to suggest a workaround. It may be best to make two separate materials for each material that you currently have and have one set to emissive while the other is not. You can then make dynamic material instances of each of these and swap the material instances at runtime. Once again, this would end up in terrible performance for gameplay but for rendering film scenes it should be an issue.

I see, thanks for your help :slight_smile: