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?
Since JDK8u60 b14 transparency is enabled in 3D shapes.
This is a quick test done with it:
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);
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.
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 .
© 2022 - 2024 — McMap. All rights reserved.