I have a triangle mesh object in JAVAFX and would like to either
- color individual triangles of the triangle mesh
or
- color individual vertices of each triangle and have the triangle be colored according to an interpolation of the colors of each vertex (say for instance with a Gouraud shading).
The particular triangle mesh object is an icosphere with millions of faces (that is why I am using a triangle mesh: I need speed).
I have NOT used texture coordinates since I have been unable to find a clear explanation to it using JAVAFX, plus I am hoping that there is a easier way.
The way coloring works in JavaFX 3D meshes is by the material you assign them. For one mesh there's one material, and it's not possible to assing different materials to different triangles of the same mesh.
So if you want to avoid textures, I'm afraid the only way is grouping triangles with the same color in the same mesh, and creating so many meshes as colors.
On the contrary, with textures is relatively easy..., since you have just one mesh, one material and one image with all the coloring.
I've made an example of an icosahedron, built a triangle mesh for it and added a single texture to color all the faces.
For that, we need:
and the 20 faces. Each face is defined by 6 indices p0, t0, p1, t1, p3, t3, where p0, p1, p2 and p3 are indices into the points array, and t0, t1, t2 and t3 are indices into the texCoords array.
public class IcosahedronMesh extends MeshView {
}
Now we need an image with the coloring for each face, based on the net of an icosahedron, like this:
(Image found here)
Note that the mapping is done from the (0,0) to (1,1) normalized coordinates to the image (left,top) to (right, bottom) pixels.
Let's finally create the scene, load the mesh and add the texture to its material:
This is how it looks like:
EDIT
Now, if you think about how the texture is applied, you could simplify the image up to several squares with the palette of colors you need:
And the texture coordinates can be really simplified:
Finally, we have to map those points in our faces. Following the same pattern as the net image:
Now we'll have a very neat icosahedron, since we get rid of the borders and bad resolution of the image:
This can be extended to any triangle mesh, or use any algorithm to refine the triangles.