Material questions

Hi.
I load .j3o scene, it have some objects with textures and I’m trying to make these objects transparent.

I have this code (found it at the forum):

public void updateMaterial(Spatial spatial)
{
    try
    {
        if (spatial instanceof Node)
        {
            Node node = (Node) spatial;
            for (int i = 0; i < node.getQuantity(); i++)
            {
                Spatial child = node.getChild(i);

                System.out.println("Name (" + i + ") [" + node.getName() + "] : " + child.getName());//debug

                updateMaterial(child);
            }
        } else if (spatial instanceof Geometry)
        {
            Geometry geo = (Geometry) spatial;
            geo.getMaterial().setTransparent(true);
            //geo.getMaterial().setColor("Color", new ColorRGBA(1, 0, 0, 0.4f));
            geo.getMaterial().getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
            geo.setQueueBucket(Bucket.Transparent);

            System.out.println("   GEO: " + geo.getName());//debug
        }
    } catch (Exception err)
    {
        System.out.println("Exception ExtNode::updateMaterial() err: " + err + " | " + err.getLocalizedMessage() + " | " + err.getMessage() + " | " + err.toString());
    }
}

updateMaterial(rootNode);

But scene is still opaque.

And second question: How to change object’s color, line
geo.getMaterial().setColor(“Color”, new ColorRGBA(1, 0, 0, 1f)); // red
didnt change it.

The code you provided isn’t complete. Which material definition are you using?

And by the way, see:
http://javadoc.jmonkeyengine.org/com/jme3/scene/Spatial.html#depthFirstTraversal(com.jme3.scene.SceneGraphVisitor)

and:
http://javadoc.jmonkeyengine.org/com/jme3/scene/SceneGraphVisitorAdapter.html

1 Like

Yep, wanted to say what pspeed says :smiley:
Simply add a SceneGraphVisitor and there you only need two lines: if a instanceof Geometry and your settings.

it’s setColor(ColorMapName), in this case it should be “Diffuse” to make it work.
There might already be an ColorRGBA.Red
It might be good to know what you are trying to achieve, because there might be better options (texture generation, multiple switchable texture sets) :slight_smile:

Nah, with the adapter, you don’t even need that. Just override the method that takes Geometry.

1 Like

Oh cool.
I never used the Adapter so I didn’t know that :stuck_out_tongue:

However I came up with yet another thing right now:
In the SceneComposer you can change the buckets when selecting the Geometry and then under Properties to your right (it’s another window) select the appropriate ones :smile:

That way your game will load faster and you have less coding to do

Thanks to you all, got my transparent TEST to work.
Now I use SceneGraphVisitorAdapter, so cool :smiley:

Thanks, “Difffuse” works, now I can control alpha

Case closed :smile:

Super cool! Am I allowed to attach and detach spatials while visiting? Thread safety considerations?

EDIT: apparently, I am not. :expressionless:

It should be safe to remove the spatial you are visiting, I think. Anything else gets tricky I guess.