为了实现一个3D星场(使用JavaFX),保持标签与观察者的视角平行。

huangapple go评论81阅读模式
英文:

For a 3d star field (in JavaFX), keeping the labels flat to the viewer perspective

问题

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Point3D;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Shape3D;
import javafx.scene.shape.Sphere;
import javafx.scene.text.Font;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Random;

import static org.fxyz3d.geometry.MathUtils.clamp;

/**
 * 2D Labels over 3D SubScene example
 */
public class StarFieldExample extends Application {

    // ... (The rest of the code is the same as provided in the original text)
}

Please note that the code has been truncated due to space limitations. If you need the complete translated code, please let me know.

英文:

NOTE: I found a solution and edited this example to show how to do it. I will take this code and move it to my much larger application.

I've written a 3d stellar charting application, that takes in GAIA stellar data and shows you a 3d representation. You can query and plot based on the query a set of stellar objects based on their properties. This application is intended to help science fiction writers, hobbyists in visualizing the real relationships in a region of space.

When I construct the view, I plot spheres representing stars (radius, color, etc.).

I label each of these stars so that users can identify stars of interest.

So my issue is that as I rotate the view (really the camera), the labels turn and users find this inconvenient. So how do I keep them facing the camera as I rotate the view?

Some details. I keep two groups. One is all the stars plotted and one for the labels. Both a star and a label are plotted at the same point. The reason for the separate groups is to be able to turn labels on and off independently from the stars (usability concerns).

So, as I rotate the field of view, I want to take the group of labels and transform each to face towards the camera.

example: (the stems project from the equatorial plane to the star, the grid maps the equatorial plane for perspective)
为了实现一个3D星场(使用JavaFX),保持标签与观察者的视角平行。

as the view turns:
为了实现一个3D星场(使用JavaFX),保持标签与观察者的视角平行。

