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. ;)