JavaFX 3D着色面...再次 [英] JavaFX 3D Colouring faces ... again

查看:167
本文介绍了JavaFX 3D着色面...再次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I studied this question, but I still don't get it. The shortest possible code below shows a Pyramid totally grey, whereas I try to give the 6 triangles making up the pyramid different colors. So ... why don't these colors show up?

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

Note that I borrowed the getTexCoords().addAll(..) statement from that question, but clearly I still am doing something wrong. Is it the uv mapping? What is that anyway? I have seen a topological explanation (sphere <-> map), but what has that got to do with 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 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

To understand how JavaFX 3D defines the color of any given 3D shape, have a look at the PhongMaterial javadoc (bold is mine):

PhongMaterial类提供表示Phong着色材质的属性的定义.它描述了光线与其所应用到的网格表面之间的相互作用. PhongMaterial通过反射和镜面反射分量以及环境和自发光项反射光. 几何表面上一个点的颜色是这四个分量的数学函数.

这意味着您首先需要提供一种材料,然后需要指定这些组件中的任何一个,例如漫反射组件.

That means that you need to supply a material in the first place, and then you need to specify any of those components, for instance the diffuse component.

如果您从引用的问题中复制图像:

If you copy the image from the cited question:

并使用它创建一个材质实例:

and create a material instance with it:

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

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

you can see that this image is used to apply colors to your pyramid:

如果修改面部的纹理索引,则基于纹理坐标,您将获得不同的颜色.

If you modify the texture indices for the faces, you will get different colors, based on the texture coordinates.

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

To know more about this, you can have a look at the FXyz3D library, that provides a TexturedMesh class based in this concept. There you will find many different 3D shape "textured" primitives, that can use different texture "modes". Most of those modes don't even require an image, as this is created internally. This allows creating for instance color gradients based on mathematical functions.

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

This is an example of a TetrahedraMesh, that makes use a 3D function to define the density map:

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

这篇关于JavaFX 3D着色面...再次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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