[Solved] Method for Changing an Individual Channel of a Material Color?

Very, very niche, I imagine.

Think of this question as more a curiosity, than an absolute need for a solution, as I can work around it. I’m just interested in knowing if there is a nice, clean, elegant solution.

Presume I have a simple Geometry with a Material and a Color, such as:

Box b = new Box(1, 1, 1);
Geometry geom = new Geometry("blue cube", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", new ColorRGBA(0.2f, 0.4f, 0.6f, 0.8f));
geom.setQueueBucket(RenderQueue.Bucket.Transparent);
geom.setMaterial(mat);
rootNode.attachChild(geom);

Now, in some other part of a program, a part that doesn’t know the four values passed to ColorRGBA, I would like to change, or even interpolate, one of the four channels of ColorRGBA to some other value. Like so (pretend I am doing this effectively):

((Geometry) spatial).getMaterial().setColor("Color", new ColorRGBA(????));

What would I put for “????” above if I wanted to, say, change only the Blue channel of RGBA, while leaving all other channels the same:

Math.min([Current Blue channel value]+0.5f, 1.0f)

For that matter, is there a more clean method of reading the current, for example, Blue channel value than something like:

Float.parseFloat((String.valueOf(((Geometry) spatial).getMaterial().getParam("Color")).split(" "))[5])

Thanks for indulging my curiosity and patience for probably bad code!

P.S. “bananapie” or something

That’s a long way to go to avoid a cast. Are you new to Java and didn’t know about casting or just didn’t think of it?

ColorRGBA color = (ColorRGBA)mat.getParam("Color").getValue();
1 Like

Fair play.

I consider myself much less a programmer than a data analyst. Just didn’t think of casting at that particular spot, though. So, yeah, the Blue channel would be like this, yeah?

((ColorRGBA) ((Geometry) spatial).getMaterial().getParam("Color")).getValue()).getBlue();

Presuming I’ve already resolved that my spatial is instanceof Geometry.

Edit: Oh, wait, wait.

I remember why I didn’t do that at first. I didn’t realize I could because I didn’t realize I could cast the MatParam Object result of getValue() to ColorRGBA.

Good to know.

Edit 2:

Ah, so the values in the ColorRGBA class are public. Is it alright to just change them individually by accessing them that way without the class having setters?

Yes. Not sure how else you could do it.

And in this case, you don’t even have to set the color back. The material will automatically pick up the new values.

Note: there is no award for putting 9 concepts on one line and in the long run you will be better off splitting what you have into multiple lines… not the least of which is so that errors show up on the line that is actually interesting.

…but also because you may find that some values you don’t need to look up all the time if you just keep a reference.

Excellent, thank you for the assistance!

Note: there is no award for putting 9 concepts on one line

Another point taken :slight_smile:

1 Like