简体   繁体   English

JavaFX 19 3D Z 缓冲区和透明度不工作

[英]JavaFX 19 3D with Z Buffer and Transparency not working

I'm trying to have two boxes with one of them half transparent and the other in orange.我想要两个盒子,其中一个是半透明的,另一个是橙色的。 Somehow it always just fully replaces the pixels but still kinda applies the transparency to the color.它总是以某种方式完全替换像素,但仍然有点将透明度应用于颜色。 What am I missing?我错过了什么? Same happens with loaded Obj files which have d/Tr set to 0.5 for example.例如,d/Tr 设置为 0.5 的已加载 Obj 文件也会发生同样的情况。

import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.scene.DepthTest;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class HelloFX extends Application {

    @Override
    public void start(Stage primaryStage) {
        boolean is3DSupported = Platform.isSupported(ConditionalFeature.SCENE3D);
        if (!is3DSupported) {
            System.out.println("Sorry, 3D is not supported in JavaFX on this platform.");
            return;
        }

        Box boxForeground = new Box(100, 500, 100);

        boxForeground.setTranslateX(250);
        boxForeground.setTranslateY(100);
        boxForeground.setTranslateZ(400);
        boxForeground.setMaterial(new PhongMaterial(new Color(0, 0, 0, 0.3)));

        Box boxBackground = new Box(100, 100, 100);

        boxBackground.setMaterial(new PhongMaterial(Color.ORANGE));
        boxBackground.setTranslateX(250);
        boxBackground.setTranslateY(200);
        boxBackground.setTranslateZ(800);

        boolean fixedEyeAtCameraZero = false;
        PerspectiveCamera camera = new PerspectiveCamera(fixedEyeAtCameraZero);
        camera.setTranslateX(150);
        camera.setTranslateY(-100);
        camera.setTranslateZ(250);

        Group root = new Group(boxForeground, boxBackground);
//        root.setDepthTest(DepthTest.ENABLE); // no effect
        root.setRotationAxis(Rotate.X_AXIS);
        root.setRotate(30);

        Scene scene = new Scene(root, 500, 300, true);
        scene.setCamera(camera);
        primaryStage.setScene(scene);
        primaryStage.setTitle("3D Example");

        primaryStage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

I've played around with many options of depth buffer, depth test settings and colors but it seemed to have no good effect.我尝试了很多深度缓冲、深度测试设置和 colors 的选项,但似乎没有什么好的效果。

My expectation is that its possible to slightly see the orange box through the black box with strong transparency.我的期望是可以透过透明感很强的黑色盒子,略微看到橙色的盒子。

Actual result:实际结果: 在此处输入图像描述

So based on the comments and my own investigation it seems to be a known bug thats hasn't been fixed in over 5 years.因此,根据评论和我自己的调查,这似乎是一个已知错误,5 年多来都没有修复。 Basically transparency works like in the 2D space of JavaFx. The one last added to the graph paints over the already painted ones - potentially using transparency blending when your colors/pixels contain alpha information.基本上透明度就像在 JavaFx 的 2D 空间中一样工作。最后添加到图形中的一个在已经绘制的那些上绘制 - 当您的颜色/像素包含 alpha 信息时可能使用透明度混合。

Bugtracker: Order-independent transparency for 3D objects Bugtracker: 3D 个对象的顺序无关透明度

Workaround:解决方法:

The current workaround is to reorder the graph so it matches the desired z-order.当前的解决方法是重新排序图形,使其与所需的 z 顺序匹配。 Depending on the use case you can group objects on a root level to get close to an ideal transparency handling.根据用例,您可以在根级别对对象进行分组以接近理想的透明度处理。 But for transparent objects that 'interact' with each other on the same level of the graph you gonna need constant reordering when the objects or the camera move.但是对于在图形的同一级别上相互“交互”的透明对象,当对象或相机移动时,您需要不断重新排序。

When done right you get the expected result:正确完成后,您将获得预期的结果:

在此处输入图像描述

Group root = new Group(boxForeground, boxBackground); // bug shows


Group root = new Group(boxBackground, boxForeground); // workaround

Apparently JavaFx has a long history of transparency issues.显然 JavaFx 长期以来一直存在透明度问题。 Which are also discussed here: JavaFX 3D Transparency也在这里讨论: JavaFX 3D 透明度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM