Hi, my player character has lots of spells. One of them, freezes the enemies.
I created a shader that visually freezes the enemies, should every enemy have this shader from the start? Even if the player is not using that freeze spell? Or is there a way to add the shader in real time to the enemy when he gets hit by the spell? Witch one is better / more optimized?
It’s pretty difficult to give you definitive answer to most of this but I’ll take a stab:
is there a way to add the shader in real time to the enemy when he gets hit by the spell?
Yes, you can either change the material on the MeshRenderer Component or you can change the Shader on the material. Alternatively, you can simply change the parameter(s) passed to the existing shader if it supports both frozen and unfrozen.
should every enemy have this shader from the start? Even if the player is not using that freeze spell?
This all depends on the complexity and compatibility of the shader with the way your enemies normally look when not frozen. If it was simply a linear combination that looked something like this:
enemyColor = normalColor * (1.0 - frozenPercent) + frozenColor * frozenPercent;
then the cost having it on all the time is neglible and other issues notwithstanding, this is a good way to go. For more complex shaders, you would need to weigh the cost of having it on all the time vs swapping it in only when needed. This brings us to your last question:
Witch one is better / more optimized?
There are many operations with potentially higher than expected costs to them. Often, you hear the biggest culprit of performance overhead being attributed to “draw calls”. A sub-issue of many draw calls is that this often results in additional state changes. GPUs love to do the same thing repeatedly over and over so if you can line operations up for them like dominos, they will plough through. This concept is generally referred to as batching. However, when you line different non domino-things up, the video card must reconfigure itself to accomodate them which interrupts the high speed flow of operations. If all of your enemies have the same material/shader, then the GPU can process them in a less interrupted fashion but when some of them have different materials/shader, the GPU must change states which incurs a performance penalty.
Given all of that, you must profile both to determine which one will work best for you.