Download

Material Effects: Changing Material or change Material Instance Parameters?

Hey everyone,

I’m currently working on a project where it will be necessary to switch Materials during runtime or at least change Material Instance parameters for some texture based effects and I’m not sure how to go about it. I’d really like to ask for some opinions and help on this matter. If you have suggestions on how to achieve something like this I’m happy to hear them.

On to my “problem”:

My characters will have the ability to infuse their weapons with different Attack Elements. I wanted to combine texture effects on the weapon mesh itself as well as particle effects.
The result looks like this.

Currently I work with a skeletal mesh (the sword) and a mask texture which is used for the emissive color (the glowing of the blade). I then created a Material Function for the glow effect and placed it in the original Material of the sword asset.

After that I created a Material Instance from the Material and added it to the skeletal mesh of the sword. I control the glowing of the blade with a scalar parameter value and added a particle effect to the Sword Actor to bring the effect to life.

Right now I’m happy with how it turned out visually, but I struggle with adding more elements to it.

I thought about creating Material Instances for every Attack Element there is and then just tweak the Instance Parameters. This would mean that I have to store the Material Instances somewhere and swap them out during runtime when the Attack Element changes.

The question is, is this a feasible idea or will this lead to performance problems? I could either use **one **material instance and tweak the parameters during runtime or I’ll just change the Material Instance which will be created beforehand for each Attack Element. But I don’t know which solution is better in terms of shader complexity and performance.

Thanks in advance and please let me know what you think and if there are other solutions all together for these kind of effects.

Wouldn’t you have an easier time making a variable shader that operates off one scalar parameter?

You can use If to determine that that scalar is.
And you can therefore use an infinite number of integers or float values to bring about completely different materials values.

Also, let’s be clear. All you should be changing is the Albedo. The rest probably stays the same. There should be near no reason to change the normal map.

Keep in mind that one thing is doing math - usually cheap, even when complex.
and another is using multiple textures.

So, the best approach would be to create a texture that goes from black to white which you can therefore use as the alpha input on a lerp - where you can pre-set different colors.

the flame and the distoetion from the heat should be all handled by the particle effect anyway.

After you make the material correctly you can even mess with the instances - and obviously any parameter can be changed at runtime already, so you wouldn’t necessarily need to use a few IF statements. however I find it easier to create a master material for all the same types of items and customize instances to taste visually - so the if statements would be the way I would go about it.

Thank you for your reply!

I’m honestly not sure if I completely understood what you are describing. So if I understand you correctly what you’re suggesting is creating one Master Material for all Swords in the game, with variable Textures and one Scalar Parameter which can be tweaked on instances of the same Material to switch between different sets of predetermined colors with a lerp that is driven by a gradient texture?

Since some days passed after posting my question I kept on working on this matter. My current solution for the Problem is as follows.

  1. I created a Material Function for the Enchantment Effect, which incorporates the enchantment effect on the weapon as well as the capability for rim-lighting, driven by parameters. This Material Function only outputs Emissive information.

  2. I created a Master Material for my Sword and plugged the Material Function into the Emissive Color Channel of the Master Material.

  3. I created instances of the Master Material for each Attack Element in the Game. Some use rim-lighting, some do not. all use different color schemes and masks

  4. I created a Struct holding the Material Instance, a Start Particle Effect, a Loop Particle Effect, and a Finish Particle Effect and a Weapon Trail Particle Effect.

  5. I created a Base Weapon Class which holds a TMap of EAttackElement and FEnchantmentEffect and the Weapon Mesh, a Trail Particle Component and an Effect Particle Component

Whenever my Attack Element switches, the correct struct for the Attack Element is selected and the Material Instance is set as a dynamic material instance on the Weapon Mesh and cached, the start particle effect is played on the effect particle component, followed up by the loop particle effect. I also set the Trail-Effect on on the corresponding Trail Particle Component.

I cache the Dynamic Material Instance to update 2 Scalar Values over time, one for Enchantment Opacity and one for the progress, since the Effect builds up over time and fades out when the Enchantment ends.

When the Enchantment ends, I play the Finish Particle Effect on the Effect Particle Component and reset the Scalar values and the Weapon Trail to default.

My question originally aimed for performance or possible problems when switching or creating different material instances during runtime. But as far as I can tell this does not affect the performance at the moment.

I really would like to hear your oppinion on my solution but I’m also interest in how you would go about it in greater detail since I’m not sure if I understood your suggestion correctly.

Thanks in advance!