Is it possible to "extend" an existing shader at runtime or not ?

hi gods of code…

let’s say i have

  • a shader A that is the main shader
  • shaders (or code blocks) B,C that do something in addition

is it possible to add B to A ? or extend A with B (in the same pass or not)

at run time :

if thisistrue then add B to A

or in the shader A it self , something like

shader A {
bla
bla
bla

add shader B (or code block B)

if thisistrue then add shader C ( or code block C)
}

this could be interesting to extend features of a base shaders like :
add rim effect
add kinda anisotropy
add glittering
etc etc

thanks for watching

Victor

No really possible to directly feed concatenated shader code, as you can’t be sure the platform can compile shader at runtime. Also it would mean insert inside the function from shader A just part of some code, hoping the var & structures contains what you expect at that time etc… or do it in multipass, which is equal to one draw call per pass…

But you could achieve that with multi_compile :

Just write a shader with all those effects inside #ifdef/#endif , then use Material.EnableKeyword (or Shader.Material.EnableKeyword for globally define) to enable/disable those feature.

Pros : your shader is optimal, as it’s that specific variant precompiled that is used
Cons : increase build size, as all combination of multi_compile keywords are compiled & stocked.

1 Like

great help, thanks a lot guillaume.