Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UniGLTF;
  5.  
  6. namespace VRM.DynamicBoneExtension
  7. {
  8. public class DynamicBoneLoadUtility {
  9. public static void LoadSecondary(Transform root, List<Transform> nodes,
  10. glTF_VRM_SecondaryAnimation secondaryAnimation)
  11. {
  12. // clear components
  13. var remove = root.Traverse()
  14. .SelectMany(x => x.GetComponents<Component>())
  15. .Where(x => x is VRMSpringBone || x is VRMSpringBoneColliderGroup
  16. || x is DynamicBone || x is DynamicBoneCollider || x is DynamicBonePlaneCollider)
  17. .ToArray();
  18. foreach (var x in remove)
  19. {
  20. if (Application.isPlaying)
  21. {
  22. GameObject.Destroy(x);
  23. }
  24. else
  25. {
  26. GameObject.DestroyImmediate(x);
  27. }
  28. }
  29.  
  30. //var secondaryAnimation = context.VRM.extensions.VRM.secondaryAnimation;
  31. var colliders = new List<DynamicBoneCollider>();
  32. foreach (var colliderGroup in secondaryAnimation.colliderGroups)
  33. {
  34. foreach (var collider in colliderGroup.colliders)
  35. {
  36. var dbCollider = nodes[colliderGroup.node].gameObject.AddComponent<DynamicBoneCollider>();
  37. dbCollider.m_Radius = collider.radius;
  38. dbCollider.m_Center = collider.offset;
  39. colliders.Add(dbCollider);
  40. }
  41. }
  42.  
  43. if (secondaryAnimation.boneGroups.Count > 0)
  44. {
  45. foreach (var boneGroup in secondaryAnimation.boneGroups)
  46. {
  47. foreach (var boneindex in boneGroup.bones)
  48. {
  49. var db = nodes[boneindex].gameObject.AddComponent<DynamicBone>();
  50. if (boneGroup.center != -1)
  51. {
  52. db.m_Root = nodes[boneGroup.center];
  53. }
  54. else
  55. {
  56. db.m_Root = db.gameObject.transform;
  57. }
  58. db.m_Damping = boneGroup.dragForce;
  59. db.m_Stiffness = boneGroup.stiffiness * 0.25f;
  60. db.m_Gravity = boneGroup.gravityDir * boneGroup.gravityPower;
  61. db.m_Radius = boneGroup.hitRadius;
  62. db.m_Colliders = new List<DynamicBoneColliderBase>();
  63. if (boneGroup.colliderGroups != null && boneGroup.colliderGroups.Any())
  64. {
  65. foreach (var colliderIndex in boneGroup.colliderGroups)
  66. {
  67. db.m_Colliders.Add(colliders[colliderIndex]);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement