Skip to content

Commit 9d0ee93

Browse files
authored
fix: add bones, if bones are not set (#310)
<!-- Copy the TICKETID for this task from Jira and add it to the PR name in brackets --> <!-- PR name should look like: [TICKETID] My Pull Request --> <!-- Add link for the ticket here editing the TICKETID--> ## [TICKETID](https://ready-player-me.atlassian.net/browse/TICKETID) ## Description - Briefly describe what this change will do <!-- Fill the section below with Added, Updated and Removed information. --> <!-- If there is no item under one of the lists remove it's title. --> <!-- Testability --> ## How to Test - Add steps to locally test these changes <!-- Update your progress with the task here --> ## Checklist - [ ] Tests written or updated for the changes. - [ ] Documentation is updated. - [ ] Changelog is updated. <!--- Remember to copy the Changes Section into the commit message when you close the PR -->
2 parents 4611b5a + 4607cc0 commit 9d0ee93

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

Runtime/Core/Scripts/Utils/AvatarMeshHelper.cs

+20-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ namespace ReadyPlayerMe.Core
77
{
88
public static class AvatarMeshHelper
99
{
10-
private static readonly string[] headMeshNames = {
10+
private static readonly string[] headMeshNames =
11+
{
1112
"Renderer_Head",
1213
"Renderer_Hair",
1314
"Renderer_Beard",
@@ -16,15 +17,15 @@ public static class AvatarMeshHelper
1617
"Renderer_EyeLeft",
1718
"Renderer_EyeRight",
1819
"Renderer_Headwear",
19-
"Renderer_Facewear",
20+
"Renderer_Facewear"
2021
};
2122

2223
[Obsolete("Use TransferMesh(GameObject source, GameObject target) instead.")]
2324
public static void TransferMesh(GameObject source, SkinnedMeshRenderer[] targetMeshes, Animator targetAnimator)
2425
{
2526
TransferMesh(source, targetMeshes[0].transform.parent.gameObject);
2627
}
27-
28+
2829
/// <summary>
2930
/// Transfers the mesh and material data from the source to the target avatar.
3031
/// </summary>
@@ -34,23 +35,30 @@ public static void TransferMesh(GameObject source, GameObject target)
3435
{
3536
// store the relevant data of the source (downloaded) avatar
3637
var rendererDict = new Dictionary<string, SkinnedMeshRenderer>();
37-
38+
3839
var sourceRenderers = source.GetComponentsInChildren<SkinnedMeshRenderer>();
3940
var targetRenderers = target.GetComponentsInChildren<SkinnedMeshRenderer>();
40-
41+
4142
foreach (var renderer in sourceRenderers)
4243
{
4344
rendererDict.Add(renderer.name, renderer);
4445
}
45-
46+
47+
var meshWithBones = targetRenderers.DefaultIfEmpty(null).FirstOrDefault((renderer) => renderer.bones.Length != 0);
48+
4649
// transfer the data to the target skinning mesh renderers
4750
foreach (var renderer in targetRenderers)
4851
{
4952
if (rendererDict.TryGetValue(renderer.name, out var sourceRenderer))
5053
{
5154
renderer.sharedMesh = sourceRenderer.sharedMesh;
5255
renderer.sharedMaterial = sourceRenderer.sharedMaterial;
53-
56+
57+
if (renderer.bones.Length == 0 && meshWithBones != null)
58+
{
59+
renderer.bones = meshWithBones.bones;
60+
continue;
61+
}
5462
// transfer the bone data
5563
foreach (var targetBone in renderer.bones)
5664
{
@@ -61,7 +69,7 @@ public static void TransferMesh(GameObject source, GameObject target)
6169
targetBone.position = sourceBone.position;
6270
targetBone.rotation = sourceBone.rotation;
6371
targetBone.localScale = sourceBone.localScale;
64-
break;
72+
break;
6573
}
6674
}
6775
}
@@ -72,13 +80,13 @@ public static void TransferMesh(GameObject source, GameObject target)
7280
renderer.material = null;
7381
}
7482
}
75-
83+
7684
// transfer the animation avatar
7785
var sourceAnimator = source.GetComponentInChildren<Animator>();
7886
var targetAnimator = target.GetComponentInChildren<Animator>();
7987
targetAnimator.avatar = sourceAnimator.avatar;
8088
}
81-
89+
8290
/// <summary>
8391
/// Returns the meshes of the head of the avatar, such as glasses, hair, teeth, etc.
8492
/// </summary>
@@ -88,15 +96,15 @@ public static GameObject[] GetHeadMeshes(GameObject avatar)
8896
{
8997
var renderers = avatar.GetComponentsInChildren<SkinnedMeshRenderer>();
9098
var headMeshes = new List<GameObject>();
91-
99+
92100
foreach (var renderer in renderers)
93101
{
94102
if (headMeshNames.Contains(renderer.name))
95103
{
96104
headMeshes.Add(renderer.gameObject);
97105
}
98106
}
99-
107+
100108
return headMeshes.ToArray();
101109
}
102110
}

0 commit comments

Comments
 (0)