InputDeviceGGT26DofPartStatus.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. namespace SC.XR.Unity.Module_InputSystem.InputDeviceHand.GGT26Dof {
  8. public class InputDeviceGGT26DofPartStatus : InputDeviceHandPartStatus {
  9. public InputDeviceGGT26DofPart inputDeviceGGT26DofPart;
  10. public InputDeviceGGT26DofPartStatus(InputDeviceGGT26DofPart _inputDeviceHandPart) : base(_inputDeviceHandPart) {
  11. inputDeviceGGT26DofPart = _inputDeviceHandPart;
  12. }
  13. float timer = 0;
  14. static bool isUpdateDataThisFrame = false;
  15. public override void OnSCLateUpdate() {
  16. base.OnSCLateUpdate();
  17. ///每帧只执行一次
  18. if (isUpdateDataThisFrame == false) {
  19. isUpdateDataThisFrame = true;
  20. UpdateStatus(InputDataGGT26Dof.handsInfo.originDataMode, InputDataGGT26Dof.handsInfo.originDataPose);
  21. }
  22. if (inputDeviceGGT26DofPart.PartType == InputDevicePartType.HandLeft) {
  23. inputDeviceGGT26DofPart.inputDataGGT26Dof.isFound = InputDataGGT26Dof.handsInfo.handLeftFind;
  24. if (inputDeviceGGT26DofPart.inputDeviceGGT26Dof.LeftHandOpen == false) {
  25. inputDeviceGGT26DofPart.inputDataGGT26Dof.isFound = false;
  26. }
  27. }
  28. if (inputDeviceGGT26DofPart.PartType == InputDevicePartType.HandRight) {
  29. inputDeviceGGT26DofPart.inputDataGGT26Dof.isFound = InputDataGGT26Dof.handsInfo.handRightFind;
  30. if (inputDeviceGGT26DofPart.inputDeviceGGT26Dof.RightHandOpen == false) {
  31. inputDeviceGGT26DofPart.inputDataGGT26Dof.isFound = false;
  32. }
  33. }
  34. if (inputDeviceHandPart.inputDataHand.isFound == false) {
  35. timer += Time.deltaTime;
  36. inputDeviceHandPart.inputDataHand.handInfo.lostPercent = timer / inputDeviceHandPart.inputDataHand.handInfo.lostTimer;
  37. if (inputDeviceHandPart.inputDataHand.handInfo.lostPercent >= 1) {
  38. inputDeviceHandPart.inputDataHand.handInfo.lostPercent = 1;
  39. inputDeviceHandPart.inputDataHand.handInfo.isLost = true;
  40. }
  41. ///hand都是多少百分比后将手势值设为复位值,以触发UP事件(如果已经Down或者Drag后)
  42. //if(inputDeviceHandPart.inputDataHand.handInfo.lostPercent > 0.90f) {
  43. // //Debug.Log("========= lost > 80 :" + inputDeviceHandPart.PartType);
  44. // inputDeviceHandPart.inputDataHand.ResetHandData(inputDeviceHandPart.PartType);
  45. //}
  46. } else {
  47. timer = 0;
  48. inputDeviceHandPart.inputDataHand.handInfo.lostPercent = 0;
  49. inputDeviceHandPart.inputDataHand.handInfo.isLost = false;
  50. }
  51. if (inputDeviceHandPart.inputDataHand.handInfo.isLost) {
  52. inputDeviceHandPart.inputDataBase.isVaild = false;
  53. //DebugMy.Log("Hand:" + inputDeviceHandPart.PartType + " Status: DisActive !", this);
  54. } else {
  55. inputDeviceHandPart.inputDataBase.isVaild = true;
  56. //DebugMy.Log("Hand:" + inputDeviceHandPart.PartType + " Status: Active", this);
  57. }
  58. }
  59. public override void OnSCFuncitonWaitForEndOfFrame() {
  60. base.OnSCFuncitonWaitForEndOfFrame();
  61. isUpdateDataThisFrame = false;
  62. }
  63. /// <summary>
  64. /// every frame invoke once
  65. /// </summary>
  66. /// <param name="mode"></param>
  67. /// <param name="pose"></param>
  68. /// <returns></returns>
  69. protected virtual void UpdateStatus(float[] mode, float[] pose) {
  70. ///识别到手的个数
  71. InputDataGGT26Dof.handsInfo.handAmount = (int)mode[0];
  72. ///0代表不知左右
  73. ///1代表左
  74. ///2代表右
  75. ///可以同时0或1或2,此时舍去一个
  76. ///
  77. if ((int)mode[0] == 0) {
  78. InputDataGGT26Dof.handsInfo.handLeftFind = false;
  79. InputDataGGT26Dof.handsInfo.handRightFind = false;
  80. } else if ((int)mode[0] == 1) {
  81. if ((int)(mode[1]) == 1) {
  82. InputDataGGT26Dof.handsInfo.handLeftFind = true;
  83. InputDataGGT26Dof.handsInfo.handRightFind = false;
  84. InputDataGGT26Dof.handsInfo.handLeftIndex = 0;
  85. } else if ((int)(mode[1]) == 2) {
  86. InputDataGGT26Dof.handsInfo.handLeftFind = false;
  87. InputDataGGT26Dof.handsInfo.handRightFind = true;
  88. InputDataGGT26Dof.handsInfo.handRighIndex = 0;
  89. }
  90. } else if ((int)mode[0] == 2) {
  91. if ((int)(mode[1]) == 1 && (int)(mode[3 + 63]) == 2) {
  92. InputDataGGT26Dof.handsInfo.handLeftFind = true;
  93. InputDataGGT26Dof.handsInfo.handRightFind = true;
  94. InputDataGGT26Dof.handsInfo.handLeftIndex = 0;
  95. InputDataGGT26Dof.handsInfo.handRighIndex = 1;
  96. }
  97. }
  98. if (Application.platform != RuntimePlatform.Android) {
  99. InputDataGGT26Dof.handsInfo.handLeftFind = true;
  100. InputDataGGT26Dof.handsInfo.handRightFind = true;
  101. InputDataGGT26Dof.handsInfo.handRighIndex = 0;
  102. InputDataGGT26Dof.handsInfo.handRighIndex = 1;
  103. ///按K键左手丢失
  104. if (Input.GetKey(KeyCode.O) == true) {
  105. InputDataGGT26Dof.handsInfo.handLeftFind = false;
  106. }
  107. ///按L键右手丢失
  108. if (Input.GetKey(KeyCode.P) == true) {
  109. InputDataGGT26Dof.handsInfo.handRightFind = false;
  110. }
  111. }
  112. if (InputDataGGT26Dof.handsInfo.handLeftFind) {
  113. InputDataGGT26Dof.handsInfo.handLeft.findFrameCount++;
  114. } else if (InputDataGGT26Dof.handsInfo.handLeftFind == false) {
  115. InputDataGGT26Dof.handsInfo.handLeft.findFrameCount = 0;
  116. }
  117. if (InputDataGGT26Dof.handsInfo.handRightFind) {
  118. InputDataGGT26Dof.handsInfo.handRight.findFrameCount++;
  119. } else if (InputDataGGT26Dof.handsInfo.handRightFind == false) {
  120. InputDataGGT26Dof.handsInfo.handRight.findFrameCount = 0;
  121. }
  122. if (InputDataGGT26Dof.handsInfo.handLeft.findFrameCount > InputDataGGT26Dof.handsInfo.handLeft.frameCountValid) {
  123. InputDataGGT26Dof.handsInfo.handLeftFind = true;
  124. } else if (InputDataGGT26Dof.handsInfo.handLeft.isLost) {
  125. InputDataGGT26Dof.handsInfo.handLeftFind = false;
  126. }
  127. if (InputDataGGT26Dof.handsInfo.handRight.findFrameCount > InputDataGGT26Dof.handsInfo.handRight.frameCountValid) {
  128. InputDataGGT26Dof.handsInfo.handRightFind = true;
  129. } else if (InputDataGGT26Dof.handsInfo.handRight.isLost) {
  130. InputDataGGT26Dof.handsInfo.handRightFind = false;
  131. }
  132. }
  133. }
  134. }