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.
var meshFilter = GetComponent<MeshFilter>();
Assert.IsNotNull(meshFilter, "Couldn't find MeshFilter component");
var mesh = meshFilter.sharedMesh; // Use sharedMesh, so no copy of the Mesh is created implicitly.
// Encode to Draco
var encodeResults = await DracoEncoder.EncodeMesh(mesh);
if (encodeResults == null)
{
Debug.LogError("Encoding Draco failed!");
return;
}
var meshName = mesh.name;
if (string.IsNullOrEmpty(meshName))
{
meshName = "Mesh";
}
for (var i = 0; i < encodeResults.Length; i++)
{
var encodeResult = encodeResults[i];
var destination = Path.Combine(Application.persistentDataPath, $"{meshName}-submesh-{i}.drc");
File.WriteAllBytes(destination, encodeResult.data.ToArray());
Debug.Log($"Saved submesh {i} to {destination}");
// It's required to dispose the results
encodeResult.Dispose();
}
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 (
positionQuantization
setting) - Normal (
normalQuantization
setting) - Texture Coordinate (
texCoordQuantization
setting) - Color (
colorQuantization
setting) - Generic (
genericQuantization
setting)
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:
- Call AcquireReadOnlyMeshData with a collection of meshes you want to encode.
- Iterate over the meshes and invoke EncodeMesh on each of them.
- Await each encoding job, handle the results (e.g. store them to a file or upload them to a server) and dispose the EncodeResult.
- 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.