如何在JavaFX 8中创建自定义3d模型?

尤金

我尝试使用官方教程在JavaFX应用程序中制作飞机,并使用以下代码:

Image diifuseMap = new Image(getClass().getResource("t.jpg").toExternalForm());
    TriangleMesh planeMesh = new TriangleMesh();
    float[] points = {
            -5, 5, 0,
            -5, -5, 0,
            5, 5, 0,
            5, -5, 0
    };
    float[] texCoords = {
            0, 0,
            0, 1,
            1, 0,
            1, 1
    };
    int[] faces = {
            0, 0, 1, 1, 2, 2,
            2, 2, 3, 3, 1, 1
    };
    planeMesh.getPoints().addAll(points);
    planeMesh.getTexCoords().addAll(texCoords);
    planeMesh.getFaces().addAll(faces);
    MeshView meshView =   new MeshView(planeMesh);
    meshView.setMaterial(new PhongMaterial(Color.BLACK, diifuseMap, null, null, null));
    Group3D plane = new Group3D(new MeshView(planeMesh));

但是,不幸的是,现场什么也没出现。谁能解释一下如何在JavaFX中创建自己的3d模型?是否可以在没有纹理的情况下创建它们(我想要线框模型)?

宝石海

现场什么也没出现

您的样本网格对我来说很好。

也许您没有正确设置相机或缩放了网格以使其可见。

您的样本网格并没有做太多,它是一个朝向相机的三角形和另一个朝向相机的三角形。

是否可以在没有纹理的情况下创建它们(我想要线框模型)?

是的,您的网格视图的DrawMode设置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装置,所述网格模型的数据可以被更有效地存储和直接映射到底层图形API比如果double[]ObservableList<Double>使用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 JavaFX 中创建自定义 ProgressIndicator?

如何在Java 3D中设置自定义FPS

如何在iOS 8中创建自定义委托?

如何在JavaFX中从CSS设置自定义字体?

如何在JavaFX的TableView中自定义setCellValueFactory的功能

如何在 JavaFX 中制作自定义 LineChart 控件?

如何在Swift 3中创建自定义通知?

如何使用PyVista从列表/数组创建自定义3D对象?

Plotly:如何为Plotly 3D Scatter图形创建自定义悬停标签?

CakePHP 3:如何创建自定义模型回调?

如何在java中动态创建自定义xml元素(或自定义javafx场景控件)

如何在 JavaFx 中为自定义组件创建属性?

如何在JavaFX CSS中创建自定义边框样式?

如何在D3中创建自定义元素

情节:如何自定义3D散点图的符号?

如何在Django中创建自定义模型字段,以及它如何工作

如何在Sequelize中在导入的模型中创建自定义方法或函数

如何创建自定义模型类

如何在Django中显示新创建的自定义模型字段的值?

我如何在 Django 模型表单中创建自定义验证

如何在ARCore的3D Sphere上放置自定义图像

如何在javafx中使用fxml创建自定义弹出窗口?

如何在自定义EVE路线中编写模型

如何在模型 Laravel 中编写自定义查询?

如何在cocos2d-x中创建自定义创建函数

如何在JavaFx中创建.xml文件

如何在 JavaFX 中创建连续的 TranslateTransition

如何在JavaFX中创建动画的LineChart?

如何在Javafx 8中触发Zoomevent