JavaFX 3D 着色面...再次

标签 java javafx javafx-3d

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

请注意,我从该问题中借用了 getTexCoords().addAll(..) 语句,但显然我仍然做错了。是 uv 映射吗?那到底是什么?我看过拓扑解释 (sphere <-> map),但这与纹理/颜色有什么关系...?

感谢您的帮助 - 迈克尔

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 stackoverflow.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 中复制图像:

palette

并用它创建一个 Material 实例:

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

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

textured pyramid

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

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

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

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

TetrahedraMesh

关于JavaFX 3D 着色面...再次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47793288/

相关文章:

java - Spring AOP 切入点表达式中包名称的通配符支持

java - 是否可以从 SWT 到 JavaFx 使用 NatTable 组件?

java - 创建播放器(不播放文件)

ssl - javafx 16 Web 引擎异常 "SSL Handshake failed"

javafx-8 - javafx中3D场景上的静态2D文本

java - 将鼠标悬停在相邻节点上时不会触发 MouseEntered/Exited

java - Android - 检索和使用 fragment 中的 View 位置

java - 从内部类引用封闭实例

video - 如何使用 JavaFX 在 3D 形状上播放视频?

java - 模拟私有(private)方法