Setting Transparency recursively.

Can anyone help me ?
I have written a function to set all the shape3Ds of a BranchGroup transparent, but it don’t display transparent.
Here is the source code :


// Fonction activant ou désactivant l'affichage transparent
  void setAffTrans(Group group,boolean param) {

    TransparencyAttributes ta = new TransparencyAttributes();
    if( param ) {
      System.out.println("Met transparency à 0.5");
      ta.setTransparency(0.5f);
    }
    else {
      System.out.println("Met transparency à 0");
      ta.setTransparency(0);
    }
    //ta.setTransparencyMode(TransparencyAttributes.BLENDED);
    //ta.setMode(TransparencyAttributes.BLENDED);
    
    Enumeration e = group.getAllChildren();
    while(e.hasMoreElements()) {
      Node node = (Node) e.nextElement();
      
      if(node instanceof Group) {
        System.out.println("Transparence : c'est un group");
        setAffTrans((Group)node,param);
      }
      else {
        if(node instanceof Shape3D) {
          System.out.println("Transparence : c'est un Shape3D");
          Shape3D shp = (Shape3D)node;
          shp.getAppearance().setTransparencyAttributes(ta);
          shp.getAppearance().setChanged(true);
        }
      }
    }
  
  };

My workaround was to make a new appearance and applying it to the shape as opposed to getting the appearance from the shape and modifying it. I think the lack of transparencies have something to do with the presence of materials. I’ll let you know more as I delve into it.


            //a = s.getAppearance();
            a = new Appearance();
            Material m = a.getMaterial();
            //m.setEmissiveColor(255f,0,0);
            //a.setMaterial(m);
            
            TransparencyAttributes ta = new TransparencyAttributes(TransparencyAttributes.BLENDED,0.4f);
            a.setTransparencyAttributes(ta);            
            s.setAppearance(a);

Edit:
Just found out that setting the material to null makes the transparency work. So you don’t need to make a new appearance; just use the shape’s one and set its material to null …


            Appearance a = s.getAppearance();
            //Material m = a1.getMaterial();
            //a = new Appearance();            
            //m.setEmissiveColor(255f,0,0);
            //a.setMaterial(m);
            
            TransparencyAttributes ta = new TransparencyAttributes(TransparencyAttributes.BLENDED,0.4f);
            a.setTransparencyAttributes(ta);
            a.setMaterial(null);
            s.setAppearance(a);

You could also try setting the Material’s color target. I had to do this to get transparency working.

Could you please explain this a little more? I tried this but no success…thanks :slight_smile:


a.getMaterial().setColorTarget(Material.NONE);

Here is my default Appearance setup:

material = new Material();
material.setColorTarget(Material.DIFFUSE);
material.setAmbientColor(AMBIENT_COLOR);
material.setSpecularColor(SPECULAR_COLOR);
material.setShininess(96.0f);

coloringAttributes = new ColoringAttributes();
coloringAttributes.setColor(DIFFUSE_COLOR);

polygonAttributes = new PolygonAttributes();
polygonAttributes.setCullFace(PolygonAttributes.CULL_BACK);

appearance = new Appearance();
appearance.setMaterial(material);
appearance.setColoringAttributes(coloringAttributes);
appearance.setPolygonAttributes(polygonAttributes);

Transparency can then be set using:

TransparencyAttributes ta = new TransparencyAttributes();
ta.setTransparencyMode(TransparencyAttributes.BLENDED);
ta.setTransparency(t);
appearance.setTransparencyAttributes(ta);

Hope this helps :slight_smile: