Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target-names from mesh extras properties validation #79

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static GLTFMesh Deserialize(GLTFRoot root, JsonReader reader)
foreach (MeshPrimitive mp in mesh.Primitives)
{
if (mp == null) continue;
mp.TargetNames = mesh.TargetNames;
if(mp.Targets == null || mp.Targets.Count != mesh.TargetNames.Count) throw new GLTFParseException("Invalid number of morphtarget names on mesh!");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static MeshPrimitive Deserialize(GLTFRoot root, JsonReader reader)
skipStartObjectRead: true);
});
break;
case "extras":
/*case "extras":
// GLTF does not support morph target names, serialize in extras for now
// https://github.com/KhronosGroup/glTF/issues/1036
if (reader.Read() && reader.TokenType == JsonToken.StartObject)
Expand All @@ -236,12 +236,39 @@ public static MeshPrimitive Deserialize(GLTFRoot root, JsonReader reader)
}
}

break;
break;*/
default:
primitive.DefaultPropertyDeserializer(root, reader);
break;
}
}

// GLTF does not support morph target names, serialize in extras for now
// https://github.com/KhronosGroup/glTF/issues/1036
if (primitive.Extras != null)
{
var extrasReader = primitive.Extras.CreateReader();
extrasReader.Read();

while (extrasReader.Read() && extrasReader.TokenType == JsonToken.PropertyName)
{
var extraProperty = extrasReader.Value.ToString();
switch (extraProperty)
{
case "targetNames":
primitive.TargetNames = extrasReader.ReadStringList();
break;
default:
extrasReader.Skip();
break;
}
}

extrasReader.Close();

if (primitive.TargetNames != null && primitive.TargetNames.Count > 0 && (primitive.Targets == null || primitive.Targets.Count != primitive.TargetNames.Count))
throw new GLTFParseException("Invalid number of morphtarget names on primitive!");
}

return primitive;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,29 @@ protected async Task ConstructUnityMesh(UnityMeshData unityMeshData, int meshInd

if (unityMeshData.MorphTargetVertices != null)
{
var firstPrim = _gltfRoot.Meshes[meshIndex].Primitives[0];
for (int i = 0; i < firstPrim.Targets.Count; i++)
{
var targetName = firstPrim.TargetNames != null ? firstPrim.TargetNames[i] : $"Morphtarget{i}";
mesh.AddBlendShapeFrame(targetName, 100,
unityMeshData.MorphTargetVertices[i],
unityMeshData.MorphTargetNormals != null ? unityMeshData.MorphTargetNormals[i] : null,
unityMeshData.MorphTargetTangents != null ? unityMeshData.MorphTargetTangents[i] : null
);
if(_gltfRoot.Meshes[meshIndex].TargetNames != null && _gltfRoot.Meshes[meshIndex].TargetNames.Count > 0)
{ // Get target names from mesh extras
for (int i = 0; i < _gltfRoot.Meshes[meshIndex].TargetNames.Count; i++)
{
var targetName = _gltfRoot.Meshes[meshIndex].TargetNames[i] != null && _gltfRoot.Meshes[meshIndex].TargetNames[i].Length > 0 ? _gltfRoot.Meshes[meshIndex].TargetNames[i] : $"Morphtarget{i}";
mesh.AddBlendShapeFrame(targetName, 100,
unityMeshData.MorphTargetVertices[i],
unityMeshData.MorphTargetNormals != null ? unityMeshData.MorphTargetNormals[i] : null,
unityMeshData.MorphTargetTangents != null ? unityMeshData.MorphTargetTangents[i] : null
);
}
} else {
// Get target names from first primitive's extras
var firstPrim = _gltfRoot.Meshes[meshIndex].Primitives[0];
for (int i = 0; i < firstPrim.Targets.Count; i++)
{
var targetName = firstPrim.TargetNames != null ? firstPrim.TargetNames[i] : $"Morphtarget{i}";
mesh.AddBlendShapeFrame(targetName, 100,
unityMeshData.MorphTargetVertices[i],
unityMeshData.MorphTargetNormals != null ? unityMeshData.MorphTargetNormals[i] : null,
unityMeshData.MorphTargetTangents != null ? unityMeshData.MorphTargetTangents[i] : null
);
}
}
}
await YieldOnTimeoutAndThrowOnLowMemory();
Expand Down