123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using SensorToolkit;
- using NodeCanvas.Framework;
- using RootMotion.Dynamics;
- using RootMotion.FinalIK;
- namespace KairoEngine.CharacterSystem
- {
- [System.Serializable]
- public class SkinRig
- {
- public string name = "Rig 1";
- public Animator animator;
- public GameObject rootBone;
- public SkinnedMeshRenderer meshRenderer;
- public TriggerSensor visionSensor;
- public RangeSensor hearingSensor;
- public GameObject head;
- public GameObject rightHand;
- public GameObject leftHand;
- public GameObject rightUnarmedObject;
- public GameObject leftUnarmedObject;
- public List<Transform> boneTargets = new List<Transform>();
- public GameObject puppetMasterObject;
- }
- public class SkinRigger : MonoBehaviour
- {
- public CharacterController character;
- public List<SkinRig> skinRigs = new List<SkinRig>();
- public void Setup(ModelData modelData, Material material)
- {
- SkinRig skinRig = null;
- foreach (var item in skinRigs)
- {
- item.animator.gameObject.SetActive(false);
- item.puppetMasterObject.SetActive(false);
- if(modelData.skinRig == item.name) skinRig = item;
- }
- character.animator = skinRig.animator;
- character.meshRenderer = skinRig.meshRenderer;
- character.visionSensor = skinRig.visionSensor;
- character.hearingSensor = skinRig.hearingSensor;
- character.head = skinRig.head;
- character.rightHand = skinRig.rightHand;
- character.leftHand = skinRig.leftHand;
- character.rightUnarmedObject = skinRig.rightUnarmedObject;
- character.leftUnarmedObject = skinRig.leftUnarmedObject;
- Blackboard blackboard = character.GetComponent<Blackboard>();
- blackboard.SetVariableValue("visionSensor", character.visionSensor);
- blackboard.SetVariableValue("soundSensor", character.hearingSensor);
- skinRig.animator.gameObject.SetActive(true);
- skinRig.rootBone.SetActive(true);
- skinRig.meshRenderer.gameObject.SetActive(true);
- Mesh mesh = new Mesh();
- mesh = modelData.mesh;
- character.meshRenderer.sharedMesh = modelData.mesh;
- character.meshRenderer.BakeMesh(mesh);
- character.meshRenderer.material = material;
- character.animator.enabled = true;
- character.animator.Rebind();
- // Set LOS Targets
- LOSTargets losTargets = GetComponent<LOSTargets>();
- if(losTargets != null)
- {
- for (int i = 0; i < losTargets.Targets.Length; i++)
- {
- losTargets.Targets[i] = skinRig.boneTargets[i];
- }
-
- }
- skinRig.puppetMasterObject.SetActive(true);
- character.puppetMaster = skinRig.puppetMasterObject.GetComponent<PuppetMaster>();
- AimIK aimIk = character.GetComponentInChildren<AimIK>();
- if(aimIk != null)
- {
- aimIk.solver.bones[0].transform = GetBone("Hips", skinRig.rootBone.transform);
- aimIk.solver.bones[1].transform = GetBone("Spine_01", skinRig.rootBone.transform);
- aimIk.solver.bones[2].transform = GetBone("Spine_02", skinRig.rootBone.transform);
- aimIk.solver.bones[3].transform = GetBone("Spine_03", skinRig.rootBone.transform);
- }
- LimbIK limbIk = character.GetComponentInChildren<LimbIK>();
- if(limbIk != null)
- {
- limbIk.solver.bone1.transform = GetBone("Shoulder_L", skinRig.rootBone.transform);
- limbIk.solver.bone2.transform = GetBone("Elbow_L", skinRig.rootBone.transform);
- limbIk.solver.bone3.transform = GetBone("Hand_L", skinRig.rootBone.transform);
- }
- }
- private Transform GetBone(string name, Transform root)
- {
- Transform[] bones = root.GetComponentsInChildren<Transform>();
- for (int i = 0; i < bones.Length; i++)
- {
- if(bones[i].name == name) return bones[i];
- }
- return null;
- }
- }
- }
|