1

I created a design in blender exported to STL and used the StlModelImporterJFX to import it into my JavaFX program and run it. Everything runs fine, the application works, there is just one thing missing...texture, so basically, I want to take my imported mesh and create an image as seen below for a smaller design.

enter image description here

Is there any program or algorithm that I can use to create an image such as that below that I can later edit manually and use as a texture for the entire Triangle Mesh? Also on a side note is it possible to edit this image live in the program and swap out colours while running? Sorry if this is poorly worded, if you want any clarification, I can provide it.

José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • 1
    JavaFx has a polygon class that can create triangles. That will give you more control of what you can do with each triangle. – SedJ601 May 22 '17 at 02:32
  • Did you create the image in Blender and it didn't get imported? You can create textures with any 3D modeling studio. If you want to edit the image on the fly you'll have to do either image manipulation or create the image in JavaFX to begin with. – user1803551 May 22 '17 at 08:49

1 Answers1

2

When you import a 3D model with a third-party 3D importer you have less control of the resulting TriangleMesh. If you want to provide texture features to your model you'll have to edit the exported file and add the texture coordinates, which is not the best approach.

But if you could generate the mesh from scratch, you could easily apply textures over it.

This question shows how you can define the texture coordinates and uses the same net image you have to provide the texture of an icosahedron.

Based on the answer on that question, the texture can be defined without an actual image, just with a palette of colors.

And you can easily change those on runtime, i.e. when you click on one face you can change the color on that face.

The Fxyz library makes use of a TexturedMesh, designed to easily apply textures to 3D shapes.

You can find many primitives there, like the icosahedron.

This question shows the result of different texture modes over an icosahedron.

This short snippet shows how you can apply a texture over faces, and change it on runtime:

private int numColors = 10;

@Override
public void start(Stage primaryStage) {
    PerspectiveCamera camera = new PerspectiveCamera(true);   
    camera.setTranslateZ(-5);

    IcosahedronMesh icoFaces = new IcosahedronMesh(100, 0);
    icoFaces.setTextureModeFaces(numColors);
    icoFaces.getTransforms().addAll(new Rotate(20, Rotate.X_AXIS), new Rotate(-10, Rotate.Y_AXIS));

    final Group group = new Group(icoFaces);

    Scene scene = new Scene(group, 600, 400, true, SceneAntialiasing.BALANCED);
    scene.setCamera(camera);

    primaryStage.setScene(scene);
    primaryStage.setTitle(("Icosahedron - FXyz3D"));
    primaryStage.show(); 

    icoFaces.setOnMouseClicked(e -> {
        ObservableFaceArray faces = ((TriangleMesh) icoFaces.getMesh()).getFaces();
        int selectedFace = e.getPickResult().getIntersectedFace();

        int colorId = faces.get(6 * selectedFace + 1);
        int newColorId = colorId + 1 >= numColors ? 0 : colorId + 1;
        faces.set(6 * selectedFace + 1, newColorId);
        faces.set(6 * selectedFace + 3, newColorId);
        faces.set(6 * selectedFace + 5, newColorId);
    });
}

Running the application:

icosahedron

And after clicking in the frontal green face:

icosahedron with new face texture

Community
  • 1
  • 1
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • Unfortunately, the files that I have imported are far too complicated to simply generate outside of blender, the one I am looking at in particular is 1,050,625 faces in total so simply generating it in JavaFX is not an option and I have to import it. – Politic Revolutionnaire May 23 '17 at 20:09
  • The only way to have textures in your JavaFX model then is to export them initially with your Blender model. I've done it before with .OBJ models. See this [project](https://bitbucket.org/JPereda/multimodel3dfx). Note the *.obj files already include the texture coordinates for each face. – José Pereda May 23 '17 at 20:14
  • Will I be able to edit the imported textures while the software is running? Also, I am using an STL right now, but can change to an OBJ, is there a major difference regarding textures? – Politic Revolutionnaire May 23 '17 at 20:16
  • Once you have a TriangleMesh, you can always get one of its faces by picking with your mouse, so changing textures is a matter of switching indices, as I showed you on my sample. The problem could be finding out the right values of the new ones. – José Pereda May 23 '17 at 20:19