Files
AutoMorpherDecompiled/Assets/@Eden_Tools/Eden_AutoMorpher/Script/BodyPoseToClothApplier.cs

265 lines
11 KiB
C#

// Warning: Some assembly references could not be resolved automatically. This might lead to incorrect decompilation of some parts,
// for ex. property getter/setter access. To get optimal decompilation results, please manually add the missing references to the list of loaded assemblies.
// EdenAutoMorpherScript, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Eden.AutoMorpher.BodyPoseToClothApplier
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class BodyPoseToClothApplier
{
private class BoneMapping
{
public Transform proxyBone;
public Transform clothBone;
public int depth;
public HumanBodyBones? humanBone;
}
public void ApplyBodyPoseToClothes(Transform bodyProxyRoot, Transform clothesRoot, Dictionary<Transform, BoneMatchUtil.BoneRootLocalData> clothToBodyMatched, Dictionary<Transform, Transform> sourceToProxy, bool copyPosition = true, bool copyRotation = false, bool copyScale = true)
{
//IL_0063: Unknown result type (might be due to invalid IL or missing references)
//IL_011e: Unknown result type (might be due to invalid IL or missing references)
//IL_0123: Unknown result type (might be due to invalid IL or missing references)
//IL_0169: Unknown result type (might be due to invalid IL or missing references)
//IL_016d: Invalid comparison between Unknown and I4
//IL_017b: Unknown result type (might be due to invalid IL or missing references)
//IL_0256: Unknown result type (might be due to invalid IL or missing references)
//IL_0272: Unknown result type (might be due to invalid IL or missing references)
//IL_028e: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)bodyProxyRoot == (Object)null)
{
throw new AutoMorpherException("Body Proxy Root is Missing", "[BodyToClothPoseApplier] ApplyDeformedProxyPoseToCloth\n - bodyProxyRoot is null");
}
if ((Object)(object)clothesRoot == (Object)null)
{
throw new AutoMorpherException("Clothes Root is Missing", "[BodyToClothPoseApplier] ApplyDeformedProxyPoseToCloth\n - clothesRoot is null");
}
if (clothToBodyMatched == null || clothToBodyMatched.Count == 0)
{
throw new AutoMorpherException("Cloth To Body Matched Map is Missing", "[BodyToClothPoseApplier] ApplyDeformedProxyPoseToCloth\n - clothToBodyMatched is null or empty");
}
if (sourceToProxy == null)
{
throw new AutoMorpherException("Source To Proxy Map is Missing", "[BodyToClothPoseApplier] ApplyDeformedProxyPoseToCloth\n - sourceToProxy is null");
}
clothesRoot.localScale = bodyProxyRoot.localScale;
Animator component = ((Component)bodyProxyRoot).GetComponent<Animator>();
if ((Object)(object)component == (Object)null)
{
throw new AutoMorpherException("Body Proxy Animator is Missing", "[BodyToClothPoseApplier] ApplyDeformedProxyPoseToCloth\n - bodyProxyRoot has no Animator");
}
Transform transform = ((Component)component).transform;
Dictionary<Transform, int> dictionary = this.BuildDepthMap(transform);
Dictionary<Transform, Transform> dictionary2 = new Dictionary<Transform, Transform>();
List<BoneMapping> list = new List<BoneMapping>();
HashSet<Transform> hashSet = new HashSet<Transform>();
foreach (KeyValuePair<Transform, BoneMatchUtil.BoneRootLocalData> item in clothToBodyMatched)
{
Transform key = item.Key;
BoneMatchUtil.BoneRootLocalData value = item.Value;
if ((Object)(object)key == (Object)null || value == null)
{
continue;
}
Transform t = value.t;
if ((Object)(object)t == (Object)null || !sourceToProxy.TryGetValue(t, out var value2) || (Object)(object)value2 == (Object)null)
{
continue;
}
HumanBodyBones hBone = value.hBone;
if (hashSet.Add(key))
{
int depth = 999;
if (dictionary != null && dictionary.TryGetValue(value2, out var value3))
{
depth = value3;
}
list.Add(new BoneMapping
{
proxyBone = value2,
clothBone = key,
depth = depth,
humanBone = (((int)hBone != 55) ? new HumanBodyBones?(hBone) : ((HumanBodyBones?)null))
});
}
if (!dictionary2.ContainsKey(value2))
{
dictionary2.Add(value2, key);
}
}
this.MirrorBoneMarkers(transform, dictionary2, dictionary, list);
foreach (BoneMapping item2 in (from m in list
where m != null && (Object)(object)m.proxyBone != (Object)null && (Object)(object)m.clothBone != (Object)null
orderby m.depth
select m).ToList())
{
if (!((Object)(object)item2.proxyBone == (Object)null) && !((Object)(object)item2.clothBone == (Object)null))
{
if (copyPosition)
{
item2.clothBone.position = item2.proxyBone.position;
}
if (copyRotation)
{
item2.clothBone.rotation = item2.proxyBone.rotation;
}
if (copyScale)
{
item2.clothBone.localScale = item2.proxyBone.localScale;
}
}
}
}
private Dictionary<Transform, int> BuildDepthMap(Transform root)
{
Dictionary<Transform, int> dictionary = new Dictionary<Transform, int>();
if ((Object)(object)root == (Object)null)
{
return dictionary;
}
Transform[] componentsInChildren = ((Component)root).GetComponentsInChildren<Transform>(true);
foreach (Transform val in componentsInChildren)
{
if (!((Object)(object)val == (Object)null))
{
dictionary[val] = this.GetDepthFromRoot(val, root);
}
}
return dictionary;
}
private int GetDepthFromRoot(Transform t, Transform root)
{
int num = 0;
Transform val = t;
while ((Object)(object)val != (Object)null && (Object)(object)val != (Object)(object)root)
{
num++;
val = val.parent;
}
return num;
}
private void MirrorBoneMarkers(Transform proxyArmature, Dictionary<Transform, Transform> proxyToCloth, Dictionary<Transform, int> proxyDepthMap, List<BoneMapping> mappings)
{
//IL_0133: Unknown result type (might be due to invalid IL or missing references)
//IL_0166: Unknown result type (might be due to invalid IL or missing references)
//IL_0174: Unknown result type (might be due to invalid IL or missing references)
//IL_0182: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)proxyArmature == (Object)null || proxyToCloth == null || mappings == null)
{
return;
}
TempBoneMarker[] componentsInChildren = ((Component)proxyArmature).GetComponentsInChildren<TempBoneMarker>(true);
if (componentsInChildren == null || componentsInChildren.Length == 0)
{
return;
}
HashSet<Transform> hashSet = new HashSet<Transform>();
for (int i = 0; i < mappings.Count; i++)
{
BoneMapping boneMapping = mappings[i];
if (boneMapping != null && (Object)(object)boneMapping.proxyBone != (Object)null)
{
hashSet.Add(boneMapping.proxyBone);
}
}
int value3;
foreach (TempBoneMarker item in (from bm in componentsInChildren
where (Object)(object)bm != (Object)null && (Object)(object)((Component)bm).transform != (Object)null
orderby (proxyDepthMap != null && proxyDepthMap.TryGetValue(((Component)bm).transform, out value3)) ? value3 : int.MaxValue
select bm).ToList())
{
Transform transform = ((Component)item).transform;
if ((Object)(object)transform == (Object)null)
{
continue;
}
Transform originalParent = item.originalParent;
if ((Object)(object)originalParent == (Object)null || !proxyToCloth.TryGetValue(originalParent, out var value) || (Object)(object)value == (Object)null)
{
continue;
}
List<Transform> list = this.CollectDirectChilds(value);
Transform val = this.FindMarkerInChild(value);
TempBoneMarker tempBoneMarker = null;
if ((Object)(object)val == (Object)null)
{
val = new GameObject(((Object)transform).name).transform;
val.SetParent(value, true);
tempBoneMarker = ((Component)val).gameObject.AddComponent<TempBoneMarker>();
}
else
{
tempBoneMarker = ((Component)val).GetComponent<TempBoneMarker>();
}
val.position = transform.position;
val.rotation = transform.rotation;
val.localScale = transform.localScale;
tempBoneMarker.originalParent = value;
tempBoneMarker.additionalInfo = item.additionalInfo;
tempBoneMarker.wrappedChildNames = item.wrappedChildNames;
for (int num = 0; num < list.Count; num++)
{
Transform val2 = list[num];
if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val2 == (Object)(object)val))
{
val2.SetParent(val, true);
}
}
if (!proxyToCloth.ContainsKey(transform))
{
proxyToCloth.Add(transform, val);
}
if (hashSet.Add(transform))
{
int depth = 999;
if (proxyDepthMap != null && proxyDepthMap.TryGetValue(transform, out var value2))
{
depth = value2;
}
mappings.Add(new BoneMapping
{
proxyBone = transform,
clothBone = val,
depth = depth,
humanBone = null
});
}
}
}
private Transform FindMarkerInChild(Transform parent)
{
if ((Object)(object)parent == (Object)null)
{
return null;
}
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
if (!((Object)(object)child == (Object)null) && (Object)(object)((Component)child).GetComponent<TempBoneMarker>() != (Object)null)
{
return child;
}
}
return null;
}
private List<Transform> CollectDirectChilds(Transform parentTransform)
{
int childCount = parentTransform.childCount;
List<Transform> list = new List<Transform>(childCount);
for (int i = 0; i < childCount; i++)
{
list.Add(parentTransform.GetChild(i));
}
return list;
}
}