1. Home
  2.   Drako
  3.   FileFormat.Drako for Java
FileFormat.Drako for Java

FileFormat.Drako for Java

 
 

Simplify Process of Reading and Writing Google Draco files Using Java API

Effortlessly edit compressed DRC files withFileFormat.Drako, a lightweight open-source Java API, streamlining Draco files convertion and automation

FileFormat.Drako for Java revolutionizes your Draco file processing experience. In this comprehensive guide, we dive into the functionalities and benefits of leveragingFileFormat.Drako, a lightweight open-source Java API, to effortlessly handle Draco files with ease.

Draco is an open-source compression library developed by Google that specializes in compressing and decompressing 3D geometric meshes and point clouds. By significantly reducing the size of 3D graphics files, Draco enables faster transmission and reduces storage requirements while maintaining visual quality. It supports various 3D applications, including web-based and mobile platforms, ensuring efficient handling of complex 3D data. Draco is widely used in industries such as gaming, virtual reality, and 3D visualization to optimize performance and enhance user experience.

FileFormat.Drako emerges as a powerful solution for seamlessly reading and writing Draco files. This lightweight open-source Java API simplifies Draco image operations, offering developers and users a user-friendly interface to handle Draco files effortlessly.

FileFormat.Drako is available under Openize License.

Key Features and Benefits

  • High Compression Ratios: Draco achieves significant reduction in file sizes, often reducing them by up to 90% without noticeable loss in visual quality. This makes it ideal for applications where bandwidth and storage are limited.
  • Support for Various Data Types: Draco supports compression for a wide range of 3D data types, including geometry meshes and point clouds. It can handle vertex positions, colors, normals, and other attributes efficiently.
  • Fast Compression and Decompression: The library is designed to offer high-speed compression and decompression, making it suitable for real-time applications such as web-based 3D viewers and virtual reality experiences.
  • Open Source and Free: Enjoy the benefits of an open-source solution with no licensing fees, making it accessible to developers and users of all levels.
  • Seamless Integration with Java Applications: IntegrateFileFormat.Drako effortlessly into your Java applications, enabling seamless Draco file handling within your existing workflows.

Previous Next

How to Get Started withFileFormat.Drako for Java

Getting started withFileFormat.Drako is quick and easy. Simply follow these steps:

  • InstallFileFormat.Drako: Install theFileFormat.Drako package via Maven or Gradle.
  • Start Converting: Utilize the simple API provided byFileFormat.Drako to read and write Draco files.

The recommended way to installFileFormat.Drako for Java is using maven. Please use the following pom dependency snippet for a smooth installation.

POM Snippet


<dependency>
  <groupId>org.openize</groupId>
  <artifactId>drako</artifactId>
  <version>1.4.0</version>
</dependency>
 
You can also download it directly from GitHub.

Read Draco File into DracoMesh in Java

This code snippet efficiently loads a Draco file, and store it in a DracoMesh instance for subsequent operations.

  • Open the DRC file named "cube.drc" using Files.readAllBytes.
  • Load the 3D mesh from the bytes into a DracoMesh object.

Copy and paste the code snippet below into your main file and execute the program.

Read .drc file to DracoMesh instance

 
    byte[] cube = Files.readAllBytes(Paths.get("cube.drc"));
    DracoMesh dm = (DracoMesh)Draco.decode(cube);

Read Draco file and write to Wavefront OBJ in Java

The following code illustrates how to open an existing 3D Draco file, and write to obj format for further processing:

  • Open the Draco file file named "input.drc" using Files.readAllBytes.
  • Load the Draco mesh from the bytes into a DracoMesh object.
  • Find the position attribute using DracoMesh.getNamedAttribute and turn its content to FloatSpan.
  • Write control points to output using OBJ's format.
  • Read each face corners using DracoMesh.readFace
  • Map the face corners to the index of control points using PointAttribute.mappedIndex.
  • Write faces to output using OBJ's format.

