Changing the diffuse-color of a Material through code?

Would it be possible to constantly change the diffuse color of a Material in an update loop? I’m trying to do this but it doesn’t seem to change the color. Any help?

Your code looks fine to me. :wink:

You might want to show us how you setup the material and how you are changing the color. Otherwise, I have about 15 random guesses I can throw your way and waste both of our time a little bit.

@pspeed fair enough xD

The code is basically just this:

[java]
Material atmosphereMaterial = new Material(assetManager, “AtmosphereShader.j3md”);

public void update(float tpf) {
ColorRGBA curSkyColor = /some stuff to get the sky color/;

 atmosphereMaterial.setColor("Diffuse", curSkyColor);

}
[/java]

I’m more or less asking if defining a uniform variable in GLSL makes in unchangeable. Would it have to reload the shader to change the color? Should I define it as uniform or are there any other ways to define the Diffuse color that would allow me to dynamically modify it? Sorry if any of these questions seem stupid, I know next to nothing about GLSL yet somehow manage to scrape by.

You can change the diffuse color just fine. Is your shader based on Lighting.j3md at all? That requires that you also set UseMaterialColors to true.

You can even forgo calling setColor() every time and just keep a reference to the color when you first set it and just change its rgba when you want. The shader will see it.

The material is part of @aaronperkins 's JmePlanet atmosphere shader. I modified it a bit, and now I’m working on sunset/sunrise code. Everything’s exactly as it should be with the colors and when they should change, but the shader doesn’t seem to update them.

I’ll try using the same ColorRGBA instance for the sky color once I get back on my work computer. Thanks for the suggestion, I’ll let you know if it works out!

@vinexgames said: The material is part of @aaronperkins 's JmePlanet atmosphere shader. I modified it a bit, and now I'm working on sunset/sunrise code. Everything's exactly as it should be with the colors and when they should change, but the shader doesn't seem to update them.

I’ll try using the same ColorRGBA instance for the sky color once I get back on my work computer. Thanks for the suggestion, I’ll let you know if it works out!

It should work either way… something sounds funny with the shader.

Try a simple test with a cube and the regular JME shaders.

@pspeed that was my next idea to test it, and after that I was going to try a few other things as well.

@pspeed A cube with “Unshaded.j3md” DOES work. I can change the color from code.
However, it still doesn’t work with the atmosphere shaders. I’ve tried just directly setting the gl_FragColor to the diffuse color. It doesn’t change the color.

Upon looking at Unshaded.j3md, it defines it’s Color as a “Color” instance, whereas my shader defines it as a “Vector4.” Would this make any difference?

@vinexgames said: @pspeed A cube with "Unshaded.j3md" DOES work. I can change the color from code. However, it still doesn't work with the atmosphere shaders. I've tried just directly setting the gl_FragColor to the diffuse color. It doesn't change the color.

Upon looking at Unshaded.j3md, it defines it’s Color as a “Color” instance, whereas my shader defines it as a “Vector4.” Would this make any difference?

It shouldn’t. Perhaps there is something else in the shader that is messing it up… or you aren’t really changing the shader code that is being used. Try forcing your gl_FragColor to red just to make sure.

<cite>@pspeed said:</cite> It shouldn't. Perhaps there is something else in the shader that is messing it up... or you aren't really changing the shader code that is being used. Try forcing your gl_FragColor to red just to make sure.

I know I’m changing the shader code, because a recent attempt to fix it just totally made the sky render completely invisible. The shader uses Multi-Pass lighting, could that be why?

If you set the gl_FragColor to red and it shows up red. And if you set diffuse to red and then gl_FragColor to diffuse and it shows up red… then it has nothing to do with multipass or anything strange.

If those tests turn out as expected then something in the shader is messing up m_Diffuse before you use it.

@pspeed I figured out the problem now the fix will be the fun part. The problem is that jMEPlanet clones the materials applied to each Quad so that there’s no problems with different chunks being at different levels of detail. I’ll have to add a flag to not clone the materials, and hope that works.

@vinexgames

There is a method on Quad called setMaterialParam that recursively sets a material parameter on each sub quad. I would add a method to Planet that takes the color as a parameter and calls setMaterialParam of all 6 quads of the atmosphere. Look at setWireframe or setVisible for an example.

setMaterialParam only handles boolean parameters right now but it’s easy to add support for others.

@aaronperkins I found that, and I successfully added support for changing the colors. Thanks though! I was looking for problems in the shader when all along it was right in front of me haha.

The end result.