Passing a custom uniform into my shader

How does one pass a uniform into a shader other than the built in uniforms JME3 gives you such as Time? I found com.jme3.shader.Uniform and naively tried just creating one, setting a name and value, adding the name to WorldParameters in my .j3md, and then using it in my shader, but I get nothing.

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:jme3_shaders

Yup, already read that several times, but if it tells me how to set my own uniforms then I am blind to it. It seems like I can only send Time or some other such built-in uniform, not a custom named variable of my own.

Check again, it gives the SolidColor.j3md example. Just copy the SolidColor.j3md and frag/vert from the library node to your assets folder and look inside, the Color is the parameter, in the shader its named m_Color

Ah, no, its not possible afaik.

Right, I can set a per-material parameter; I was hoping to pass a uniform that will apply to every material using my shader. I have 20-30 materials all using the same shader program and I was hoping I could just pass the parameter in one place, the way things like Time are passed. Maybe this isn’t possible?

Do you not want to set per-material uniforms because it’s inconvenient or because you will be setting them often?



If the latter, what kind of values are you trying to pass? If not, what is it about this value that can’t be baked into the material defs or the the shaders?



…just wondering.

I was implementing fog in my shader, so I wanted to pass the global fogColor and fogDistance into the shader. I couldn’t hardcode them because I wanted the fog color to change with the sky/sunlight color. I didn’t need to set it per-Material because fog by its nature should all be the same color. So a global uniform would have been ideal.



I did end up implementing it the Material way, but it’s ugly…looping through 30 materials on every update feels really wasteful.

That’s why I asked. Just set the same fog color instance to all of the materials and hold a reference to it. Modify that color object and it will be applied to all of the materials.



I have 13 materials in my app and they all share the same fog color and ambient values.

Oh, and for that reason, I tuck fog distance into the alpha for the color… so I don’t have to set it on every material.