Individualized shader effects

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
WizardofChaos
Posts: 17
Joined: Tue Jan 11, 2022 8:43 pm

Individualized shader effects

Post by WizardofChaos »

Hi there! I'm still working on a few shader effects and the tools for Irrlicht seem to be generic -- that is I'd use the same shader object for absolutely everything involved with that shader. If I wanted to include someone's health percentage as a shader parameter, for example, how would I do that? Would I just create a separate shader call-back and recompile the shader?
Local man gets so irritated with modern space sims that he starts writing his own
CuteAlien
Admin
Posts: 9644
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Individualized shader effects

Post by CuteAlien »

This one is probably best passed as a uniform variable in OnSetConstants of your shader callback.
You can also create more than one material per shader-code by recompiling it, but you should do all compiling (aka all addHighLevelShaderMaterial calls) once at the start (or at least definitely not per frame). Note that OnSetConstants is called for every object that draws with that material. So you can set different uniform value for different objects which share the same material.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
WizardofChaos
Posts: 17
Joined: Tue Jan 11, 2022 8:43 pm

Re: Individualized shader effects

Post by WizardofChaos »

CuteAlien wrote: Sun Jun 11, 2023 7:10 pm This one is probably best passed as a uniform variable in OnSetConstants of your shader callback.
You can also create more than one material per shader-code by recompiling it, but you should do all compiling (aka all addHighLevelShaderMaterial calls) once at the start (or at least definitely not per frame). Note that OnSetConstants is called for every object that draws with that material. So you can set different uniform value for different objects which share the same material.
Sorry -- when you say "uniform variable" what do you mean? Like using the "userdata" parameter? How would I actually do that?
Local man gets so irritated with modern space sims that he starts writing his own
CuteAlien
Admin
Posts: 9644
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Individualized shader effects

Post by CuteAlien »

Uniform variables are variables in your shader which can receive new values from the CPU. Meaning you can set them in c++.
In Irrlicht you set them with setVertexShaderConstant and setPixelShaderConstant (I do not know why they are called "constants" in Irrlicht, maybe because they kinda replace some of the things which were previously done with constants? Or maybe they were typically called constants before hi-level shader languages existed? Or maybe because the variable name is constant per shader? Anyway - hlsl/glsl and all other modern shader languages call them "uniforms". And we should probably mention that in the documentation...).

Take a look at example 10 - we use a few of those. Basically you just have to ensure they are marked as "uniforms" in the shader code (in glsl, while in hlsl all global variables are uniform by default anyway so nothing to do there) and have the same variable name when you access them in c++. If you use Irrlicht trunk instead of Irrlicht 1.8 then you can also first get an id for the shader uniform variable name and then call setVertexShaderConstant with the id instead of the name (which is a lot faster as it can avoid a lot of string comparisons, otherwise it's the same).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
WizardofChaos
Posts: 17
Joined: Tue Jan 11, 2022 8:43 pm

Re: Individualized shader effects

Post by WizardofChaos »

Oh, yeah, I got that part, what with setting matrices, I meant specifically like with health bar info being passed in -- so, like, if I wanted to use the same callback, I could maybe pass in the ID for the node as "user data" when adding the shader material, and then pull info off that?
Local man gets so irritated with modern space sims that he starts writing his own
CuteAlien
Admin
Posts: 9644
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Individualized shader effects

Post by CuteAlien »

Ah, you mean what you want is to set per node data for a shader material? You got sevaral options there.

Simplest one is the one you mentioned already. If it's just about one node being different for a one variable then it's often simplest to just add another shader material so you can pass a new callback for that specific node.

Another option sometimes is to abuse some SMaterial variable which you don't need in your shader. OnSetMaterial is called every time the material changes, so you can copy that value in there and set it later in OnSetConstants. Just have to be a bit careful not to use a variable which has other side-effects. And also it comes with a small cost as it increases material changes and those are not completely free.

And if you render with the SceneManager you can add a lightmanager to find out which node is rendered right now. And then your shader callback can for example ask your lightmananger about which node is active right now. It's probably the cleanest solution if you got a bunch of per node variables to set.

edit: Since around [r6569] (November '23) svn trunk also supports UserData for SMaterial which also can be useful for this
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply