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

[0.x][1.0] SkinnedMeshRenderer.bones に null が含まれていてもExportできる #2341

Merged
merged 5 commits into from
Jul 4, 2024
Merged
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
14 changes: 13 additions & 1 deletion Assets/UniGLTF/Runtime/MeshUtility/MeshAttachInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ public void ReplaceMesh(GameObject dst)
if (Bones != null)
{
// recalc bindposes
Mesh.bindposes = Bones.Select(x => x.worldToLocalMatrix * dst.transform.localToWorldMatrix).ToArray();
Mesh.bindposes = Bones.Select(x =>
{
if (x != null)
{
return x.worldToLocalMatrix * dst.transform.localToWorldMatrix;
}
else
{
// ボーンが削除された
return dst.transform.localToWorldMatrix;
}
}
).ToArray();

if (dst.GetComponent<SkinnedMeshRenderer>() is SkinnedMeshRenderer dstRenderer)
{
Expand Down
26 changes: 23 additions & 3 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static TransformWithSkin BuildHierarchy(glTF gltf, int i, List<Transform>
// invisible in loading
renderer.enabled = false;

if (mesh.ShouldSetRendererNodeAsBone )
if (mesh.ShouldSetRendererNodeAsBone)
{
renderer.bones = new[] { renderer.transform };

Expand Down Expand Up @@ -188,7 +188,17 @@ public static void SetupSkinning(GltfData data, List<TransformWithSkin> nodes, i
skinnedMeshRenderer.sharedMesh = null;

var skin = data.GLTF.skins[x.SkinIndex.Value];
var joints = skin.joints.Select(y => nodes[y].Transform).ToArray();
var joints = skin.joints.Select(y =>
{
if (y >= 0 && y < nodes.Count)
{
return nodes[y].Transform;
}
else
{
return null;
}
}).ToArray();
if (joints.Any())
{
// have bones
Expand All @@ -209,7 +219,17 @@ public static void SetupSkinning(GltfData data, List<TransformWithSkin> nodes, i
// https://docs.unity3d.com/ScriptReference/Mesh-bindposes.html
//
var meshCoords = skinnedMeshRenderer.transform; // ?
var calculatedBindPoses = joints.Select(y => y.worldToLocalMatrix * meshCoords.localToWorldMatrix).ToArray();
var calculatedBindPoses = joints.Select(y =>
{
if (y != null)
{
return y.worldToLocalMatrix * meshCoords.localToWorldMatrix;
}
else
{
return Matrix4x4.identity * meshCoords.localToWorldMatrix;
}
}).ToArray();
mesh.bindposes = calculatedBindPoses;
}
}
Expand Down
14 changes: 13 additions & 1 deletion Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,19 @@ public virtual void Export(ITextureSerializer textureSerializer)
var skin = new glTFSkin
{
inverseBindMatrices = accessor,
joints = uniqueBones.Select(y => Nodes.IndexOf(y)).ToArray(),
joints = uniqueBones.Select(y =>
{
var index = Nodes.IndexOf(y);
if (index < 0)
{
// bones の先頭を使う
return 0;
}
else
{
return index;
}
}).ToArray(),
skeleton = Nodes.IndexOf(smr.rootBone),
};
var skinIndex = _gltf.skins.Count;
Expand Down
14 changes: 10 additions & 4 deletions Assets/VRM/Runtime/Extensions/VrmHumanoidExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public static void Apply(this glTF_VRM_Humanoid self, UniHumanoid.AvatarDescript

foreach (var x in desc.human)
{
var nodeIndex = nodes.FindIndex(y => y.name == x.boneName);
if (nodeIndex < 0)
{
continue;
}
var key = x.humanBone.FromHumanBodyBone();
var found = self.humanBones.FirstOrDefault(y => y.vrmBone == key);
if (found == null)
Expand All @@ -70,8 +75,7 @@ public static void Apply(this glTF_VRM_Humanoid self, UniHumanoid.AvatarDescript
self.humanBones.Add(found);
}

found.node = nodes.FindIndex(y => y.name == x.boneName);

found.node = nodeIndex;
found.useDefaultValues = x.useDefaultValues;
found.axisLength = x.axisLength;
found.center = x.center;
Expand All @@ -95,17 +99,19 @@ public static UniHumanoid.AvatarDescription ToDescription(this glTF_VRM_Humanoid
int index = 0;
foreach (var x in self.humanBones)
{
if (x.node < 0 || x.node >= nodes.Count) continue;
boneLimits[index] = new UniHumanoid.BoneLimit
{
boneName = nodes[x.node].name,
useDefaultValues = x.useDefaultValues,
axisLength = x.axisLength,
center = x.center,
min = x.min,
max = x.max,
humanBone = x.vrmBone.ToHumanBodyBone(),
};
if (x.node >= 0 && x.node < nodes.Count)
{
boneLimits[index].boneName = nodes[x.node].name;
}
index++;
}

Expand Down
12 changes: 11 additions & 1 deletion Assets/VRM10/Runtime/IO/Model/ModelExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,17 @@ private static VrmLib.Skin CreateSkin(INativeArrayManager arrayManager,
skin.Root = nodes[skinnedMeshRenderer.rootBone.gameObject];
}

skin.Joints = skinnedMeshRenderer.bones.Select(x => nodes[x.gameObject]).ToList();
skin.Joints = skinnedMeshRenderer.bones.Select(x =>
{
if (x != null)
{
return nodes[x.gameObject];
}
else
{
return null;
}
}).ToList();
return skin;
}

Expand Down
13 changes: 12 additions & 1 deletion Assets/VRM10/Runtime/IO/Vrm10Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ public static IEnumerable<glTFMesh> ExportMeshes(List<MeshGroup> groups, List<ob
{
gltfSkin = new glTFSkin()
{
joints = skin.Joints.Select(joint => nodes.IndexOfThrow(joint)).ToArray()
joints = skin.Joints.Select(joint =>
{
var index = nodes.IndexOf(joint);
if (index < 0)
{
return 0;
}
else
{
return index;
}
}).ToArray()
};
if (skin.InverseMatrices == null)
{
Expand Down