Optimize model loading
This commit is contained in:
@@ -10,6 +10,9 @@ public class UmaContainer : MonoBehaviour
|
||||
public JObject CharaData;
|
||||
public GameObject Body;
|
||||
public GameObject Tail;
|
||||
public GameObject Head;
|
||||
public GameObject PhysicsController;
|
||||
|
||||
public List<Texture2D> TailTextures = new List<Texture2D>();
|
||||
public Animator UmaAnimator;
|
||||
public AnimatorOverrideController OverrideController;
|
||||
@@ -28,26 +31,6 @@ public class UmaContainer : MonoBehaviour
|
||||
public bool IsMini = false;
|
||||
public List<Texture2D> MiniHeadTextures = new List<Texture2D>();
|
||||
|
||||
[Header("Head Assembly")]
|
||||
public Transform UmaNeckBone;
|
||||
public Transform UmaHeadBone;
|
||||
public List<GameObject> Heads = new List<GameObject>();
|
||||
public List<Transform> HeadNeckBones = new List<Transform>();
|
||||
public List<Transform> HeadHeadBones = new List<Transform>();
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (Body != null && Heads.Count > 0)
|
||||
{
|
||||
if (UmaNeckBone == null) UmaNeckBone = FindBoneInChildren(Body.transform, "Neck");
|
||||
if (UmaHeadBone == null) UmaHeadBone = FindBoneInChildren(Body.transform, "Head");
|
||||
for (int i = 0; i < Heads.Count; i++)
|
||||
{
|
||||
HeadNeckBones[i].transform.SetPositionAndRotation(UmaNeckBone.position, UmaNeckBone.rotation);
|
||||
HeadHeadBones[i].transform.SetPositionAndRotation(UmaHeadBone.position, UmaHeadBone.rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Transform FindBoneInChildren(Transform trans, string name)
|
||||
{
|
||||
foreach (Transform t in trans)
|
||||
@@ -74,4 +57,97 @@ public class UmaContainer : MonoBehaviour
|
||||
else { Destroy(gameObject); }
|
||||
|
||||
}
|
||||
|
||||
public void MergeModel()
|
||||
{
|
||||
if (!Body) return;
|
||||
List<Transform> bodybones = new List<Transform>(Body.GetComponentInChildren<SkinnedMeshRenderer>().bones);
|
||||
List<Transform> emptyBones = new List<Transform>();
|
||||
emptyBones.Add(Body.GetComponentInChildren<SkinnedMeshRenderer>().rootBone.Find("Tail_Ctrl"));
|
||||
while (Body.transform.childCount > 0)
|
||||
{
|
||||
var child = Body.transform.GetChild(0);
|
||||
child.SetParent(transform);
|
||||
}
|
||||
Destroy(Body);
|
||||
|
||||
|
||||
//MergeHead
|
||||
if (Head)
|
||||
{
|
||||
var headskins = Head.GetComponentsInChildren<SkinnedMeshRenderer>();
|
||||
foreach (SkinnedMeshRenderer headskin in headskins)
|
||||
{
|
||||
emptyBones.AddRange(MergeBone(headskin, bodybones));
|
||||
}
|
||||
var eyes = new GameObject("Eyes");
|
||||
eyes.transform.SetParent(transform);
|
||||
while (Head.transform.childCount > 0)
|
||||
{
|
||||
var child = Head.transform.GetChild(0);
|
||||
child.SetParent(child.name.Contains("info") ? eyes.transform : transform);
|
||||
}
|
||||
Destroy(Head);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//MergeTail
|
||||
if (Tail)
|
||||
{
|
||||
var tailskin = Tail.GetComponentInChildren<SkinnedMeshRenderer>();
|
||||
emptyBones.AddRange(MergeBone(tailskin, bodybones));
|
||||
while (Tail.transform.childCount > 0)
|
||||
{
|
||||
var child = Tail.transform.GetChild(0);
|
||||
child.SetParent(transform);
|
||||
}
|
||||
Destroy(Tail);
|
||||
emptyBones.ForEach(a => { if (a) Destroy(a.gameObject); });
|
||||
}
|
||||
|
||||
//MergeAvatar
|
||||
UmaAnimator = gameObject.AddComponent<Animator>();
|
||||
UmaAnimator.avatar = AvatarBuilder.BuildGenericAvatar(gameObject, gameObject.name);
|
||||
UmaAnimator.runtimeAnimatorController = OverrideController = Instantiate(UmaViewerBuilder.Instance.OverrideController);
|
||||
|
||||
}
|
||||
|
||||
public Transform[] MergeBone(SkinnedMeshRenderer from, List<Transform> targetBones)
|
||||
{
|
||||
var rootbone = targetBones.FindLast(a => a.name.Equals(from.rootBone.name));
|
||||
if (rootbone) from.rootBone = rootbone;
|
||||
|
||||
List<Transform> emptyBones = new List<Transform>();
|
||||
Transform[] tmpBone = new Transform[from.bones.Length];
|
||||
for (int i = 0; i < tmpBone.Length; i++)
|
||||
{
|
||||
var targetbone = targetBones.FindLast(a => a.name.Equals(from.bones[i].name));
|
||||
if (targetbone)
|
||||
{
|
||||
tmpBone[i] = targetbone;
|
||||
from.bones[i].position = targetbone.position;
|
||||
while (from.bones[i].transform.childCount > 0)
|
||||
{
|
||||
from.bones[i].transform.GetChild(0).SetParent(targetbone);
|
||||
}
|
||||
emptyBones.Add(from.bones[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpBone[i] = from.bones[i];
|
||||
}
|
||||
}
|
||||
from.bones = tmpBone;
|
||||
return emptyBones.ToArray() ;
|
||||
}
|
||||
|
||||
public void LoadPhysics()
|
||||
{
|
||||
var springs = PhysicsController.GetComponentsInChildren<CySpringDataContainer>();
|
||||
foreach (CySpringDataContainer spring in springs)
|
||||
{
|
||||
spring.InitializePhysics();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user