applying texture to mesh in javafx

孤街浪徒 提交于 2019-12-10 22:51:31

问题


I was having trouble applying a texture to a mesh using JavaFX and FXyz 0.1.1.

I found this question and even with the detailed answer there could not figure it out. I started over from scratch, copying the code from the answer exactly and the scene is black with no visible icosohedron.

I'm using Java 8. The provided image is a gif and the code references it as a png. I've tried it with both png and gif versions of the file. As far as I can tell everything else is exactly as the code in the answer to the referenced question.

I am able to run this and texture that sphere without issue, but I would like to be able to use an icosohedron instead of a sphere.


回答1:


If you are using the FXyz library you can very easily apply different textures to an icosahedron or to any of the different primitives you can find in the library.

This snippet shows 5 different texture modes:

@Override
public void start(Stage primaryStage) {
    PerspectiveCamera camera = new PerspectiveCamera(true);   
    camera.setTranslateY(3);
    camera.setTranslateX(4);
    camera.setTranslateZ(-15);

    IcosahedronMesh icoLine = new IcosahedronMesh(100, 0);
    icoLine.setDrawMode(DrawMode.LINE);
    icoLine.getTransforms().addAll(new Rotate(10, Rotate.X_AXIS), new Rotate(-20, Rotate.Y_AXIS));

    IcosahedronMesh icoColor = new IcosahedronMesh(100, 0);
    icoColor.setTextureModeNone(Color.LIGHTGREEN);
    icoColor.getTransforms().addAll(new Rotate(20, Rotate.X_AXIS), new Rotate(-20, Rotate.Y_AXIS));

    IcosahedronMesh icoFunction = new IcosahedronMesh(100, 0);
    icoFunction.setTextureModeVertices3D(1530, p -> Math.cos(p.z));
    icoFunction.getTransforms().addAll(new Rotate(30, Rotate.X_AXIS), new Rotate(-20, Rotate.Y_AXIS));

    IcosahedronMesh icoFaces = new IcosahedronMesh(100, 0);
    icoFaces.setTextureModeFaces(5);
    icoFaces.getTransforms().addAll(new Rotate(20, Rotate.X_AXIS), new Rotate(-10, Rotate.Y_AXIS));

    IcosahedronMesh icoImage = new IcosahedronMesh(100, 0);
    icoImage.setTextureModeImage(getClass().getResource("icon.jpg").toExternalForm());
    icoImage.getTransforms().addAll(new Rotate(20, Rotate.X_AXIS), new Rotate(-20, Rotate.Y_AXIS));

    IcosahedronMesh icoPattern = new IcosahedronMesh(100, 0);
    icoPattern.setTextureModePattern(Patterns.CarbonPatterns.CARBON_KEVLAR, 100);
    icoPattern.getTransforms().addAll(new Rotate(20, Rotate.X_AXIS), new Rotate(-30, Rotate.Y_AXIS));


    GridPane grid = new GridPane();
    grid.add(new Group(icoLine), 0, 0);
    grid.add(new Group(icoColor), 1, 0);
    grid.add(new Group(icoFunction), 2, 0);

    grid.add(new Group(icoFaces), 0, 1);
    grid.add(new Group(icoImage), 1, 1);
    grid.add(new Group(icoPattern), 2, 1);
    Scene scene = new Scene(grid, 600, 400, true, SceneAntialiasing.BALANCED);
    scene.setCamera(camera);

    primaryStage.setScene(scene);
    primaryStage.setTitle(("Icosahedron - FXyz3D"));
    primaryStage.show(); 

}



来源:https://stackoverflow.com/questions/43929652/applying-texture-to-mesh-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!