GSXRPluginAndroidOther.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. class GSXRPluginAndroidOther : GSXRPlugin
  6. {
  7. public static GSXRPluginAndroidOther Create()
  8. {
  9. return new GSXRPluginAndroidOther();
  10. }
  11. private GSXRPluginAndroidOther() { }
  12. public override bool IsInitialized() { return svrCamera != null; }
  13. public override bool IsRunning() { return eyes != null; }
  14. public override IEnumerator Initialize()
  15. {
  16. yield return base.Initialize();
  17. deviceInfo = GetDeviceInfo();
  18. yield break;
  19. }
  20. public override IEnumerator BeginVr(int cpuPerfLevel, int gpuPerfLevel)
  21. {
  22. yield return base.BeginVr(cpuPerfLevel, gpuPerfLevel);
  23. yield break;
  24. }
  25. public override void SetVSyncCount(int vSyncCount)
  26. {
  27. QualitySettings.vSyncCount = vSyncCount;
  28. }
  29. Vector2 mouseNDCRotate = Vector2.zero;
  30. Vector2 mouseNDCPosition = Vector2.zero;
  31. Vector2 mousePressPointTemp1 = Vector2.zero;
  32. Vector3 mousePressEuler = Vector3.zero;
  33. public Vector3 GetPosition {
  34. get {
  35. if (Input.touchCount >= 1) {
  36. return Camera.main.ScreenToWorldPoint(Input.touches[0].position);
  37. } else if (Input.mousePresent) {
  38. ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  39. Debug.DrawRay(ray.origin, ray.direction, Color.white);
  40. return Camera.main.ScreenToWorldPoint(Input.mousePosition);
  41. }
  42. return Vector3.zero;
  43. }
  44. }
  45. Ray ray;
  46. public Quaternion GetRotation {
  47. get {
  48. if (Input.touchCount >= 1) {
  49. ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
  50. return Quaternion.LookRotation(ray.direction, Camera.main.transform.up);
  51. } else if (Input.mousePresent) {
  52. ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  53. Debug.DrawRay(ray.origin, ray.direction, Color.yellow);
  54. return Quaternion.LookRotation(ray.direction, Camera.main.transform.up);
  55. }
  56. return Quaternion.identity;
  57. }
  58. }
  59. public override int GetHeadPose(ref HeadPose headPose, int frameIndex)
  60. {
  61. int poseStatus = 0;
  62. headPose.orientation = GetRotation;
  63. headPose.position = GetPosition;
  64. poseStatus |= (int)TrackingMode.kTrackingOrientation;
  65. poseStatus |= (int)TrackingMode.kTrackingPosition;
  66. //Debug.Log("Input.mousePosition:"+ Input.mousePosition+" "+ Screen.width+" "+Screen.height);
  67. //if (Input.GetMouseButton(0)) // 0/Left mouse button
  68. //{
  69. // poseStatus |= (int)TrackingMode.kTrackingOrientation;
  70. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  71. //}
  72. //if(Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) // 1/Right mouse button
  73. //{
  74. // mousePressPointTemp1 = Input.mousePosition;
  75. // mousePressEuler = GSXRManager.Instance.head.eulerAngles;
  76. //} else if(Input.GetMouseButton(0) || Input.GetMouseButton(1)) {
  77. // mouseNDCRotate.x = 2 * ((Input.mousePosition.x - mousePressPointTemp1.x) / Screen.width) ;
  78. // mouseNDCRotate.y = 2 * ((Input.mousePosition.y - mousePressPointTemp1.y) / Screen.height) ;
  79. // poseStatus |= (int)TrackingMode.kTrackingOrientation;
  80. //}
  81. //if(Input.GetKey(KeyCode.W)) {
  82. // mouseNDCPosition.y += Time.deltaTime * 0.2f;
  83. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  84. //} else if(Input.GetKey(KeyCode.S)) {
  85. // mouseNDCPosition.y -= Time.deltaTime * 0.2f;
  86. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  87. //} else {
  88. // mouseNDCPosition.y = 0;
  89. //}
  90. //if(Input.GetKey(KeyCode.A)) {
  91. // mouseNDCPosition.x -= Time.deltaTime * 0.2f;
  92. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  93. //} else if(Input.GetKey(KeyCode.D)) {
  94. // mouseNDCPosition.x += Time.deltaTime * 0.2f;
  95. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  96. //} else {
  97. // mouseNDCPosition.x = 0;
  98. //}
  99. ////if(Input.mouseScrollDelta.y != 0) {
  100. //// mouseNDCPosition.x = 0;
  101. //// mouseNDCPosition.y = Input.mouseScrollDelta.y * 0.2f;
  102. //// poseStatus |= (int)TrackingMode.kTrackingPosition;
  103. ////} else {
  104. //// mouseNDCPosition = Vector2.zero;
  105. ////}
  106. /////复位
  107. //if(Input.GetKey(KeyCode.Escape) == true) {
  108. // mouseNDCRotate = Vector2.zero;
  109. // mouseNDCPosition = Vector2.zero;
  110. // mousePressPointTemp1 = Vector2.zero;
  111. // mousePressEuler = Vector3.zero;
  112. // GSXRManager.Instance.head.position = Vector3.zero;
  113. // poseStatus |= (int)TrackingMode.kTrackingOrientation;
  114. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  115. //}
  116. //headPose.orientation.eulerAngles = mousePressEuler + new Vector3(-mouseNDCRotate.y * 45f, mouseNDCRotate.x * 90f, 0);
  117. //headPose.position = new Vector3(mouseNDCPosition.x,0, mouseNDCPosition.y);
  118. //headPose.position = GSXRManager.Instance.head.TransformPoint(headPose.position);
  119. return poseStatus;
  120. }
  121. public override DeviceInfo GetDeviceInfo()
  122. {
  123. DeviceInfo info = new DeviceInfo();
  124. info.displayWidthPixels = Screen.width;
  125. info.displayHeightPixels = Screen.height;
  126. info.displayRefreshRateHz = 60.0f;
  127. info.targetEyeWidthPixels = Screen.width / 2;
  128. info.targetEyeHeightPixels = Screen.height;
  129. info.targetFovXRad = Mathf.Deg2Rad * 47;
  130. info.targetFovYRad = Mathf.Deg2Rad * 20.1f;
  131. info.targetFrustumLeft.left = -0.02208847f;
  132. info.targetFrustumLeft.right = 0.02208847f;
  133. info.targetFrustumLeft.top = 0.0123837f;
  134. info.targetFrustumLeft.bottom = -0.0123837f;
  135. info.targetFrustumLeft.near = 0.0508f;
  136. info.targetFrustumLeft.far = 100f;
  137. info.targetFrustumRight.left = -0.02208847f;
  138. info.targetFrustumRight.right = 0.02208847f;
  139. info.targetFrustumRight.top = 0.0123837f;
  140. info.targetFrustumRight.bottom = -0.0123837f;
  141. info.targetFrustumRight.near = 0.0508f;
  142. info.targetFrustumRight.far = 100f;
  143. return info;
  144. }
  145. public override void SubmitFrame(int frameIndex, float fieldOfView, int frameType)
  146. {
  147. }
  148. public override void Shutdown()
  149. {
  150. base.Shutdown();
  151. }
  152. }