So I’m not sure why this is happening.
I don’t even know what caused it either. It just randomly started happening. It used to work perfectly, then all of the sudden, this monstrosity.
What happens in this project is when I equip an item on the chest or the head it gets severely warped. I can’t find any reason for this warping. It doesn’t happen when I equip any other items, or weapons, which are all using the same script. It is a bit of a weird one for me.
I tried some debugging and narrowing down the issue. I am fairly certain it has nothing to do with the model itself, or exporting the model. I tried deleting all my models and importing them again. So I know its not that. It appears to be after I instantiate the model and try to stitch it to the mesh that it is having problems. I am not sure why, everything is in place and its all using the same code. Here is the stitching code:
public class Stitcher
{
/// <summary>
/// Stitch clothing onto an avatar. Both clothing and avatar must be instantiated however clothing may be destroyed after.
/// </summary>
/// <param name="sourceClothing"></param>
/// <param name="targetAvatar"></param>
/// <returns>Newly created clothing on avatar</returns>
public GameObject Stitch(GameObject sourceClothing, GameObject targetAvatar)
{
var boneCatalog = new TransformCatalog(targetAvatar.transform);
var skinnedMeshRenderers = sourceClothing.GetComponentsInChildren<SkinnedMeshRenderer>();
var targetClothing = AddChild(sourceClothing, targetAvatar.transform);
foreach (var sourceRenderer in skinnedMeshRenderers)
{
var targetRenderer = AddSkinnedMeshRenderer(sourceRenderer, targetClothing);
targetRenderer.bones = TranslateTransforms(sourceRenderer.bones, boneCatalog);
}
return targetClothing;
}
private GameObject AddChild(GameObject source, Transform parent)
{
source.transform.parent = parent;
foreach (Transform child in source.transform)
{
Object.Destroy(child.gameObject);
}
return source;
}
private SkinnedMeshRenderer AddSkinnedMeshRenderer(SkinnedMeshRenderer source, GameObject parent)
{
GameObject meshObject = new GameObject(source.name);
meshObject.transform.parent = parent.transform;
var target = meshObject.AddComponent<SkinnedMeshRenderer>();
target.sharedMesh = source.sharedMesh;
target.materials = source.materials;
return target;
}
private Transform[] TranslateTransforms(Transform[] sources, TransformCatalog transformCatalog)
{
var targets = new Transform[sources.Length];
for (var index = 0; index < sources.Length; index++)
targets[index] = DictionaryExtensions.Find(transformCatalog, sources[index].name);
return targets;
}
#region TransformCatalog
private class TransformCatalog : Dictionary<string, Transform>
{
#region Constructors
public TransformCatalog(Transform transform)
{
Catalog(transform);
}
#endregion
#region Catalog
private void Catalog(Transform transform)
{
if (ContainsKey(transform.name))
{
Remove(transform.name);
Add(transform.name, transform);
}
else
Add(transform.name, transform);
foreach (Transform child in transform)
Catalog(child);
}
#endregion
}
#endregion
#region DictionaryExtensions
private class DictionaryExtensions
{
public static TValue Find<TKey, TValue>(Dictionary<TKey, TValue> source, TKey key)
{
TValue value;
source.TryGetValue(key, out value);
return value;
}
}
#endregion
}
I don’t think this is the issue, but I can’t find anywhere else where there could be an issue. The thing is, this is the same code used for the other equipment parts, so if it works for them then it should work for the 2 areas that are messing up.
Any help?