Extract Assets from GLTF via Java
Extract Assets from GLTF using Java library without any 3D modeling software.
How to Extract Assets from GLTF File Using Java
In order to extract assets from GLTF file, we’ll use
API which is a feature-rich, powerful and easy to use API for Java platform. You can download its latest version directly from
and install it within your Maven-based project by adding the following configurations to the pom.xml.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-3d</artifactId>
<version>version of aspose-3d API</version>
<classifier>jdk17</classifier>
</dependency>
Steps to Extract Assets from GLTF via Java
Java programmers can easily extract assets from GLTF in just a few lines of code.
- Load GLTF file via the constructor of Scene class
- Create zip file format object as output file format
- Create archive class and handle extract asset class
- Call the Extract method and save the file
System Requirements
Aspose.3D for JAVA is supported on all major operating systems. Just make sure that you have the following prerequisites.
- Microsoft Windows or a compatible OS with Java Runtime Environment for JSP/JSF Application and Desktop Applications.
- Get latest version of Aspose.3D for Java directly from Maven.
JAVA code to Extract Assets from GLTF
//Source files that need to extract assets
String file = "template.gltf";
//create an instance of Scene
Scene scene = new Scene(file);
//Create zip files and perform asset extraction on source files
String zipOutput ="OutputFile.zip";
File newFile=new File(zipOutput);
newFile.createNewFile();
FileOutputStream output = new FileOutputStream(zipOutput);
Zip za = new Zip(output);
Extract(scene,za,true);
//close stream resource
za.closeIo();
//Callable Extract method,The parameter texture indicates: whether to extract the texture
private void Extract(Scene scene, Zip za, boolean texture) throws IOException {
Extractor extractor = new Extractor(za, texture);
extractor.Extract(scene);
}
//Create a compressed file processing class
class Zip
{
private ZipOutputStream archive;
private HashSet<String> entries = new HashSet<String>();
public Zip(OutputStream stream)
{
archive = new ZipOutputStream(stream);
}
public void Add(String fileName, byte[] content, boolean enableCompression) throws IOException {
String entryName = PickName(fileName);
if(enableCompression){
archive.setLevel(5);
}else{
archive.setLevel(0);
}
ZipEntry entry = new ZipEntry(entryName);
archive.putNextEntry(entry);
archive.write(content, 0, content.length);
}
public void closeIo(){
if(archive!=null){
try {
archive.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String PickName(String fileName)
{
if (!entries.contains(fileName))
{
entries.add(fileName);
return fileName;
}
String arrFile[]=fileName.split(".");
String baseName = arrFile[0];
String ext = arrFile[1];
for (int idx = 2; ; idx++)
{
String newName = baseName + "_" + idx;
if (!StringUtils.isBlank(ext))
newName += ext;
if (entries.contains(newName))
continue;
entries.add(newName);
return newName;
}
}
}
//Create an asset extraction processing class
class Extractor
{
private Zip zip;
private boolean texture;
HashSet<A3DObject> visited = new HashSet<A3DObject>();
public Extractor(Zip zip,boolean texture)
{
this.zip = zip;
this.texture = texture;
}
private boolean CanVisit(A3DObject obj)
{
if (visited.contains(obj))
return false;
visited.add(obj);
return true;
}
public void Extract(Scene scene) throws IOException {
if (scene.getLibrary() != null && scene.getLibrary().size() > 0)
{
for (A3DObject obj:scene.getLibrary())
{
Visit(obj);
}
}
VisitNode(scene.getRootNode());
}
private void VisitNode(Node node) throws IOException {
if (!CanVisit(node))
return;
if (texture)
{
for (Material mat:node.getMaterials())
{
VisitMaterial(mat);
}
}
for (Entity entity:node.getEntities())
{
if (entity instanceof Mesh){
Save((Mesh)entity, node.getName());
}
}
for (Node child:node.getChildNodes())
{
VisitNode(child);
}
}
private void VisitMaterial(Material mat) throws IOException {
if (!CanVisit(mat))
return;
if (!texture)
return;
for (TextureSlot tslot: mat)
{
if (tslot.getTexture() instanceof Texture)
{
Save((Texture)tslot.getTexture());
}
}
}
private void Visit(A3DObject obj) throws IOException {
if (texture && obj instanceof Texture)
{
Save((Texture)obj);
}
else if (obj instanceof Mesh)
{
Save((Mesh)obj, null);
}
else if (obj instanceof Node)
{
VisitNode((Node)obj);
}
}
private void Save(Mesh mesh, String nodeName) throws IOException {
if (!CanVisit(mesh))
return;
Scene scene = new Scene(mesh);
MemoryStream ms=new MemoryStream();
scene.save(ms, FileFormat.FBX7400ASCII);
String name = nodeName;
if (StringUtils.isBlank(name))
name = mesh.getName();
if (StringUtils.isBlank(name))
name = "mesh";
String ext = ".fbx";
zip.Add(name + ext, ms.toArray(), true);
}
private void Save(Texture tex) throws IOException {
if (tex.getContent() == null || !CanVisit(tex))
return;
String fileName=tex.getFileName();
if(tex.getFileName()!=null){
String arrFile[]=fileName.split(".");
fileName = arrFile[0];
}
zip.Add(fileName, tex.getContent(), false);
}
}
Free App to Extract Assets from GLTF
Extractor GLTF right now by visiting our Live Demos website.The live demo has the following benefits
Java 3D Scene Manipulation Library
Aspose.3D is a CAD and Gameware API to load, modify and convert 3D files. API is a standalone and does not require any any 3D modeling or rendering software. One can easily use API for Discreet3DS, WavefrontOBJ, STL (ASCII, Binary), Universal3D, FBX (ASCII, Binary), Collada, glTF, PLY, GLB, DirectX and more formats.GLTF What is GLTF File Format?
glTF (GL transmission format) is a 3D file format that stores 3D model information in JSON format. The use of JSON minimizes both the size of 3D assets and the runtime processing needed to unpack and use those assets. It was adopted for the efficient transmission and loading of 3D scenes and models by applications.
Read MoreOther Supported App to Extract Assets from Formats
Using JAVA, One can also extract assets from many other file formats including.