Extract Assets from VRML via Java
Extract Assets from VRML using Java library without any 3D modeling software.
How to Extract Assets from VRML File Using Java
In order to extract assets from VRML 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 VRML via Java
Java programmers can easily extract assets from VRML in just a few lines of code.
- Load VRML 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 VRML
//Source files that need to extract assets
String file = "template.vrml";
//create an instance of Scene
Scene scene = new Scene();
scene.open(file, FileFormat.VRML);
//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 VRML
Extractor VRML 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.VRML What is VRML File Format?
The virtual reality modeling language (VRML) is a file format for representation of interactive 3D world objects over the world wide web (www). It finds its usage in creating three-dimensional representations of complex scenes such as illustrations, definition and virtual reality presentations. The format has been superseded by X3D. Many 3D modelling applications can save objects and scenes in VRML format.
Read MoreOther Supported App to Extract Assets from Formats
Using JAVA, One can also extract assets from many other file formats including.