我是j3d中的完整n00b(但是Java专家)。刚刚开始,我遇到了一个透明度问题。我有一个绘制旋转平面四边形的简单示例(显示背面时消失,因为我没有禁用背面剔除)。
将Color3b和COLOR_3行取消注释(以及相应的Color4b和COLOR_4行注释),我看到旋转的四边形,红色。
然而,当我评论color-3行并取消注释color-4行时,我看到一个黑色方块(相对于白色背景),即使alpha值设置为255(完全不透明)。
我做错了什么?谷歌没有帮助,甚至java.forums.net上的Java3D论坛都没有帮助。 StackOverflow,救救我!您可以复制并通过以下程序,运行它,看看会发生什么。
以下是我的规格:
OSX 10.5.5上的Java 6 J3D 1.5.2 JOGL 1.1.1
谢谢,
- 罗布
以下是代码:
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.applet.Applet;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class Hello extends Applet
{
public Hello() throws Exception
{
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
scene.compile();
SimpleUniverse univ = new SimpleUniverse(canvas3D);
univ.getViewingPlatform().setNominalViewingTransform();
univ.addBranchGraph(scene);
}
public BranchGroup createSceneGraph() throws Exception
{
BranchGroup root = new BranchGroup();
// A white background.
Background bgd = new Background(1.0f, 1.0f, 1.0f);
root.addChild(bgd);
// This will spin the quad around
TransformGroup spin = new TransformGroup();
spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
root.addChild(spin);
// define the quad:
Point3d p1 = new Point3d(-0.5, -0.5, 0);
Point3d p2 = new Point3d(0.5, -0.5, 0);
Point3d p3 = new Point3d(0.5, 0.5, 0);
Point3d p4 = new Point3d(-0.5, 0.5, 0);
// colors
Color4b c = new Color4b((byte)255, (byte)0, (byte)0, (byte)255);
//Color3b c = new Color3b((byte)255, (byte)0, (byte)0);
QuadArray quads = new QuadArray(4,
GeometryArray.COORDINATES | GeometryArray.COLOR_4);
// GeometryArray.COORDINATES | GeometryArray.COLOR_3);
quads.setCoordinate(0, p1);
quads.setCoordinate(1, p2);
quads.setCoordinate(2, p3);
quads.setCoordinate(3, p4);
quads.setColor(0, c);
quads.setColor(1, c);
quads.setColor(2, c);
quads.setColor(3, c);
// Not sure if I need this. Doesn't seem to make a difference.
Appearance appearance = new Appearance();
TransparencyAttributes trans = new TransparencyAttributes();
trans.setTransparencyMode(TransparencyAttributes.BLENDED);
appearance.setTransparencyAttributes(trans);
// Create the shape...
Shape3D shape = new Shape3D();
shape.setGeometry(quads);
shape.setAppearance(appearance);
spin.addChild(shape);
Alpha rotationAlpha = new Alpha(-1, 4000);
RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, spin);
BoundingSphere bounds = new BoundingSphere();
rotator.setSchedulingBounds(bounds);
spin.addChild(rotator);
return root;
}
public static void main(String[] args) throws Exception
{
Frame frame = new MainFrame(new Hello(), 256, 256);
}
}
答案 0 :(得分:1)
用Color4f替换Color4b,对我有用。用下面的行替换代码中的相应行
Color color = new Color(255, 0, 0, 50);
Color4f c = new Color4f(color);
QuadArray quads = new QuadArray(4,
GeometryArray.COORDINATES | GeometryArray.COLOR_4);
我使用AWT Color,发现更容易传入所有ColorNx()构造函数,即Color3b(),Color4b()和Color4f()等。如果您感觉自然,可以直接使用float参数。实际修复是使用Color4f,而不是AWT的东西。即使使用AWT Color也无法解决Color4b的问题。只需使用Color4f,我没有看到任何问题。
我的平台: Java版“1.6.0_10”,Java 3D 1.5.2,Core2 Duo,OpenSUSE 11.0,Intel G33图形。