Hello. New to three.js and all that. I have become the maintainer of 20 year old Java code which includes 3D drawing originally done in Java3D which I am now converting to three.js. A typical drawing comprises ~30,000+ triangles, all defined in Java. I need to send these triangle vertices and normals over to a three.js application, which I assume means writing in Java and reading in JavaScript. Plain ASCII is doable but onerous and time consuming. Is there any way to write gltf format in Java? Better yet, binary gltf. Any examples? Thank you.
I haven’t used these myself, but JglTF is probably the place to start: GitHub - javagl/JglTF: Java libraries related to glTF.
If it’s just triangle vertices and normals, and not materials or a scene graph or much else, it’s probably also easy to put wrote into two buffers or JSON arrays and then load them both with the JavaScript fetch()
API, constructing a THREE.BufferGeometry from that.
To write a glTF file in Java, you can use a glTF library such as gltf-model or jgltf.
Here’s an example using the gltf-model library to write a simple glTF file:
- Add the gltf-model dependency to your project. You can do this with Maven by adding the following to your pom.xml file:
xml
Copy code
com.github.javaports
gltf-model
1.0.0
- Create a GltfModel object and add a scene to it:
java
Copy code
import com.igormaznitsa.jgltf.GltfModel;
import com.igormaznitsa.jgltf.Scenes;
GltfModel model = new GltfModel();
Scenes scenes = model.getScenes();
scenes.addScene(scenes.newScene().setName(“Scene 0”));