Copy and paste the code snippet below into your main file and execute the program.

Read .drc file to DracoMesh and export to wavefront obj

 
  //load draco file
  byte[] bytes = Files.readAllBytes(Paths.get("input.drc"));
  DracoMesh mesh = (DracoMesh)Draco.decode(bytes);
  if (mesh == null)
      throw new IllegalStateException("Input file is not a valid draco file.");
  PointAttribute attrPos = mesh.getNamedAttribute(AttributeType.POSITION);
  FloatSpan points = attrPos.getBuffer().asSpan().asFloatSpan();
  
  try(FileOutputStream fos = new FileOutputStream("output.obj", false)) {
      try (OutputStreamWriter writer = new OutputStreamWriter(fos)) {
          for (int i = 0; i < points.size(); i += 3) {
              writer.write(String.format("v %f %f %f\n", points.get(i), points.get(i + 1), points.get(i + 2)));
          }
          int[] face = new int[3];
          for (int i = 0; i < mesh.getNumFaces(); i++) {
              mesh.readFace(i, face);
              int a = attrPos.mappedIndex(face[0]) + 1;
              int b = attrPos.mappedIndex(face[1]) + 1;
              int c = attrPos.mappedIndex(face[2]) + 1;
              writer.write(String.format("f %d %d %d\n", a, b, c));
          }
      }
  }

Encode control points, triangles to Draco file Programatically in Java

The provided code demonstrates how to programmatically create a Draco file from control points and triangles, and save it as "output.drc" using Java. Here's a breakdown of the steps:

  • Create a DracoMesh instance.
  • Wrap control points to PointAttribute.
  • Add the PointAttribute to DracoMesh.
  • Add the triangle indices.
  • Create a DracoEncodeOptions object with the specified encoding parameters.
  • Encode the mesh to bytes using Draco.encode.

Copy and paste the code snippet below into your main file and execute the program.

Encode vectors and faces to .drc file

 
  Vector3[] controlPoints = new Vector3[]
  {
          new Vector3( -5, 0, 5.0f),
          new Vector3( 5, 0, 5.0f),
          new Vector3( 5, 10, 5.0f),
          new Vector3( -5, 10, 5.0f),
          new Vector3( -5, 0, -5.0f),
          new Vector3( 5, 0, -5.0f),
          new Vector3( 5, 10, -5.0f),
          new Vector3( -5, 10, -5.0f)
  };

  int[] indices = new int[]
  {
          0,1,2, 0, 2, 3, // Front face (Z+)
          1,5,6, 1, 6, 2, // Right side (X+)
          5,4,7, 5, 7, 6, // Back face (Z-)
          4,0,3, 4, 3, 7, // Left side (X-)
          0,4,5, 0, 5, 1, // Bottom face (Y-)
          3,2,6, 3, 6, 7 // Top face (Y+)
  };

  DracoMesh mesh = new DracoMesh();
  PointAttribute attrPos = PointAttribute.wrap(AttributeType.POSITION, controlPoints);
  mesh.addAttribute(attrPos);
  //add triangle indices
  mesh.getIndices().addRange(indices);
  //number of the control points, it's required for the encoder to produce correct result.
  mesh.setNumPoints(8);
  //You can also use following methods to deduplicate the attributes to reduce the file size
  //mesh.deduplicateAttributeValues();
  //mesh.deduplicatePointIds();
  DracoEncodeOptions opt = new DracoEncodeOptions();
  byte[] drcBytes = Draco.encode(mesh, opt);

  Files.write(Paths.get("output.drc"), drcBytes);


FileFormat.Drako offers an intuitive API, comprehensive format support, and seamless integration with Java applications, empowering developers and users alike to effortlessly manage Draco files. Whether you're converting draco files to other files, or loading draco for rendering,FileFormat.Drako simplifies the process, ensuring optimal efficiency and quality at every step.

Explore our GitHub repository to contribute, suggest improvements, and enhance this Open Source API: FileFormat.Drako-for-Java