refactor MeshUtility.Process params

This commit is contained in:
ousttrue 2023-12-04 17:00:15 +09:00
parent 103b575439
commit e3404e1f16
4 changed files with 41 additions and 68 deletions

View File

@ -199,7 +199,11 @@ namespace UniGLTF.MeshUtility
{
try
{
var (results, created) = MeshUtility.Process(_exportTarget, context.Instance);
// prefab が instantiate されていた場合に
// Mesh統合設定を instantiate に置き換える
var groupCopy = MeshUtility.CopyInstantiate(_exportTarget, context.Instance);
var (results, created) = MeshUtility.Process(context.Instance, groupCopy);
WriteAssets(context.Instance, context.AssetFolder, results);
}
@ -216,7 +220,7 @@ namespace UniGLTF.MeshUtility
{
using (var context = new UndoContext("MeshUtility", _exportTarget))
{
var (results, created) = MeshUtility.Process(_exportTarget, null);
var (results, created) = MeshUtility.Process(_exportTarget, MeshUtility.MeshIntegrationGroups);
foreach (var go in created)
{

View File

@ -99,40 +99,6 @@ namespace UniGLTF.MeshUtility
});
}
void RemoveComponent<T>(T c) where T : Component
{
if (c == null)
{
return;
}
var t = c.transform;
if (Application.isPlaying)
{
GameObject.Destroy(c);
}
else
{
GameObject.DestroyImmediate(c);
}
if (t.childCount == 0)
{
var list = t.GetComponents<Component>();
// Debug.Log($"{list[0].GetType().Name}");
if (list.Length == 1 && list[0] == t)
{
if (Application.isPlaying)
{
GameObject.Destroy(t.gameObject);
}
else
{
GameObject.DestroyImmediate(t.gameObject);
}
}
}
}
static GameObject GetOrCreateEmpty(GameObject go, string name)
{
foreach (var child in go.transform.GetChildren())
@ -156,7 +122,7 @@ namespace UniGLTF.MeshUtility
/// <param name="go">MeshIntegrationGroup を作ったとき root</param>
/// <param name="instance">go が prefab だった場合に instance されたもの</param>
/// <returns></returns>
protected virtual IEnumerable<MeshIntegrationGroup> CopyInstantiate(GameObject go, GameObject instance)
public virtual IEnumerable<MeshIntegrationGroup> CopyInstantiate(GameObject go, GameObject instance)
{
if (instance == null)
{
@ -174,9 +140,9 @@ namespace UniGLTF.MeshUtility
}
}
public virtual (List<MeshIntegrationResult>, List<GameObject>) Process(GameObject _go, GameObject _instance)
public virtual (List<MeshIntegrationResult>, List<GameObject>) Process(
GameObject target, IEnumerable<MeshIntegrationGroup> groupCopy)
{
var target = _instance ?? _go;
if (FreezeBlendShape || FreezeRotation || FreezeScaling)
{
// MeshをBakeする
@ -188,10 +154,6 @@ namespace UniGLTF.MeshUtility
BoneNormalizer.Replace(target, newMesh, FreezeRotation, FreezeScaling);
}
// prefab が instantiate されていた場合に
// Mesh統合設定を instantiate に置き換える
var groupCopy = CopyInstantiate(_go, _instance);
var newList = new List<GameObject>();
var empty = GetOrCreateEmpty(target, "mesh");
@ -212,24 +174,29 @@ namespace UniGLTF.MeshUtility
{
foreach (var r in result.SourceMeshRenderers)
{
RemoveComponent(r);
if (Application.isPlaying)
{
GameObject.Destroy(r.gameObject.GetComponent<MeshFilter>());
GameObject.Destroy(r);
}
else
{
GameObject.DestroyImmediate(r.gameObject.GetComponent<MeshFilter>());
GameObject.DestroyImmediate(r);
}
}
foreach (var r in result.SourceSkinnedMeshRenderers)
{
RemoveComponent(r);
if (Application.isPlaying)
{
GameObject.Destroy(r);
}
else
{
GameObject.DestroyImmediate(r);
}
}
}
// foreach (var result in results)
// {
// foreach (var renderer in result.SourceMeshRenderers)
// {
// GameObject.DestroyImmediate(renderer);
// }
// foreach (var renderer in result.SourceSkinnedMeshRenderers)
// {
// GameObject.DestroyImmediate(renderer);
// }
// }
MeshIntegrationGroups.Clear();

View File

@ -10,7 +10,7 @@ namespace VRM
public class VrmMeshUtility : UniGLTF.MeshUtility.GltfMeshUtility
{
bool _generateFirstPerson = false;
protected override IEnumerable<UniGLTF.MeshUtility.MeshIntegrationGroup> CopyInstantiate(GameObject go, GameObject instance)
public override IEnumerable<UniGLTF.MeshUtility.MeshIntegrationGroup> CopyInstantiate(GameObject go, GameObject instance)
{
var copy = new List<UniGLTF.MeshUtility.MeshIntegrationGroup>();
_generateFirstPerson = false;
@ -89,9 +89,10 @@ namespace VRM
/// <summary>
/// glTF に比べて Humanoid や FirstPerson の処理が追加される
/// </summary>
public override (List<UniGLTF.MeshUtility.MeshIntegrationResult>, List<GameObject>) Process(GameObject go, GameObject instance)
public override (List<UniGLTF.MeshUtility.MeshIntegrationResult>, List<GameObject>) Process(
GameObject target, IEnumerable<UniGLTF.MeshUtility.MeshIntegrationGroup> copyGroup)
{
_vrmInstance = go.GetComponent<VRMFirstPerson>();
_vrmInstance = target.GetComponent<VRMFirstPerson>();
if (_vrmInstance == null)
{
throw new ArgumentException();
@ -102,7 +103,7 @@ namespace VRM
throw new NotImplementedException();
// 必用?
var animator = go.GetComponent<Animator>();
var animator = target.GetComponent<Animator>();
var newAvatar = AvatarDescription.RecreateAvatar(animator);
animator.avatar = newAvatar;
}
@ -110,11 +111,11 @@ namespace VRM
// TODO: update: spring
// TODO: update: constraint
// TODO: update: firstPerson offset
var (list, newList) = base.Process(go, instance);
var (list, newList) = base.Process(target, copyGroup);
if (FreezeBlendShape || FreezeRotation || FreezeScaling)
{
var animator = go.GetComponent<Animator>();
var animator = target.GetComponent<Animator>();
var newAvatar = AvatarDescription.RecreateAvatar(animator);
animator.avatar = newAvatar;
}

View File

@ -10,7 +10,7 @@ namespace UniVRM10
public class Vrm10MeshUtility : UniGLTF.MeshUtility.GltfMeshUtility
{
bool _generateFirstPerson = false;
protected override IEnumerable<UniGLTF.MeshUtility.MeshIntegrationGroup> CopyInstantiate(GameObject go, GameObject instance)
public override IEnumerable<UniGLTF.MeshUtility.MeshIntegrationGroup> CopyInstantiate(GameObject go, GameObject instance)
{
var copy = new List<UniGLTF.MeshUtility.MeshIntegrationGroup>();
_generateFirstPerson = false;
@ -93,9 +93,10 @@ namespace UniVRM10
/// <summary>
/// glTF に比べて Humanoid や FirstPerson の処理が追加される
/// </summary>
public override (List<UniGLTF.MeshUtility.MeshIntegrationResult>, List<GameObject>) Process(GameObject go, GameObject instance)
public override (List<UniGLTF.MeshUtility.MeshIntegrationResult>, List<GameObject>) Process(
GameObject target, IEnumerable<UniGLTF.MeshUtility.MeshIntegrationGroup> groupCopy)
{
_vrmInstance = go.GetComponent<Vrm10Instance>();
_vrmInstance = target.GetComponent<Vrm10Instance>();
if (_vrmInstance == null)
{
throw new ArgumentException();
@ -106,7 +107,7 @@ namespace UniVRM10
throw new NotImplementedException();
// 必用?
var animator = go.GetComponent<Animator>();
var animator = target.GetComponent<Animator>();
var newAvatar = AvatarDescription.RecreateAvatar(animator);
animator.avatar = newAvatar;
}
@ -114,11 +115,11 @@ namespace UniVRM10
// TODO: update: spring
// TODO: update: constraint
// TODO: update: firstPerson offset
var (list, newList) = base.Process(go, instance);
var (list, newList) = base.Process(target, groupCopy);
if (FreezeBlendShape || FreezeRotation || FreezeScaling)
{
var animator = go.GetComponent<Animator>();
var animator = target.GetComponent<Animator>();
var newAvatar = AvatarDescription.RecreateAvatar(animator);
animator.avatar = newAvatar;
}