How to change the opacity of an image in jme3?

I loaded a .png picture into the scene, and what to change its opacity along time.
First thing I tried was the method mentioned here:

But at this line of codes:
material.setBoolean("m_UseAlpha", true);

I got an error saying there is no key “m_UseAlpha”. I tried “UseAlpha”, “useAlpha”. None of them worked.
Any suggestion? Thx

The post you quoted is QUITE outdated. I think you should write your own Material and shader for that…

That thread is 6 years old.

What is it that you actually want to do? Just a transparent image?

You can do that with the stock shaders easily. Set the blend mode of the material, put your object in the transparent bucket.

…and if that doesn’t make sense: do the tutorials.

I think the description is quite understandable: The OP wants to change the alpha value of the whole material (to achieve a fading effect). Maybe using material colors and changing the alpha value of the material colors over time could do the trick?

1 Like

Yeah, if that’s what he wants to do (and I agree with your interpretation) then that’s the way to do it.

He will also have to do this bit, too, though:

Thanks a lot Ogli!
I followed your suggestion and got the effect I wanted.
Here are the codes

    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setTexture("ColorMap", assetManager.loadTexture("Textures/circle.png"));
    opacity -= 0.1f;
    ColorRGBA color = new ColorRGBA(1f, 1f, 1f, opacity);
    mat.setColor("Color", color);
    mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
    tag.setMaterial(mat);

With the above method, one should not put the object into the transparent bucket. Otherwise the object will disappear completely.

Then something else is wrong… because if you leave it in the opaque bucket it won’t sort properly with the scene. The only difference between the opaque and transparent bucket is the order things are drawn.

Or do you mean disappear completely when opacity reaches 0? Then, yes, but in the opaque bucket that is also happening but you are just randomly having it sort properly to draw the background instead of the scene that’s behind it. And changing the opacity isn’t really what you want to be doing.

You don’t need to create a new material each time. Just change the alpha in the ‘color’ variable - it will change all the other stuff when rendering the next frame.

Like @pspeed said, you need to put that into the transparent (or translucent) bucket. Otherwise it will lead to visual errors in almost all scenes.

Maybe there is another stupid error in the Unshaded material (some shader error or something like that). There have been several stupid and simple bugs been detected in jME 3 lately:

Show us a screen shot of the effect so we can see how you intend for it to work.

Unshaded works fine for transparency. If you set the opacity to 0 then it will be invisible. If this is not the effect you want then you do not want opacity.

I will code a little example to test this case and report back the results.
If there is another bug then it will be detected via this.
Otherwise it will be a good example for you and you can simply copy the code.
Now is lunch time, so expect some delay until the code arrives.

Also… if this is the guiNode then don’t change the bucket. The guiNode is in the Gui bucket and will already handle transparency appropriately.

You didn’t say in your post but I’m thinking that one of two things is happening here:

  1. you are using the guiNode and then yes, setting the transparent bucket will break things
  2. you don’t really want opacity and you actually want your object of fade the image to black.

My guess is (1)… in which case, ignore all of this bucket nonsense. :slight_smile:

Oh yes, that’s right.
The thing I want to code will contain several cubes with monkey textures which will fade from fully visible to fully invisible and back again in a 3D scene.
If that’s not what you want, then that code will not help.

You are right. I added it to guiNode. Seems it’s super easy in this case.
It’s good to know there is a difference about transparency handling between Gui Bucket and others.

I think the example you were planing to code will be still valuable though.
I’d expect to do something like that soon or later.