DebugHand.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using UnityEngine;
  3. namespace Leap.Unity
  4. {
  5. public class DebugHand : IHandModel
  6. {
  7. public bool VisualizeBasis
  8. {
  9. get
  10. {
  11. return this.visualizeBasis;
  12. }
  13. set
  14. {
  15. this.visualizeBasis = value;
  16. }
  17. }
  18. public override ModelType HandModelType
  19. {
  20. get
  21. {
  22. return ModelType.Graphics;
  23. }
  24. }
  25. public override Chirality Handedness
  26. {
  27. get
  28. {
  29. return this.handedness;
  30. }
  31. set
  32. {
  33. }
  34. }
  35. public override Hand GetLeapHand()
  36. {
  37. return this.hand_;
  38. }
  39. public override void SetLeapHand(Hand hand)
  40. {
  41. this.hand_ = hand;
  42. }
  43. public override bool SupportsEditorPersistence()
  44. {
  45. return true;
  46. }
  47. public override void InitHand()
  48. {
  49. this.DrawDebugLines();
  50. }
  51. public override void UpdateHand()
  52. {
  53. this.DrawDebugLines();
  54. }
  55. protected void DrawDebugLines()
  56. {
  57. Hand leapHand = this.GetLeapHand();
  58. Debug.DrawLine(leapHand.Arm.ElbowPosition.ToVector3(), leapHand.Arm.WristPosition.ToVector3(), Color.red);
  59. Debug.DrawLine(leapHand.WristPosition.ToVector3(), leapHand.PalmPosition.ToVector3(), Color.white);
  60. Debug.DrawLine(leapHand.PalmPosition.ToVector3(), (leapHand.PalmPosition + leapHand.PalmNormal * leapHand.PalmWidth / 2f).ToVector3(), Color.black);
  61. if (this.VisualizeBasis)
  62. {
  63. this.DrawBasis(leapHand.PalmPosition, leapHand.Basis, leapHand.PalmWidth / 4f);
  64. this.DrawBasis(leapHand.Arm.ElbowPosition, leapHand.Arm.Basis, 0.01f);
  65. }
  66. for (int i = 0; i < 5; i++)
  67. {
  68. Finger finger = leapHand.Fingers[i];
  69. for (int j = 0; j < 4; j++)
  70. {
  71. Bone bone = finger.Bone((Bone.BoneType)j);
  72. Debug.DrawLine(bone.PrevJoint.ToVector3(), bone.PrevJoint.ToVector3() + bone.Direction.ToVector3() * bone.Length, this.colors[j]);
  73. if (this.VisualizeBasis)
  74. {
  75. this.DrawBasis(bone.PrevJoint, bone.Basis, 0.01f);
  76. }
  77. }
  78. }
  79. }
  80. public void DrawBasis(Vector position, LeapTransform basis, float scale)
  81. {
  82. Vector3 vector = position.ToVector3();
  83. Debug.DrawLine(vector, vector + basis.xBasis.ToVector3() * scale, Color.red);
  84. Debug.DrawLine(vector, vector + basis.yBasis.ToVector3() * scale, Color.green);
  85. Debug.DrawLine(vector, vector + basis.zBasis.ToVector3() * scale, Color.blue);
  86. }
  87. private Hand hand_;
  88. [SerializeField]
  89. private bool visualizeBasis = true;
  90. protected Color[] colors = new Color[]
  91. {
  92. Color.gray,
  93. Color.yellow,
  94. Color.cyan,
  95. Color.magenta
  96. };
  97. [SerializeField]
  98. private Chirality handedness;
  99. }
  100. }