JavaFX 3D Transparency

孤街醉人 提交于 2019-11-28 04:22:19

问题


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?


回答1:


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




回答2:


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.




回答3:


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
         earthMaterial.diffuseMapProperty();
         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 .



来源:https://stackoverflow.com/questions/29308397/javafx-3d-transparency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!