Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Runtime/Scripts/BoneWeightData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public void ApplyOnMesh(Mesh mesh)
mesh.SetBoneWeights(m_BonesPerVertex, m_BoneWeights);
}

/// <summary>
/// Assigns the weight and index buffers as outputs to the GetDracoBonesJob job
/// </summary>
/// <param name="job"></param>
internal void AssignToJob(ref DracoNative.GetDracoBonesJob job)
{
job.bonesPerVertex = m_BonesPerVertex;
job.boneWeights = m_BoneWeights;
}

/// <summary>
/// Releases allocated resources.
/// </summary>
Expand Down
104 changes: 91 additions & 13 deletions Runtime/Scripts/DracoDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,86 @@ DecodeSettings decodeSettings
return await DecodeMesh(encodedData, decodeSettings, null);
}

public static async Task<Mesh> DecodeMesh(
NativeSlice<byte>[] encodedDataArray,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to use plain NativeArray<byte>.ReadOnly here and in all API calls going forward.

Using NativeSlice<byte> instead of just NativeArray<byte> is a mistake I made not really knowing what NativeSlice's purpose is and that NativeArray.GetSubArray solves the actual problem much better.

This is not something you should care about, but I'd prefer to fix this first before adding more public methods that have odd type choices.

DecodeSettings decodeSettings,
Dictionary<VertexAttribute, int>[] attributeIdMaps,
int[] vertexIntervals,
int[] indicesIntervals,
Bounds?[] bounds
)
{
CertifySupportedPlatform(
#if UNITY_EDITOR
false
#endif
);

var meshDataArray = Mesh.AllocateWritableMeshData(1);
var mesh = meshDataArray[0];
Bounds combinedBounds = default;
bool calculateNormals = false;
BoneWeightData boneWeightData = null;

for (var i = 0; i < encodedDataArray.Length; i++)
{
var encodedData = encodedDataArray[i];
var attributeIdMap = attributeIdMaps[i];
var encodedDataPtr = GetUnsafeReadOnlyIntPtr(encodedData);

var result = await DecodeMesh(
mesh,
encodedDataPtr,
encodedData.Length,
decodeSettings,
attributeIdMap,
i,
vertexIntervals,
indicesIntervals,
bounds,
boneWeightData
);
if (!result.success)
{
meshDataArray.Dispose();
return null;
}

boneWeightData = result.boneWeightData;

if (i == 0)
combinedBounds = result.bounds;
else
combinedBounds.Encapsulate(result.bounds);

calculateNormals |= result.calculateNormals;
}

var unityMesh = new Mesh();
Mesh.ApplyAndDisposeWritableMeshData(meshDataArray, unityMesh);
unityMesh.bounds = combinedBounds;
if (boneWeightData != null)
{
boneWeightData.ApplyOnMesh(unityMesh);
boneWeightData.Dispose();
}

if (unityMesh.GetTopology(0) == MeshTopology.Triangles)
{
if (calculateNormals)
{
unityMesh.RecalculateNormals();
}

if ((decodeSettings & DecodeSettings.RequireTangents) != 0)
{
unityMesh.RecalculateTangents();
}
}

return unityMesh;
}

/// <inheritdoc cref="DecodeMesh(NativeSlice{byte},DecodeSettings)"/>
/// <param name="attributeIdMap">Attribute type to index map</param>
public static async Task<Mesh> DecodeMesh(
Expand Down Expand Up @@ -291,7 +371,7 @@ Dictionary<VertexAttribute, int> attributeIdMap
decodeSettings,
attributeIdMap
#if UNITY_EDITOR
,sync
, sync: sync
#endif
);
UnsafeUtility.ReleaseGCObject(gcHandle);
Expand Down Expand Up @@ -320,13 +400,18 @@ static async Task<DecodeResult> DecodeMesh(
IntPtr encodedData,
int size,
DecodeSettings decodeSettings,
Dictionary<VertexAttribute, int> attributeIdMap
Dictionary<VertexAttribute, int> attributeIdMap,
int submeshIndex = 0,
int[] vertexIntervals = null,
int[] indicesIntervals = null,
Bounds?[] bounds = null,
BoneWeightData boneWeightData = null
#if UNITY_EDITOR
,bool sync = false
#endif
)
{
var dracoNative = new DracoNative(meshData, decodeSettings);
var dracoNative = new DracoNative(meshData, decodeSettings, submeshIndex, vertexIntervals, indicesIntervals, bounds?[submeshIndex], boneWeightData);

#if UNITY_EDITOR
if (sync) {
Expand Down Expand Up @@ -363,19 +448,12 @@ Dictionary<VertexAttribute, int> attributeIdMap
return new DecodeResult();
}

var bounds = dracoNative.CreateBounds();
var success = dracoNative.PopulateMeshData(bounds);
BoneWeightData boneWeightData = null;
if (success && dracoNative.hasBoneWeightData)
{
boneWeightData = new BoneWeightData(dracoNative.bonesPerVertex, dracoNative.boneWeights);
dracoNative.DisposeBoneWeightData();
}
var success = dracoNative.PopulateMeshData(out var subMeshBounds);
return new DecodeResult(
success,
bounds,
subMeshBounds,
calculateNormals,
boneWeightData
dracoNative.boneWeightData
);
}

Expand Down
160 changes: 0 additions & 160 deletions Runtime/Scripts/DracoMeshLoader.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Runtime/Scripts/DracoMeshLoader.cs.meta

This file was deleted.

Loading