private void AddDynamicBone(VRMSpringBone springBone, Transform Root) { var dynamicbone = springBone.gameObject.AddComponent <DynamicBone>(); dynamicbone.m_Root = Root; dynamicbone.m_UpdateRate = Params.UpdateRate; dynamicbone.m_UpdateMode = Params.UpdateMode; if (Params.Gravity_Option) { dynamicbone.m_Gravity = springBone.m_gravityPower * springBone.m_gravityDir; } else { dynamicbone.m_Gravity = Vector3.zero; } dynamicbone.m_Elasticity = springBone.m_stiffnessForce * 0.25f; dynamicbone.m_Stiffness = Params.Stiffness; dynamicbone.m_Force = Params.Force; dynamicbone.m_Damping = springBone.m_dragForce * 0.6f; dynamicbone.m_Radius = springBone.m_hitRadius; dynamicbone.m_Colliders = new List <DynamicBoneColliderBase>(); foreach (var colliderGroup in springBone.ColliderGroups) { DynamicBoneCollider[] colliders = colliderGroup.gameObject.GetComponents <DynamicBoneCollider>(); if (colliders != null) { dynamicbone.m_Colliders.AddRange(colliders); } } }
/// <summary> /// 指定された<see cref="VRMSpringBone"/>を基に<see cref="DynamicBone"/>を設定します。 /// </summary> /// <param name="springBone"></param> /// <param name="dynamicBoneColliderGroups">キーに<see cref="VRMSpringBoneColliderGroup"/>、値に対応する<see cref="DynamicBoneCollider"/>のリストを持つジャグ配列。</param> /// <param name="swayingParametersConverter"></param> private static void ConvertVRMSpringBone( VRMSpringBone springBone, IDictionary <VRMSpringBoneColliderGroup, IEnumerable <MonoBehaviour> > dynamicBoneColliderGroups, Converter.SwayingParametersConverter swayingParametersConverter ) { var springBoneParameters = new SpringBoneParameters(stiffnessForce: springBone.m_stiffnessForce, dragForce: springBone.m_dragForce); var boneInfo = new BoneInfo(vrmMeta: springBone.gameObject.GetComponentsInParent <VRMMeta>()[0]); foreach (var transform in springBone.RootBones) { var dynamicBone = springBone.gameObject.AddComponent(DynamicBoneType); DynamicBoneType.GetField("m_Root").SetValue(dynamicBone, transform); DynamicBoneType.GetField("m_Exclusions").SetValue(dynamicBone, new List <Transform>()); DynamicBoneParameters dynamicBoneParameters = null; if (swayingParametersConverter != null) { dynamicBoneParameters = swayingParametersConverter( springBoneParameters: springBoneParameters, boneInfo: boneInfo ); } if (dynamicBoneParameters != null) { DynamicBoneType.GetField("m_Damping").SetValue(dynamicBone, dynamicBoneParameters.Damping); DynamicBoneType.GetField("m_DampingDistrib") .SetValue(dynamicBone, dynamicBoneParameters.DampingDistrib); DynamicBoneType.GetField("m_Elasticity").SetValue(dynamicBone, dynamicBoneParameters.Elasticity); DynamicBoneType.GetField("m_ElasticityDistrib") .SetValue(dynamicBone, dynamicBoneParameters.ElasticityDistrib); DynamicBoneType.GetField("m_Stiffness").SetValue(dynamicBone, dynamicBoneParameters.Stiffness); DynamicBoneType.GetField("m_StiffnessDistrib") .SetValue(dynamicBone, dynamicBoneParameters.StiffnessDistrib); DynamicBoneType.GetField("m_Inert").SetValue(dynamicBone, dynamicBoneParameters.Inert); DynamicBoneType.GetField("m_InertDistrib") .SetValue(dynamicBone, dynamicBoneParameters.InertDistrib); } DynamicBoneType.GetField("m_Gravity") .SetValue(dynamicBone, springBone.m_gravityDir * springBone.m_gravityPower); DynamicBoneType.GetField("m_Radius").SetValue(dynamicBone, springBone.m_hitRadius); if (dynamicBoneColliderGroups != null) { var colliders = Activator.CreateInstance(type: DynamicBoneColliderBaseListType); MethodInfo addMethod = DynamicBoneColliderBaseListType.GetMethod("Add"); foreach (var collider in springBone.ColliderGroups.SelectMany( colliderGroup => dynamicBoneColliderGroups[colliderGroup] )) { addMethod.Invoke(colliders, new[] { collider }); } DynamicBoneType.GetField("m_Colliders").SetValue(dynamicBone, colliders); } } }
/// <summary> /// 指定された<see cref="VRMSpringBone"/>を基に<see cref="DynamicBone"/>を設定します。 /// </summary> /// <param name="springBone"></param> /// <param name="dynamicBoneColliderGroups">キーに<see cref="VRMSpringBoneColliderGroup"/>、値に対応する<see cref="DynamicBoneCollider"/>のリストを持つジャグ配列。</param> /// <param name="swayingParametersConverter"></param> private static void ConvertVRMSpringBone( VRMSpringBone springBone, IDictionary <VRMSpringBoneColliderGroup, IEnumerable <dynamic> > dynamicBoneColliderGroups, Converter.SwayingParametersConverter swayingParametersConverter ) { var springBoneParameters = new SpringBoneParameters() { StiffnessForce = springBone.m_stiffnessForce, DragForce = springBone.m_dragForce, }; var boneInfo = new BoneInfo(vrmMeta: springBone.gameObject.GetComponentsInParent <VRMMeta>()[0]); foreach (var transform in springBone.RootBones) { dynamic dynamicBone = springBone.gameObject.AddComponent(DynamicBoneType); dynamicBone.m_Root = transform; dynamicBone.m_Exclusions = new List <Transform>(); DynamicBoneParameters dynamicBoneParameters = null; if (swayingParametersConverter != null) { dynamicBoneParameters = swayingParametersConverter( springBoneParameters: springBoneParameters, boneInfo: boneInfo ); } if (dynamicBoneParameters != null) { dynamicBone.m_Damping = dynamicBoneParameters.Damping; dynamicBone.m_DampingDistrib = dynamicBoneParameters.DampingDistrib; dynamicBone.m_Elasticity = dynamicBoneParameters.Elasticity; dynamicBone.m_ElasticityDistrib = dynamicBoneParameters.ElasticityDistrib; dynamicBone.m_Stiffness = dynamicBoneParameters.Stiffness; dynamicBone.m_StiffnessDistrib = dynamicBoneParameters.StiffnessDistrib; dynamicBone.m_Inert = dynamicBoneParameters.Inert; dynamicBone.m_InertDistrib = dynamicBoneParameters.InertDistrib; } dynamicBone.m_Gravity = springBone.m_gravityDir * springBone.m_gravityPower; dynamicBone.m_Radius = springBone.m_hitRadius; if (dynamicBoneColliderGroups != null) { dynamic colliders = Activator.CreateInstance(type: DynamicBoneColliderBaseListType); foreach (var collider in springBone.ColliderGroups.SelectMany( colliderGroup => dynamicBoneColliderGroups[colliderGroup] )) { colliders.Add(collider); } dynamicBone.m_Colliders = colliders; } } }
public static SerializableSpringBone FromSpringBone(VRMSpringBone bone) { Debug.Log($"Convert SpringBone to serializable: Root={bone.RootBones[0].name}"); var serializableBone = new SerializableSpringBone(); serializableBone.AttachedTo = HierarchyPath.FromTransform(bone.gameObject.transform); // Copy values serializableBone.m_comment = bone.m_comment; serializableBone.m_stiffnessForce = bone.m_stiffnessForce; serializableBone.m_gravityPower = bone.m_gravityPower; serializableBone.m_gravityDir = bone.m_gravityDir; serializableBone.m_dragForce = bone.m_dragForce; if (bone.m_center != null) { serializableBone.m_center = HierarchyPath.FromTransform(bone.m_center); } if (bone.RootBones != null) { foreach (var rootBone in bone.RootBones) { serializableBone.RootBones.Add(HierarchyPath.FromTransform(rootBone)); } } serializableBone.m_hitRadius = bone.m_hitRadius; if (bone.ColliderGroups != null) { foreach (var colliderGroup in bone.ColliderGroups) { serializableBone.ColliderGroups.Add(HierarchyPath.FromTransform(colliderGroup.transform)); } } return(serializableBone); }
/// <summary> /// VRMSpringBone、およびVRMSpringBoneColliderGroupをコピーします。 /// </summary> /// <param name="sourceSpringBone"></param> /// <param name="destination"></param> /// <param name="sourceSkeletonBones"></param> /// <param name="transformMapping"></param> /// <returns>更新された <c>transformMapping</c> を返します。</returns> private static IDictionary <Transform, Transform> CopySpringBone( VRMSpringBone sourceSpringBone, GameObject destination, Dictionary <HumanBodyBones, Transform> sourceSkeletonBones, IDictionary <Transform, Transform> transformMapping ) { var destinationSecondary = destination.transform.Find("secondary").gameObject; ComponentUtility.CopyComponent(sourceSpringBone); ComponentUtility.PasteComponentAsNew(destinationSecondary); var destinationSpringBone = destinationSecondary.GetComponents <VRMSpringBone>().Last(); if (destinationSpringBone.m_center) { destinationSpringBone.m_center = transformMapping.ContainsKey(destinationSpringBone.m_center) ? transformMapping[destinationSpringBone.m_center] : (transformMapping[destinationSpringBone.m_center] = BoneMapper.FindCorrespondingBone( sourceBone: destinationSpringBone.m_center, source: sourceSpringBone.transform.root.gameObject, destination, sourceSkeletonBones )); } for (var i = 0; i < destinationSpringBone.RootBones.Count; i++) { var sourceSpringBoneRoot = destinationSpringBone.RootBones[i]; destinationSpringBone.RootBones[i] = sourceSpringBoneRoot ? (transformMapping.ContainsKey(sourceSpringBoneRoot) ? transformMapping[sourceSpringBoneRoot] : (transformMapping[sourceSpringBoneRoot] = BoneMapper.FindCorrespondingBone( sourceBone: sourceSpringBoneRoot, source: sourceSpringBone.transform.root.gameObject, destination, sourceSkeletonBones ))) : null; } for (var i = 0; i < destinationSpringBone.ColliderGroups.Length; i++) { var sourceColliderGroup = destinationSpringBone.ColliderGroups[i]; var destinationColliderGroupTransform = sourceColliderGroup ? (transformMapping.ContainsKey(sourceColliderGroup.transform) ? transformMapping[sourceColliderGroup.transform] : (transformMapping[sourceColliderGroup.transform] = BoneMapper.FindCorrespondingBone( sourceBone: sourceColliderGroup.transform, source: sourceSpringBone.transform.root.gameObject, destination, sourceSkeletonBones ))) : null; VRMSpringBoneColliderGroup destinationColliderGroup = null; if (destinationColliderGroupTransform) { CopyVRMSpringBones.CopySpringBoneColliderGroups( sourceBone: sourceColliderGroup.transform, destinationBone: destinationColliderGroupTransform ); destinationColliderGroup = destinationColliderGroupTransform.GetComponent <VRMSpringBoneColliderGroup>(); } destinationSpringBone.ColliderGroups[i] = destinationColliderGroup; } return(transformMapping); }