在javafx中将纹理应用于网格 [英] applying texture to mesh in javafx

查看:261
本文介绍了在javafx中将纹理应用于网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JavaFX和FXyz 0.1.1将纹理应用到网格时遇到了问题。



我发现


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.

解决方案

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

}

这篇关于在javafx中将纹理应用于网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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