gpt4 book ai didi

java - 在场景中放置 MeshView

转载 作者:行者123 更新时间:2023-11-30 08:12:29 24 4
gpt4 key购买 nike

我想在 JavaFX 中的 MeshView 上显示 3D 图形。因为我想构建一个简单的模型查看器,所以为了清晰起见,我创建了选项卡。其中一个包含一个组 (meshGroup),我在其中添加了一个 SubScene(在一个组中有 MeshView)。我无法将该模型以完整可用尺寸放置在 AnchorPane 的中间。 (我也试过 Pane & HBox )。 Image

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.Group?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Viewer">
<center>
<TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
<tabs>
<Tab text="Model">
<content>
<AnchorPane>
<children>
<Group fx:id="meshGroup" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Info">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
</center>
<left>
<ListView prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</left>
</BorderPane>

Controller 看起来像这样:

package sample;

import javafx.animation.Interpolator;
import javafx.animation.RotateTransition;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.SubScene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.TriangleMesh;
import javafx.scene.transform.Rotate;
import javafx.util.Duration;

import java.net.URL;
import java.util.ResourceBundle;

public class Viewer implements Initializable {

@FXML
Group meshGroup;

private PerspectiveCamera camera;
private MeshView meshView;

private static final int VIEWPORT_SIZE = 500;
private static final double MODEL_SCALE_FACTOR = 40;
private static final double MODEL_X_OFFSET = 0;
private static final double MODEL_Y_OFFSET = 0;
private static final double MODEL_Z_OFFSET = VIEWPORT_SIZE * 21;

@Override
public void initialize(URL location, ResourceBundle resources) {

initCamera();
Platform.runLater(() -> showFigure());
}

private void initCamera() {
this.camera = new PerspectiveCamera(true);
this.camera.setNearClip(0.1);
this.camera.setFarClip(10000.0);
this.camera.setTranslateZ(-1000);
}

private Group buildScene() {
Group group = new Group();

meshView.setTranslateX(VIEWPORT_SIZE / 2 + MODEL_X_OFFSET);
meshView.setTranslateY(VIEWPORT_SIZE / 2 * 9.0 / 16 + MODEL_Y_OFFSET);
meshView.setTranslateZ(VIEWPORT_SIZE / 2 + MODEL_Z_OFFSET);
meshView.setScaleX(MODEL_SCALE_FACTOR);
meshView.setScaleY(MODEL_SCALE_FACTOR);
meshView.setScaleZ(MODEL_SCALE_FACTOR);

PointLight pointLight = new PointLight(Color.WHITE);
pointLight.setTranslateZ(VIEWPORT_SIZE / 2);
pointLight.setTranslateY(VIEWPORT_SIZE / 2);

group.getChildren().addAll(meshView, pointLight);
return group;
}

private SubScene createScene3D(Group group) {
SubScene scene3d = new SubScene(group, VIEWPORT_SIZE, VIEWPORT_SIZE * 9.0 / 16);
scene3d.setFill(Color.WHITE);
scene3d.setCamera(this.camera);
scene3d.setPickOnBounds(true);
return scene3d;
}

private void showFigure() {
meshView = buildMesh();
// Add MeshView to Group
Group meshInGroup = buildScene();
// Create SubScene
SubScene subScene = createScene3D(meshInGroup);
// Add subScene to meshGroup
this.meshGroup.getChildren().add(subScene);

RotateTransition rotate = rotate3dGroup(meshInGroup);
this.meshGroup.getChildren().add(createControls(rotate));
}

private HBox createControls(RotateTransition rotateTransition) {
CheckBox cull = new CheckBox("Cull Back");
meshView.cullFaceProperty().bind(
Bindings.when(
cull.selectedProperty())
.then(CullFace.BACK)
.otherwise(CullFace.NONE)
);
CheckBox wireframe = new CheckBox("Wireframe");
meshView.drawModeProperty().bind(
Bindings.when(
wireframe.selectedProperty())
.then(DrawMode.LINE)
.otherwise(DrawMode.FILL)
);

CheckBox rotate = new CheckBox("Rotate");
rotate.selectedProperty().addListener(observable -> {
if (rotate.isSelected()) {
rotateTransition.play();
} else {
rotateTransition.pause();
}
});


HBox controls = new HBox(10, rotate, cull, wireframe);
controls.setPadding(new Insets(10));
return controls;
}


private RotateTransition rotate3dGroup(Group group) {
RotateTransition rotate = new RotateTransition(Duration.seconds(10), group);
rotate.setAxis(Rotate.Y_AXIS);
rotate.setFromAngle(0);
rotate.setToAngle(360);
rotate.setInterpolator(Interpolator.LINEAR);
rotate.setCycleCount(RotateTransition.INDEFINITE);

return rotate;
}


private MeshView buildMesh() {
TriangleMesh mesh = new TriangleMesh();
float hw = 100 / 2f;
float hh = 100 / 2f;
float hd = 100 / 2f;

mesh.getPoints().addAll(
hw, hh, hd,
hw, hh, -hd,
hw, -hh, hd,
hw, -hh, -hd,
-hw, hh, hd,
-hw, hh, -hd,
-hw, -hh, hd,
-hw, -hh, -hd
);

mesh.getTexCoords().addAll(
100, 0,
200, 0,
0, 100,
100, 100,
200, 100,
300, 100,
400, 100,
0, 200,
100, 200,
200, 200,
300, 200,
400, 200,
100, 300,
200, 300
);

mesh.getFaces().addAll(
0, 10, 2, 5, 1, 9,
2, 5, 3, 4, 1, 9,
4, 7, 5, 8, 6, 2,
6, 2, 5, 8, 7, 3,
0, 13, 1, 9, 4, 12,
4, 12, 1, 9, 5, 8,
2, 1, 6, 0, 3, 4,
3, 4, 6, 0, 7, 3,
0, 10, 4, 11, 2, 5,
2, 5, 4, 11, 6, 6,
1, 9, 3, 4, 5, 8,
5, 8, 3, 4, 7, 3
);

return new MeshView(mesh);
}
}

