场景中什么也没有出现
你的示例网格对我来说效果不错。
也许您没有正确设置相机,或者没有缩放网格以使其可见。
您的示例网格没有太多作用,它是一个面向相机的三角形和第二个背向相机的三角形。
是否可以在没有纹理的情况下创建它们(我想要线框模型)?
Yes, 设置绘图模式 http://download.java.net/jdk8/jfxdocs/javafx/scene/shape/Shape3D.html#setDrawMode%28javafx.scene.shape.DrawMode%29让您的网格视图Line http://download.java.net/jdk8/jfxdocs/javafx/scene/shape/DrawMode.html#LINE.
示例程序说明
我改变了你的脸的顺序,使它们都面向相同的方向,这样你就会得到一个面向观察者的正方形,而不是一个面向观察者的三角形和一个远离观察者的三角形:
int[] faces = {
2, 2, 1, 1, 0, 0,
2, 2, 3, 3, 1, 1
};
此外,纹理贴图需要更改(为了使上面的面数组在纹理上获得正确的方向,因此它不会颠倒)。
float[] texCoords = {
1, 1,
1, 0,
0, 1,
0, 0
};
我设置了一个剔除控件并以动画方式旋转模型,以便您可以看到三角形的“后”面(黑色),并且清楚正在渲染的内容。我还添加了切换纹理或线框模式的漫反射贴图(一些大理石)的功能。
示例程序输出
示例程序
import javafx.animation.*;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.CheckBox;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
public class InlineModelViewer extends Application {
private static final int VIEWPORT_SIZE = 800;
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 / 2;
private static final String textureLoc = "https://www.sketchuptextureclub.com/public/texture_f/slab-marble-emperador-cream-light-preview.jpg";
private Image texture;
private PhongMaterial texturedMaterial = new PhongMaterial();
private MeshView meshView = loadMeshView();
private MeshView loadMeshView() {
float[] points = {
-5, 5, 0,
-5, -5, 0,
5, 5, 0,
5, -5, 0
};
float[] texCoords = {
1, 1,
1, 0,
0, 1,
0, 0
};
int[] faces = {
2, 2, 1, 1, 0, 0,
2, 2, 3, 3, 1, 1
};
TriangleMesh mesh = new TriangleMesh();
mesh.getPoints().setAll(points);
mesh.getTexCoords().setAll(texCoords);
mesh.getFaces().setAll(faces);
return new MeshView(mesh);
}
private Group buildScene() {
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);
return new Group(meshView);
}
@Override
public void start(Stage stage) {
texture = new Image(textureLoc);
texturedMaterial.setDiffuseMap(texture);
Group group = buildScene();
RotateTransition rotate = rotate3dGroup(group);
VBox layout = new VBox(
createControls(rotate),
createScene3D(group)
);
stage.setTitle("Model Viewer");
Scene scene = new Scene(layout, Color.CORNSILK);
stage.setScene(scene);
stage.show();
}
private SubScene createScene3D(Group group) {
SubScene scene3d = new SubScene(group, VIEWPORT_SIZE, VIEWPORT_SIZE * 9.0/16, true, SceneAntialiasing.BALANCED);
scene3d.setFill(Color.rgb(10, 10, 40));
scene3d.setCamera(new PerspectiveCamera());
return scene3d;
}
private VBox 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();
}
});
CheckBox texture = new CheckBox("Texture");
meshView.materialProperty().bind(
Bindings.when(
texture.selectedProperty())
.then(texturedMaterial)
.otherwise((PhongMaterial) null)
);
VBox controls = new VBox(10, rotate, texture, 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;
}
public static void main(String[] args) {
System.setProperty("prism.dirtyopts", "false");
launch(args);
}
}
谁能解释一下如何创建我自己的 3D 模型
对于 StackOverflow 来说,这个问题太宽泛了。有些大学和艺术学院颁发此类文凭。
谁能解释为什么 Mesh.setAll 采用 float[] 而 API 的其余部分使用 double ?
JavaFX 3D 实现提供了与图形硬件(例如 DirectX 和 OpenGL)通信的本机 API 的包装器。这些 API 使用浮点精度而不是双精度。使用float[]
直接在API中意味着网格模型数据可以比如果更有效地存储并直接映射到底层图形APIdouble[]
or an ObservableList<Double>
被使用。