JavaFX 3D Face painting ... again

I studied this question , but I still do not understand it. The shortest possible code below shows Pyramid completely gray, while I try to give the 6 triangles make up Pyramid different colors. So ... why aren't these colors displayed?

Note that I borrowed the getTexCoords().addAll(..) from this question, but obviously I'm still doing something wrong. Is it uv mapping ? What it is? I saw a topological explanation ( spheremap ), but what does this have to do with textures / colors ...?

Appreciate your help - Michael

 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); } } 
+5
source share
1 answer

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

The PhongMaterial class provides property definitions that represent shaded Phong material. It describes the interaction of light with the surface of the grid to which it is applied. PhongMaterial reflects light from the point of view of the diffuse and mirror component together with ambient and the concept of self-lighting. The color of a point on a geometric surface is a mathematical function of these four components .

This means that you need to provide the material first, and then you need to specify any of these components, such as a diffuse component.

If you copy the image from the above question :

palette

and create a copy of the material 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:

textured pyramid

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

To learn more about this, you can check out the FXyz3D library , which provides the TexturedMesh class based on this concept. There you will find many different “textured” 3D form primitives that can use different “modes” of texture. Most of these modes do not even require an image, as it is created internally. This allows you to create, for example, color gradients based on mathematical functions.

This is an example of TetrahedraMesh that uses a three-dimensional function to determine a density map:

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

Tetrahedramesh

+4
source

Source: https://habr.com/ru/post/1274035/


All Articles