JavaFX 2D 文本与 3D 场景中的背景 [英] JavaFX 2D text with background in 3D scene

查看:34
本文介绍了JavaFX 2D 文本与 3D 场景中的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的项目,我需要 3D 场景中的 2D 文本(不是叠加!).所以我尝试在我的场景中添加一个 BorderPaneLabel/Text 节点:

For my project, I need 2D text inside a 3D scene (not as overlay!). So I've tried adding a BorderPane with Label/Text nodes to my scene:

然而,问题是,当我用相机放大、缩小或飞来飞去时,面板的白色背景有时会与标签重叠(它们显然具有相同的深度).

The problem is however, that the white background of the panel is at times overlapping with the label (they have the same depth apparently) when I zoom in, out or fly around with my camera.

有没有办法从面板上提升"标签?我试过设置 setDepthTest(true); 没有效果.

Is there a way to "elevate" the label from its panel? I've tried setting setDepthTest(true); with no effect.

这是一个显示问题的简单示例.Xform 类来自 Oracle 的分子示例 (http://docs.oracle.com/javase/8/javafx/graphics-tutorial/sampleapp3d-code.htm#CJAGGIFG):

Here is a simple example showing the problem. The Xform class is from the molecule sample of Oracle (http://docs.oracle.com/javase/8/javafx/graphics-tutorial/sampleapp3d-code.htm#CJAGGIFG):

package mypackage;

import mypackage.Xform;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Example  extends Application {

    private Stage primaryStage;
    private final Group root = new Group();

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        primaryStage.setTitle("Example");
        this.primaryStage.setWidth(500);
        this.primaryStage.setHeight(500);

        Scene scene = new Scene(this.root, 500, 500, true, SceneAntialiasing.BALANCED);
        scene.setFill(Color.WHITESMOKE);

        Text text = new Text();
        text.setText("This is a text sample");
        text.setStyle("-fx-font-size: 20;");
        text.setCache(true);

        BorderPane borderPane = new BorderPane();
        borderPane.setStyle("-fx-border-color: black;-fx-background-color: #66CCFF;");
        borderPane.setTop(text);

        this.root.getChildren().add(borderPane);

        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setNearClip(0.1);
        camera.setFarClip(10000.0);
        camera.setTranslateX(100);
        camera.setTranslateZ(-500);

        Xform cameraXform = new Xform();
        Xform cameraXform2 = new Xform();
        Xform cameraXform3 = new Xform();

        cameraXform.getChildren().add(cameraXform2);
        cameraXform2.getChildren().add(cameraXform3);
        cameraXform3.getChildren().add(camera);
        //cameraXform3.setRotateZ(180.0);
        cameraXform.ry.setAngle(400.0); // 320
        cameraXform.rx.setAngle(20.0); // 40

        scene.setCamera(camera);

        this.primaryStage.setScene(scene);
        this.primaryStage.show();
    }

    public static void main(String[] args) {
        System.setProperty("prism.lcdtext", "false");
        System.setProperty("prism.text", "t2k");
        launch(args);
    }

}

推荐答案

由于您将 Text 节点嵌入到样式化的边框窗格中,并在场景中同时渲染两者,因此还为边框窗格真的很有帮助.

Since you are embedding the Text node in a styled border pane, and rendering both in the scene, setting also the cache for the border pane really helps.

borderPane.setCache(true);

你将从这里开始:

为此:

此外,您可以设置此提示,以提高分辨率.

Also, you can set this hint, to improve resolution.

borderPane.setCacheHint(CacheHint.SCALE_AND_ROTATE);

这篇关于JavaFX 2D 文本与 3D 场景中的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