GameControllerDevicePartManipulation.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using SC.XR.Unity.Module_InputSystem;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. public class GameControllerDevicePartManipulation : AbstractDevicePartManipulation
  7. {
  8. private SCPointEventData eventData;
  9. private SCPose pointerPose;
  10. private Quaternion startRotation;
  11. private Quaternion localRotationInGameController;
  12. private Transform oneControllerTransform;
  13. public override void OneDevicePartInit(ManipulationHandler manipulationHandler, Dictionary<InputDevicePartType, SCPointEventData> eventDataDic, Transform targetTransform, MoveLogic moveLogic, RotateLogic rotateLogic, ScaleLogic scaleLogic)
  14. {
  15. base.OneDevicePartInit(manipulationHandler, eventDataDic, targetTransform, moveLogic, rotateLogic, scaleLogic);
  16. this.eventData = this.eventDataDic.Values.ToArray()[0];
  17. pointerPose = new SCPose(Vector3.zero, Quaternion.identity);
  18. Vector3 grabPosition = Vector3.zero;
  19. pointerPose.position = this.eventData.Position3D;
  20. pointerPose.rotation = Quaternion.identity;
  21. grabPosition = this.eventData.Position3D;
  22. oneControllerTransform = this.eventData.inputDevicePartBase.inputDevicePartUIBase.modelBase.transform;
  23. startRotation = targetTransform.rotation;
  24. localRotationInGameController = Quaternion.Inverse(oneControllerTransform.rotation) * startRotation;
  25. moveLogic.Setup(pointerPose, grabPosition, targetTransform, targetTransform.localScale);
  26. }
  27. public override Tuple<Vector3, Quaternion, Vector3> OneDevicePartUpdate()
  28. {
  29. pointerPose.position = this.eventData.Position3D;
  30. pointerPose.rotation = startRotation;
  31. Quaternion rotation = targetTransform.rotation;
  32. if (manipulationHandler.oneGameControllerRotate)
  33. {
  34. rotation = oneControllerTransform.rotation * localRotationInGameController;
  35. }
  36. Vector3 position = moveLogic.Update(pointerPose, rotation, targetTransform.localScale, true); ;
  37. return new Tuple<Vector3, Quaternion, Vector3>(position, rotation, targetTransform.localScale);
  38. }
  39. private Dictionary<InputDevicePartType, Vector3> position3DDic;
  40. private SCPose[] position3DPoses = new SCPose[2];
  41. public override void TwoDevicePartInit(ManipulationHandler manipulationHandler, Dictionary<InputDevicePartType, SCPointEventData> eventDataDic, Transform targetTransform, MoveLogic moveLogic, RotateLogic rotateLogic, ScaleLogic scaleLogic)
  42. {
  43. base.TwoDevicePartInit(manipulationHandler, eventDataDic, targetTransform, moveLogic, rotateLogic, scaleLogic);
  44. if (position3DDic == null)
  45. {
  46. position3DDic = new Dictionary<InputDevicePartType, Vector3>();
  47. }
  48. position3DDic.Clear();
  49. foreach (var eventData in eventDataDic)
  50. {
  51. position3DDic.Add(eventData.Key, eventData.Value.Position3D);
  52. }
  53. if (manipulationHandler.canTwoHandScale)
  54. {
  55. scaleLogic.Setup(eventDataDic.Values.Select(item => item.Position3D).ToArray(), targetTransform);
  56. }
  57. if (manipulationHandler.canTwoHandRotate)
  58. {
  59. rotateLogic.Setup(eventDataDic.Values.Select(item => item.Position3D).ToArray(), targetTransform);
  60. }
  61. int count = 0;
  62. foreach (SCPointEventData eventDataItem in eventDataDic.Values)
  63. {
  64. position3DPoses[count] = new SCPose(eventDataItem.Position3D, Quaternion.identity);
  65. count++;
  66. }
  67. SCPose pointerCentroidPose = GetHandTipPointCentroid(position3DPoses);
  68. Vector3 grabCentroid = GetRaycastPointCentroid(position3DDic.Values.ToArray());
  69. moveLogic.Setup(pointerCentroidPose, grabCentroid, targetTransform, targetTransform.localScale);
  70. }
  71. public override Tuple<Vector3, Quaternion, Vector3> TwoDevicePartUpdate(Func<Vector3, Vector3> scaleConstraint)
  72. {
  73. Vector3 scale = targetTransform.localScale;
  74. if (manipulationHandler.canTwoHandScale)
  75. {
  76. scale = scaleLogic.UpdateMap(eventDataDic.Values.Select(item => item.Position3D).ToArray());
  77. }
  78. scale = scaleConstraint(scale);
  79. Quaternion rotation = targetTransform.rotation;
  80. if (manipulationHandler.canTwoHandRotate)
  81. {
  82. rotation = rotateLogic.Update(eventDataDic.Values.Select(item => item.Position3D).ToArray(), rotation);
  83. }
  84. int count = 0;
  85. foreach (SCPointEventData eventDataItem in eventDataDic.Values)
  86. {
  87. position3DPoses[count].position = eventDataItem.Position3D;
  88. position3DPoses[count].rotation = Quaternion.identity;//handTipTransformArray[count].rotation;
  89. count++;
  90. }
  91. SCPose pointerCentroidPose = GetHandTipPointCentroid(position3DPoses);
  92. Vector3 position = moveLogic.Update(pointerCentroidPose, rotation, scale, true);
  93. return new Tuple<Vector3, Quaternion, Vector3>(position, rotation, scale);
  94. }
  95. private SCPose GetHandTipPointCentroid(SCPose[] tipPoints)
  96. {
  97. Vector3 sumPos = Vector3.zero;
  98. Vector3 sumDir = Vector3.zero;
  99. int count = tipPoints.Length;
  100. for (int i = 0; i < count; i++)
  101. {
  102. sumPos += tipPoints[i].position;
  103. sumDir += tipPoints[i].rotation * Vector3.forward;
  104. }
  105. Vector3 resultPos = sumPos / Math.Max(1, count);
  106. Quaternion resultRot = Quaternion.LookRotation(sumDir / Math.Max(1, count));
  107. return new SCPose(resultPos, resultRot);
  108. }
  109. private Vector3 GetRaycastPointCentroid(Vector3[] raycastPoints)
  110. {
  111. Vector3 sum = Vector3.zero;
  112. int count = raycastPoints.Length;
  113. for (int i = 0; i < count; i++)
  114. {
  115. sum += raycastPoints[i];
  116. }
  117. return sum / Math.Max(1, count);
  118. }
  119. }