Share code between shaders

Hi guys,

is it possible to share static shader code between multiple shaders? I am aware of the #include<…> feature, but this only works for predefined shaders of the babylon library. Is it possible to use #include<…> for my own written files?

Example: I have a gpu-noise function and i want to use this in different fragment shaders. To do this, I have to copy the whole block of code all the time.

greets,
Tarek

You can place your code in the shader manually so that include would work as well:

BABYLON.Effect.IncludesShadersStore["myfunctions"] = "..."

and then in your shader use

#include<myfunctions>

3 Likes

Cool, thank you!

dude waaaaaa… for real… holy crap. I feel like @sebavan just gave me a golden ticket. I wish this was talked about in the docs somewhere as it is extremely useful and powerful. Sometimes you just need somebody else to ask a question you were not thinking of… Thank you @treksn

1 Like

Fancy doing a PR for the doc ?

Got you fam

2 Likes

https://www.babylonjs-playground.com/#0MAYNY

There’s that, finishing up the writeup now.

1 Like