mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-13 05:54:59 -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 center;
|
||||
public float axisLength;
|
||||
private static string[] cashedHumanTraitBoneName = null;
|
||||
|
||||
public static BoneLimit From(HumanBone bone)
|
||||
{
|
||||
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,
|
||||
useDefaultValues = bone.limit.useDefaultValues,
|
||||
min = bone.limit.min,
|
||||
|
|
@ -36,7 +37,13 @@ namespace UniHumanoid
|
|||
|
||||
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())
|
||||
{
|
||||
|
|
@ -80,10 +87,28 @@ namespace UniHumanoid
|
|||
|
||||
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
|
||||
{
|
||||
skeleton = root.Traverse().Select(x => x.ToSkeletonBone()).ToArray(),
|
||||
human = human.Select(x => x.ToHumanBone()).ToArray(),
|
||||
skeleton = skeletonBones,
|
||||
human = humanBones,
|
||||
armStretch = armStretch,
|
||||
legStretch = legStretch,
|
||||
upperArmTwist = upperArmTwist,
|
||||
|
|
@ -154,7 +179,7 @@ namespace UniHumanoid
|
|||
return avatarDescription;
|
||||
}
|
||||
|
||||
public static AvatarDescription Create(AvatarDescription src=null)
|
||||
public static AvatarDescription Create(AvatarDescription src = null)
|
||||
{
|
||||
var avatarDescription = ScriptableObject.CreateInstance<AvatarDescription>();
|
||||
avatarDescription.name = "AvatarDescription";
|
||||
|
|
@ -178,6 +203,7 @@ namespace UniHumanoid
|
|||
avatarDescription.upperLegTwist = 0.5f;
|
||||
avatarDescription.lowerLegTwist = 0.5f;
|
||||
}
|
||||
|
||||
return avatarDescription;
|
||||
}
|
||||
|
||||
|
|
@ -235,8 +261,9 @@ namespace UniHumanoid
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -97,18 +97,17 @@ namespace VRM
|
|||
|
||||
public void TraverseRenderers(VRMImporterContext context = null)
|
||||
{
|
||||
Renderers = Traverse(transform)
|
||||
.Select(x => x.GetComponent<Renderer>())
|
||||
.Where(x => x != null)
|
||||
.Select(x => new RendererFirstPersonFlags
|
||||
var rendererComponents = transform.GetComponentsInChildren<Renderer>();
|
||||
foreach (var renderer in rendererComponents)
|
||||
{
|
||||
Renderers.Add(new RendererFirstPersonFlags
|
||||
{
|
||||
Renderer = x,
|
||||
Renderer = renderer,
|
||||
FirstPersonFlag = context == null
|
||||
? FirstPersonFlag.Auto
|
||||
: GetFirstPersonFlag(context, x)
|
||||
})
|
||||
.ToList()
|
||||
;
|
||||
: GetFirstPersonFlag(context, renderer)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static FirstPersonFlag GetFirstPersonFlag(VRMImporterContext context, Renderer r)
|
||||
|
|
|
|||
|
|
@ -75,6 +75,7 @@ namespace VRM
|
|||
{
|
||||
return EnumUtil.TryParseOrDefault<VRMBone>(human.ToString(), VRMBone.unknown);
|
||||
}
|
||||
|
||||
public static HumanBodyBones ToHumanBodyBone(this VRMBone bone)
|
||||
{
|
||||
#if UNITY_5_6_OR_NEWER
|
||||
|
|
@ -92,7 +93,8 @@ namespace VRM
|
|||
[JsonSchema(Title = "vrm.humanoid.bone")]
|
||||
public class glTF_VRM_HumanoidBone : JsonSerializableBase
|
||||
{
|
||||
[JsonSchema(Description = "Human bone name.", EnumValues = new object[] {
|
||||
[JsonSchema(Description = "Human bone name.", EnumValues = new object[]
|
||||
{
|
||||
"hips",
|
||||
"leftUpperLeg",
|
||||
"rightUpperLeg",
|
||||
|
|
@ -148,8 +150,9 @@ namespace VRM
|
|||
"rightLittleIntermediate",
|
||||
"rightLittleDistal",
|
||||
"upperChest",
|
||||
}, EnumSerializationType =EnumSerializationType.AsString)]
|
||||
}, EnumSerializationType = EnumSerializationType.AsString)]
|
||||
public string bone;
|
||||
|
||||
public VRMBone vrmBone
|
||||
{
|
||||
set
|
||||
|
|
@ -183,7 +186,7 @@ namespace VRM
|
|||
|
||||
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(() => useDefaultValues);
|
||||
if (!useDefaultValues)
|
||||
|
|
@ -281,6 +284,7 @@ namespace VRM
|
|||
};
|
||||
humanBones.Add(found);
|
||||
}
|
||||
|
||||
found.node = nodes.FindIndex(y => y.name == x.boneName);
|
||||
|
||||
found.useDefaultValues = x.useDefaultValues;
|
||||
|
|
@ -301,9 +305,13 @@ namespace VRM
|
|||
description.armStretch = armStretch;
|
||||
description.legStretch = legStretch;
|
||||
description.hasTranslationDoF = hasTranslationDoF;
|
||||
description.human = humanBones
|
||||
.Where(x => x.node >= 0 && x.node < nodes.Count)
|
||||
.Select(x => new UniHumanoid.BoneLimit
|
||||
|
||||
var boneLimits = new UniHumanoid.BoneLimit[humanBones.Count];
|
||||
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,
|
||||
useDefaultValues = x.useDefaultValues,
|
||||
|
|
@ -312,10 +320,13 @@ namespace VRM
|
|||
min = x.min,
|
||||
max = x.max,
|
||||
humanBone = x.vrmBone.ToHumanBodyBone(),
|
||||
})
|
||||
.Where(x => x.humanBone != HumanBodyBones.LastBone)
|
||||
.ToArray();
|
||||
};
|
||||
index++;
|
||||
}
|
||||
|
||||
description.human = boneLimits;
|
||||
|
||||
return description;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ using System.IO;
|
|||
using System.Text;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
@ -15,7 +16,9 @@ namespace VRM
|
|||
public static class VRMSpringUtility
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
|
||||
#region save
|
||||
|
||||
[MenuItem(VRMVersion.MENU + "/SaveSpringBoneToJSON", validate = true)]
|
||||
static bool SaveSpringBoneToJSONIsEnable()
|
||||
{
|
||||
|
|
@ -38,10 +41,10 @@ namespace VRM
|
|||
static void SaveSpringBoneToJSON()
|
||||
{
|
||||
var path = EditorUtility.SaveFilePanel(
|
||||
"Save spring to json",
|
||||
null,
|
||||
"VRMSpring.json",
|
||||
"json");
|
||||
"Save spring to json",
|
||||
null,
|
||||
"VRMSpring.json",
|
||||
"json");
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return;
|
||||
|
|
@ -54,7 +57,7 @@ namespace VRM
|
|||
ExportSecondary(root, nodes,
|
||||
spring.colliderGroups.Add,
|
||||
spring.boneGroups.Add
|
||||
);
|
||||
);
|
||||
|
||||
File.WriteAllText(path, spring.ToJson());
|
||||
}
|
||||
|
|
@ -62,6 +65,7 @@ namespace VRM
|
|||
#endregion
|
||||
|
||||
#region load
|
||||
|
||||
[MenuItem(VRMVersion.MENU + "/LoadSpringBoneFromJSON", true)]
|
||||
static bool LoadSpringBoneFromJSONIsEnable()
|
||||
{
|
||||
|
|
@ -84,9 +88,9 @@ namespace VRM
|
|||
static void LoadSpringBoneFromJSON()
|
||||
{
|
||||
var path = EditorUtility.OpenFilePanel(
|
||||
"Load spring from json",
|
||||
null,
|
||||
"json");
|
||||
"Load spring from json",
|
||||
null,
|
||||
"json");
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return;
|
||||
|
|
@ -101,7 +105,9 @@ namespace VRM
|
|||
|
||||
LoadSecondary(root, nodes, spring);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endif
|
||||
|
||||
public static void ExportSecondary(Transform root, List<Transform> nodes,
|
||||
|
|
@ -127,7 +133,6 @@ namespace VRM
|
|||
offset = x.Offset,
|
||||
radius = x.Radius,
|
||||
};
|
||||
|
||||
}).ToList();
|
||||
|
||||
addSecondaryColliderGroup(colliderGroup);
|
||||
|
|
@ -166,10 +171,29 @@ namespace VRM
|
|||
}
|
||||
|
||||
// clear components
|
||||
var remove = root.Traverse()
|
||||
.SelectMany(x => x.GetComponents<Component>())
|
||||
.Where(x => x is VRMSpringBone || x is VRMSpringBoneColliderGroup)
|
||||
.ToArray();
|
||||
var vrmSpringBones = root.GetComponentsInChildren<VRMSpringBone>();
|
||||
var vrmSpringBoneColliderGroup = root.GetComponentsInChildren<VRMSpringBoneColliderGroup>();
|
||||
|
||||
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)
|
||||
{
|
||||
if (Application.isPlaying)
|
||||
|
|
@ -207,17 +231,33 @@ namespace VRM
|
|||
{
|
||||
vrmBoneGroup.m_center = nodes[boneGroup.center];
|
||||
}
|
||||
|
||||
vrmBoneGroup.m_comment = boneGroup.comment;
|
||||
vrmBoneGroup.m_dragForce = boneGroup.dragForce;
|
||||
vrmBoneGroup.m_gravityDir = boneGroup.gravityDir;
|
||||
vrmBoneGroup.m_gravityPower = boneGroup.gravityPower;
|
||||
vrmBoneGroup.m_hitRadius = boneGroup.hitRadius;
|
||||
vrmBoneGroup.m_stiffnessForce = boneGroup.stiffiness;
|
||||
|
||||
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
|
||||
|
|
@ -225,6 +265,5 @@ namespace VRM
|
|||
secondary.gameObject.AddComponent<VRMSpringBone>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user