在 javafx 中将纹理应用于网格

applying texture to mesh in javafx

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

我找到了 this question,即使有详细的答案也无法弄清楚。我从头开始,完全复制答案中的代码,场景是黑色的,没有可见的二十面体。

我正在使用 Java 8. 提供的图像是 gif,代码将其引用为 png。我已经对文件的 png 和 gif 版本进行了尝试。据我所知,其他一切都与所引用问题的答案中的代码完全相同。

我能够 运行 this 和球体的纹理没有问题,但我希望能够使用二十面体而不是球体。

如果您使用的是 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

此片段显示了 5 种不同的纹理模式:

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

}