JavaFX 3D Transparency
Asked Answered
E

3

9

I'm looking for a way to render a transparent object in JavaFX 3D. So far, nothing. I found issue https://bugs.openjdk.java.net/browse/JDK-8090548. Is there a workaround or is this just something I can't use? Will I need something besides JavaFX (like Java3D) if I need a transparent object?

Eous answered 27/3, 2015 at 19:7 Comment(0)
O
16

Since JDK8u60 b14 transparency is enabled in 3D shapes.

This is a quick test done with it:

Transparency

A cylinder with diffuse color Color.web("#ffff0080"), is added on top of a box and two spheres.

group.getChildren().addAll(sphere1, sphere2, box, cylinder);

There's no depth sort algorithm though, meaning that order of how 3D shapes are added to the scene matters. We need to change the order to allow transparency in the box:

group.getChildren().addAll(sphere1, sphere2, cylinder, box);

Transparency

Othilie answered 13/5, 2015 at 11:45 Comment(0)
B
6

Update

This answer is obsolete, as of Java 8u60b14 as transparency was added to JavaFX in that build.


As the issue you link in your question notes, transparency is not supported in JavaFX 3D for Java 8. It may be implemented for Java 9.

There is a workaround a user mentions in comments on the issue tracker which involves a hack to the native code for the JavaFX OpenGL pipeline. If you are desperate for this functionality, you could try that hack. If that is not suitable for you, then you will need to choose a different technology.

Baeyer answered 27/3, 2015 at 21:51 Comment(1)
Thank you. It looks like using Java 3D (instead of JavaFX 3D) will be the simpler solution for getting transparency to work.Eous
N
2

Here's a partial solution. To add transparency to a sphere with the image of the earth texture mapped to it, set both a diffuseMap and a diffuseColor:

private void makeEarth() {
         PhongMaterial earthMaterial = new PhongMaterial();
         Image earthImage = new Image("file:imgs/earth.jpg");
         earthMaterial.setDiffuseMap(earthImage);
         earthMaterial.setDiffuseColor(new Color(1,1,1,0.6));  // Note alpha of 0.6

         earth = createSphere(0,0,0,300,earthMaterial);
         earthMaterial.setSpecularColor(Color.INDIANRED);         
         earth.setRotationAxis(Rotate.Y_AXIS);
         world.getChildren().add(earth);
    }

This works only to allow the background image of the scene (set by scene.setFill(starFieldImagePattern);) to show through. It doesn't yet work for allowing other shapes to show through.

Apparently, the reason this works is that the diffuse color is multiplied by the diffuseMap color when calculating the color of the pixels. See https://docs.oracle.com/javase/8/javafx/api/javafx/scene/paint/PhongMaterial.html .

Niggle answered 29/12, 2016 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.