Godot version: 3.1
Issue description:
When writing custom particle shaders, developers usually need some kind of random number generator. Because none is present in the Godot shader API, the easiest way is to look into particles_material.cpp and copy rand_from_seed from there (hash may be also useful for seed generation). Wouldn't it be much more comfortable to add these functions to the shader API and automatically include their declaration in the shader once they are used?
To get them, just create a new ParticlesMaterial. Then click the dropdown next to the material and select "Convert to ShaderMaterial". That will create a shader with all the functions from the default particles material.
While that's true, I don't need all the functions. I just need random numbers, which could easily be included in a "standard library".
The issue with a "standard library" approach is that it increases the compile time of all shaders. Since shaders are compiled at run time, it would increase the length of the already noticeable hiccups that occur when a new object enters into view. The current approach is to minimize the code that Godot adds behind the scenes in order to keep compile times down.
That being said, maybe someone could create a standard library system similar to https://github.com/godotengine/godot/pull/31263 That way users could define functions they want in all shaders on a per-project basis and it would not add bloat to the engine.
Would it be possible to detect which "standard library" functions are used by a shader, and include only those functions?
Yea. That's entirely possible. IMO what your describing is the best approach. It's kind of done in GLES2 already in stdlib.glsl. But it would still add overhead to Godot's shader preprocessor step.
However, if you limit it to functions defined in a shader library, then it may not add much overhead. There is also the recently implemented visual shader plugin. We could implement a similar shader plugin system.
Most helpful comment
Yea. That's entirely possible. IMO what your describing is the best approach. It's kind of done in GLES2 already in stdlib.glsl. But it would still add overhead to Godot's shader preprocessor step.
However, if you limit it to functions defined in a shader library, then it may not add much overhead. There is also the recently implemented visual shader plugin. We could implement a similar shader plugin system.