Unity package that integrates the Draco 3D data compression library within Unity.
Following build targets are supported
Note: Burst support is broken on iOS builds at the moment. Please deactivate Burst AOT in the project settings until this is resolved.
The easiest way to install is to download and open the Installer Package
It runs a script that installs the Draco 3D Data Compression Unity Package via a scoped registry. After that it is listed in the Package Manager and can be updated from there.
Minimalistic way of loading a draco file (source):
public class DracoDemo : MonoBehaviour {
public string filePath;
async void Start() {
// Load file into memory
var fullPath = Path.Combine(Application.streamingAssetsPath, filePath);
var data = File.ReadAllBytes(fullPath);
// Convert data to Unity mesh
var draco = new DracoMeshLoader();
// Async decoding has to start on the main thread and spawns multiple C# jobs.
var mesh = await draco.ConvertDracoMeshToUnity(data);
if (mesh != null) {
// Use the resulting mesh
GetComponent<MeshFilter>().mesh= mesh;
}
}
}
Starting with Unity 2020.2 you can create Meshes efficiently via MeshDataArray
.
The important difference is that instead of returning a Mesh
directly, it just configures the MeshData
properly and fills its buffers. It's up to the user to:
Mesh
instance(s)Mesh.ApplyAndDisposeWritableMeshData
Here's an examply how to do this (source):
public class DracoDemoMeshData : MonoBehaviour {
public string filePath;
public bool requireNormals;
public bool requireTangents;
async void Start() {
// Load file into memory
var fullPath = Path.Combine(Application.streamingAssetsPath, filePath);
var data = File.ReadAllBytes(fullPath);
// Convert data to Unity mesh
var draco = new DracoMeshLoader();
// Allocate single mesh data (you can/should bulk allocate multiple at once, if you're loading multiple draco meshes)
var meshDataArray = Mesh.AllocateWritableMeshData(1);
// Async decoding has to start on the main thread and spawns multiple C# jobs.
var result = await draco.ConvertDracoMeshToUnity(
meshDataArray[0],
data,
requireNormals, // Set to true if you require normals. If Draco data does not contain them, they are allocated and we have to calculate them below
requireTangents // Retrieve tangents is not supported, but this will ensure they are allocated and can be calculated later (see below)
);
if (result.success) {
// Apply onto new Mesh
var mesh = new Mesh();
Mesh.ApplyAndDisposeWritableMeshData(meshDataArray,mesh);
// If Draco mesh has bone weigths, apply them now.
// To get these, you have to supply the correct attribute IDs
// to `ConvertDracoMeshToUnity` above (optional paramters).
if (result.boneWeightData != null) {
result.boneWeightData.ApplyOnMesh(mesh);
result.boneWeightData.Dispose();
}
if (result.calculateNormals) {
// If draco didn't contain normals, calculate them.
mesh.RecalculateNormals();
}
if (requireTangents) {
// If required (e.g. for consistent specular shading), calculate tangents
mesh.RecalculateTangents();
}
// Use the resulting mesh
GetComponent<MeshFilter>().mesh = mesh;
}
}
}
See the signatures of all DracoMeshLoader.ConvertDracoMeshToUnity
variants to see all options available.
The examples above and more can be found in the DracoUnityDemo project.
The binary libraries used in this package are not code-signed. macOS in particular will not let you load the ktx_unity.bundle
for that reason (see issue).
Here's the steps to make it work on macOS
If you want to deploy your software using DracoUnity you either have to
This project is a fork of the existing Unity integration
Like this demo? You can show your appreciation and ...
To develop this package, check out the repository and add it as local repository in the Unity Package Manager.
The native libraries are built via CI in this GitHub action
Look into the YAML file to see how the project is built with CMake.
Copyright (c) 2019 Andreas Atteneder, All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use files in this repository except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Builds upon and includes builds of Google's Draco 3D data compression library (released under the terms of Apache License 2.0).