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

209 lines
12 KiB
C#

// Decompiled with JetBrains decompiler
// Type: Eden.AutoMorpher.BodyPoseToClothApplier
// Assembly: EdenAutoMorpherScript, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: D39968B3-E151-4276-BDB4-E82752BBAFF0
// Assembly location: D:\dev\AutoMorpher\Assets\@Eden_Tools\Eden_AutoMorpher\Script\EdenAutoMorpherScript.dll
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Eden.AutoMorpher
{
public class BodyPoseToClothApplier
{
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)
{
if (Object.op_Equality((Object)bodyProxyRoot, (Object)null))
throw new AutoMorpherException("Body Proxy Root is Missing", "[BodyToClothPoseApplier] ApplyDeformedProxyPoseToCloth\n - bodyProxyRoot is null");
if (Object.op_Equality((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>();
Transform transform = !Object.op_Equality((Object)component, (Object)null) ? ((Component)component).transform : throw new AutoMorpherException("Body Proxy Animator is Missing", "[BodyToClothPoseApplier] ApplyDeformedProxyPoseToCloth\n - bodyProxyRoot has no Animator");
Dictionary<Transform, int> proxyDepthMap = this.BuildDepthMap(transform);
Dictionary<Transform, Transform> proxyToCloth = new Dictionary<Transform, Transform>();
List<BodyPoseToClothApplier.BoneMapping> boneMappingList = new List<BodyPoseToClothApplier.BoneMapping>();
HashSet<Transform> transformSet = new HashSet<Transform>();
foreach (KeyValuePair<Transform, BoneMatchUtil.BoneRootLocalData> keyValuePair in clothToBodyMatched)
{
Transform key1 = keyValuePair.Key;
BoneMatchUtil.BoneRootLocalData boneRootLocalData = keyValuePair.Value;
if (!Object.op_Equality((Object)key1, (Object)null) && boneRootLocalData != null)
{
Transform t = boneRootLocalData.t;
Transform key2;
if (!Object.op_Equality((Object)t, (Object)null) && sourceToProxy.TryGetValue(t, out key2) && !Object.op_Equality((Object)key2, (Object)null))
{
HumanBodyBones hBone = boneRootLocalData.hBone;
if (transformSet.Add(key1))
{
int num1 = 999;
int num2;
if (proxyDepthMap != null && proxyDepthMap.TryGetValue(key2, out num2))
num1 = num2;
boneMappingList.Add(new BodyPoseToClothApplier.BoneMapping()
{
proxyBone = key2,
clothBone = key1,
depth = num1,
humanBone = hBone != 55 ? new HumanBodyBones?(hBone) : new HumanBodyBones?()
});
}
if (!proxyToCloth.ContainsKey(key2))
proxyToCloth.Add(key2, key1);
}
}
}
this.MirrorBoneMarkers(transform, proxyToCloth, proxyDepthMap, boneMappingList);
foreach (BodyPoseToClothApplier.BoneMapping boneMapping in boneMappingList.Where<BodyPoseToClothApplier.BoneMapping>((Func<BodyPoseToClothApplier.BoneMapping, bool>)(m => m != null && Object.op_Inequality((Object)m.proxyBone, (Object)null) && Object.op_Inequality((Object)m.clothBone, (Object)null))).OrderBy<BodyPoseToClothApplier.BoneMapping, int>((Func<BodyPoseToClothApplier.BoneMapping, int>)(m => m.depth)).ToList<BodyPoseToClothApplier.BoneMapping>())
{
if (!Object.op_Equality((Object)boneMapping.proxyBone, (Object)null) && !Object.op_Equality((Object)boneMapping.clothBone, (Object)null))
{
if (copyPosition)
boneMapping.clothBone.position = boneMapping.proxyBone.position;
if (copyRotation)
boneMapping.clothBone.rotation = boneMapping.proxyBone.rotation;
if (copyScale)
boneMapping.clothBone.localScale = boneMapping.proxyBone.localScale;
}
}
}
private Dictionary<Transform, int> BuildDepthMap(Transform root)
{
Dictionary<Transform, int> dictionary = new Dictionary<Transform, int>();
if (Object.op_Equality((Object)root, (Object)null))
return dictionary;
foreach (Transform componentsInChild in ((Component)root).GetComponentsInChildren<Transform>(true))
{
if (!Object.op_Equality((Object)componentsInChild, (Object)null))
dictionary[componentsInChild] = this.GetDepthFromRoot(componentsInChild, root);
}
return dictionary;
}
private int GetDepthFromRoot(Transform t, Transform root)
{
int depthFromRoot = 0;
for (Transform transform = t; Object.op_Inequality((Object)transform, (Object)null) && Object.op_Inequality((Object)transform, (Object)root); transform = transform.parent)
++depthFromRoot;
return depthFromRoot;
}
private void MirrorBoneMarkers(
Transform proxyArmature,
Dictionary<Transform, Transform> proxyToCloth,
Dictionary<Transform, int> proxyDepthMap,
List<BodyPoseToClothApplier.BoneMapping> mappings)
{
if (Object.op_Equality((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> transformSet = new HashSet<Transform>();
for (int index = 0; index < mappings.Count; ++index)
{
BodyPoseToClothApplier.BoneMapping mapping = mappings[index];
if (mapping != null && Object.op_Inequality((Object)mapping.proxyBone, (Object)null))
transformSet.Add(mapping.proxyBone);
}
int num1;
foreach (TempBoneMarker tempBoneMarker1 in ((IEnumerable<TempBoneMarker>)componentsInChildren).Where<TempBoneMarker>((Func<TempBoneMarker, bool>)(bm => Object.op_Inequality((Object)bm, (Object)null) && Object.op_Inequality((Object)((Component)bm).transform, (Object)null))).OrderBy<TempBoneMarker, int>((Func<TempBoneMarker, int>)(bm => proxyDepthMap != null && proxyDepthMap.TryGetValue(((Component)bm).transform, out num1) ? num1 : int.MaxValue)).ToList<TempBoneMarker>())
{
Transform transform1 = ((Component)tempBoneMarker1).transform;
if (!Object.op_Equality((Object)transform1, (Object)null))
{
Transform originalParent = tempBoneMarker1.originalParent;
Transform transform2;
if (!Object.op_Equality((Object)originalParent, (Object)null) && proxyToCloth.TryGetValue(originalParent, out transform2) && !Object.op_Equality((Object)transform2, (Object)null))
{
List<Transform> transformList = this.CollectDirectChilds(transform2);
Transform transform3 = this.FindMarkerInChild(transform2);
TempBoneMarker tempBoneMarker2;
if (Object.op_Equality((Object)transform3, (Object)null))
{
transform3 = new GameObject(((Object)transform1).name).transform;
transform3.SetParent(transform2, true);
tempBoneMarker2 = ((Component)transform3).gameObject.AddComponent<TempBoneMarker>();
}
else
tempBoneMarker2 = ((Component)transform3).GetComponent<TempBoneMarker>();
transform3.position = transform1.position;
transform3.rotation = transform1.rotation;
transform3.localScale = transform1.localScale;
tempBoneMarker2.originalParent = transform2;
tempBoneMarker2.additionalInfo = tempBoneMarker1.additionalInfo;
tempBoneMarker2.wrappedChildNames = tempBoneMarker1.wrappedChildNames;
for (int index = 0; index < transformList.Count; ++index)
{
Transform transform4 = transformList[index];
if (!Object.op_Equality((Object)transform4, (Object)null) && !Object.op_Equality((Object)transform4, (Object)transform3))
transform4.SetParent(transform3, true);
}
if (!proxyToCloth.ContainsKey(transform1))
proxyToCloth.Add(transform1, transform3);
if (transformSet.Add(transform1))
{
int num2 = 999;
int num3;
if (proxyDepthMap != null && proxyDepthMap.TryGetValue(transform1, out num3))
num2 = num3;
mappings.Add(new BodyPoseToClothApplier.BoneMapping()
{
proxyBone = transform1,
clothBone = transform3,
depth = num2,
humanBone = new HumanBodyBones?()
});
}
}
}
}
}
private Transform FindMarkerInChild(Transform parent)
{
if (Object.op_Equality((Object)parent, (Object)null))
return (Transform)null;
for (int index = 0; index < parent.childCount; ++index)
{
Transform child = parent.GetChild(index);
if (!Object.op_Equality((Object)child, (Object)null) && Object.op_Inequality((Object)((Component)child).GetComponent<TempBoneMarker>(), (Object)null))
return child;
}
return (Transform)null;
}
private List<Transform> CollectDirectChilds(Transform parentTransform)
{
int childCount = parentTransform.childCount;
List<Transform> transformList = new List<Transform>(childCount);
for (int index = 0; index < childCount; ++index)
transformList.Add(parentTransform.GetChild(index));
return transformList;
}
private class BoneMapping
{
public Transform proxyBone;
public Transform clothBone;
public int depth;
public HumanBodyBones? humanBone;
}
}
}