Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UniGLTF;
- namespace VRM.DynamicBoneExtension
- {
- public class DynamicBoneLoadUtility {
- public static void LoadSecondary(Transform root, List<Transform> nodes,
- glTF_VRM_SecondaryAnimation secondaryAnimation)
- {
- // clear components
- var remove = root.Traverse()
- .SelectMany(x => x.GetComponents<Component>())
- .Where(x => x is VRMSpringBone || x is VRMSpringBoneColliderGroup
- || x is DynamicBone || x is DynamicBoneCollider || x is DynamicBonePlaneCollider)
- .ToArray();
- foreach (var x in remove)
- {
- if (Application.isPlaying)
- {
- GameObject.Destroy(x);
- }
- else
- {
- GameObject.DestroyImmediate(x);
- }
- }
- //var secondaryAnimation = context.VRM.extensions.VRM.secondaryAnimation;
- var colliders = new List<DynamicBoneCollider>();
- foreach (var colliderGroup in secondaryAnimation.colliderGroups)
- {
- foreach (var collider in colliderGroup.colliders)
- {
- var dbCollider = nodes[colliderGroup.node].gameObject.AddComponent<DynamicBoneCollider>();
- dbCollider.m_Radius = collider.radius;
- dbCollider.m_Center = collider.offset;
- colliders.Add(dbCollider);
- }
- }
- if (secondaryAnimation.boneGroups.Count > 0)
- {
- foreach (var boneGroup in secondaryAnimation.boneGroups)
- {
- foreach (var boneindex in boneGroup.bones)
- {
- var db = nodes[boneindex].gameObject.AddComponent<DynamicBone>();
- if (boneGroup.center != -1)
- {
- db.m_Root = nodes[boneGroup.center];
- }
- else
- {
- db.m_Root = db.gameObject.transform;
- }
- db.m_Damping = boneGroup.dragForce;
- db.m_Stiffness = boneGroup.stiffiness * 0.25f;
- db.m_Gravity = boneGroup.gravityDir * boneGroup.gravityPower;
- db.m_Radius = boneGroup.hitRadius;
- db.m_Colliders = new List<DynamicBoneColliderBase>();
- if (boneGroup.colliderGroups != null && boneGroup.colliderGroups.Any())
- {
- foreach (var colliderIndex in boneGroup.colliderGroups)
- {
- db.m_Colliders.Add(colliders[colliderIndex]);
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement