如何在JavaFX的8创建自定义的三维模型? [英] How to create custom 3d model in JavaFX 8?

查看:599
本文介绍了如何在JavaFX的8创建自定义的三维模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着使用官方教程做的JavaFX应用程序的飞机,拥有下一个code:

 图片diifuseMap =新的图像(的getClass()的getResource(t.jpg)toExternalForm());
    TriangleMesh planeMesh =新TriangleMesh();
    浮动[]点= {
            -5,5,0,
            -5,-5,0,
            5,5,0,
            5,-5,0
    };
    浮动[] texCoords = {
            0,0,
            0,1
            1,0,
            1,1
    };
    INT []的面孔= {
            0,0,1,1,2,2,
            2,2,3,3,1,1
    };
    。planeMesh.getPoints()和addAll(分);
    。planeMesh.getTexCoords()和addAll(texCoords);
    。planeMesh.getFaces()和addAll(面);
    MeshView meshView =新MeshView(planeMesh);
    meshView.setMaterial(新PhongMaterial(Color.BLACK,diifuseMap,NULL,NULL,NULL));
    Group3D飞机=新Group3D(新MeshView(planeMesh));
 

不过,遗憾的是,没有出现在现场。任何人都可以解释如何创建自己的3D模型在JavaFX的?而且是有可能创造他们没有质感(我想线框模型)?

解决方案
  

没有出现在现场

您样品网呈现OK我。

也许你没有你的相机设置正确,或有你网缩放,这是可见的。

您样品网没有做太多,它是一个三角形朝向摄像头和背向摄像机第二个三角形面对的问题。

  

和是否有可能创建它们没有质感(我想线框模型)?

是的,<一个href="http://download.java.net/jdk8/jfxdocs/javafx/scene/shape/Shape3D.html#setDrawMode%28javafx.scene.shape.DrawMode%29"相对=nofollow>设置DrawMode 进行网格视图的

示例程序说明

我改变了你的脸的顺序,使他们都面临着同样的方向,使你得到一个正方形面对观众,而不是一个三角形向观众面对,一个远离观察者:

  INT []的面孔= {
    2,2,1,1,0,0,
    2,2,3,3,1,1
};
 

另外,根据纹理贴图需要改变(用于上述面阵列可以得到正确的方位上的纹理,所以它不是倒置)。

 浮法[] texCoords = {
    1,1,
    1,0,
    0,1
    0,0
};
 

我成立了一个扑杀回控制和动态旋转模型,这样你可以看到三角形的背侧(黑色),并很清楚什么是被渲染。我还添加了切换漫反射贴图(一些大理石)的纹理或线框模式的能力。

示例程序输出

示例程序

 进口javafx.animation *。
进口javafx.application.Application;
进口javafx.beans.binding.Bindings;
进口javafx.geometry.Insets;
进口javafx.scene *。
进口javafx.scene.control.CheckBox;
进口javafx.scene.image.Image;
进口javafx.scene.layout.VBox;
进口javafx.scene.paint *。
进口javafx.scene.shape *。
进口javafx.scene.transform.Rotate;
进口javafx.stage.Stage;
进口javafx.util.Duration;

