Material change transparency

I have a simple code that has defines two materials with png transparency. But transparency of the material set in simpleInitApp works and material change breaks transparency. It seems that I should init something in renderer but have no idea!

    Material mat_left;
Material mat_right;
Geometry geom;

@Override
public void simpleInitApp() {
    //Box b = new Box(1, 1, 1);
    Quad plane=new Quad(5,5);
    Quad plane1=new Quad(50,50);
    geom = new Geometry("Box", plane);
    Geometry geom1 = new Geometry("Box1", plane1);
    Material background = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Texture back_tex = assetManager.loadTexture("Textures/background8.png");
    background.setTexture("ColorMap", back_tex);
    geom1.setMaterial(background);
    rootNode.attachChild(geom1);
    geom1.setLocalTranslation(0, 0, -1f);
    mat_left = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat_left.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.PremultAlpha);
    //mat_left.getAdditionalRenderState().
    Texture tex = assetManager.loadTexture("Textures/zm-left.png");
    mat_left.setTexture("ColorMap", tex);
    mat_right = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat_right.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.PremultAlpha);
    tex = assetManager.loadTexture("Textures/zm-right.png");
    mat_right.setTexture("ColorMap", tex);
    geom.setMaterial(mat_right);
    
    rootNode.attachChild(geom);
    inputManager.addMapping("Change",new KeyTrigger(KeyInput.KEY_LEFT));
    inputManager.addMapping("Change1",new KeyTrigger(KeyInput.KEY_RIGHT));
    inputManager.addListener(analogListener, "Change");
    inputManager.addListener(analogListener, "Change1");
    inputManager.setCursorVisible(true);
    flyCam.setEnabled(false);
    cam.setLocation(new Vector3f(4,4,25));
}

private AnalogListener analogListener = new AnalogListener() {
    public void onAnalog(String name, float value, float tpf) {
        if("Change".equals(name))
        {
            geom.setMaterial(mat_left);
        }else if("Change1".equals(name))
        {
            geom.setMaterial(mat_right);
        }
    }

If “geom.setMaterial(mat_right);” in Simpleinitapp is set to “geom.setMaterial(mat_left);”, then left image is shown correctly!

Result:

It’s been almost a week since someone has asked a question where the answer is alpha sorting… that’s like a record for us as usually this question comes up every two days. :smile:

First, transparent things should go in the transparent bucket or they will be sorted with the opaque geometry and then it’s just random whether they get drawn correctly or not. This is because transparent pixels will fill the z buffer and then the background won’t get drawn if it happens to be drawn second.

Even then you will have issues with your sprites sorting among themselves… and then you should use alphaDiscardThreshold. There are a few million hits (may be exaggerating) on the forum if you search for that.

1 Like

haha, maybe we need a pinned topic somewhere for keeping FAQ where people can’t comment but just go for a read/search. Maybe it’s more effort to upkeep though…

This topic deserves a tutorial page, probably… or a “Transparency for Dummies” like the other “for Dummies” article. It’s a tough one because it’s hard to know what to google for to start. So even an article might get unnoticed but at least I won’t have to type the same responses every time. :smile:

Transparency in 3D graphics is a bit horrifying when it’s exposed for the first time.

Could you please provide “alphaDiscardThreshold” class, is that for material, texture, …
Where is transparent bucket? I didn’t load a model to have a GUI (I saw that in a GUI for opening models in IDE) So how to access that? Where is Z buffer? For example:
rootnode.getRenderer().getZBuffer();
Something like that exists?
No tutorial explain how to access zbuffer or transparent bucket as far as I know if exists could you please provide it?
I know there are many topics about it let me show you some examples:
“You have to call setRenderQueueMode(Renderer.QUEUE_TRANSPARENT) on all transparent Spatials.”
Where is “setRenderQueueMode”? not a Geometry, Material, Texture, rootnode method! This method is for a “loadModel()” result. Or this:
“as.setTestFunction(BlendState.TestFunction.GreaterThan);” !?
When I was in high school I had principal caught so many students for being late but the problem was that his watch slept a bit so he thought that he caught a major crime every morning in a week. One day I took his watch stealthy and corrected it so everything got back to normal.

http://wiki.jmonkeyengine.org/doku.php/jme3:intermediate:how_to_use_materials , Ctrl+F, “alphaDiscardTreshold”, but you should read the whole document.

ZBuffer, go to wikipedia and read about it, you need only to know the theory, to understand some things, like transparency, why and how the geometries are sorted before rendering etc. You’ll not use z buffer on java’s level of coding.

Queue bucket: http://javadoc.jmonkeyengine.org/com/jme3/scene/Spatial.html

Seems like you also might be missing the javadoc link in general:
http://javadoc.jmonkeyengine.org/

It’s also available in the directly from the SDK.

bolded the text that’s the hint.

Handy screen map in case you missed it before…
http://i.imgur.com/aGomB9e.png

http://i.imgur.com/V4N9sOY.png

1 Like

"geo.setQueueBucket(Bucket.Translucent); " worked
Thanks

Translucent is for something else. Transparent is what you want.