JavaFX的2D文字与背景的3D场景 [英] JavaFX 2D text with background in 3D scene
问题描述
有关我的项目,我需要内部的3D场景2D文本(而不是覆盖!)。所以我尝试添加一个 BorderPane
与标签
/ 文本
节点,以我的场景:
现在的问题是但是,面板的白色背景,有时与标签(它们具有相同的深度明显),当我放大,缩小或绕飞与我的相机重叠。
有没有一种方法来提升,从它的面板上的标签吗?我试着设置 setDepthTest(真);
有没有效果
下面是一个简单的例子,显示的问题。该的Xform
类是从Oracle的分子样品(的http://docs.oracle.com/javase/8/javafx/graphics-tutorial/sampleapp3d-$c$c.htm#CJAGGIFG):
mypackage的包;
进口mypackage.Xform;
进口javafx.application.Application;
进口javafx.scene.Group;
进口javafx.scene.PerspectiveCamera;
进口javafx.scene.Scene;
进口javafx.scene.SceneAntialiasing;
进口javafx.scene.layout.BorderPane;
进口对javafx.scene.paint.Color;
进口javafx.scene.text.Text;
进口javafx.stage.Stage;
公共类实例扩展应用{
私人阶段primaryStage;
私人最终小组根=新集团();
@覆盖
公共无效启动(阶段primaryStage)抛出异常{
this.primaryStage = primaryStage;
primaryStage.setTitle(示例);
this.primaryStage.setWidth(500);
this.primaryStage.setHeight(500);
一幕一幕=新场景(this.root,500,500,真实,SceneAntialiasing.BALANCED);
scene.setFill(Color.WHITESMOKE);
文本文本=新文本();
text.setText(这是一个文本样本);
text.setStyle( - FX-字体大小:20;);
text.setCache(真正的);
BorderPane borderPane =新BorderPane();
borderPane.setStyle( - FX-边框颜色:黑色; -fx-背景颜色:#66CCFF;);
borderPane.setTop(文本);
this.root.getChildren()加(borderPane)。
PerspectiveCamera摄像头=新PerspectiveCamera(真正的);
camera.setNearClip(0.1);
camera.setFarClip(10000.0);
camera.setTranslateX(100);
camera.setTranslateZ(-500);
的Xform cameraXform =新的XForm();
的Xform cameraXform2 =新的XForm();
的Xform cameraXform3 =新的XForm();
cameraXform.getChildren()加(cameraXform2)。
cameraXform2.getChildren()加(cameraXform3)。
。cameraXform3.getChildren()加(摄像头);
//cameraXform3.setRotateZ(180.0);
cameraXform.ry.setAngle(400.0); // 320
cameraXform.rx.setAngle(20.0); // 40
scene.setCamera(照相机);
this.primaryStage.setScene(场景);
this.primaryStage.show();
}
公共静态无效的主要(字串[] args){
System.setProperty(prism.lcdtext,假);
System.setProperty(prism.text,T2K);
启动(参数);
}
}
由于要嵌入文本
节点的风格边框面板,并且无论是在场景渲染,还设置了缓存边界窗格中确实有帮助。
borderPane.setCache(真正的);
您会从这样的:
这样:
此外,您还可以设置这个提示,以提高分辨率。
borderPane.setCacheHint(CacheHint.SCALE_AND_ROTATE);
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.
Is there a way to "elevate" the label from its panel? I've tried setting setDepthTest(true);
with no effect.
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);
}
}
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);
You will go from this:
to this:
Also, you can set this hint, to improve resolution.
borderPane.setCacheHint(CacheHint.SCALE_AND_ROTATE);
这篇关于JavaFX的2D文字与背景的3D场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!