1. Home
  2.   Drako
  3.   Openize.Drako for .NET
Openize.Drako for .NET

Openize.Drako for .NET

 
 

Simplify Process of Reading and Writing Google Draco files Using .NET API

Effortlessly edit compressed DRC files with Openize.Drako, a lightweight open-source .NET API, streamlining Draco files convertion and automation

Openize.Drako for .NET revolutionizes your Draco file processing experience. In this comprehensive guide, we dive into the functionalities and benefits of leveraging Openize.Drako, a lightweight open-source .NET 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.

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

Openize.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 C# Applications: Integrate Openize.Drako effortlessly into your C# applications, enabling seamless Draco file handling within your existing workflows.

Previous Next

How to Get Started with Openize.Drako for .NET

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

  • Install Openize.Drako: Install the Openize.Drako package via NuGet Package Manager or .NET CLI.
  • Integrate with Your Project: Reference the Openize.Drako library in your C# project.
  • Start Converting: Utilize the simple API provided by Openize.Drako to read and write Draco files.

The recommended way to install Openize.Drako for .NET is using NuGet. Please use the following command for a smooth installation.

Install Openize.Drako for .NET via NuGet

NuGet> Install-Package Openize.Drako 
You can also download it directly from GitHub.

Read Draco File into DracoMesh in C#

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 File.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 = File.ReadAllBytes("cube.drc");
    DracoMesh dm = (DracoMesh)Draco.Decode(cube);

Read Draco file and write to Wavefront OBJ in C#

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 File.ReadAllBytes.
  • Load the Draco mesh from the bytes into a DracoMesh object.
  • Find the position attribute using DracoMesh.GetNamedAttribute and turn its content to Span<float>.
  • 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
    var bytes = File.ReadAllBytes("input.drc");
    var mesh = Draco.Decode(bytes) as DracoMesh;
    if (mesh == null)
        throw new InvalidDataException("Input file is not a valid draco file.");
    var attrPos = mesh.GetNamedAttribute(AttributeType.Position);
    var points = MemoryMarshal.Cast(attrPos.Buffer.AsSpan());
    var sb = new StringBuilder();
    for (int i = 0; i < points.Length; i += 3)
    {
        sb.AppendLine($"v {points[i]} {points[i + 1]} {points[i + 2]}");
    }
    Span face = stackalloc int[3];
    for (int i = 0; i < mesh.NumFaces; i++)
    {
        mesh.ReadFace(i, face);
        var a = attrPos.MappedIndex(face[0]) + 1;
        var b = attrPos.MappedIndex(face[1]) + 1;
        var c = attrPos.MappedIndex(face[2]) + 1;
        sb.AppendLine($"f {a} {b} {c}");
    }
    File.WriteAllText("output.obj", sb.ToString());

Encode control points, triangles to Draco file Programatically in C#

The provided code demonstrates how to programmatically create a Draco file from control points and triangles, and save it as "output.drc" using C#. 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+)
  };

  var mesh = new DracoMesh();
  //construct an attribute for position, with type float[3], 
  var attrPos = PointAttribute.Wrap(AttributeType.Position, controlPoints);
  mesh.AddAttribute(attrPos);
  //add triangle indices
  mesh.Indices.AddRange(indices);
  //number of the control points, it's required for the encoder to produce correct result.
  mesh.NumPoints = 8;
  //You can also use following methods to deduplicate the attributes to reduce the file size
  //mesh.DeduplicateAttributeValues();
  //mesh.DeduplicatePointIds();

  var opt = new DracoEncodeOptions();
  var drcBytes = Draco.Encode(mesh, opt);
  File.WriteAllBytes("output.drc", drcBytes);

Openize.Drako offers an intuitive API, comprehensive format support, and seamless integration with C# 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, Openize.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: Openize.Drako-for-.NET