Optimize model loading
This commit is contained in:
@@ -10,6 +10,9 @@ public class UmaContainer : MonoBehaviour
|
|||||||
public JObject CharaData;
|
public JObject CharaData;
|
||||||
public GameObject Body;
|
public GameObject Body;
|
||||||
public GameObject Tail;
|
public GameObject Tail;
|
||||||
|
public GameObject Head;
|
||||||
|
public GameObject PhysicsController;
|
||||||
|
|
||||||
public List<Texture2D> TailTextures = new List<Texture2D>();
|
public List<Texture2D> TailTextures = new List<Texture2D>();
|
||||||
public Animator UmaAnimator;
|
public Animator UmaAnimator;
|
||||||
public AnimatorOverrideController OverrideController;
|
public AnimatorOverrideController OverrideController;
|
||||||
@@ -28,26 +31,6 @@ public class UmaContainer : MonoBehaviour
|
|||||||
public bool IsMini = false;
|
public bool IsMini = false;
|
||||||
public List<Texture2D> MiniHeadTextures = new List<Texture2D>();
|
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)
|
public static Transform FindBoneInChildren(Transform trans, string name)
|
||||||
{
|
{
|
||||||
foreach (Transform t in trans)
|
foreach (Transform t in trans)
|
||||||
@@ -74,4 +57,97 @@ public class UmaContainer : MonoBehaviour
|
|||||||
else { Destroy(gameObject); }
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
{
|
{
|
||||||
Destroy(CurrentUMAContainer);
|
Destroy(CurrentUMAContainer);
|
||||||
}
|
}
|
||||||
CurrentUMAContainer = new GameObject($"{id}_{costumeId}").AddComponent<UmaContainer>();
|
CurrentUMAContainer = new GameObject($"Chara_{id}_{costumeId}").AddComponent<UmaContainer>();
|
||||||
|
|
||||||
UnloadAllBundle();
|
UnloadAllBundle();
|
||||||
|
|
||||||
@@ -98,6 +98,7 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
Debug.LogError("No body, can't load!");
|
Debug.LogError("No body, can't load!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (genericCostume)
|
else if (genericCostume)
|
||||||
{
|
{
|
||||||
string texPattern1 = "", texPattern2 = "", texPattern3 = "", texPattern4 = "";
|
string texPattern1 = "", texPattern2 = "", texPattern3 = "", texPattern4 = "";
|
||||||
@@ -192,7 +193,6 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
RecursiveLoadAsset(asset1);
|
RecursiveLoadAsset(asset1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int tailId = (int)charaData["tailModelId"];
|
int tailId = (int)charaData["tailModelId"];
|
||||||
@@ -225,15 +225,17 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Load FacialMorph
|
//Load FacialMorph
|
||||||
if (CurrentUMAContainer.Heads.Count > 0)
|
if (CurrentUMAContainer.Head)
|
||||||
{
|
{
|
||||||
var firsehead = CurrentUMAContainer.Heads[0];
|
var firsehead = CurrentUMAContainer.Head;
|
||||||
var FaceDriven = firsehead.GetComponent<AssetHolder>()._assetTable.list.Find(a => { return a.Key == "facial_target"; }).Value as FaceDrivenKeyTarget;
|
var FaceDriven = firsehead.GetComponent<AssetHolder>()._assetTable.list.Find(a => { return a.Key == "facial_target"; }).Value as FaceDrivenKeyTarget;
|
||||||
CurrentUMAContainer.FaceDrivenKeyTargets = FaceDriven;
|
CurrentUMAContainer.FaceDrivenKeyTargets = FaceDriven;
|
||||||
FaceDriven.callBack = UmaViewerUI.Instance;
|
FaceDriven.callBack = UmaViewerUI.Instance;
|
||||||
FaceDriven.Initialize(firsehead.GetComponentsInChildren<Transform>().ToList());
|
FaceDriven.Initialize(firsehead.GetComponentsInChildren<Transform>().ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CurrentUMAContainer.MergeModel();
|
||||||
|
CurrentUMAContainer.LoadPhysics();
|
||||||
LoadAsset(UmaViewerMain.Instance.AbList.FirstOrDefault(a => a.Name.EndsWith($"anm_eve_chr{id}_00_idle01_loop")));
|
LoadAsset(UmaViewerMain.Instance.AbList.FirstOrDefault(a => a.Name.EndsWith($"anm_eve_chr{id}_00_idle01_loop")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,6 +329,8 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
//Load Head
|
//Load Head
|
||||||
RecursiveLoadAsset(asset);
|
RecursiveLoadAsset(asset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CurrentUMAContainer.MergeModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LoadProp(UmaDatabaseEntry entry)
|
public void LoadProp(UmaDatabaseEntry entry)
|
||||||
@@ -576,7 +580,7 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
Type aType = asset.GetType();
|
Type aType = asset.GetType();
|
||||||
if (aType == typeof(AnimationClip))
|
if (aType == typeof(AnimationClip))
|
||||||
{
|
{
|
||||||
if (CurrentUMAContainer && CurrentUMAContainer.Body)
|
if (CurrentUMAContainer && CurrentUMAContainer.UmaAnimator)
|
||||||
{
|
{
|
||||||
AnimationClip bbb = asset as AnimationClip;
|
AnimationClip bbb = asset as AnimationClip;
|
||||||
bbb.wrapMode = WrapMode.Loop;
|
bbb.wrapMode = WrapMode.Loop;
|
||||||
@@ -591,19 +595,12 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
{
|
{
|
||||||
if (bundle.name.Contains("cloth"))
|
if (bundle.name.Contains("cloth"))
|
||||||
{
|
{
|
||||||
if (bundle.name.Contains("chr"))
|
if (!CurrentUMAContainer.PhysicsController)
|
||||||
{
|
{
|
||||||
GameObject head = Instantiate(asset as GameObject, CurrentUMAContainer.Heads[0].transform);
|
CurrentUMAContainer.PhysicsController = new GameObject("PhysicsController");
|
||||||
|
CurrentUMAContainer.PhysicsController.transform.SetParent(CurrentUMAContainer.transform);
|
||||||
}
|
}
|
||||||
else if (bundle.name.Contains("bdy"))
|
Instantiate(asset as GameObject, CurrentUMAContainer.PhysicsController.transform);
|
||||||
{
|
|
||||||
GameObject head = Instantiate(asset as GameObject, CurrentUMAContainer.Body.transform);
|
|
||||||
}
|
|
||||||
else if (bundle.name.Contains("tail"))
|
|
||||||
{
|
|
||||||
GameObject head = Instantiate(asset as GameObject, CurrentUMAContainer.Tail.transform);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (bundle.name.Contains("/head/"))
|
else if (bundle.name.Contains("/head/"))
|
||||||
{
|
{
|
||||||
@@ -625,10 +622,6 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (aType == typeof(FaceDrivenKeyTarget))
|
|
||||||
{
|
|
||||||
//CurrentContainer.FaceDrivenKeyTargets.Add(Instantiate(asset as FaceDrivenKeyTarget));
|
|
||||||
}
|
|
||||||
else if (aType == typeof(Shader))
|
else if (aType == typeof(Shader))
|
||||||
{
|
{
|
||||||
ShaderList.Add(asset as Shader);
|
ShaderList.Add(asset as Shader);
|
||||||
@@ -655,7 +648,6 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
{
|
{
|
||||||
CurrentUMAContainer.Body = Instantiate(go, CurrentUMAContainer.transform);
|
CurrentUMAContainer.Body = Instantiate(go, CurrentUMAContainer.transform);
|
||||||
CurrentUMAContainer.UmaAnimator = CurrentUMAContainer.Body.GetComponent<Animator>();
|
CurrentUMAContainer.UmaAnimator = CurrentUMAContainer.Body.GetComponent<Animator>();
|
||||||
CurrentUMAContainer.UmaAnimator.runtimeAnimatorController = CurrentUMAContainer.OverrideController = Instantiate(OverrideController);
|
|
||||||
|
|
||||||
if (CurrentUMAContainer.IsGeneric)
|
if (CurrentUMAContainer.IsGeneric)
|
||||||
{
|
{
|
||||||
@@ -745,9 +737,7 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
private void LoadHead(GameObject go)
|
private void LoadHead(GameObject go)
|
||||||
{
|
{
|
||||||
GameObject head = Instantiate(go, CurrentUMAContainer.transform);
|
GameObject head = Instantiate(go, CurrentUMAContainer.transform);
|
||||||
CurrentUMAContainer.Heads.Add(head);
|
CurrentUMAContainer.Head=head;
|
||||||
CurrentUMAContainer.HeadNeckBones.Add(UmaContainer.FindBoneInChildren(head.transform, "Neck"));
|
|
||||||
CurrentUMAContainer.HeadHeadBones.Add(UmaContainer.FindBoneInChildren(head.transform, "Head"));
|
|
||||||
|
|
||||||
foreach (Renderer r in head.GetComponentsInChildren<Renderer>())
|
foreach (Renderer r in head.GetComponentsInChildren<Renderer>())
|
||||||
{
|
{
|
||||||
@@ -755,6 +745,10 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
{
|
{
|
||||||
if (head.name.Contains("mchr"))
|
if (head.name.Contains("mchr"))
|
||||||
{
|
{
|
||||||
|
if (r.name.Contains("Hair"))
|
||||||
|
{
|
||||||
|
CurrentUMAContainer.Tail = head;
|
||||||
|
}
|
||||||
if (r.name == "M_Face")
|
if (r.name == "M_Face")
|
||||||
{
|
{
|
||||||
m.SetTexture("_MainTex", CurrentUMAContainer.MiniHeadTextures.First(t => t.name.Contains("face") && t.name.Contains("diff")));
|
m.SetTexture("_MainTex", CurrentUMAContainer.MiniHeadTextures.First(t => t.name.Contains("face") && t.name.Contains("diff")));
|
||||||
@@ -812,9 +806,7 @@ public class UmaViewerBuilder : MonoBehaviour
|
|||||||
|
|
||||||
private void LoadTail(GameObject gameObject)
|
private void LoadTail(GameObject gameObject)
|
||||||
{
|
{
|
||||||
Transform hHip = UmaContainer.FindBoneInChildren(CurrentUMAContainer.Body.transform, "Hip");
|
CurrentUMAContainer.Tail = Instantiate(gameObject, CurrentUMAContainer.transform);
|
||||||
if (hHip == null) return;
|
|
||||||
CurrentUMAContainer.Tail = Instantiate(gameObject, hHip);
|
|
||||||
var textures = CurrentUMAContainer.TailTextures;
|
var textures = CurrentUMAContainer.TailTextures;
|
||||||
foreach (Renderer r in CurrentUMAContainer.Tail.GetComponentsInChildren<Renderer>())
|
foreach (Renderer r in CurrentUMAContainer.Tail.GetComponentsInChildren<Renderer>())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ namespace Gallop
|
|||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
InitializePhysics();
|
//InitializePhysics();
|
||||||
}
|
}
|
||||||
public void InitializePhysics()
|
public void InitializePhysics()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user