我在这上面花了很多时间,但我无法找到摆脱 MeshView 的直接方法。感谢任何反馈。

最佳答案

使您的子场景随其父级调整大小的一个简单解决方案是将其尺寸绑定(bind)到父级的尺寸。

例如:

private SubScene createScene3D(Group group) {
SubScene scene3d = new SubScene(group, VIEWPORT_SIZE, VIEWPORT_SIZE, true, SceneAntialiasing.BALANCED);
scene3d.widthProperty().bind(((AnchorPane)meshGroup.getParent()).widthProperty());
scene3d.heightProperty().bind(((AnchorPane)meshGroup.getParent()).heightProperty());

scene3d.setFill(Color.WHITE);
scene3d.setCamera(this.camera);
scene3d.setPickOnBounds(true);
return scene3d;
}

请注意,我已经考虑了 AnchorPane 而不是您添加子场景的 Group。另请注意使用深度缓冲和抗锯齿以获得更好的渲染效果。

既然子场景占据了整个标签区域,您可能不得不重新考虑初始缩放和平移。

这只会将盒子放在中间:

private static final double MODEL_SCALE_FACTOR = 6;

private void initCamera() {
this.camera = new PerspectiveCamera(true);
this.camera.setNearClip(0.1);
this.camera.setFarClip(10000.0);
this.camera.setTranslateZ(-2000);
}
private Group buildScene() {
Group group = new Group();

meshView.setScaleX(MODEL_SCALE_FACTOR);
meshView.setScaleY(MODEL_SCALE_FACTOR);
meshView.setScaleZ(MODEL_SCALE_FACTOR);

PointLight pointLight = new PointLight(Color.WHITE);
pointLight.setTranslateZ(-2*VIEWPORT_SIZE );
pointLight.setTranslateY(-2*VIEWPORT_SIZE );

group.getChildren().addAll(meshView, pointLight);
return group;
}

Meshviewer

关于java - 在场景中放置 MeshView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30467440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com