The starfire loader already supports material-level transparency (from its
docs). So try that if it is enough control for your specifications.
But you mentioned opacity, so if you're talking about transparency from
texture alpha -
You need to:
(a) Load a texture with transparency information (tga files are nice, png i
think is supported with JAI (java advanced imaging) now too)
(b) Search through the Loader3DS branchgroup recursively searching for
shape3d nodes.
(c) Set the appearances TransparencyAttributes to use
TransparencyAttributes.BLENDED.
(d) Apply that texture to them all, or perform some heuristic to match the
textures by image name or something.
Included is some code to do exactly that. To get the vertex color alpha to
work with this, MODULATE texture mode is needed as well.
>From: Scott Mobile <[EMAIL PROTECTED]>
>Reply-To: Discussion list for Java 3D API <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: [JAVA3D] How to create models for loaders with alpha
>transparency??
>Date: Wed, 3 Apr 2002 19:15:40 -0600
>
>I need to create some models that use alpha transparency in the textures
>(example being trees, fences with transparent areas between slats such as
>in the Sun Java Fly demo, etc, etc.), but can't figure out what to use to
>create the geometry, and therefore which loader to use.
>
>Transparency is cake in 3DS Studio Max, and 3DS files are the ones I work
>with the most (using the StarFire loader), but the opacity doesn't get
>imported, and the transparent areas show solid black. I don't wish to get
>into modifying or creating any loaders. I know this can be done because of
>the Sun Fly demo.
>
>If VRML/WRL format is the way to go for this (again, such as the Fly demo
>contains), what's some software that can create the right kind of
>texture-based transparency to load in Java3D? If not, what are the
>alternatives?
>
>BTW--for those of you looking for samples of Java3D games in action, our
>MMORPG project is an open beta, and is currently on-line. I welcome any
>comments/critism.
>
>Scott
>Virtopia 3D Project
>http://vp2.onebigvillage.com
_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com
public void GetShapeNodes(BranchGroup parentGroup)
{
for (int i = 0; i < parentGroup.numChildren(); i++)
{
Node tNode = parentGroup.getChild(i);
if (tNode.getClass() == BranchGroup.class)
{
GetShapeNodes((BranchGroup) tNode);
}
else if (tNode.getClass() == TransformGroup.class)
{
GetShapeNodes((TransformGroup) tNode);
}
else if (tNode.getClass() == Shape3D.class)
{
gShapeObjects.add(tNode);
}
}
}
private void GetShapeNodes(TransformGroup parentGroup)
{
for (int i = 0; i < parentGroup.numChildren(); i++)
{
Node tNode = parentGroup.getChild(i);
if (tNode.getClass() == BranchGroup.class)
{
GetShapeNodes((BranchGroup) tNode);
}
else if (tNode.getClass() == TransformGroup.class)
{
GetShapeNodes((TransformGroup) tNode);
}
else if (tNode.getClass() == Shape3D.class)
{
gShapeObjects.add(tNode);
}
}
}
public static Image LoadImage(String fileName)
{
// use the targa reader?
if (fileName.endsWith(".tga"))
{
TargaReader newReader = null;
try
{
newReader = new TargaReader(fileName, 0);
}
catch (FileNotFoundException e)
{
System.err.println(e);
System.exit(1);
}
return newReader.getImage();
}
// cool, use default toolkit to deal with this puppy. Don't image share,
cause we might paint later or something.
else if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") ||
fileName.endsWith(".gif") || fileName.endsWith(".png"))
{
return Toolkit.getDefaultToolkit().createImage(fileName);
}
else
{
System.out.println("GeometricObject::LoadImage - Unexpected file format! "
+ fileName + "\n");
System.exit(0);
return null;
}
}
public static Texture LoadTexture(String fileName, boolean enableMipMap)
{
TextureLoader texLoader;
Image img = LoadImage(fileName);
if (enableMipMap)
texLoader = new TextureLoader(img, TextureLoader.GENERATE_MIPMAP, null);
else
texLoader = new TextureLoader(img, null);
return texLoader.getTexture();
}
public static Appearance GetRegularAppearanceFromTexture(String fileName,
boolean enableMipMap)
{
Texture tex = LoadTexture(fileName, enableMipMap);
TransparencyAttributes tAtt = new TransparencyAttributes();
Shape3D shapeNode = null;
Appearance app = new Appearance();
app.setTexture(tex);
app.setTransparencyAttributes(tAtt);
return app;
}
public static Appearance GetAlphaAppearanceFromTexture(String fileName,
boolean enableMipMap)
{
Texture tex = LoadTexture(fileName, enableMipMap);
TransparencyAttributes tAtt = new
TransparencyAttributes(TransparencyAttributes.BLENDED, 1.0f);
Shape3D shapeNode = null;
Appearance app = new Appearance();
app.setTexture(tex);
app.setTransparencyAttributes(tAtt);
return app;
}
public static Appearance GetAdditiveAppearanceFromTexture(String fileName,
boolean enableMipMap)
{
Texture tex = LoadTexture(fileName, enableMipMap);
TransparencyAttributes tAtt = new
TransparencyAttributes(TransparencyAttributes.BLENDED,
1.0f, TransparencyAttributes.BLEND_ONE,
TransparencyAttributes.BLEND_ONE);
Shape3D shapeNode = null;
Appearance app = new Appearance();
app.setTexture(tex);
app.setTransparencyAttributes(tAtt);
return app;
}
public void SetAlphaTexture(String fileName, boolean enableMipMap)
{
Texture tex = LoadTexture(fileName, enableMipMap);
TransparencyAttributes tAtt = new
TransparencyAttributes(TransparencyAttributes.BLENDED, 1.0f);
Shape3D shapeNode = null;
Appearance app = null;
for (int i = 0; i < gShapeObjects.size(); i++)
{
shapeNode = (Shape3D) gShapeObjects.get(i);
app = shapeNode.getAppearance();
app.setTexture(tex);
app.setTransparencyAttributes(tAtt);
shapeNode.setAppearance(app);
}
}