Importing 3D models to javaFX Group

I like javaFX because of:

Clean code, easy to understand and learn.

Requires No major game loop to update states:
-So I can use AnimationTimer as much as I want to update primitives/if conditions/node coordinates.
-TranslateTransition is my favorite feature, as it allows to create continous (endless) animations:
so I can create several TranslateTransition, it do not requires Threading, as all animation runs instantly, unless I add delay.

Everything is fine with 2D. However I want to step to 3D:

-I dont need light effects.
-I dont need rendering stuff or any other stuff for graphics.

I just want to be able to add ( import? ) 3D objects ( stl, obj…) and manipulate them ( scale, transition ( change coordinates ), collision detection ).

Mostly what Im looking for :
way to use 3D model as a node, and add it to the Group.
(Group in JavaFX is like a sack who collect 1 or more elements, once it is added to the scene, elements become visible)

I saw http://www.interactivemesh.org/models/jfx3dimporter.html
But it lacks of real examples and Im not sure if its what Im looking.

Perhaps this stuff with 3D is possible with openGL,
But I dislike openGL, because it is very unlean code and I cannot figure out what is going on.

( While my priority is collision detection, transitions, TranslateTransition, AnimationTimer. )

Maybe there is another library for C++ who would have this?

On first pass, it looks to me like the library you found is quite cool and should work as you are hoping it will work. Have you tried downloading it and running any of the models? I’d be very surprised if the imported model did not have it’s own root node that is attached to the JavaFX node-based structure.

I’d like to give it a try, also. I’m very curious about this aspect, and want to know more about it. I’m also wanting to learn about importing 3D Models in the JavaScript world–using a VR/AR framework called AFrame. I suspect there will be some overlap as far as the tech goes. Maybe between us we can write a quick-easy tutorial for the next person who want to give it a try.

I tried to import this simple model as stl file.

However, once I tried to visualize, I saw in scene with a 3D cyan rectangle. It wasnt my model loaded for some reason.
I have Tried to use this code:

I asked this quoestion on several places. I have came in conslusion, that it is more common to use 3D models with game engine.
In Java case, JMonkey somehow also allows to import models. Yet have not figured out how.
Just got this link:
https://jmonkeyengine.github.io/wiki/jme3.html#tutorials-for-beginners

Since javaScript is for browser, Im not sure, but it may have problems with cashe if you attempt 3D.

I’m not able to recreate the situation from your explanation. How does one load an STL file from your link? The only download button I see outputs a PNG. It will help me if you spell out your steps more completely.

My first test was the following:

I downloaded the “JavaFX 3D Model Browser” from this page.

I then downloaded the Hubble Space Telescope from here.

jimModelBrowserJFX-0_4_1.jar ran without complaint. I was able to load the Hubble data file with no problem and am tooling around it on another window right now. The Hubble file is named hst.3ds.

I’m going to play around with this a little more, then see if I can find an STL format file to view.


I was able to download and load a .stl file in the JavaFX model browser, this one from the Hubble.

I was also able to download and view, in the browser, the “Cylinder Head” on this page.

In all cases where the data was not fxml to begin with, there was an option to export the data to an fxml file. I was able to view the export-created fxml files without a problem.


It looks like this page has models in several different formats to try out.


Main issue, then, is perhaps how to load these models in a java program? Working on that now.

OK, the following basically worked, as far as pulling the graphic into a Java program:


		FXMLLoader fxmlLoader = new FXMLLoader();
		fxmlLoader.setLocation(this.getClass().getResource("hst.fxml"));
		Group graphic = fxmlLoader.<Group>load();	  
        
		root.getChildren().add(graphic);

A couple things:

I got the hst.fxml file by exporting the hst.3ds file mentioned earlier from the ModelBrowser.

The import in this case was to a Group object. Not all loads do this. Another example I tried loading had to be loaded into a TriangleMesh. Then, the TriangleMesh had to be loaded into a MeshView. Another example loaded to a MeshView. In each of these cases, the code above has to be modified to account for the object type being specified in the .fxml file.

My controls for lighting and keying the camera around are rather clumsy and I don’t have a good example yet. If I get a nice clean example, I’ll post it. But you are going to have to be on top of things like positioning the camera and the object to where the object can be seen, as well as handling light.

I hope this helps somewhat.

As I remember, I just downloaded random jars from interactiveMesh. Im not even used any fxml file.

If you could provide full java code( that you used to load model) and fxml code it would be great to see.

I cant understand that documentation at all.

BTW, do model was loaded with (material/texture)?

Step 1: convert model to fxml.

You can do this using the app that is downloadable from here. I had no trouble getting this app to run on my Windows system–it was just a matter of extracting all and clicking the .jar file enclosed.

The export control should be easy to find. I loaded and then exported this 3D model of the Hubble.

Step 2: put the exported .fxml and texture files (in this case, .bmp) into your Java project.

I used a simple, flat directory structure:
graphics_example
FXML_Example.java
hbltel_1.bmp
hbltel_2.bmp
hbltel_3.bmp
hbltel_4.bmp
hbltel_w.bmp
hst.fxml

Note: the texture files come packed within the download zip file from NASA. When the .fxml file was generated, url references to these textures were embedded. I made no edits to the .fxml file. It is very large.

Step 3. Following is a simple class that should load and display the model. (Note, I didn’t implement any camera movement, so the viewpoint is fixed.)


package graphics_example;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.AmbientLight;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class FXML_Example extends Application 
{ 
	public static void main(String[] args)
	{
		launch(args);
	}
	
	PerspectiveCamera camera;

	@Override
	public void start(Stage primaryStage) throws Exception 
	{
		Group root = new Group();
		Scene scene = new Scene(root, 1000, 600, true, SceneAntialiasing.BALANCED);	
		
		camera = new PerspectiveCamera(true);
		camera.setTranslateZ(-700);
		camera.setNearClip(0.1);
		camera.setFarClip(3000.0);
		camera.setFieldOfView(60);
		scene.setCamera(camera);
		
		// ADDING A 3D Model via FXML
		FXMLLoader fxmlLoader = new FXMLLoader();
		fxmlLoader.setLocation(this.getClass().getResource("hst.fxml"));
		Group graphic = fxmlLoader.<Group>load();	         
		root.getChildren().add(graphic);		
	
		PointLight light = new PointLight(Color.WHITE);
		light.setTranslateX(0);
		light.setTranslateY(-3000);
		light.setTranslateZ(-1600);
		root.getChildren().add(light);
		
		AmbientLight ambiance = new AmbientLight(Color.WHITE);
		root.getChildren().add(ambiance);
		
		scene.setFill(Color.SKYBLUE);
		
		primaryStage.setScene(scene);;
		primaryStage.setTitle("3D Model from FXML");
		primaryStage.show();
	}	
}

@elect alerted me to this GitHub project, called Java Open Asset Import Library which may be a good alternative. Check with @elect for more info on that!

I just took a look at the stackoverflow link you gave.

That answer loads directly from an STL file. No need to do the conversion, as I outlined.

It has a complication in that it loads to a Mesh object which is not a Node. To get the data into a JavaFX node, the Mesh is loaded into a MeshView, which is a subclass of Node.

It might be a bit tricky to set up the data in the Java project correctly, but this solution should work fine, and should display the textures, too, if you include them in the project.