Change vertex/fragment shaders of a ShaderMaterial material

I don’t know if it’s relevant for Babylon, but I have a use case where I need to update the vertex/fragment shaders without recreating a ShaderMaterial object from scratch.

There’s no method for this on the ShaderMaterial class.

I currently do this:

(mat as any)._shaderPath.vertex = newVertexCode;
(mat as any)._shaderPath.fragment = newFragmentCode;

Updating _shaderPath do the job because it is used as a parameter in the engine.createEffect(...) call, and so it will return a new effect if something has changed in _shaderPath since the last time it was called.

As _shaderPath is not visible from the outside I need to cast my material to any to access it: would it make sense to have a getter for it, as we have for options?

Then, I could do:

mat.shaderPath.vertex = newVertexCode;
mat.shaderPath.fragment = newFragmentCode;

Also, maybe it was what was asking @wwwonka in Live-reloading of shaderMaterial?

1 Like

Hey! I’m ok if you want to PR a get/set accessor for the shaderpath

Ok, will do that!

1 Like

PR created:

1 Like