JavaFX 3D 着色面...再次

JavaFX 3D Colouring faces ... again

我研究了这个question,但我还是不明白。下面最短的可能代码显示 Pyramid 完全灰色,而我尝试给 6 triangles 组成 pyramid 不同的颜色。那么...为什么不显示这些颜色?

请注意,我从那个问题中借用了 getTexCoords().addAll(..) 语句,但显然我仍然做错了。是 uv mapping 吗?那到底是什么?我看过一个拓扑解释(sphere <-> map),但这和textures/colors...有什么关系?

感谢您的帮助 - 迈克尔

public class ColoredPyramid extends Application {
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 200, 200, true);
        primaryStage.setTitle("Colored Pyramid");
        primaryStage.setScene(scene);
        primaryStage.show();

        TriangleMesh colouredPyramid = new TriangleMesh();
        float height = 100;
        float hypotenuse = 150;
        colouredPyramid.getPoints().addAll(0, 0, 0); //0-index:: top
        colouredPyramid.getPoints().addAll(0, height, -hypotenuse / 2); //1-index:: x=0, z=-hyp/2 ==> Closest to user
        colouredPyramid.getPoints().addAll(-hypotenuse / 2, height, 0); //2-index:: x=hyp/2,  z=0 ==> Leftest
        colouredPyramid.getPoints().addAll(hypotenuse / 2, height, 0);  //3-index:: x=hyp/2,  z=0 ==> rightest
        colouredPyramid.getPoints().addAll(0, height, hypotenuse / 2); ////4-index:: x=0, z=hyp/2  ==> Furthest from user

        //Next statement copied from whosebug.com/questions/26831871/coloring-individual-triangles-in-a-triangle-mesh-on-javafx
        colouredPyramid.getTexCoords().addAll(
            0.1f, 0.5f, // 0 red
            0.3f, 0.5f, // 1 green
            0.5f, 0.5f, // 2 blue
            0.7f, 0.5f, // 3 yellow
            0.9f, 0.5f  // 4 orange
        );

        colouredPyramid.getFaces().addAll(0, 0, 2, 0, 1, 0); //Left front face ---> RED
        colouredPyramid.getFaces().addAll(0, 1, 1, 1, 3, 1); //Right front face ---> GREEN
        colouredPyramid.getFaces().addAll(0, 2, 3, 2, 4, 2); //Right back face ---> BLUE
        colouredPyramid.getFaces().addAll(0, 3, 4, 3, 2, 3); //Left back face ---> RED
        colouredPyramid.getFaces().addAll(4, 4, 1, 4, 2, 4); //Base: left triangle face ---> YELLOW
        colouredPyramid.getFaces().addAll(4, 0, 3, 0, 1, 0); //Base: right triangle face ---> ORANGE

        MeshView meshView = new MeshView(colouredPyramid);
        Group group = new Group(meshView);
        group.setTranslateX(100);
        group.setTranslateY(80);
        root.getChildren().add(group);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

要了解 JavaFX 3D 如何定义任何给定 3D 形状的颜色,请查看 PhongMaterial javadoc(粗体是我的):

The PhongMaterial class provides definitions of properties that represent a Phong shaded material. It describes the interaction of light with the surface of the Mesh it is applied to. The PhongMaterial reflects light in terms of a diffuse and specular component together with an ambient and a self illumination term. The color of a point on a geometric surface is mathematical function of these four components.

这意味着您需要首先提供一个 material,然后您需要指定任何这些组件,例如漫反射组件。

如果从引用的图片中复制question:

并用它创建一个 material 实例:

PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(new Image(getClass().getResourceAsStream("bB2jV.png")));
meshView.setMaterial(material);

您可以看到此图像用于为您的金字塔应用颜色:

如果您修改面的纹理索引,您将根据纹理坐标获得不同的颜色。

要了解更多信息,您可以查看 FXyz3D library,它提供了基于此概念的 TexturedMesh class。在那里你会发现许多不同的 3D 形状 "textured" 基元,它们可以使用不同的纹理 "modes"。大多数这些模式甚至不需要图像,因为这是内部创建的。这允许创建基于数学函数的颜色渐变。

这是一个 TetrahedraMesh 的示例,它使用 3D 函数来定义密度图:

TetrahedraMesh tetra = new TetrahedraMesh(10, 5, null);
tetra.setTextureModeVertices3D(1530, p -> p.magnitude());