Save a shader to be reused by other shaders..

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Maloherve

Hi,
I’m trying to save a noise shader I made in order to import it in other shaders.
I saw that it was possible using a sampler2D with a noise texture, but the only one available is the OpenSimplexNoise, whereas I’m trying to use a custom noise (for exemple cellular noise).
Can this be done ? How can I save a shader in a form that would be reusable in this case ?
Thanks.

Have you looked at the menus for saving a shader, i.e. select the node, then go to InspectorMaterialShader and selecting “Save” from the dropdown menu?

Ertain | 2020-05-19 19:05

I have but none of the available save types allow to be imported into a sampler2D in my new shader.

Maloherve | 2020-05-20 06:58

:bust_in_silhouette: Reply From: Zylann

I saw that it was possible using a sampler2D with a noise texture

A shader is not a texture. You cannot assign a shader as parameter of another shader in this way. You can make your shader render to a viewport and then assign the ViewportTexture.

How can I save a shader in a form that would be reusable in this case ?

Unfortunately it is not possible to use functions from another shader inside a new shader. This is sometimes supported in other engines by using a form of #include so the same code can be re-used (like a fancy copy/paste), which was proposed but never added so far ( [WIP] Add basic shader preprocessing support by bitsawer · Pull Request #17797 · godotengine/godot · GitHub ).
At the moment the only way is to move your code into functions and copy them across all shaders where you want to use them.