Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using RootMotion.FinalIK;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- public class ModelObjReferances : MonoBehaviour
- {
- private PlayerObjReferances playerRefs;
- [Header("(Manual fill)")]
- public Race race;
- public Gender gender;
- //[SerializeField] private string modelClothesLocation;
- [Header("(Auto fill)")]
- public Photon.Pun.PhotonView pv;
- public GameObject model;
- public RootMotion.BipedReferences referances;
- private GameObject fingersParentLeft;
- private GameObject fingersParentRight;
- public GameObject handPanelObj;
- // For Clothes changing
- public Dictionary<string, Transform> boneMap { get; private set; }
- // Storing finger colliders
- [SerializeField] private List<CapsuleCollider> fingerCollidersLeft;
- [SerializeField] private List<CapsuleCollider> fingerCollidersRight;
- private WaitForSeconds delay0;
- private WaitForSeconds delay1;
- #region Classes
- [System.Serializable]
- public class ModelRoot
- {
- public GameObject root;
- public GameObject pelvis;
- public GameObject groundSeeker;
- // down the spine
- public GameObject spine_01;
- public GameObject spine_02;
- public GameObject spine_03;
- public GameObject clavicle_l;
- public GameObject upperarm_l;
- public GameObject lowerarm_l;
- public GameObject hand_l;
- public GameObject index_l;
- public GameObject clavicle_r;
- public GameObject upperarm_r;
- public GameObject lowerarm_r;
- public GameObject hand_r;
- public GameObject index_r;
- public GameObject neck_01;
- public GameObject head;
- public GameObject hairPlaceholder;
- // down the thigh_l
- public GameObject thigh_l;
- public GameObject caif_l;
- public GameObject foot_l;
- public GameObject ball_l;
- // down the thigh_r
- public GameObject thigh_r;
- public GameObject caif_r;
- public GameObject foot_r;
- public GameObject ball_r;
- // fingers_03 the ones with colliders
- // left hand
- [Header("Finger tips. Left hand.")]
- public GameObject index_03_l;
- public GameObject middle_03_l;
- public GameObject pinky_03_l;
- public GameObject ring_03_l;
- public GameObject thumb_03_l;
- // right hand
- [Header("Finger tips. Right hand.")]
- public GameObject index_03_r;
- public GameObject middle_03_r;
- public GameObject pinky_03_r;
- public GameObject ring_03_r;
- public GameObject thumb_03_r;
- public ModelRoot(GameObject gRoot)
- {
- root = gRoot;
- pelvis = root.transform.GetChild(0).gameObject;
- groundSeeker = root.transform.GetChild(1).gameObject;
- // down the spine
- spine_01 = pelvis.transform.GetChild(0).gameObject;
- spine_02 = spine_01.transform.GetChild(0).gameObject;
- spine_03 = spine_02.transform.GetChild(0).gameObject;
- clavicle_l = spine_03.transform.GetChild(2).gameObject;
- upperarm_l = clavicle_l.transform.GetChild(0).gameObject;
- lowerarm_l = upperarm_l.transform.GetChild(0).gameObject;
- hand_l = lowerarm_l.transform.GetChild(0).gameObject;
- index_l = hand_l.transform.GetChild(0).gameObject;
- // finger tips - left
- index_03_l = hand_l.transform.GetChild(0).GetChild(0).GetChild(0).gameObject;
- middle_03_l = hand_l.transform.GetChild(1).GetChild(0).GetChild(0).gameObject;
- pinky_03_l = hand_l.transform.GetChild(2).GetChild(0).GetChild(0).gameObject;
- ring_03_l = hand_l.transform.GetChild(3).GetChild(0).GetChild(0).gameObject;
- thumb_03_l = hand_l.transform.GetChild(4).GetChild(0).GetChild(0).gameObject;
- clavicle_r = spine_03.transform.GetChild(3).gameObject;
- upperarm_r = clavicle_r.transform.GetChild(0).gameObject;
- lowerarm_r = upperarm_r.transform.GetChild(0).gameObject;
- hand_r = lowerarm_r.transform.GetChild(0).gameObject;
- index_r = hand_r.transform.GetChild(0).gameObject;
- // finger tips - right
- index_03_r = hand_r.transform.GetChild(0).GetChild(0).GetChild(0).gameObject;
- middle_03_r = hand_r.transform.GetChild(1).GetChild(0).GetChild(0).gameObject;
- pinky_03_r = hand_r.transform.GetChild(2).GetChild(0).GetChild(0).gameObject;
- ring_03_r = hand_r.transform.GetChild(3).GetChild(0).GetChild(0).gameObject;
- thumb_03_r = hand_r.transform.GetChild(4).GetChild(0).GetChild(0).gameObject;
- neck_01 = spine_03.transform.GetChild(4).gameObject;
- head = neck_01.transform.GetChild(0).gameObject;
- try { hairPlaceholder = head.transform.GetChild(1).gameObject; }
- catch (Exception e) { Debug.LogError("Could not find hairPlaceholder. Make sure that model has it in <...neck_01/head/ >. ");}
- // down the thigh_l
- thigh_l = pelvis.transform.GetChild(1).gameObject;
- caif_l = thigh_l.transform.GetChild(0).gameObject;
- foot_l = caif_l.transform.GetChild(1).gameObject;
- ball_l = foot_l.transform.GetChild(0).gameObject;
- // down the thigh_r
- thigh_r = pelvis.transform.GetChild(2).gameObject;
- caif_r = thigh_r.transform.GetChild(0).gameObject;
- foot_r = caif_r.transform.GetChild(1).gameObject;
- ball_r = foot_r.transform.GetChild(0).gameObject;
- }
- }
- [System.Serializable]
- public class ModelGrounder
- {
- public GameObject grounder;
- public GameObject leftLegIK;
- public GameObject rightLegIK;
- public GameObject raycastPoint;
- public ModelGrounder(GameObject gGrounder)
- {
- grounder = gGrounder;
- leftLegIK = grounder.transform.GetChild(0).gameObject;
- rightLegIK = grounder.transform.GetChild(1).gameObject;
- raycastPoint = grounder.transform.GetChild(2).gameObject;
- }
- }
- [System.Serializable]
- public class ModelOutfits
- {
- public List<GameObject> clothes;
- public ModelOutfits(string location)
- {
- clothes = new List<GameObject>();
- try
- {
- foreach (GameObject g in Resources.LoadAll(location, typeof(GameObject)))
- clothes.Add(g);
- }
- catch (Exception e)
- { Debug.LogError("ModelObjRef:: class modelOutfits could not load all outfits and/or is empty. Error: " + e); }
- }
- public GameObject GetClothesByName(string name)
- {
- foreach (GameObject g in clothes)
- if (g.name.Equals(name))
- return g;
- return null;
- }
- }
- public ModelRoot root;
- public ModelGrounder grounder;
- //public ModelOutfits modelOutfits;
- #endregion
- private void Awake()
- {
- playerRefs = transform.root.GetComponent<PlayerObjReferances>();
- model = gameObject;
- pv = GetComponent<Photon.Pun.PhotonView>();
- //GetComponent<VRIK>().gameObject.SetActive(true);
- for (int i = 0; i < gameObject.transform.childCount; i++)
- {
- if (gameObject.transform.GetChild(i).transform.name == "root")
- root = new ModelRoot(gameObject.transform.GetChild(i).gameObject);
- if (gameObject.transform.GetChild(i).transform.name == "Grounder")
- grounder = new ModelGrounder(gameObject.transform.GetChild(i).gameObject);
- }
- fingerCollidersLeft = new List<CapsuleCollider>();
- fingerCollidersRight = new List<CapsuleCollider>();
- //
- AddComponents();
- SetHandTags();
- SetRefsVRIK(GetComponent<VRIK>().references);
- //SetSolverRefsVRIK(GetComponent<VRIK>()); // set from playerObjRefs
- SetRefsBiped(referances);
- GetComponent<FullBodyBipedIK>().SetReferences(referances, root.spine_01.transform);
- SetRefsAnimMan(GetComponent<AnimationManager>());
- SetRefsIKOrder(GetComponent<IKExecutionOrder>());
- //GetComponent<VRIK>().gameObject.SetActive(true);
- SetRefsCharSettings(GetComponent<CharSettings>());
- SetRefsGrounder(grounder.grounder.GetComponent<GrounderIK>());
- // Model clothes load
- /*
- if (modelClothesLocation != "")
- modelOutfits = new ModelOutfits(modelClothesLocation);
- else
- Debug.LogError("ModelObjRef:: modelTorsoLocation not selected. Model clothes did not load...");
- */
- // For Clothes changing
- SkinnedMeshRenderer targetMeshRendererHead = transform.Find("CC_Base_Head").GetComponent<SkinnedMeshRenderer>();
- SkinnedMeshRenderer targetMeshRenderer = transform.Find("CC_Base_Body").GetComponent<SkinnedMeshRenderer>();
- boneMap = new Dictionary<string, Transform>();
- if (targetMeshRendererHead)
- {
- foreach (Transform bone in targetMeshRendererHead.bones)
- boneMap[bone.gameObject.name] = bone;
- }
- if (targetMeshRenderer)
- {
- foreach (Transform bone in targetMeshRenderer.bones)
- boneMap[bone.gameObject.name] = bone;
- }
- AddingToCulledLayer();
- delay0 = new WaitForSeconds(0f);
- delay1 = new WaitForSeconds(0.5f);
- }
- private void Start()
- {
- //SetSolverRefsVRIK(GetComponent<VRIK>());
- //SetRefsAnimMan(GetComponent<AnimationManager>());
- model.AddComponent<SyncPositions>().enabled = false;
- }
- void SetRefsVRIK(VRIK.References refs)
- {
- refs.root = model.transform; // model ref
- refs.pelvis = root.pelvis.transform;
- refs.spine = root.spine_01.transform;
- refs.chest = root.spine_02.transform;
- refs.neck = root.neck_01.transform;
- refs.head = root.head.transform;
- refs.leftShoulder = root.clavicle_l.transform;
- refs.leftUpperArm = root.upperarm_l.transform;
- refs.leftForearm = root.lowerarm_l.transform;
- refs.leftHand = root.hand_l.transform;
- refs.rightShoulder = root.clavicle_r.transform;
- refs.rightUpperArm = root.upperarm_r.transform;
- refs.rightForearm = root.lowerarm_r.transform;
- refs.rightHand = root.hand_r.transform;
- refs.leftThigh = root.thigh_l.transform;
- refs.leftCalf = root.caif_l.transform;
- refs.leftFoot = root.foot_l.transform;
- refs.leftToes = root.ball_l.transform;
- refs.rightThigh = root.thigh_r.transform;
- refs.rightCalf = root.caif_r.transform;
- refs.rightFoot = root.foot_r.transform;
- refs.rightToes = root.ball_r.transform;
- }
- void SetSolverRefsVRIK(VRIK ik)
- {
- ik.solver.spine.headTarget = playerRefs.rigSpineHeadTarget.transform;
- ik.solver.leftArm.target = playerRefs.rigLeftHandTarget.transform;
- ik.solver.rightArm.target = playerRefs.rigRightHandTarget.transform;
- ik.enabled = true;
- }
- void SetRefsBiped(RootMotion.BipedReferences refs)
- {
- refs.root = model.transform;
- refs.pelvis = root.pelvis.transform;
- refs.leftThigh = root.thigh_l.transform;
- refs.leftCalf = root.caif_l.transform;
- refs.leftFoot = root.foot_l.transform;
- refs.rightThigh = root.thigh_r.transform;
- refs.rightCalf = root.caif_r.transform;
- refs.rightFoot = root.foot_r.transform;
- refs.leftUpperArm = root.upperarm_l.transform;
- refs.leftForearm = root.lowerarm_l.transform;
- refs.leftHand = root.hand_l.transform;
- refs.rightUpperArm = root.upperarm_r.transform;
- refs.rightForearm = root.lowerarm_r.transform;
- refs.rightHand = root.hand_r.transform;
- refs.head = root.head.transform;
- refs.spine = new Transform[2];
- refs.spine.SetValue(root.spine_01.transform, 0);
- refs.spine.SetValue(root.spine_02.transform, 1);
- }
- void SetRefsAnimMan(AnimationManager anim)
- {
- // setting from playerObjRefs
- //anim.SetHandTransforms(playerRefs.rigLeftHandTransforms, playerRefs.rigRightHandTransforms);
- anim.SetAnim(model);
- anim.SetEffectors();
- }
- void SetRefsIKOrder(IKExecutionOrder ike)
- {
- ike.IKComponents = new RootMotion.FinalIK.IK[3];
- ike.IKComponents[0] = GetComponent<VRIK>();
- ike.IKComponents[1] = grounder.leftLegIK.GetComponent<LimbIK>();
- ike.IKComponents[2] = grounder.rightLegIK.GetComponent<LimbIK>();
- ike.animator = GetComponent<Animator>();
- }
- void SetRefsCharSettings(CharSettings charSettings)
- {
- charSettings.SetRefs(grounder.raycastPoint, root.groundSeeker, root.root, model);
- }
- void SetRefsGrounder(GrounderIK grik)
- {
- //grik.legs = new IK[2];
- //grik.legs[0] = grounder.leftLegIK.GetComponent<LimbIK>();
- //grik.legs[1] = grounder.rightLegIK.GetComponent<LimbIK>();
- //grik.pelvis = root.pelvis.transform;
- //grik.characterRoot = model.transform;
- }
- void SetHandTags()
- {
- root.hand_l.transform.tag = "LeftHand";
- root.index_l.transform.tag = "LeftHand";
- root.hand_r.transform.tag = "RightHand";
- root.index_r.transform.tag = "RightHand";
- }
- void AddComponents() // adding rigidbody and box colliders to hands and index fingers
- {
- //
- //model.AddComponent<SyncPositions>().enabled = false;
- //
- // left hand
- if (root.hand_l.GetComponent<HandPoser>() == null)
- root.hand_l.AddComponent<HandPoser>();
- // index
- if (root.index_l.GetComponent<Rigidbody>() == null)
- {
- Rigidbody rbL = root.index_l.AddComponent<Rigidbody>();
- rbL.mass = 1f;
- rbL.angularDrag = 0.05f;
- rbL.useGravity = false;
- rbL.isKinematic = true;
- }
- if (root.index_l.GetComponent<BoxCollider>() == null)
- {
- BoxCollider boxL = root.index_l.AddComponent<BoxCollider>();
- boxL.isTrigger = true;
- boxL.center = new Vector3(-0.047f, 0.01f, 0.033f);
- boxL.size = new Vector3(0.14f, 0.05f, 0.088f);
- }
- // right hand
- if (root.hand_r.GetComponent<HandPoser>() == null)
- root.hand_r.AddComponent<HandPoser>();
- // index
- if (root.index_r.GetComponent<Rigidbody>() == null)
- {
- Rigidbody rbR = root.index_r.AddComponent<Rigidbody>();
- rbR.mass = 1f;
- rbR.angularDrag = 0.05f;
- rbR.useGravity = false;
- rbR.isKinematic = true;
- }
- if (root.index_r.GetComponent<BoxCollider>() == null)
- {
- BoxCollider boxR = root.index_r.AddComponent<BoxCollider>();
- boxR.isTrigger = true;
- boxR.center = new Vector3(0.0485f, -0.01f, -0.033f);
- boxR.size = new Vector3(0.144f, 0.05f, 0.092f);
- }
- // Adding hand colliders
- //AddLeftHandCollider(root.hand_l);
- //AddRightHandCollider(root.hand_r);
- // Adding parent for finger colliders
- if (!fingersParentLeft)
- {
- fingersParentLeft = new GameObject("FingersParentLeft");
- fingersParentLeft.transform.parent = root.hand_l.transform.parent;
- }
- if (!fingersParentRight)
- {
- fingersParentRight = new GameObject("FingersParentRight");
- fingersParentRight.transform.parent = root.hand_r.transform.parent;
- }
- fingerCollidersLeft.Clear();
- fingerCollidersRight.Clear();
- if (pv.IsMine)
- {
- // Adding finger colliders
- AddFingerCollider(root.index_03_l, true, 0);
- AddFingerCollider(root.middle_03_l, true, 0);
- AddFingerCollider(root.pinky_03_l, true, 0);
- AddFingerCollider(root.ring_03_l, true, 0);
- AddFingerCollider(root.thumb_03_l, true, 0);
- AddFingerCollider(root.index_03_r, false, 0.02f);
- AddFingerCollider(root.middle_03_r, false, 0.02f);
- AddFingerCollider(root.pinky_03_r, false, 0.02f);
- AddFingerCollider(root.ring_03_r, false, 0.02f);
- AddFingerCollider(root.thumb_03_r, false, 0.02f);
- AddHandPanel(root.hand_l);
- }
- }
- // TODO right hand settings a bit different
- void AddFingerCollider(GameObject finger, bool isLeftHand, float xOffset)
- {
- if (!finger.transform.GetComponentInChildren<CapsuleCollider>())
- {
- // Finger itself
- Rigidbody rb = finger.AddComponent<Rigidbody>();
- rb.mass = 100f;
- rb.drag = 1f;
- rb.angularDrag = 1f;
- rb.useGravity = false;
- rb.isKinematic = false;
- // Fingers collider
- GameObject fingerColObj = new GameObject("FingerCollider");
- //fingerColObj.transform.parent = finger.transform;
- if (isLeftHand)
- fingerColObj.transform.parent = fingersParentLeft.transform;
- else if (!isLeftHand)
- fingerColObj.transform.parent = fingersParentRight.transform;
- fingerColObj.transform.localRotation = new Quaternion(0, 0, 0, 0);
- //fingerColObj.transform.localPosition = new Vector3(0, 0, 0);
- //fingerColObj.transform.position = finger.transform.position;
- fingerColObj.transform.position = new Vector3(finger.transform.position.x + xOffset,
- finger.transform.position.y,
- finger.transform.position.z);
- fingerColObj.layer = 18;
- VibrateOnTouch vot = fingerColObj.AddComponent<VibrateOnTouch>();
- vot.isLeft = isLeftHand;
- Rigidbody colRb = fingerColObj.AddComponent<Rigidbody>();
- colRb.mass = 5f;
- colRb.drag = 1f;
- colRb.angularDrag = 1f;
- colRb.useGravity = false;
- colRb.isKinematic = false;
- FixedJoint fj = finger.AddComponent<FixedJoint>();
- fj.connectedBody = colRb;
- CapsuleCollider capCol = fingerColObj.AddComponent<CapsuleCollider>();
- capCol.center = new Vector3(-0.01f, 0f, 0f);
- capCol.radius = 0.01f;
- capCol.direction = 0; // 0 - x axis, 1 - y axis...
- capCol.height = 0.03f;
- if (isLeftHand)
- fingerCollidersLeft.Add(capCol);
- else if (!isLeftHand)
- fingerCollidersRight.Add(capCol);
- }
- }
- void AddLeftHandCollider(GameObject hand)
- {
- if (hand.transform.childCount < 6)
- {
- // Hand itself
- Rigidbody rb = hand.AddComponent<Rigidbody>();
- rb.mass = 1f;
- rb.useGravity = false;
- rb.isKinematic = true;
- // Hand collider
- GameObject handColObj = new GameObject("HandCollider");
- handColObj.transform.parent = hand.transform;
- handColObj.transform.localRotation = new Quaternion(0, 0, 0, 0);
- handColObj.transform.localPosition = new Vector3(0, 0, 0);
- Rigidbody colRb = handColObj.AddComponent<Rigidbody>();
- colRb.mass = 1f;
- colRb.angularDrag = 0f;
- colRb.useGravity = false;
- FixedJoint fj = hand.AddComponent<FixedJoint>();
- fj.connectedBody = colRb;
- BoxCollider boxCol = handColObj.AddComponent<BoxCollider>();
- boxCol.center = new Vector3(-0.078f, 0.009f, 0.00085f);
- boxCol.size = new Vector3(0.093f, 0.031f, 0.09f);
- }
- }
- void AddRightHandCollider(GameObject hand)
- {
- if (hand.transform.childCount < 6)
- {
- // Hand itself
- Rigidbody rb = hand.AddComponent<Rigidbody>();
- rb.mass = 1f;
- rb.useGravity = false;
- rb.isKinematic = true;
- // Hand collider
- GameObject handColObj = new GameObject("HandCollider");
- handColObj.transform.parent = hand.transform;
- handColObj.transform.localRotation = new Quaternion(0, 0, 0, 0);
- handColObj.transform.localPosition = new Vector3(0, 0, 0);
- Rigidbody colRb = handColObj.AddComponent<Rigidbody>();
- colRb.mass = 1f;
- colRb.angularDrag = 0f;
- colRb.useGravity = false;
- FixedJoint fj = hand.AddComponent<FixedJoint>();
- fj.connectedBody = colRb;
- BoxCollider boxCol = handColObj.AddComponent<BoxCollider>();
- boxCol.center = new Vector3(0.074f, -0.009f, 0.00085f);
- boxCol.size = new Vector3(0.108f, 0.0252f, 0.09f);
- }
- }
- private void AddHandPanel(GameObject attachPart)
- {
- handPanelObj = Instantiate(Resources.Load("HandPanel") as GameObject);
- if (handPanelObj)
- {
- HandPanel handPanel = handPanelObj.GetComponent<HandPanel>();
- handPanel.targetTransform = attachPart.transform;
- handPanel.playerObjRefs = playerRefs;
- //handPanelObj.transform.parent = transform;
- handPanelObj.transform.position = attachPart.transform.position;
- handPanelObj.transform.rotation = attachPart.transform.rotation;
- handPanelObj.transform.parent = attachPart.transform.parent;
- }
- else
- {
- Debug.LogError("Could not find HandPanel prefab.");
- }
- }
- private void AddingToCulledLayer() // Head render culling - Camera set to not render 'culled' layer
- {
- if (pv.IsMine)
- {
- //Debug.LogError("RUNNING...1");
- GameObject culledObj0 = transform.Find("CC_Base_Head").gameObject;
- if (culledObj0)
- culledObj0.layer = 8;
- else
- Debug.LogError("ModelObjRefs:: Couldn't find <" + culledObj0.name + "> object.");
- GameObject culledObj1 = transform.Find("CC_Game_Eye").gameObject;
- if (culledObj1)
- culledObj1.layer = 8;
- else
- Debug.LogError("ModelObjRefs:: Couldn't find <" + culledObj1.name + "> object.");
- GameObject culledObj2 = transform.Find("CC_Base_Teeth").gameObject;
- if (culledObj2)
- culledObj2.layer = 8;
- else
- Debug.LogError("ModelObjRefs:: Couldn't find <" + culledObj2.name + "> object.");
- GameObject culledObj3 = transform.Find("CC_Base_Tongue").gameObject;
- if (culledObj3)
- culledObj3.layer = 8;
- else
- Debug.LogError("ModelObjRefs:: Couldn't find <" + culledObj3.name + "> object.");
- // For other models that have more parts that need culling
- var culledObj4 = transform.Find("ears");
- if (culledObj4)
- culledObj4.gameObject.layer = 8;
- }
- }
- // Turn on and off hand colliders
- public void ToggleHandColliders(bool leftHand, bool toggle)
- {
- if (toggle == true)
- {
- StartCoroutine(ToggleHandCollidersDelay(leftHand, delay1));
- }
- else
- {
- if (leftHand)
- foreach (CapsuleCollider col in fingerCollidersLeft)
- col.enabled = false;
- else
- foreach (CapsuleCollider col in fingerCollidersRight)
- col.enabled = false;
- }
- }
- private IEnumerator ToggleHandCollidersDelay(bool leftHand, WaitForSeconds delay)
- {
- yield return delay;
- if (leftHand)
- foreach (CapsuleCollider col in fingerCollidersLeft)
- col.enabled = true;
- else
- foreach (CapsuleCollider col in fingerCollidersRight)
- col.enabled = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement