Skip to content

Commit

Permalink
support for draco v5
Browse files Browse the repository at this point in the history
  • Loading branch information
pfcDorn committed Jan 18, 2024
1 parent 20b1c5f commit 1b840bf
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Runtime/Scripts/SceneImporter/ImporterMeshes.cs
Expand Up @@ -15,6 +15,9 @@
using Draco;
using UnityGLTF.Plugins;
#endif
#if !HAVE_DRACO_VERSION_5
using DecodeResult = DracoMeshLoader.DecodeResult;
#endif

namespace UnityGLTF
{
Expand Down Expand Up @@ -136,7 +139,7 @@ protected virtual async Task ConstructDracoMesh(GLTFMesh mesh, int meshIndex, Ca
{
var firstPrim = mesh.Primitives.Count > 0 ? mesh.Primitives[0] : null;
Mesh.MeshDataArray meshDataArray = Mesh.AllocateWritableMeshData(mesh.Primitives.Count);
var dracoDecodeResults = new DracoMeshLoader.DecodeResult[mesh.Primitives.Count];
var dracoDecodeResults = new DecodeResult[mesh.Primitives.Count];
for (int i = 0; i < mesh.Primitives.Count; i++)
{
var primitive = mesh.Primitives[i];
Expand Down Expand Up @@ -172,13 +175,25 @@ protected virtual async Task ConstructDracoMesh(GLTFMesh mesh, int meshIndex, Ca
bool needsTangents = _options.ImportTangents != GLTFImporterNormals.None && hasTangents;
bool needsNormals = _options.ImportNormals != GLTFImporterNormals.None || needsTangents;
#pragma warning restore 0219

#if HAVE_DRACO_VERSION_5
DecodeSettings decodeSettings = DecodeSettings.ConvertSpace;
if (needsNormals)
decodeSettings |= DecodeSettings.RequireNormals;
if (needsTangents)
decodeSettings |= DecodeSettings.RequireTangents;
if (firstPrim != null && firstPrim.Targets != null)
decodeSettings |= DecodeSettings.ForceUnityVertexLayout;

dracoDecodeResults[i] = await DracoDecoder.DecodeMesh(meshDataArray[i], bufferViewData, decodeSettings, DracoDecoder.CreateAttributeIdMap(weightsAttributeId, jointsAttributeId));

#else
var draco = new DracoMeshLoader();

dracoDecodeResults[i] = await draco.ConvertDracoMeshToUnity(meshDataArray[i], bufferViewData,
needsNormals, needsTangents,
weightsAttributeId, jointsAttributeId, firstPrim.Targets != null);

#endif
if (!dracoDecodeResults[i].success)
{
Debug.LogError("Error decoding draco mesh", this);
Expand Down Expand Up @@ -303,14 +318,15 @@ protected void ApplyImportOptionsOnMesh(Mesh mesh)
if(uv.Length > 0)
mesh.uv2 = uv;
}

}

#if HAVE_DRACO
/// <summary>
/// Populate a UnityEngine.Mesh from Draco generated SubMeshes
/// </summary>
/// <returns></returns>
protected async Task ConstructUnityMesh(GLTFMesh gltfMesh, DracoMeshLoader.DecodeResult[] decodeResults, Mesh.MeshDataArray meshes, int meshIndex, string meshName)
protected async Task ConstructUnityMesh(GLTFMesh gltfMesh, DecodeResult[] decodeResults, Mesh.MeshDataArray meshes, int meshIndex, string meshName)
{
uint verticesLength = 0;
for (int i = 0; i < meshes.Length; i++)
Expand Down

0 comments on commit 1b840bf

Please sign in to comment.