- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
注意:我找到了一个解决方案并编辑了这个例子来展示如何去做。我将采用此代码并将其移动到我的更大的应用程序中。
我编写了一个 3d 恒星图表应用程序,它接收 GAIA 恒星数据并向您显示 3d 表示。您可以查询和绘制基于查询的一组基于其属性的恒星对象。此应用程序旨在帮助科幻作家、爱好者可视化空间区域中的真实关系。
当我构建 View 时,我绘制了代表星星(半径、颜色等)的球体。
我给这些星星中的每一个都贴上标签,以便用户可以识别出感兴趣的星星。
所以我的问题是,当我旋转 View (实际上是相机)时,标签会转动,用户会觉得这很不方便。那么如何在我旋转 View 时让它们面向相机呢?
一些细节。我保留两组。一个是绘制的所有星星,一个是标签。星星和标签都绘制在同一点上。分开组的原因是能够独立于星星打开和关闭标签(可用性问题)。
因此,当我旋转视野时,我想获取标签组并将每个标签转换为面向相机。
这是工作示例。我找到了一个 FXyz 库示例并将其更改为执行我想要的操作。 FXyz 唯一使用的是 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);
}
}
最佳答案
正如 Kleopatra 所建议的,我将代码包含在此处作为答案。
于是我四处寻找,在FXyz库包中找到了类似的解决方案。我修改它以适应我想要的,现在我有了我想要的工作版本。我从 FXyz 中包含的唯一部分是 MathUtils 钳位静态方法。
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);
// 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);
}
}
关于java - 对于 3d 星空(在 JavaFX 中),使标签与观察者的视角保持一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64265755/
我想知道哪个插件在 eclipse 中提供 Java 视角?相同的插件是否有助于创建新的 Java 项目?如果不是哪个插件贡献了它? 最佳答案 org.eclipse.jdt.ui插件提供java视角
每当我在 Eclipse 中打开 Java 透视图时,它就会如图所示。Java EE 的观点完全没问题。有人遇到同样的问题并解决了吗? 最佳答案 尝试重新调整你的观点。已选择 Java 透视图后,转到
我在基于社区服务器的平台上有一个博客,我想将其移至 Wordpress。由于这是一个我已经加入的社区,我不想失去该社区的固定读者,所以我计划交叉发布我在社区服务器平台上发布的每一个新帖子。我将发布博客
我正在尝试从旧的 Android 相机 API 切换到新的 CameraX API。我正在为增强现实应用程序使用预览模式,我需要获取一些信息,例如预览使用的相机的视角和大小。 到目前为止,这是我的代码
我们有一个快捷键可以从调用方法的任何地方跳转到方法定义,即通过在方法名称上使用 Ctrl+左键单击。但是,如果我当前位于方法定义上并且我想要转到调用或使用该方法的位置,该怎么办?这有什么捷径吗? 我已
我试图改变我的图的一个子图中的角度,它是一个 3d 图。我愿意: import matplotlib.pyplot as plt f1 = plt.figure() ax1 = f1.add_subp
可以使用 -perspective idPerspective 启动 eclipse 那么我怎样才能得到这个 idPerspective 呢? 最佳答案 如果您正在使用 PDE(插件开发环境)运行 E
我有标准版的 eclipse Galileo。现在我想在上面开发企业应用程序。知道如何获得 Java EE 视角吗?标准版如何安装Java EE工具? 最佳答案 我也遇到了同样的问题。我首先使用 Ub
我创建了一个简单的 Eclipse 插件,它只是从菜单中的命令打开一个新 View 。我正在使用 Eclipse RCP 2018,当我将插件作为应用程序运行时,一切看起来与添加了菜单选项的 java
我使用 C# 和 .NET 已经有一段时间了,尽管我之前一直使用 OOP 语言(更准确地说是 Java),但在处理 .NET 应用程序时,我发现接口(interface)使用的比我在 java 见过。
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
当我调试 Android 应用程序时,它会从 Java 角度激活“调试”角度;但当我使用红色终止按钮终止应用程序时,它不会切换回 Java 视角。 最佳答案 可能的重复:In Eclipse, how
如果使用 Eclipse EE,是否有任何理由使用 Java Perspective 而不是 Java EE Perspective?换句话说,对于 Eclipse EE,Java Perspecti
我是一名优秀的程序员,十分优秀!