Files
AR-Menu/Library/PackageCache/com.unity.cloud.draco@9c0decae37ec/Documentation~/use-case-encoding.md
2025-11-30 08:35:03 +02:00

4.1 KiB

Use case: Encoding

Draco for Unity let's you encode Draco™ data at run-time.

Before you start

Make sure you have Draco for Unity installed and referenced the Draco.Encode assembly.

Encode

Encoding can be achieved by calling one of the EncodeMesh overloads. Since sub-meshes are not supported they'll get split into separate Draco meshes. The result is an array of EncodeResults, one for each sub-mesh.

Here's an example that encodes a Mesh and saves the results into files.

[!code-csencode-draco-runtime]

Optimize and tweak

EncodeMesh's optional quantization and speed parameters offers many ways to customize the encoding process.

Speed parameters

The settings encodingSpeed and decodingSpeed allow you to tweak the tradeoff between resulting data size in bytes and encoding/decoding speed by turning on/off different compression features. In general, the lowest setting, 0, will have the most compression but worst speed. 10 will have the least compression, but best speed.

Quantization settings

Quantization lets you reduce the resulting byte size by limiting the value precision to a certain number of bits. A value of 0 for the quantization parameter will not perform any quantization on the specified attribute. Any value other than 0 will quantize the input values for the specified attribute to that number of bits.

In general, the more you quantize your attributes the better compression rate you will get. It is up to your project to decide how much deviation it will tolerate.

Quantization is specified per attribute type:

Position Quantization

Alternatively the ideal position quantization can be calculated from the mesh's size, its size/scale in the world and the desired precision. Have a look at QuantizationSettings.FromWorldSize for details.

Encode using the advanced Mesh API

If you want to encode multiple Meshes you'll get the best performance by using the advanced Mesh API EncodeMesh overloads that take a Mesh/MeshData pair as first parameters.

The steps are:

  1. Call AcquireReadOnlyMeshData with a collection of meshes you want to encode.
  2. Iterate over the meshes and invoke EncodeMesh on each of them.
  3. Await each encoding job, handle the results (e.g. store them to a file or upload them to a server) and dispose the EncodeResult.
  4. Dispose the MeshDataArray that AcquireReadOnlyMeshData returned.

Encode Sample

A fully setup sample scene can be found in the Encode Sample.

Trademarks

Unity is a registered trademark of Unity Technologies.

Draco™ is a trademark of Google LLC.