• Shaders
  • Parametric 'render_mode' & shader function library?

Hello all,

I have been tinkering with shaders lately in Godot 3.3.2 and while I am very happy with the vertex/fragment/lighting split, render_mode system & overall ease of writing shaders, I feel the workflow quickly gets out of hand when it comes to generating & maintaining multiple permutations of a shader (e.g. back-facing vs front-facing) or even when developing a 'library' of common shader functionality.

As far as I am aware, unlike the SpatialMaterial, a ShaderMaterial/shader cannot have its render_mode exposed as a parameter, so I am curious to know what your workflow is for dealing with multiple shader permutations, provided it isn't just copying and pasting any modification/fix from one shader to another.

Similarly, how do you deal with common shader functionality/math that may be used inside multiple shaders? Again the only solution that seems to work at the minute is to maintain a 'master' shader & copy & paste the 'common' functionality into each and every shader file that will need it.

I know 'Shader' has fields for 'code' & 'custom_defines', I wonder if anyone has figured out a mechanism to possibly automate the deployment & maintenance of common shader code.

One potential solution which I may attempt when I get some time to spare is to write and editor plugin to supply some common shader code with a special section surrounded by some markers (probably just a formatted comment), and have the plugin copy any functionality over into any other target shader.

Has any of you attempted something similar or know of a similar (or hopefully more elegant) idea floating around on the web?

Thanks in advance

As far as I am aware, unlike the SpatialMaterial, a ShaderMaterial/shader cannot have its render_mode exposed as a parameter, so I am curious to know what your workflow is for dealing with multiple shader permutations, provided it isn't just copying and pasting any modification/fix from one shader to another.

There isn't really a way around that, but you could use a script or editor plugin to generate all the required variations.

See also https://github.com/godotengine/godot-proposals/issues/944.

Thanks for your reply & sharing the link to the 'sharing & preprocessor' proposal.

I have seen this proposal (should have probably mentioned it in my original post) & this is something I would like to see actioned, however this topic was hinting more at existing 'solutions' or alternative workflows people had gone for, to deal with those permutations.

I guess I will have to give the editor plugin / scripts a go & see if I can come up with something that doesn't feel too clunky/patchy. I imagine something with a 'manual' update step should be fairly straight-forward. What I haven't experimented with or know much about is if changes to resources can be tracked within a tool / editor script (e.g. detect if the common shader was edited in order to trigger changes to be propagated).

I will tinker and if I manage to come up with something remotely serviceable, will report back!

Write a Reply...