Hi! I want to talk about shaders and creating a preprocessor for them.

Godot's shading language is based mostly on glsl, and glsl is based mostly on C. C and glsl both use a program known as a preprocessor, which changes the source code before it gets compiled. For example I may be able to define a macro that defines a small function that I may use a lot in a shader, like saturate.

#define SATURATE(x) clamp(x, 0, 1)
...

float number = SATURATE(other_var);

But what maybe the most useful feature is the ability to include functions, variables, and even other defines from another file. So there's no need to duplicate the same code in multiple related shaders.

#include "res://common_lib.txt"

// From include
do_something();

Name me one shader designer that wouldn't love this ability. I know I would. :)

Now most if not all of us know that a shader preprocessor doesn't seem to be available in the Godot editor. But that doesn't stop us from creating a plug-in that adds this. I'm thinking it could go like this. Create an extended shader object that will have this macro ability. Because this would have to be done so that if the shader's code changes, it can preprocess it on its own without the editor's intervention. Create a bottom row editor similar to the shader editor that supports macros and such, and show errors that may appear.

What are your thoughts on this matter?

Edit: Well I took the liberty to start this project. :) Edit2: Ok. I think I'm done with this. I think. Feel free to make pull requests or just plain fork it. ;)

Interesting subject, especially since this could also lead to implementing node groups into visual shaders. Which is a thing the visual shaders need something bad.

5 days later

Well I don't know about anyone else, but I'm now currently working on the preprocessor. See ya! ==(ノ_)ノ

Some progress made! <( ̄︶ ̄)↗ https://github.com/SIsilicon/Extended-Shader-Plugin

I'm kinda stuck on making the editor though. I tried copying the shader editor plugin from the source, but unfortunately some of its functions are private and need to be recreated as well, which might take I well. I won't give up though!

5 days later
17 days later

I saw custom nodes and expressions mentioned but no groups. Still, a very positive turn of events to be sure.

@Megalomaniak said: I saw custom nodes and expressions mentioned but no groups. Still, a very positive turn of events to be sure.

Indeed! I barely touched the visual shader since its release, but this could make me reconsider the way I make shaders. =)

3 years later