公共类InlineModelViewer扩展应用{

  私有静态最终诠释VIEWPORT_SIZE = 800;

  私有静态最后双MODEL_SCALE_FACTOR = 40;
  私有静态最后双MODEL_X_OFFSET = 0;
  私有静态最后双MODEL_Y_OFFSET = 0;
  私有静态最后双MODEL_Z_OFFSET = VIEWPORT_SIZE / 2;

  //私有静态最后弦乐textureLoc =htt​​p://icons.iconarchive.com/icons/aha-soft/desktop-halloween/256/Skull-and-bones-icon.png; //图标许可证的LinkWare,反向链接到http://www.aha-soft.com
  私有静态最后弦乐textureLoc = "http://d366vafdki9sdu.cloudfront.net/files/37/2688/seamless-marble-textures-pack-screenshots-4.jpg";
  http://www.creattor.com/textures-patterns/seamless-marble-textures-pack-2688://纹理从源
  //质地许可证:http://creativecommons.org/licenses/by-nc/3.0/ =&GT;知识共享署名 - 非商业性使用3.0 Unported

  私人影像质感;
  私人PhongMaterial texturedMaterial =新PhongMaterial();

  私人MeshView meshView = loadMeshView();

  私人MeshView loadMeshView(){
    浮动[]点= {
        -5,5,0,
        -5,-5,0,
        5,5,0,
        5,-5,0
    };
    浮动[] texCoords = {
        1,1,
        1,0,
        0,1
        0,0
    };
    INT []的面孔= {
        2,2,1,1,0,0,
        2,2,3,3,1,1
    };

    TriangleMesh网=新TriangleMesh();
    。mesh.getPoints()SETALL(分);
    。mesh.getTexCoords()SETALL(texCoords);
    。mesh.getFaces()SETALL(面);

    返回新MeshView(目);
  }

  私营集团建立场景(){
    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);

    返回新集团(meshView);
  }

  @覆盖
  公共无效启动(阶段阶段){
    纹理=新的图像(textureLoc);
    texturedMaterial.setDiffuseMap(纹理);

    群组=建立场景();

    RotateTransition旋转= rotate3dGroup(组);

    垂直框布局=新的垂直框(
        的createControls(旋转),
        createScene3D(组)
    );

    stage.setTitle(模型查看器);

    一幕一幕=新场景(布局,Color.CORNSILK);
    stage.setScene(场景);
    stage.show();
  }

  私人射手网createScene3D(集团集团){
    射手网scene3d =新射手网(组,VIEWPORT_SIZE,VIEWPORT_SIZE * 9.0 / 16,真正的,真实的);
    scene3d.setFill(Color.rgb(10,10,40));
    scene3d.setCamera(新PerspectiveCamera());
    返回scene3d;
  }

  私人垂直框的createControls(RotateTransition rotateTransition){
    复选框扑杀=新的复选框(卡尔后退);
    meshView.cullFaceProperty()。绑定(
        Bindings.when(
            cull.selectedProperty())
              。然后(CullFace.BACK)
              不然的话(CullFace.NONE)
    );
    复选框线框=新的复选框(线框);
    meshView.drawModeProperty()。绑定(
        Bindings.when(
            wireframe.selectedProperty())
              。然后(DrawMode.LINE)
              不然的话(DrawMode.FILL)
    );

    复选框旋转=新的复选框(旋转);
    。rotate.selectedProperty()的addListener(观察到 - &GT; {
      如果(rotate.isSelected()){
        rotateTransition.play();
      } 其他 {
        rotateTransition.pause();
      }
    });

    复选框纹理=新的复选框(纹理);
    meshView.materialProperty()。绑定(
        Bindings.when(
            texture.selectedProperty())
              。然后(texturedMaterial)
              不然的话((PhongMaterial)空)
    );

    垂直框控件=新的垂直框(10,旋转,质地,宰杀,线框);
    controls.setPadding(新插图(10));
    返回控制;
  }

  私人RotateTransition rotate3dGroup(集团集团){
    RotateTransition旋转=新RotateTransition(Duration.seconds(10),基);
    rotate.setAxis(Rotate.Y_AXIS);
    rotate.setFromAngle(0);
    rotate.setToAngle(360);
    rotate.setInterpolator(Interpolator.LINEAR);
    rotate.setCycleCount(RotateTransition.INDEFINITE);

    返回旋转;
  }

  公共静态无效的主要(字串[] args){
    System.setProperty(prism.dirtyopts,假);
    启动(参数);
  }
}
 

  

任何人都可以解释如何创建自己的3D模型

这是太广泛了计算器的问题。有迹象表明,手在那种东西文凭​​的大学和艺术院校。

I tried to make a plane in JavaFX application using official tutorial and has the next code:

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));

But, unfortunately, nothing appeared in the scene. Can anybody explain how to create my own 3d models in JavaFX? And is it possible to create them without texture(I want wireframe model)?

解决方案

nothing appeared in the scene

Your sample mesh rendered OK for me.

Perhaps you didn't have your camera setup correctly or have your mesh scaled so that it was visible.

Your sample mesh doesn't do much, it is one triangle facing towards the camera and a second triangle facing away from the camera.

And is it possible to create them without texture(I want wireframe model)?

Yes, set the DrawMode for your mesh view to Line.

Sample Program Explanation

I changed the order of your faces so that they both face the same direction so that you get a square facing the viewer rather than one triangle facing towards the viewer and one away from the viewer:

int[] faces = {
    2, 2, 1, 1, 0, 0,
    2, 2, 3, 3, 1, 1
};

Also the texture map needs to change (for the above face array to get the right orientation on the texture so it is not upside down).

float[] texCoords = {
    1, 1,
    1, 0,
    0, 1,
    0, 0
};

I set up a cull back control and animated rotating the model so that you could see the "back" sides of the triangles (in black) and it is clear what is being rendered. I also added the ability to toggle a diffuse map (some marble) for a texture or a wireframe mode.

Sample Program Output

Sample Program

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 = "http://icons.iconarchive.com/icons/aha-soft/desktop-halloween/256/Skull-and-bones-icon.png"; // icon license linkware, backlink to http://www.aha-soft.com
  private static final String textureLoc = "http://d366vafdki9sdu.cloudfront.net/files/37/2688/seamless-marble-textures-pack-screenshots-4.jpg";
  // texture sourced from: http://www.creattor.com/textures-patterns/seamless-marble-textures-pack-2688
  // texture license: http://creativecommons.org/licenses/by-nc/3.0/ => Creative Commons Attribution-NonCommercial 3.0 Unported

  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, true);
    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);
  }
}

Can anybody explain how to create my own 3d models

That is too broad a question for StackOverflow. There are universities and art colleges that hand out diplomas in that kind of stuff.

这篇关于如何在JavaFX的8创建自定义的三维模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