RigBone.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class RigBone {
  6. public GameObject gameObject;
  7. public HumanBodyBones bone;
  8. public bool isValid;
  9. public Transform transform {
  10. get { return animator.GetBoneTransform(bone); }
  11. }
  12. private Animator animator;
  13. private Quaternion savedValue;
  14. public RigBone(GameObject g, HumanBodyBones b) {
  15. gameObject = g;
  16. bone = b;
  17. isValid = false;
  18. animator = gameObject.GetComponent<Animator>();
  19. if (animator == null) {
  20. Debug.Log("no Animator Component");
  21. return;
  22. }
  23. Avatar avatar = animator.avatar;
  24. if (avatar == null || !avatar.isHuman || !avatar.isValid) {
  25. Debug.Log("Avatar is not Humanoid or it is not valid");
  26. return;
  27. }
  28. isValid = true;
  29. savedValue = animator.GetBoneTransform(bone).localRotation;
  30. }
  31. public void set(float a, float x, float y, float z) {
  32. set(Quaternion.AngleAxis(a, new Vector3(x,y,z)));
  33. }
  34. public void set(Quaternion q) {
  35. animator.GetBoneTransform(bone).localRotation = q;
  36. savedValue = q;
  37. }
  38. public void mul(float a, float x, float y, float z) {
  39. mul(Quaternion.AngleAxis(a, new Vector3(x,y,z)));
  40. }
  41. public void mul(Quaternion q) {
  42. Transform tr = animator.GetBoneTransform(bone);
  43. tr.localRotation = q * tr.localRotation;
  44. }
  45. public void offset(float a, float x, float y, float z) {
  46. offset(Quaternion.AngleAxis(a, new Vector3(x,y,z)));
  47. }
  48. public void offset(Quaternion q) {
  49. animator.GetBoneTransform(bone).localRotation = q * savedValue;
  50. }
  51. public void changeBone(HumanBodyBones b) {
  52. bone = b;
  53. savedValue = animator.GetBoneTransform(bone).localRotation;
  54. }
  55. }