InputDeviceHandPartLeftRightEvent.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. namespace SC.XR.Unity.Module_InputSystem.InputDeviceHand {
  9. public class InputDeviceHandPartLeftRightEvent : InputDeviceHandPartEventBase {
  10. public InputDeviceHandPartLeftRightEvent(InputDevicePartDispatchEventHand inputDevicePartDispatchEventHand) : base(inputDevicePartDispatchEventHand) {
  11. }
  12. enum XDirection {
  13. Left = -1,
  14. Right = 1,
  15. }
  16. List<Vector3> HandTrendList = new List<Vector3>(new Vector3[30]);
  17. int DirectionResult = 0;
  18. //int DirectionResultL = 0;
  19. //int DirectionResultR = 0;
  20. //int DirectionResultY = 0;
  21. //int DirectionResultZ = 0;
  22. int currentNum = 0;
  23. float timer = 0;
  24. float timeTriggerEvent;
  25. protected override void OnUpdateEvent() {
  26. currentEvent = HandEventType.Null;
  27. // timer += Time.deltaTime;
  28. if(timer == 0) {
  29. timer = Time.frameCount;
  30. }
  31. if((Time.frameCount - timer) >= samplingTime && (Time.time - timeTriggerEvent) > 0.5f) {
  32. //timer = 0;
  33. timer = Time.frameCount;
  34. if(handInfo.isLost ==false) {
  35. HandTrendList[currentNum] = handInfo.finger[(int)(FINGER.forefinger)].joint[(int)JOINT.Four].localPosition;
  36. } else {
  37. HandTrendList[currentNum] = Vector3.zero;
  38. }
  39. //int j = 0;
  40. //DebugMy.Log("trendList * 1000 =========== " + HandTrendList[currentNum] * 1000 + " ==========", this);
  41. //foreach(var trend in HandTrendList) {
  42. // if(currentNum == j) {
  43. // DebugMy.Log("trendList * 1000:[" + j++ + "]:" + trend * 1000 + "<===", this);
  44. // } else {
  45. // DebugMy.Log("trendList * 1000:[" + j++ + "]:" + trend * 1000, this);
  46. // }
  47. //}
  48. DirectionResult = 0;
  49. for(int i = HandTrendList.Count - 1; i >= 0; i--) {
  50. if(HandTrendList[i].x > (HandTrendList[((i - 1) < 0) ? HandTrendList.Count - 1 : i - 1].x + noise)) {
  51. DirectionResult += (int)XDirection.Right;
  52. //DebugMy.Log(" " + XDirection.Right + " " + DirectionResult, this);
  53. } else if((HandTrendList[i].x + noise) < HandTrendList[((i - 1) < 0) ? HandTrendList.Count - 1 : i - 1].x) {
  54. DirectionResult += (int)XDirection.Left;
  55. //DebugMy.Log(" " + XDirection.Left + " " + DirectionResult, this);
  56. }
  57. //Debug.Log("xxxx:: "+ (HandTrendList[i].x - (HandTrendList[((i - 1) < 0) ? HandTrendList.Count - 1 : i - 1].x)));
  58. }
  59. //if(DirectionResultR > (HandTrendList.Count / 3) && DirectionResultL < (HandTrendList.Count / 6)) {
  60. // currentEvent = Event.Rigth;
  61. //}
  62. //if(DirectionResultL > (HandTrendList.Count / 3) && DirectionResultR < (HandTrendList.Count / 6)) {
  63. // currentEvent = Event.Left;
  64. //}
  65. ///此log可用于查看noise大小,正常手不动DirectionResult为0,当不为0时,调大 noise
  66. // Debug.Log(" Noise ----- ----- X: " + DirectionResult+":"+ DirectionResultR+":"+DirectionResultL+":"+ DirectionResultY+":"+ DirectionResultZ);
  67. if(DirectionResult >= effect) {
  68. //DebugMy.Log(" Event ----- ----- Rigth:"+ inputDevice26Dof.PartType, this);
  69. currentEvent = HandEventType.Rigth;
  70. } else if(DirectionResult <= -effect) {
  71. // DebugMy.Log(" Event ----- ----- Left:" + inputDevice26Dof.PartType, this);
  72. currentEvent = HandEventType.Left;
  73. }
  74. if(currentEvent == HandEventType.Null) {
  75. currentNum++;
  76. if(currentNum == HandTrendList.Count) {
  77. currentNum = 0;
  78. }
  79. } else {
  80. currentNum = 0;
  81. for(int i = 0; i < HandTrendList.Count; i++) {
  82. HandTrendList[i] = Vector3.zero;
  83. }
  84. timeTriggerEvent = Time.time;
  85. }
  86. }
  87. }
  88. }
  89. }