Question about transparency

I’ve seen the demoes where there is a transparent texture on a cube, but how do you give a shape a color and make it transparent?

I’ve tried this:

Shape3D psiPlane = new Shape3D(TestUtils.createPlane(0.4f, 0.4f, 0f, 0.6f, 0.6f));

Appearance aPlane = new Appearance();
Material material = new Material();

material.setColorTarget(Material.AMBIENT_AND_DIFFUSE);
aPlane.setMaterial(material);

aPlane.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0));
TransparencyAttributes ta = new TransparencyAttributes();
ta.setTransparencyMode(TransparencyAttributes.BLENDED);
ta.setTransparency(0.5f);
aPlane.setTransparencyAttributes(ta);
psiPlane.setAppearance(aPlane);

and this code creates a plane that’s like a piece of glass. If you are looking in the direction of the light perpendicular to the plane, then the plane is completely transparent, and the more of an angle you look at the plane, the less transparent the plane becomes.

How would you set the plane to a color so as to give the glass some “tint”?

I commented out the following line:
material.setColorTarget(Material.AMBIENT_AND_DIFFUSE);

and tried the following:
Material material = new Material();
material.setDiffuseColor(new Color3f(.45f, .52f, 1f));
material.setAmbientColor(new Color3f(.45f, .52f, 1f));
material.setSpecularColor(new Color3f(.45f, .52f, 1f));

but then the plane is not transparent at all.

I appreciate any help.

Thanks,

Jason

This is a bit complicated and I still don’t fully understand why it’s so. Also it looks like it’s an open issue in Xith3d how to deal with that in the future. Doing a search on this brings the following articles from an old thread. Let’s quote Yuri:

[quote]Well, some points on alpha blending.

There is no bug in this functionality, but this is a bit incompatible with Java3D. I just committed new test - Xith3DTransparencyAlphaTestLight.java - to CVS.

You should use setColorTarget(…) on your material to get transparency working in some (most) cases. The reason for this is that Java3D-style material accepts 3-component colors only, so for now material shader sets alpha to opaque. If you specify color target, appropriate material color will be replaced with color from coloring attributes/vertex colors/transparency attributes.

This is still not clear of what to do with this incompatibility, and I personally prefer to go to more advanced Material that will support different front/back materials and 2-sided lighing model, as well as more sophisticated light control.
[/quote]
And [quote]From Java3D docs:

Quote:
public void setColorTarget(int colorTarget)

Sets the color target for per-vertex colors. When lighting is enabled and per-vertex colors are present (and not ignored) in the geometry for a given Shape3D node, those per-vertex colors are used in place of the specified material color(s) for this Material object.<<

In Xith3D, if no vertex colors present, the color combined from ColoringAttributes and TransparencyAttributes used as a vertex color and set to be same for all vertices.

Also note that default color target in Java3D is DIFFUSE and in Xith3D is NONE.

Yuri

P.S. There is also JavaDoc comments for this method/constants in Xith3D Material.java
[/quote]

Thank you for finding the information, it helps out.

I hope to see more development in this area and to hear others’ opinions on how transparency should be implemented. Having glass objects in a scene will be pretty important to me, so I’m interested to see how this could be done using Xith3D.

Jason

+[quote]Thank you for finding the information, it helps out.

I hope to see more development in this area and to hear others’ opinions on how transparency should be implemented. Having glass objects in a scene will be pretty important to me, so I’m interested to see how this could be done using Xith3D.
[/quote]
Well, glass transparency works fine currently, you just have to be aware of the mentioneded Xith to Java3d incompatibility. (And Yuri’s note that maybe in future it could change).

To resume: in case you use a texture with transparency in the form of an alpha layer (an RGBA PNG file for example) all you’ve to do is use a TransparencyAttributes object.
In case you want to (addtionally) specify with a 0…1f value how transparent your Shape3D has to be, you’ve also to set the Material’s colorTarget to (at least) DIFFUSE.

Currently I use something like the following to build the Appearance object for a Shape3D, to cope with one or both mentioned cases:


float mTransparency = .. // Manually specified transparency from 0.0f to 1.0f
boolean mAlphatexture = .. // whether the Texture2D has format RGBA. 
...
Material material = new Material();            
if (mTransparency > 0) {
  material.setColorTarget(Material.DIFFUSE);
}
appearance.setMaterial(material);

if (mAlphatexture || mTransparency > 0) {
  TransparencyAttributes transp = new TransparencyAttributes(); 
  transp.setMode(TransparencyAttributes.BLENDED);
  if (mTransparency > 0) {
    transp.setTransparency(mTransparency);
  }
  apearance.setTransparencyAttributes(transp);
}
...