mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-06-02 22:14:30 -05:00
Merge pull request #413 from Kohei-Yanagida/suppress_gc_due_to_using_LINQ
Suppress gabage collection due to using linq
This commit is contained in:
commit
ab4cf96cda
|
|
@ -19,12 +19,13 @@ namespace UniHumanoid
|
||||||
public Vector3 max;
|
public Vector3 max;
|
||||||
public Vector3 center;
|
public Vector3 center;
|
||||||
public float axisLength;
|
public float axisLength;
|
||||||
|
private static string[] cashedHumanTraitBoneName = null;
|
||||||
|
|
||||||
public static BoneLimit From(HumanBone bone)
|
public static BoneLimit From(HumanBone bone)
|
||||||
{
|
{
|
||||||
return new BoneLimit
|
return new BoneLimit
|
||||||
{
|
{
|
||||||
humanBone = (HumanBodyBones)Enum.Parse(typeof(HumanBodyBones), bone.humanName.Replace(" ", ""), true),
|
humanBone = (HumanBodyBones) Enum.Parse(typeof(HumanBodyBones), bone.humanName.Replace(" ", ""), true),
|
||||||
boneName = bone.boneName,
|
boneName = bone.boneName,
|
||||||
useDefaultValues = bone.limit.useDefaultValues,
|
useDefaultValues = bone.limit.useDefaultValues,
|
||||||
min = bone.limit.min,
|
min = bone.limit.min,
|
||||||
|
|
@ -36,7 +37,13 @@ namespace UniHumanoid
|
||||||
|
|
||||||
public static String ToHumanBoneName(HumanBodyBones b)
|
public static String ToHumanBoneName(HumanBodyBones b)
|
||||||
{
|
{
|
||||||
foreach (var x in HumanTrait.BoneName)
|
// 呼び出し毎にGCが発生するのでキャッシュする
|
||||||
|
if (cashedHumanTraitBoneName == null)
|
||||||
|
{
|
||||||
|
cashedHumanTraitBoneName = HumanTrait.BoneName;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var x in cashedHumanTraitBoneName)
|
||||||
{
|
{
|
||||||
if (x.Replace(" ", "") == b.ToString())
|
if (x.Replace(" ", "") == b.ToString())
|
||||||
{
|
{
|
||||||
|
|
@ -80,10 +87,28 @@ namespace UniHumanoid
|
||||||
|
|
||||||
public HumanDescription ToHumanDescription(Transform root)
|
public HumanDescription ToHumanDescription(Transform root)
|
||||||
{
|
{
|
||||||
|
var transforms = root.GetComponentsInChildren<Transform>();
|
||||||
|
var skeletonBones = new SkeletonBone[transforms.Length];
|
||||||
|
var index = 0;
|
||||||
|
foreach (var t in transforms)
|
||||||
|
{
|
||||||
|
skeletonBones[index] = t.ToSkeletonBone();
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
var humanBones = new HumanBone[human.Length];
|
||||||
|
index = 0;
|
||||||
|
foreach (var bonelimit in human)
|
||||||
|
{
|
||||||
|
humanBones[index] = bonelimit.ToHumanBone();
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return new HumanDescription
|
return new HumanDescription
|
||||||
{
|
{
|
||||||
skeleton = root.Traverse().Select(x => x.ToSkeletonBone()).ToArray(),
|
skeleton = skeletonBones,
|
||||||
human = human.Select(x => x.ToHumanBone()).ToArray(),
|
human = humanBones,
|
||||||
armStretch = armStretch,
|
armStretch = armStretch,
|
||||||
legStretch = legStretch,
|
legStretch = legStretch,
|
||||||
upperArmTwist = upperArmTwist,
|
upperArmTwist = upperArmTwist,
|
||||||
|
|
@ -154,7 +179,7 @@ namespace UniHumanoid
|
||||||
return avatarDescription;
|
return avatarDescription;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AvatarDescription Create(AvatarDescription src=null)
|
public static AvatarDescription Create(AvatarDescription src = null)
|
||||||
{
|
{
|
||||||
var avatarDescription = ScriptableObject.CreateInstance<AvatarDescription>();
|
var avatarDescription = ScriptableObject.CreateInstance<AvatarDescription>();
|
||||||
avatarDescription.name = "AvatarDescription";
|
avatarDescription.name = "AvatarDescription";
|
||||||
|
|
@ -178,6 +203,7 @@ namespace UniHumanoid
|
||||||
avatarDescription.upperLegTwist = 0.5f;
|
avatarDescription.upperLegTwist = 0.5f;
|
||||||
avatarDescription.lowerLegTwist = 0.5f;
|
avatarDescription.lowerLegTwist = 0.5f;
|
||||||
}
|
}
|
||||||
|
|
||||||
return avatarDescription;
|
return avatarDescription;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -235,8 +261,9 @@ namespace UniHumanoid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -97,18 +97,17 @@ namespace VRM
|
||||||
|
|
||||||
public void TraverseRenderers(VRMImporterContext context = null)
|
public void TraverseRenderers(VRMImporterContext context = null)
|
||||||
{
|
{
|
||||||
Renderers = Traverse(transform)
|
var rendererComponents = transform.GetComponentsInChildren<Renderer>();
|
||||||
.Select(x => x.GetComponent<Renderer>())
|
foreach (var renderer in rendererComponents)
|
||||||
.Where(x => x != null)
|
{
|
||||||
.Select(x => new RendererFirstPersonFlags
|
Renderers.Add(new RendererFirstPersonFlags
|
||||||
{
|
{
|
||||||
Renderer = x,
|
Renderer = renderer,
|
||||||
FirstPersonFlag = context == null
|
FirstPersonFlag = context == null
|
||||||
? FirstPersonFlag.Auto
|
? FirstPersonFlag.Auto
|
||||||
: GetFirstPersonFlag(context, x)
|
: GetFirstPersonFlag(context, renderer)
|
||||||
})
|
});
|
||||||
.ToList()
|
}
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static FirstPersonFlag GetFirstPersonFlag(VRMImporterContext context, Renderer r)
|
static FirstPersonFlag GetFirstPersonFlag(VRMImporterContext context, Renderer r)
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,7 @@ namespace VRM
|
||||||
{
|
{
|
||||||
return EnumUtil.TryParseOrDefault<VRMBone>(human.ToString(), VRMBone.unknown);
|
return EnumUtil.TryParseOrDefault<VRMBone>(human.ToString(), VRMBone.unknown);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HumanBodyBones ToHumanBodyBone(this VRMBone bone)
|
public static HumanBodyBones ToHumanBodyBone(this VRMBone bone)
|
||||||
{
|
{
|
||||||
#if UNITY_5_6_OR_NEWER
|
#if UNITY_5_6_OR_NEWER
|
||||||
|
|
@ -92,7 +93,8 @@ namespace VRM
|
||||||
[JsonSchema(Title = "vrm.humanoid.bone")]
|
[JsonSchema(Title = "vrm.humanoid.bone")]
|
||||||
public class glTF_VRM_HumanoidBone : JsonSerializableBase
|
public class glTF_VRM_HumanoidBone : JsonSerializableBase
|
||||||
{
|
{
|
||||||
[JsonSchema(Description = "Human bone name.", EnumValues = new object[] {
|
[JsonSchema(Description = "Human bone name.", EnumValues = new object[]
|
||||||
|
{
|
||||||
"hips",
|
"hips",
|
||||||
"leftUpperLeg",
|
"leftUpperLeg",
|
||||||
"rightUpperLeg",
|
"rightUpperLeg",
|
||||||
|
|
@ -148,8 +150,9 @@ namespace VRM
|
||||||
"rightLittleIntermediate",
|
"rightLittleIntermediate",
|
||||||
"rightLittleDistal",
|
"rightLittleDistal",
|
||||||
"upperChest",
|
"upperChest",
|
||||||
}, EnumSerializationType =EnumSerializationType.AsString)]
|
}, EnumSerializationType = EnumSerializationType.AsString)]
|
||||||
public string bone;
|
public string bone;
|
||||||
|
|
||||||
public VRMBone vrmBone
|
public VRMBone vrmBone
|
||||||
{
|
{
|
||||||
set
|
set
|
||||||
|
|
@ -183,7 +186,7 @@ namespace VRM
|
||||||
|
|
||||||
protected override void SerializeMembers(GLTFJsonFormatter f)
|
protected override void SerializeMembers(GLTFJsonFormatter f)
|
||||||
{
|
{
|
||||||
f.Key("bone"); f.Value((string)bone.ToString());
|
f.Key("bone"); f.Value((string) bone.ToString());
|
||||||
f.KeyValue(() => node);
|
f.KeyValue(() => node);
|
||||||
f.KeyValue(() => useDefaultValues);
|
f.KeyValue(() => useDefaultValues);
|
||||||
if (!useDefaultValues)
|
if (!useDefaultValues)
|
||||||
|
|
@ -281,6 +284,7 @@ namespace VRM
|
||||||
};
|
};
|
||||||
humanBones.Add(found);
|
humanBones.Add(found);
|
||||||
}
|
}
|
||||||
|
|
||||||
found.node = nodes.FindIndex(y => y.name == x.boneName);
|
found.node = nodes.FindIndex(y => y.name == x.boneName);
|
||||||
|
|
||||||
found.useDefaultValues = x.useDefaultValues;
|
found.useDefaultValues = x.useDefaultValues;
|
||||||
|
|
@ -301,9 +305,13 @@ namespace VRM
|
||||||
description.armStretch = armStretch;
|
description.armStretch = armStretch;
|
||||||
description.legStretch = legStretch;
|
description.legStretch = legStretch;
|
||||||
description.hasTranslationDoF = hasTranslationDoF;
|
description.hasTranslationDoF = hasTranslationDoF;
|
||||||
description.human = humanBones
|
|
||||||
.Where(x => x.node >= 0 && x.node < nodes.Count)
|
var boneLimits = new UniHumanoid.BoneLimit[humanBones.Count];
|
||||||
.Select(x => new UniHumanoid.BoneLimit
|
int index = 0;
|
||||||
|
foreach (var x in humanBones)
|
||||||
|
{
|
||||||
|
if (x.node < 0 || x.node >= nodes.Count) continue;
|
||||||
|
boneLimits[index] = new UniHumanoid.BoneLimit
|
||||||
{
|
{
|
||||||
boneName = nodes[x.node].name,
|
boneName = nodes[x.node].name,
|
||||||
useDefaultValues = x.useDefaultValues,
|
useDefaultValues = x.useDefaultValues,
|
||||||
|
|
@ -312,10 +320,13 @@ namespace VRM
|
||||||
min = x.min,
|
min = x.min,
|
||||||
max = x.max,
|
max = x.max,
|
||||||
humanBone = x.vrmBone.ToHumanBodyBone(),
|
humanBone = x.vrmBone.ToHumanBodyBone(),
|
||||||
})
|
};
|
||||||
.Where(x => x.humanBone != HumanBodyBones.LastBone)
|
index++;
|
||||||
.ToArray();
|
}
|
||||||
|
|
||||||
|
description.human = boneLimits;
|
||||||
|
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -15,7 +16,9 @@ namespace VRM
|
||||||
public static class VRMSpringUtility
|
public static class VRMSpringUtility
|
||||||
{
|
{
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
|
|
||||||
#region save
|
#region save
|
||||||
|
|
||||||
[MenuItem(VRMVersion.MENU + "/SaveSpringBoneToJSON", validate = true)]
|
[MenuItem(VRMVersion.MENU + "/SaveSpringBoneToJSON", validate = true)]
|
||||||
static bool SaveSpringBoneToJSONIsEnable()
|
static bool SaveSpringBoneToJSONIsEnable()
|
||||||
{
|
{
|
||||||
|
|
@ -38,10 +41,10 @@ namespace VRM
|
||||||
static void SaveSpringBoneToJSON()
|
static void SaveSpringBoneToJSON()
|
||||||
{
|
{
|
||||||
var path = EditorUtility.SaveFilePanel(
|
var path = EditorUtility.SaveFilePanel(
|
||||||
"Save spring to json",
|
"Save spring to json",
|
||||||
null,
|
null,
|
||||||
"VRMSpring.json",
|
"VRMSpring.json",
|
||||||
"json");
|
"json");
|
||||||
if (string.IsNullOrEmpty(path))
|
if (string.IsNullOrEmpty(path))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
|
@ -54,7 +57,7 @@ namespace VRM
|
||||||
ExportSecondary(root, nodes,
|
ExportSecondary(root, nodes,
|
||||||
spring.colliderGroups.Add,
|
spring.colliderGroups.Add,
|
||||||
spring.boneGroups.Add
|
spring.boneGroups.Add
|
||||||
);
|
);
|
||||||
|
|
||||||
File.WriteAllText(path, spring.ToJson());
|
File.WriteAllText(path, spring.ToJson());
|
||||||
}
|
}
|
||||||
|
|
@ -62,6 +65,7 @@ namespace VRM
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region load
|
#region load
|
||||||
|
|
||||||
[MenuItem(VRMVersion.MENU + "/LoadSpringBoneFromJSON", true)]
|
[MenuItem(VRMVersion.MENU + "/LoadSpringBoneFromJSON", true)]
|
||||||
static bool LoadSpringBoneFromJSONIsEnable()
|
static bool LoadSpringBoneFromJSONIsEnable()
|
||||||
{
|
{
|
||||||
|
|
@ -84,9 +88,9 @@ namespace VRM
|
||||||
static void LoadSpringBoneFromJSON()
|
static void LoadSpringBoneFromJSON()
|
||||||
{
|
{
|
||||||
var path = EditorUtility.OpenFilePanel(
|
var path = EditorUtility.OpenFilePanel(
|
||||||
"Load spring from json",
|
"Load spring from json",
|
||||||
null,
|
null,
|
||||||
"json");
|
"json");
|
||||||
if (string.IsNullOrEmpty(path))
|
if (string.IsNullOrEmpty(path))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
|
@ -101,7 +105,9 @@ namespace VRM
|
||||||
|
|
||||||
LoadSecondary(root, nodes, spring);
|
LoadSecondary(root, nodes, spring);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public static void ExportSecondary(Transform root, List<Transform> nodes,
|
public static void ExportSecondary(Transform root, List<Transform> nodes,
|
||||||
|
|
@ -127,7 +133,6 @@ namespace VRM
|
||||||
offset = x.Offset,
|
offset = x.Offset,
|
||||||
radius = x.Radius,
|
radius = x.Radius,
|
||||||
};
|
};
|
||||||
|
|
||||||
}).ToList();
|
}).ToList();
|
||||||
|
|
||||||
addSecondaryColliderGroup(colliderGroup);
|
addSecondaryColliderGroup(colliderGroup);
|
||||||
|
|
@ -166,10 +171,29 @@ namespace VRM
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear components
|
// clear components
|
||||||
var remove = root.Traverse()
|
var vrmSpringBones = root.GetComponentsInChildren<VRMSpringBone>();
|
||||||
.SelectMany(x => x.GetComponents<Component>())
|
var vrmSpringBoneColliderGroup = root.GetComponentsInChildren<VRMSpringBoneColliderGroup>();
|
||||||
.Where(x => x is VRMSpringBone || x is VRMSpringBoneColliderGroup)
|
|
||||||
.ToArray();
|
var length = (vrmSpringBones?.Length ?? 0) + (vrmSpringBoneColliderGroup?.Length ?? 0);
|
||||||
|
var remove = new Component[length];
|
||||||
|
|
||||||
|
var index = 0;
|
||||||
|
if (vrmSpringBones != null)
|
||||||
|
{
|
||||||
|
foreach (var vrmSpringBone in vrmSpringBones)
|
||||||
|
{
|
||||||
|
remove[index++] = vrmSpringBone;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vrmSpringBoneColliderGroup != null)
|
||||||
|
{
|
||||||
|
foreach (var vrmSpringBoneCollider in vrmSpringBoneColliderGroup)
|
||||||
|
{
|
||||||
|
remove[index++] = vrmSpringBoneCollider;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var x in remove)
|
foreach (var x in remove)
|
||||||
{
|
{
|
||||||
if (Application.isPlaying)
|
if (Application.isPlaying)
|
||||||
|
|
@ -207,17 +231,33 @@ namespace VRM
|
||||||
{
|
{
|
||||||
vrmBoneGroup.m_center = nodes[boneGroup.center];
|
vrmBoneGroup.m_center = nodes[boneGroup.center];
|
||||||
}
|
}
|
||||||
|
|
||||||
vrmBoneGroup.m_comment = boneGroup.comment;
|
vrmBoneGroup.m_comment = boneGroup.comment;
|
||||||
vrmBoneGroup.m_dragForce = boneGroup.dragForce;
|
vrmBoneGroup.m_dragForce = boneGroup.dragForce;
|
||||||
vrmBoneGroup.m_gravityDir = boneGroup.gravityDir;
|
vrmBoneGroup.m_gravityDir = boneGroup.gravityDir;
|
||||||
vrmBoneGroup.m_gravityPower = boneGroup.gravityPower;
|
vrmBoneGroup.m_gravityPower = boneGroup.gravityPower;
|
||||||
vrmBoneGroup.m_hitRadius = boneGroup.hitRadius;
|
vrmBoneGroup.m_hitRadius = boneGroup.hitRadius;
|
||||||
vrmBoneGroup.m_stiffnessForce = boneGroup.stiffiness;
|
vrmBoneGroup.m_stiffnessForce = boneGroup.stiffiness;
|
||||||
|
|
||||||
if (boneGroup.colliderGroups != null && boneGroup.colliderGroups.Any())
|
if (boneGroup.colliderGroups != null && boneGroup.colliderGroups.Any())
|
||||||
{
|
{
|
||||||
vrmBoneGroup.ColliderGroups = boneGroup.colliderGroups.Select(x => colliders[x]).ToArray();
|
var colliderGroups = new VRMSpringBoneColliderGroup[boneGroup.colliderGroups.Length];
|
||||||
|
int i = 0;
|
||||||
|
foreach (var colliderGroup in boneGroup.colliderGroups)
|
||||||
|
{
|
||||||
|
colliderGroups[i] = colliders[colliderGroup];
|
||||||
|
}
|
||||||
|
|
||||||
|
vrmBoneGroup.ColliderGroups = colliderGroups;
|
||||||
}
|
}
|
||||||
vrmBoneGroup.RootBones = boneGroup.bones.Select(x => nodes[x]).ToList();
|
|
||||||
|
var boneList = new List<Transform>();
|
||||||
|
foreach (var x in boneGroup.bones)
|
||||||
|
{
|
||||||
|
boneList.Add(nodes[x]);
|
||||||
|
}
|
||||||
|
|
||||||
|
vrmBoneGroup.RootBones = boneList;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -225,6 +265,5 @@ namespace VRM
|
||||||
secondary.gameObject.AddComponent<VRMSpringBone>();
|
secondary.gameObject.AddComponent<VRMSpringBone>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user