JavaFX 3D Transparency

前端 未结 3 902
闹比i
闹比i 2020-12-19 06:18

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

相关标签:
3条回答
  • 2020-12-19 07:04

    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 .

    0 讨论(0)
  • 2020-12-19 07:05

    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.

    0 讨论(0)
  • 2020-12-19 07:09

    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

    0 讨论(0)
提交回复
热议问题