如何在javafx

时间:2019-04-17 14:22:48

标签: javafx text javafx-3d

我想在javafx中的球体上绘制文本。

我尝试仅将其保留为javafx。 我正在使用PerspectiveCamera

一种选择是仅绘制3D文本(如果存在),使其接触球体并与查看者平行以提高可读性。更好的选择是将弯曲的文本直接绘制在球体上,同时也面向观察者。

2 个答案:

答案 0 :(得分:6)

解决方案1. Text3D

没有内置的JavaFX 3D Text节点。但是,您可以使用FXyz库中的Text3DMesh节点。

类似这样的东西:

build.gradle

plugins {
  id 'application'
  id 'org.openjfx.javafxplugin' version '0.0.7'
}

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    implementation 'org.fxyz3d:fxyz3d:0.4.0'
}

javafx {
    version = "12"
    modules = [ 'javafx.controls' ]
}

mainClassName = 'text3d.Text3D'

text3d.Text3D.java

public class Text3D extends Application {

    private final Rotate rotateX = new Rotate(10, Rotate.X_AXIS);
    private final Rotate rotateY = new Rotate(-10, Rotate.Y_AXIS);
    private double mousePosX, mousePosY, mouseOldX, mouseOldY;

    @Override
    public void start(Stage stage) throws Exception {
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setNearClip(0.1);
        camera.setFarClip(10000.0);
        camera.getTransforms().addAll (rotateX, rotateY, new Translate(-100, 100, -1000));

        Text3DMesh text3D = new Text3DMesh("Text3D");
        text3D.setFont(Font.getFamilies().get(new Random().nextInt(Font.getFamilies().size())));
        text3D.setFontSize(200);
        text3D.setTextureModeVertices3D(1530, p -> p.y);
        Bounds bounds = text3D.getBoundsInParent();
        text3D.setTranslateX(- bounds.getWidth() / 2d);
        text3D.setTranslateY(bounds.getHeight() / 2d);
        text3D.setTranslateZ(-bounds.getDepth());

        double radius = Math.max(bounds.getWidth(), bounds.getHeight()) * 1.2;
        SpheroidMesh spheroid = new SpheroidMesh(radius);
        spheroid.setTextureModeVertices3D(1530, p -> p.x * p.y);
        spheroid.setTranslateZ(radius);

        Group group = new Group(spheroid, text3D);

        Scene scene = new Scene(group, 400, 300, true, SceneAntialiasing.BALANCED);
        scene.setFill(Color.BISQUE);
        scene.setCamera(camera);

        scene.setOnMousePressed(event -> {
            mousePosX = event.getSceneX();
            mousePosY = event.getSceneY();
        });

        scene.setOnMouseDragged(event -> {
            mousePosX = event.getSceneX();
            mousePosY = event.getSceneY();
            rotateX.setAngle(rotateX.getAngle() - (mousePosY - mouseOldY));
            rotateY.setAngle(rotateY.getAngle() + (mousePosX - mouseOldX));
            mouseOldX = mousePosX;
            mouseOldY = mousePosY;
        });

        stage.setScene(scene);
        stage.setTitle("FXyz3D Sample");
        stage.show();
    }
}

Text3D

解决方案2。图像

可以通过添加图像作为球体的扩散贴图来完成另一种方法。同样,这可以通过FXyz完成。

首先,您需要在场景上渲染常规Text 2D节点,并对其进行快照以生成图像。

text3d.Image3D

public class Image3D extends Application {

    private final Rotate rotateX = new Rotate(10, Rotate.X_AXIS);
    private final Rotate rotateY = new Rotate(-10, Rotate.Y_AXIS);

    private double mousePosX;
    private double mousePosY;
    private double mouseOldX;
    private double mouseOldY;

    @Override
    public void start(Stage stage) throws Exception {
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setNearClip(0.1);
        camera.setFarClip(10000.0);
        camera.setFieldOfView(20);
        camera.getTransforms().addAll (rotateX, rotateY, new Translate(-100, 100, -2000));

        Text text = new Text(" Text3D ");
        text.setStroke(Color.DARKGOLDENROD);
        text.setFill(Color.DARKGOLDENROD);
        text.setFont(Font.font(Font.getFamilies().get(new Random().nextInt(Font.getFamilies().size())), 30));
        Group root = new Group(text);
        Scene sceneAux = new Scene(root, root.getBoundsInLocal().getWidth(), root.getBoundsInLocal().getHeight());
        SnapshotParameters sp = new SnapshotParameters();
        double s = Screen.getPrimary().getOutputScaleX();
        sp.setTransform(new Scale(s, s));
        sp.setFill(Color.DARKMAGENTA);
        Image image = root.snapshot(sp, null);

        double radius = 400;
        SpheroidMesh spheroid = new SpheroidMesh(radius);
        ((PhongMaterial) spheroid.getMaterial()).setDiffuseMap(image);
        Group group = new Group(spheroid);

        Scene scene = new Scene(group, 400, 300, true, SceneAntialiasing.BALANCED);
        scene.setFill(Color.BISQUE);
        scene.setCamera(camera);

        scene.setOnMousePressed(event -> {
            mousePosX = event.getSceneX();
            mousePosY = event.getSceneY();
        });

        scene.setOnMouseDragged(event -> {
            mousePosX = event.getSceneX();
            mousePosY = event.getSceneY();
            rotateX.setAngle(rotateX.getAngle() - (mousePosY - mouseOldY));
            rotateY.setAngle(rotateY.getAngle() + (mousePosX - mouseOldX));
            mouseOldX = mousePosX;
            mouseOldY = mousePosY;
        });

        stage.setScene(scene);
        stage.setTitle("FXyz3D Sample");
        stage.show();
    }

}

Image3D

编辑

解决方案3

虽然这不是FXyz的一部分,但您也可以应用一些变换将Text3DMesh包裹在球体上。

每个字母基本上是一个TriangleMesh,带有X(offset)转换转换。

所以我们取每个字母的所有点,删除变换,然后应用偏移量,所以我们有了绝对坐标:

 x = x + offset;

然后,我们根据所需的给定弧长和文本节点的实际宽度w计算弯曲角度。

 t = (x - w/2) / (w/2) * Pi/2;

然后,对于每个点,其新坐标将为:

 x = (R + z) * cos(t);
 z = (R + z) * Sin(t);

text3d.CurvedText3D.java

public class CurvedText3D extends Application {

    private final Rotate rotateX = new Rotate(10, Rotate.X_AXIS);
    private final Rotate rotateY = new Rotate(-10, Rotate.Y_AXIS);

    private double mousePosX, mousePosY, mouseOldX, mouseOldY;

    @Override
    public void start(Stage stage) throws Exception {
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setNearClip(0.1);
        camera.setFarClip(10000.0);
        camera.setFieldOfView(40);
        camera.getTransforms().addAll (rotateX, rotateY, new Translate(-100, 100, -2000));

        Text3DMesh text3D = new Text3DMesh("Round Text3D",
                Font.getFamilies().get(new Random().nextInt(Font.getFamilies().size())), 100, true);
        text3D.setHeight(30);
        text3D.setTextureModeNone(Color.CRIMSON);
        Bounds bounds = text3D.getBoundsInParent();
        double width = bounds.getWidth();

        double radius = Math.max(width, bounds.getHeight());

        text3D.getMeshes().stream()
                .forEach(m -> {
                    ObservableFloatArray points = ((TriangleMesh) m.getMesh()).getPoints();
                    float[] v = new float[points.size()];
                    points.toArray(v);
                    double offset = m.getTransforms().get(0).getTx();
                    m.getTransforms().clear();
                    for (int i = 0; i < v.length; i += 3) {
                        v[i] += offset;
                        double t0 =  (v[i] - width / 2d) / (width / 2d) *  Math.PI / 2;
                        double t1 = (radius + v[i + 2]) * Math.cos(t0);
                        double t2 = (radius + v[i + 2]) * Math.sin(t0);
                        v[i] = (float) t1;
                        v[i + 2] = (float) t2;
                    }
                    ((TriangleMesh) m.getMesh()).getPoints().setAll(v);
                });

        SpheroidMesh spheroid = new SpheroidMesh(radius * 0.9);
        spheroid.setTextureModeVertices3D(1530, p -> p.x * p.y);

        Group group = new Group(spheroid, text3D);

        Scene scene = new Scene(group, 400, 300, true, SceneAntialiasing.BALANCED);
        scene.setFill(Color.BISQUE);
        scene.setCamera(camera);

        scene.setOnMousePressed(event -> {
            mousePosX = event.getSceneX();
            mousePosY = event.getSceneY();
        });

        scene.setOnMouseDragged(event -> {
            mousePosX = event.getSceneX();
            mousePosY = event.getSceneY();
            rotateX.setAngle(rotateX.getAngle() - (mousePosY - mouseOldY));
            rotateY.setAngle(rotateY.getAngle() + (mousePosX - mouseOldX));
            mouseOldX = mousePosX;
            mouseOldY = mousePosY;
        });

        stage.setScene(scene);
        stage.setTitle("FXyz3D Sample");
        stage.show();
    }

}

Curved text 3d

答案 1 :(得分:2)

我想到的一个想法是,在(TextField / Area / Flow)这样的适当节点中正常渲染文本,然后对该节点进行快照。然后,生成的图像可以用作包裹球体的纹理。