How can you get the value of a uniform in a ShaderMaterial (aka mat.getColor3(...))?

I am developing a custom shader and have some custom uniforms that I can set with the setXXXX() APIs of the ShaderMaterial.

However, I want to be able to get the value of a uniform and to my surprise, there are no getter APIs. So the question reduces to how do you get the value of a uniform from a ShaderMaterial? It seems that it’s just one way - you can only set, but not get the value. Am I missing something?

Welcome abroad!

That’s correct, as the value would have to be read back from the GPU. If you’d like to “remember” the values you sent, you could keep an object with the values and retrieve them when needed?

1 Like

its already stored on the shaderMaterial class

1 Like

That wouldn’t work with Typescript through, since _vectors3 is private: Basic ShaderMaterial | Babylon.js Playground (babylonjs.com)

1 Like

Thank you for the prompt reply and solution. I already do that, but it’s a bit cumbersome, especially if you want to generalize.

I was about to bring the fact that we do have those values already stored - in the private members of the class, just as you and @Pryme8 mentioned and given that, why couldn’t we have getter functions that simply return whatever is stored in those private members?

I see no problem having such API since you don’t really need a read-back from the GPU (the values are cached already), as uniforms in the shaders should be treated as read-only and anyone not complying to this rule is basically asking for trouble.

Finally, if it’s a matter of “what happens if you get a uniform’s value before setting it?”, then I would say that if you don’t set a uniform, the shader receives an implicit value anyways, so we should have defaults written in those private members.

I think it’s ok to add getters, if you want to contribute a PR for it?

1 Like