Here is the working example. I found a FXyz library example and changed it to do what I want. The only thing from FXyz used is Mathutils.clamp(...).

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Point3D;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Shape3D;
import javafx.scene.shape.Sphere;
import javafx.scene.text.Font;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import java.util.HashMap;
import java.util.Random;
import static org.fxyz3d.geometry.MathUtils.clamp;
/**
* example for floating labels
*/
public class StarFieldExample extends Application {
final double sceneWidth = 600;
final double sceneHeight = 600;
private double mousePosX;
private double mousePosY;
private double mouseOldX;
private double mouseOldY;
private double mouseDeltaX;
private double mouseDeltaY;
private final Font font = new Font("arial", 10);
// We'll use custom Rotate transforms to manage the coordinate conversions
private final Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
private final Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
private final Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);
private final Group root = new Group();
private final Group nodeGroup = new Group();  //all 3D nodes in scene
private final Group labelGroup = new Group(); //all generic 3D labels
//All shapes and labels linked via hash for easy update during camera movement
private final HashMap<Shape3D, Label> shape3DToLabel = new HashMap<>();
private SubScene subScene;
private final Random random = new Random();
private final static double RADIUS_MAX = 7;
private final static double X_MAX = 300;
private final static double Y_MAX = 300;
private final static double Z_MAX = 300;
public Pane createStarField() {
//attach our custom rotation transforms so we can update the labels dynamically
nodeGroup.getTransforms().addAll(rotateX, rotateY, rotateZ);
subScene = new SubScene(nodeGroup, sceneWidth, sceneHeight, true, SceneAntialiasing.BALANCED);
subScene.setFill(Color.BLACK);
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setNearClip(0.1);
camera.setFarClip(10000.0);
camera.setTranslateZ(-1000);
subScene.setCamera(camera);
Group sceneRoot = new Group(subScene);
sceneRoot.getChildren().add(labelGroup);
generateRandomStars(20);
subScene.setOnMousePressed((MouseEvent me) -> {
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
mouseOldX = me.getSceneX();
mouseOldY = me.getSceneY();
}
);
subScene.setOnMouseDragged((MouseEvent me) -> {
mouseOldX = mousePosX;
mouseOldY = mousePosY;
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
mouseDeltaX = (mousePosX - mouseOldX);
mouseDeltaY = (mousePosY - mouseOldY);
double modifier = 5.0;
double modifierFactor = 0.1;
if (me.isPrimaryButtonDown()) {
if (me.isAltDown()) { //roll
rotateZ.setAngle(((rotateZ.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // +
} else {
rotateY.setAngle(((rotateY.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // +
rotateX.setAngle(
clamp(
(((rotateX.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180),
-60,
60
)
); // -
}
}
updateLabels();
}
);
// add to the 2D portion of this component
Pane pane = new Pane();
pane.setPrefSize(sceneWidth, sceneHeight);
pane.setMaxSize(Pane.USE_COMPUTED_SIZE, Pane.USE_COMPUTED_SIZE);
pane.setMinSize(Pane.USE_COMPUTED_SIZE, Pane.USE_COMPUTED_SIZE);
pane.setBackground(Background.EMPTY);
pane.getChildren().add(sceneRoot);
pane.setPickOnBounds(false);
subScene.widthProperty().bind(pane.widthProperty());
subScene.heightProperty().bind(pane.heightProperty());
Platform.runLater(this::updateLabels);
return (pane);
}
public void generateRandomStars(int numberStars) {
for (int i = 0; i < numberStars; i++) {
double radius = random.nextDouble() * RADIUS_MAX;
Color color = randomColor();
double x = random.nextDouble() * X_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
double y = random.nextDouble() * Y_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
double z = random.nextDouble() * Z_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
String labelText = "Star " + i;
boolean fadeFlag = random.nextBoolean();
createSphereLabel(radius, x, y, z, color, labelText);
}
}
private Color randomColor() {
int r = random.nextInt(255);
int g = random.nextInt(255);
int b = random.nextInt(255);
return Color.rgb(r, g, b);
}
private void createSphereLabel(double radius, double x, double y, double z, Color color, String labelText) {
Sphere sphere = new Sphere(radius);
sphere.setTranslateX(x);
sphere.setTranslateY(y);
sphere.setTranslateZ(z);
sphere.setMaterial(new PhongMaterial(color));
//add our nodes to the group that will later be added to the 3D scene
nodeGroup.getChildren().add(sphere);
Label label = new Label(labelText);
label.setTextFill(color);
label.setFont(font);
labelGroup.getChildren().add(label);
//Add to hashmap so updateLabels() can manage the label position
shape3DToLabel.put(sphere, label);
}
private void updateLabels() {
shape3DToLabel.forEach((node, label) -> {
Point3D coordinates = node.localToScene(Point3D.ZERO, true);
//Clipping Logic
//if coordinates are outside of the scene it could
//stretch the screen so don't transform them
double x = coordinates.getX();
double y = coordinates.getY();
// is it left of the view?
if (x < 0) {
x = 0;
}
// is it right of the view?
if ((x + label.getWidth() + 5) > subScene.getWidth()) {
x = subScene.getWidth() - (label.getWidth() + 5);
}
// is it above the view?
if (y < 0) {
y = 0;
}
// is it below the view
if ((y + label.getHeight()) > subScene.getHeight()) {
y = subScene.getHeight() - (label.getHeight() + 5);
}
//update the local transform of the label.
label.getTransforms().setAll(new Translate(x, y));
});
}
//////////////////////////////////
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = createStarField();
root.getChildren().add(pane);
Scene scene = new Scene(root, sceneWidth, sceneHeight);
primaryStage.setTitle("2D Labels over 3D SubScene");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

答案1

得分: 2

As Kleopatra suggested, I am including the code here as an answer.

So I looked around and found a similar solution in the FXyz library package. I modified it to fit what I wanted and now I have a working version of what I want. 
The only part that I included from FXyz is the MathUtils clamp static method. 

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Point3D;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Shape3D;
import javafx.scene.shape.Sphere;
import javafx.scene.text.Font;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Random;

import static org.fxyz3d.geometry.MathUtils.clamp;

/**
 * example for flat labels
 */
public class StarFieldExample extends Application {

    final double sceneWidth = 600;
    final double sceneHeight = 600;

    private double mousePosX;
    private double mousePosY;
    private double mouseOldX;
    private double mouseOldY;
    private double mouseDeltaX;
    private double mouseDeltaY;

    private final Font font = new Font("arial", 10);

    private final Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
    private final Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
    private final Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);

    private final Group root = new Group();
    private final Group nodeGroup = new Group();
    private final Group labelGroup = new Group();

    private final HashMap<Shape3D, Label> shape3DToLabel = new HashMap<>();

    private SubScene subScene;

    private final Random random = new Random();

    private final static double RADIUS_MAX = 7;
    private final static double X_MAX = 300;
    private final static double Y_MAX = 300;
    private final static double Z_MAX = 300;

    public Pane createStarField() {
        nodeGroup.getTransforms().addAll(rotateX, rotateY, rotateZ);

        subScene = new SubScene(nodeGroup, sceneWidth, sceneHeight, true, SceneAntialiasing.BALANCED);
        subScene.setFill(Color.BLACK);

        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setNearClip(0.1);
        camera.setFarClip(10000.0);
        camera.setTranslateZ(-1000);

        subScene.setCamera(camera);
        Group sceneRoot = new Group(subScene);
        sceneRoot.getChildren().add(labelGroup);

        generateRandomStars(20);

        subScene.setOnMousePressed((MouseEvent me) -> {
            mousePosX = me.getSceneX();
            mousePosY = me.getSceneY();
            mouseOldX = me.getSceneX();
            mouseOldY = me.getSceneY();
        });

        subScene.setOnMouseDragged((MouseEvent me) -> {
            mouseOldX = mousePosX;
            mouseOldY = mousePosY;
            mousePosX = me.getSceneX();
            mousePosY = me.getSceneY();
            mouseDeltaX = (mousePosX - mouseOldX);
            mouseDeltaY = (mousePosY - mouseOldY);
            double modifier = 5.0;
            double modifierFactor = 0.1;

            if (me.isPrimaryButtonDown()) {
                if (me.isAltDown()) {
                    rotateZ.setAngle(((rotateZ.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180);
                } else {
                    rotateY.setAngle(((rotateY.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180);
                    rotateX.setAngle(
                            clamp(
                                    (((rotateX.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180),
                                    -60,
                                    60
                            )
                    );
                }
            }
            updateLabels();
        });

        Pane pane = new Pane();
        pane.setPrefSize(sceneWidth, sceneHeight);
        pane.setMaxSize(Pane.USE_COMPUTED_SIZE, Pane.USE_COMPUTED_SIZE);
        pane.setMinSize(Pane.USE_COMPUTED_SIZE, Pane.USE_COMPUTED_SIZE);
        pane.setBackground(Background.EMPTY);
        pane.getChildren().add(sceneRoot);
        pane.setPickOnBounds(false);

        subScene.widthProperty().bind(pane.widthProperty());
        subScene.heightProperty().bind(pane.heightProperty());
        Platform.runLater(this::updateLabels);
        return (pane);
    }

    public void generateRandomStars(int numberStars) {
        for (int i = 0; i < numberStars; i++) {
            double radius = random.nextDouble() * RADIUS_MAX;
            Color color = randomColor();
            double x = random.nextDouble() * X_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
            double y = random.nextDouble() * Y_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
            double z = random.nextDouble() * Z_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);

            String labelText = "Star " + i;
            boolean fadeFlag = random.nextBoolean();
            createSphereLabel(radius, x, y, z, color, labelText);
        }
    }

    private Color randomColor() {
        int r = random.nextInt(255);
        int g = random.nextInt(255);
        int b = random.nextInt(255);
        return Color.rgb(r, g, b);
    }

    private void createSphereLabel(double radius, double x, double y, double z, Color color, String labelText) {
        Sphere sphere = new Sphere(radius);
        sphere.setTranslateX(x);
        sphere.setTranslateY(y);
        sphere.setTranslateZ(z);
        sphere.setMaterial(new PhongMaterial(color));
        nodeGroup.getChildren().add(sphere);

        Label label = new Label(labelText);
        label.setTextFill(color);
        label.setFont(font);
        labelGroup.getChildren().add(label);

        shape3DToLabel.put(sphere, label);
    }

    private void updateLabels() {
        shape3DToLabel.forEach((node, label) -> {
            Point3D coordinates = node.localToScene(Point3D.ZERO, true);

            double x = coordinates.getX();
            double y = coordinates.getY();

            if (x < 0) {
                x = 0;
            }

            if ((x + label.getWidth() + 5) > subScene.getWidth()) {
                x = subScene.getWidth() - (label.getWidth() + 5);
            }

            if (y < 0) {
                y = 0;
            }

            if ((y + label.getHeight()) > subScene.getHeight()) {
                y = subScene.getHeight() - (label.getHeight() + 5);
            }

            label.getTransforms().setAll(new Translate(x, y));
        });
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane pane = create

<details>
<summary>英文:</summary>

As Kleopatra suggested, I am including the code here as an answer.

So I looked around and found a similar solution in the FXyz library package. I modified it to fit what I wanted and now I have a working version of what I want. 
The only part that I included from FXyz is the MathUtils clamp static method. 

    
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.geometry.Point3D;
    import javafx.scene.*;
    import javafx.scene.control.Label;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.Background;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.paint.PhongMaterial;
    import javafx.scene.shape.Shape3D;
    import javafx.scene.shape.Sphere;
    import javafx.scene.text.Font;
    import javafx.scene.transform.Rotate;
    import javafx.scene.transform.Translate;
    import javafx.stage.Stage;
    
    import java.util.HashMap;
    import java.util.Random;
    
    import static org.fxyz3d.geometry.MathUtils.clamp;
    
    /**
     * example for flat labels
     */
    public class StarFieldExample extends Application {
    
        final double sceneWidth = 600;
        final double sceneHeight = 600;
    
        private double mousePosX;
        private double mousePosY;
        private double mouseOldX;
        private double mouseOldY;
        private double mouseDeltaX;
        private double mouseDeltaY;
    
        private final Font font = new Font(&quot;arial&quot;, 10);
    
        // We&#39;ll use custom Rotate transforms to manage the coordinate conversions
        private final Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
        private final Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
        private final Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);
    
        private final Group root = new Group();
        private final Group nodeGroup = new Group();  //all 3D nodes in scene
        private final Group labelGroup = new Group(); //all generic 3D labels
    
        //All shapes and labels linked via hash for easy update during camera movement
        private final HashMap&lt;Shape3D, Label&gt; shape3DToLabel = new HashMap&lt;&gt;();
    
        private SubScene subScene;
    
        private final Random random = new Random();
    
        private final static double RADIUS_MAX = 7;
        private final static double X_MAX = 300;
        private final static double Y_MAX = 300;
        private final static double Z_MAX = 300;
    
        public Pane createStarField() {
    
            //attach our custom rotation transforms so we can update the labels dynamically
            nodeGroup.getTransforms().addAll(rotateX, rotateY, rotateZ);
    
            subScene = new SubScene(nodeGroup, sceneWidth, sceneHeight, true, SceneAntialiasing.BALANCED);
            subScene.setFill(Color.BLACK);
    
            PerspectiveCamera camera = new PerspectiveCamera(true);
            camera.setNearClip(0.1);
            camera.setFarClip(10000.0);
            camera.setTranslateZ(-1000);
    
            subScene.setCamera(camera);
            Group sceneRoot = new Group(subScene);
            sceneRoot.getChildren().add(labelGroup);
    
            generateRandomStars(20);
    
            subScene.setOnMousePressed((MouseEvent me) -&gt; {
                        mousePosX = me.getSceneX();
                        mousePosY = me.getSceneY();
                        mouseOldX = me.getSceneX();
                        mouseOldY = me.getSceneY();
                    }
            );
    
            subScene.setOnMouseDragged((MouseEvent me) -&gt; {
                        mouseOldX = mousePosX;
                        mouseOldY = mousePosY;
                        mousePosX = me.getSceneX();
                        mousePosY = me.getSceneY();
                        mouseDeltaX = (mousePosX - mouseOldX);
                        mouseDeltaY = (mousePosY - mouseOldY);
                        double modifier = 5.0;
                        double modifierFactor = 0.1;
    
                        if (me.isPrimaryButtonDown()) {
                            if (me.isAltDown()) { //roll
                                rotateZ.setAngle(((rotateZ.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // +
                            } else {
                                rotateY.setAngle(((rotateY.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // +
                                rotateX.setAngle(
                                        clamp(
                                                (((rotateX.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180),
                                                -60,
                                                60
                                        )
                                ); // -
                            }
                        }
                        updateLabels();
                    }
            );
    
            // add to the 2D portion of this component
            Pane pane = new Pane();
            pane.setPrefSize(sceneWidth, sceneHeight);
            pane.setMaxSize(Pane.USE_COMPUTED_SIZE, Pane.USE_COMPUTED_SIZE);
            pane.setMinSize(Pane.USE_COMPUTED_SIZE, Pane.USE_COMPUTED_SIZE);
            pane.setBackground(Background.EMPTY);
            pane.getChildren().add(sceneRoot);
            pane.setPickOnBounds(false);
    
            subScene.widthProperty().bind(pane.widthProperty());
            subScene.heightProperty().bind(pane.heightProperty());
            Platform.runLater(this::updateLabels);
            return (pane);
        }
    
        public void generateRandomStars(int numberStars) {
            for (int i = 0; i &lt; numberStars; i++) {
                double radius = random.nextDouble() * RADIUS_MAX;
                Color color = randomColor();
                double x = random.nextDouble() * X_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
                double y = random.nextDouble() * Y_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
                double z = random.nextDouble() * Z_MAX * 2 / 3 * (random.nextBoolean() ? 1 : -1);
    
                String labelText = &quot;Star &quot; + i;
                boolean fadeFlag = random.nextBoolean();
                createSphereLabel(radius, x, y, z, color, labelText);
            }
        }
    
        private Color randomColor() {
            int r = random.nextInt(255);
            int g = random.nextInt(255);
            int b = random.nextInt(255);
            return Color.rgb(r, g, b);
        }
    
    
        private void createSphereLabel(double radius, double x, double y, double z, Color color, String labelText) {
            Sphere sphere = new Sphere(radius);
            sphere.setTranslateX(x);
            sphere.setTranslateY(y);
            sphere.setTranslateZ(z);
            sphere.setMaterial(new PhongMaterial(color));
            //add our nodes to the group that will later be added to the 3D scene
            nodeGroup.getChildren().add(sphere);
    
            Label label = new Label(labelText);
            label.setTextFill(color);
            label.setFont(font);
            labelGroup.getChildren().add(label);
    
            //Add to hashmap so updateLabels() can manage the label position
            shape3DToLabel.put(sphere, label);
        }
    
        private void updateLabels() {
            shape3DToLabel.forEach((node, label) -&gt; {
                Point3D coordinates = node.localToScene(Point3D.ZERO, true);
    
                //Clipping Logic
                //if coordinates are outside of the scene it could
                //stretch the screen so don&#39;t transform them
                double x = coordinates.getX();
                double y = coordinates.getY();
    
                // is it left of the view?
                if (x &lt; 0) {
                    x = 0;
                }
    
                // is it right of the view?
                if ((x + label.getWidth() + 5) &gt; subScene.getWidth()) {
                    x = subScene.getWidth() - (label.getWidth() + 5);
                }
    
                // is it above the view?
                if (y &lt; 0) {
                    y = 0;
                }
    
                // is it below the view
                if ((y + label.getHeight()) &gt; subScene.getHeight()) {
                    y = subScene.getHeight() - (label.getHeight() + 5);
                }
    
                //update the local transform of the label.
                label.getTransforms().setAll(new Translate(x, y));
            });
        }
    
        //////////////////////////////////
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            Pane pane = createStarField();
            root.getChildren().add(pane);
            Scene scene = new Scene(root, sceneWidth, sceneHeight);
            primaryStage.setTitle(&quot;2D Labels over 3D SubScene&quot;);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }

</details>



huangapple
  • 本文由 发表于 2020年10月8日 23:25:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64265755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定