SkinRigger.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using SensorToolkit;
  6. using NodeCanvas.Framework;
  7. using RootMotion.Dynamics;
  8. using RootMotion.FinalIK;
  9. namespace KairoEngine.CharacterSystem
  10. {
  11. [System.Serializable]
  12. public class SkinRig
  13. {
  14. public string name = "Rig 1";
  15. public Animator animator;
  16. public GameObject rootBone;
  17. public SkinnedMeshRenderer meshRenderer;
  18. public TriggerSensor visionSensor;
  19. public RangeSensor hearingSensor;
  20. public GameObject head;
  21. public GameObject rightHand;
  22. public GameObject leftHand;
  23. public GameObject rightUnarmedObject;
  24. public GameObject leftUnarmedObject;
  25. public List<Transform> boneTargets = new List<Transform>();
  26. public GameObject puppetMasterObject;
  27. }
  28. public class SkinRigger : MonoBehaviour
  29. {
  30. public CharacterController character;
  31. public List<SkinRig> skinRigs = new List<SkinRig>();
  32. public void Setup(ModelData modelData, Material material)
  33. {
  34. SkinRig skinRig = null;
  35. foreach (var item in skinRigs)
  36. {
  37. item.animator.gameObject.SetActive(false);
  38. item.puppetMasterObject.SetActive(false);
  39. if(modelData.skinRig == item.name) skinRig = item;
  40. }
  41. character.animator = skinRig.animator;
  42. character.meshRenderer = skinRig.meshRenderer;
  43. character.visionSensor = skinRig.visionSensor;
  44. character.hearingSensor = skinRig.hearingSensor;
  45. character.head = skinRig.head;
  46. character.rightHand = skinRig.rightHand;
  47. character.leftHand = skinRig.leftHand;
  48. character.rightUnarmedObject = skinRig.rightUnarmedObject;
  49. character.leftUnarmedObject = skinRig.leftUnarmedObject;
  50. Blackboard blackboard = character.GetComponent<Blackboard>();
  51. blackboard.SetVariableValue("visionSensor", character.visionSensor);
  52. blackboard.SetVariableValue("soundSensor", character.hearingSensor);
  53. skinRig.animator.gameObject.SetActive(true);
  54. skinRig.rootBone.SetActive(true);
  55. skinRig.meshRenderer.gameObject.SetActive(true);
  56. Mesh mesh = new Mesh();
  57. mesh = modelData.mesh;
  58. character.meshRenderer.sharedMesh = modelData.mesh;
  59. character.meshRenderer.BakeMesh(mesh);
  60. character.meshRenderer.material = material;
  61. character.animator.enabled = true;
  62. character.animator.Rebind();
  63. // Set LOS Targets
  64. LOSTargets losTargets = GetComponent<LOSTargets>();
  65. if(losTargets != null)
  66. {
  67. for (int i = 0; i < losTargets.Targets.Length; i++)
  68. {
  69. losTargets.Targets[i] = skinRig.boneTargets[i];
  70. }
  71. }
  72. skinRig.puppetMasterObject.SetActive(true);
  73. character.puppetMaster = skinRig.puppetMasterObject.GetComponent<PuppetMaster>();
  74. AimIK aimIk = character.GetComponentInChildren<AimIK>();
  75. if(aimIk != null)
  76. {
  77. aimIk.solver.bones[0].transform = GetBone("Hips", skinRig.rootBone.transform);
  78. aimIk.solver.bones[1].transform = GetBone("Spine_01", skinRig.rootBone.transform);
  79. aimIk.solver.bones[2].transform = GetBone("Spine_02", skinRig.rootBone.transform);
  80. aimIk.solver.bones[3].transform = GetBone("Spine_03", skinRig.rootBone.transform);
  81. }
  82. LimbIK limbIk = character.GetComponentInChildren<LimbIK>();
  83. if(limbIk != null)
  84. {
  85. limbIk.solver.bone1.transform = GetBone("Shoulder_L", skinRig.rootBone.transform);
  86. limbIk.solver.bone2.transform = GetBone("Elbow_L", skinRig.rootBone.transform);
  87. limbIk.solver.bone3.transform = GetBone("Hand_L", skinRig.rootBone.transform);
  88. }
  89. }
  90. private Transform GetBone(string name, Transform root)
  91. {
  92. Transform[] bones = root.GetComponentsInChildren<Transform>();
  93. for (int i = 0; i < bones.Length; i++)
  94. {
  95. if(bones[i].name == name) return bones[i];
  96. }
  97. return null;
  98. }
  99. }
  100. }