简单的透明度示例在 Java 3D 中不起作用

发布于 2024-07-10 11:05:54 字数 3121 浏览 8 评论 0原文

我是 j3d 方面的新手(但是是 Java 方面的专家)。 刚开始,我在透明度方面遇到了问题。 我有一个简单的示例,它绘制了一个旋转平面四边形(显示背面时消失,因为我没有禁用背面剔除)。

在取消注释 Color3b 和 COLOR_3 行(以及注释相应的 Color4b 和 COLOR_4 行)的情况下,我看到旋转的四边形,颜色为红色。

然而,当我注释 color-3 行并取消注释 color-4 行时,我看到一个黑色方块(白色背景),即使 alpha 值设置为 255(完全不透明)。

我究竟做错了什么? Google 没有提供帮助,甚至 java.forums.net 上的 Java3D 论坛也没有什么帮助。 StackOverflow,救救我! 您可以复制并粘贴以下程序,运行它并看看会发生什么。

这是我的规格:

OSX 10.5.5 上的 Java 6 J3D 1.5.2 JOGL 1.1.1

谢谢,

--Rob

这是代码:

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);
 }
} 

I'm a complete n00b in j3d (but an expert in Java). Just starting out, I'm running into a problem playing with transparency. I've got a simple example which draws a rotating planar quad (disappears when showing the back face because I haven't disabled backface culling).

With the Color3b and COLOR_3 lines uncommented (and the corresponding Color4b and COLOR_4 lines commented), I see the rotating quad, colored red.

However, when I comment the color-3 lines and uncomment the color-4 lines, I see a BLACK square (against the white background), even though the alpha value is set to 255 (fully opaque).

What am I doing wrong? Google doesn't help, and even the Java3D forum over at java.forums.net is less than helpful. StackOverflow, save me! You can copy and past the below program, run it and see what happens.

Here are my specs:

Java 6 on OSX 10.5.5
J3D 1.5.2
JOGL 1.1.1

Thanks,

--Rob

Here's the code:

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);
 }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小霸王臭丫头 2024-07-17 11:05:54

用 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 显卡。

Replacing Color4b with Color4f, worked for me. Replace corresponding lines in your code with the lines below,

Color color = new Color(255, 0, 0, 50);

Color4f c = new Color4f(color);
QuadArray quads = new QuadArray(4, 
           GeometryArray.COORDINATES | GeometryArray.COLOR_4);

I used AWT Color, found it easier to pass in all ColorNx() constructors, i.e. Color3b(), Color4b(), and Color4f() etc. You may use float arguments directly, if that feels natural to you. The actual fix is the use of Color4f, not the AWT thingy. Even using AWT Color didn't solve the issue with Color4b. Just go with Color4f, I don't see any problem.

My platform: Java Version "1.6.0_10", Java 3D 1.5.2, Core2 Duo, OpenSUSE 11.0, Intel G33 Graphics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文