SvrPluginWin.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. class SvrPluginWin : SvrPlugin
  6. {
  7. public static SvrPluginWin Create()
  8. {
  9. return new SvrPluginWin ();
  10. }
  11. private SvrPluginWin() { }
  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 override int GetHeadPose(ref HeadPose headPose, int frameIndex)
  34. {
  35. int poseStatus = 0;
  36. headPose.orientation = Quaternion.identity;
  37. headPose.position = Vector3.zero;
  38. //Debug.Log("Input.mousePosition:"+ Input.mousePosition+" "+ Screen.width+" "+Screen.height);
  39. //if (Input.GetMouseButton(0)) // 0/Left mouse button
  40. //{
  41. // poseStatus |= (int)TrackingMode.kTrackingOrientation;
  42. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  43. //}
  44. //if(Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) // 1/Right mouse button
  45. //{
  46. // mousePressPointTemp1 = Input.mousePosition;
  47. // mousePressEuler = SvrManager.Instance.head.eulerAngles;
  48. //} else if(Input.GetMouseButton(0) || Input.GetMouseButton(1)) {
  49. // mouseNDCRotate.x = 2 * ((Input.mousePosition.x - mousePressPointTemp1.x) / Screen.width) ;
  50. // mouseNDCRotate.y = 2 * ((Input.mousePosition.y - mousePressPointTemp1.y) / Screen.height) ;
  51. // poseStatus |= (int)TrackingMode.kTrackingOrientation;
  52. //}
  53. //if(Input.GetKey(KeyCode.W)) {
  54. // mouseNDCPosition.y += Time.deltaTime * 0.2f;
  55. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  56. //} else if(Input.GetKey(KeyCode.S)) {
  57. // mouseNDCPosition.y -= Time.deltaTime * 0.2f;
  58. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  59. //} else {
  60. // mouseNDCPosition.y = 0;
  61. //}
  62. //if(Input.GetKey(KeyCode.A)) {
  63. // mouseNDCPosition.x -= Time.deltaTime * 0.2f;
  64. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  65. //} else if(Input.GetKey(KeyCode.D)) {
  66. // mouseNDCPosition.x += Time.deltaTime * 0.2f;
  67. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  68. //} else {
  69. // mouseNDCPosition.x = 0;
  70. //}
  71. ////if(Input.mouseScrollDelta.y != 0) {
  72. //// mouseNDCPosition.x = 0;
  73. //// mouseNDCPosition.y = Input.mouseScrollDelta.y * 0.2f;
  74. //// poseStatus |= (int)TrackingMode.kTrackingPosition;
  75. ////} else {
  76. //// mouseNDCPosition = Vector2.zero;
  77. ////}
  78. /////复位
  79. //if(Input.GetKey(KeyCode.Escape) == true) {
  80. // mouseNDCRotate = Vector2.zero;
  81. // mouseNDCPosition = Vector2.zero;
  82. // mousePressPointTemp1 = Vector2.zero;
  83. // mousePressEuler = Vector3.zero;
  84. // SvrManager.Instance.head.position = Vector3.zero;
  85. // poseStatus |= (int)TrackingMode.kTrackingOrientation;
  86. // poseStatus |= (int)TrackingMode.kTrackingPosition;
  87. //}
  88. headPose.orientation.eulerAngles = mousePressEuler + new Vector3(-mouseNDCRotate.y * 45f, mouseNDCRotate.x * 90f, 0);
  89. headPose.position = new Vector3(mouseNDCPosition.x,0, mouseNDCPosition.y);
  90. headPose.position = SvrManager.Instance.head.TransformPoint(headPose.position);
  91. return poseStatus;
  92. }
  93. public override DeviceInfo GetDeviceInfo()
  94. {
  95. DeviceInfo info = new DeviceInfo();
  96. info.displayWidthPixels = Screen.width;
  97. info.displayHeightPixels = Screen.height;
  98. info.displayRefreshRateHz = 60.0f;
  99. info.targetEyeWidthPixels = Screen.width / 2;
  100. info.targetEyeHeightPixels = Screen.height;
  101. info.targetFovXRad = Mathf.Deg2Rad * 47;
  102. info.targetFovYRad = Mathf.Deg2Rad * 20.1f;
  103. info.targetFrustumLeft.left = -0.02208847f;
  104. info.targetFrustumLeft.right = 0.02208847f;
  105. info.targetFrustumLeft.top = 0.0123837f;
  106. info.targetFrustumLeft.bottom = -0.0123837f;
  107. info.targetFrustumLeft.near = 0.0508f;
  108. info.targetFrustumLeft.far = 100f;
  109. info.targetFrustumRight.left = -0.02208847f;
  110. info.targetFrustumRight.right = 0.02208847f;
  111. info.targetFrustumRight.top = 0.0123837f;
  112. info.targetFrustumRight.bottom = -0.0123837f;
  113. info.targetFrustumRight.near = 0.0508f;
  114. info.targetFrustumRight.far = 100f;
  115. return info;
  116. }
  117. public override void SubmitFrame(int frameIndex, float fieldOfView, int frameType)
  118. {
  119. RenderTexture.active = null;
  120. GL.Clear (false, true, Color.black);
  121. //float cameraFov = fieldOfView;
  122. //float fovMarginX = (cameraFov / deviceInfo.targetFovXRad) - 1;
  123. //float fovMarginY = (cameraFov / deviceInfo.targetFovYRad) - 1;
  124. //Rect textureRect = new Rect(fovMarginX * 0.5f, fovMarginY * 0.5f, 1 - fovMarginX, 1 - fovMarginY);
  125. Rect textureRect = new Rect(0, 0, 1, 1);
  126. Vector2 leftCenter = new Vector2(Screen.width * 0.25f, Screen.height * 0.5f);
  127. Vector2 rightCenter = new Vector2(Screen.width * 0.75f, Screen.height * 0.5f);
  128. Vector2 eyeExtent = new Vector3(Screen.width * 0.25f, Screen.height * 0.5f);
  129. eyeExtent.x -= 10.0f;
  130. eyeExtent.y -= 10.0f;
  131. Rect leftScreen = Rect.MinMaxRect(
  132. leftCenter.x - eyeExtent.x,
  133. leftCenter.y - eyeExtent.y,
  134. leftCenter.x + eyeExtent.x,
  135. leftCenter.y + eyeExtent.y);
  136. Rect rightScreen = Rect.MinMaxRect(
  137. rightCenter.x - eyeExtent.x,
  138. rightCenter.y - eyeExtent.y,
  139. rightCenter.x + eyeExtent.x,
  140. rightCenter.y + eyeExtent.y);
  141. if (eyes != null) for (int i = 0; i < eyes.Length; i++)
  142. {
  143. if (eyes[i].isActiveAndEnabled == false) continue;
  144. if (eyes[i].TexturePtr == null) continue;
  145. if (eyes[i].imageTransform != null && eyes[i].imageTransform.gameObject.activeSelf == false) continue;
  146. if (eyes[i].imageTransform != null && !eyes[i].imageTransform.IsChildOf(svrCamera.transform)) continue; // svr only
  147. var eyeRectMin = eyes[i].clipLowerLeft; eyeRectMin /= eyeRectMin.w;
  148. var eyeRectMax = eyes[i].clipUpperRight; eyeRectMax /= eyeRectMax.w;
  149. if (eyes[i].Side == SvrEye.eSide.Left || eyes[i].Side == SvrEye.eSide.Both)
  150. {
  151. leftScreen = Rect.MinMaxRect(
  152. leftCenter.x + eyeExtent.x * eyeRectMin.x,
  153. leftCenter.y + eyeExtent.y * eyeRectMin.y,
  154. leftCenter.x + eyeExtent.x * eyeRectMax.x,
  155. leftCenter.y + eyeExtent.y * eyeRectMax.y);
  156. Graphics.DrawTexture(leftScreen, eyes[i].TexturePtr, textureRect, 0, 0, 0, 0);
  157. }
  158. if (eyes[i].Side == SvrEye.eSide.Right || eyes[i].Side == SvrEye.eSide.Both)
  159. {
  160. rightScreen = Rect.MinMaxRect(
  161. rightCenter.x + eyeExtent.x * eyeRectMin.x,
  162. rightCenter.y + eyeExtent.y * eyeRectMin.y,
  163. rightCenter.x + eyeExtent.x * eyeRectMax.x,
  164. rightCenter.y + eyeExtent.y * eyeRectMax.y);
  165. Graphics.DrawTexture(rightScreen, eyes[i].TexturePtr, textureRect, 0, 0, 0, 0);
  166. }
  167. }
  168. if (overlays != null) for (int i = 0; i < overlays.Length; i++)
  169. {
  170. if (overlays[i].isActiveAndEnabled == false) continue;
  171. if (overlays[i].TexturePtr == null) continue;
  172. if (overlays[i].imageTransform != null && overlays[i].imageTransform.gameObject.activeSelf == false) continue;
  173. if (overlays[i].imageTransform != null && !overlays[i].imageTransform.IsChildOf(svrCamera.transform)) continue; // svr only
  174. var eyeRectMin = overlays[i].clipLowerLeft; eyeRectMin /= eyeRectMin.w;
  175. var eyeRectMax = overlays[i].clipUpperRight; eyeRectMax /= eyeRectMax.w;
  176. textureRect.Set(overlays[i].uvLowerLeft.x, overlays[i].uvLowerLeft.y,
  177. overlays[i].uvUpperRight.x - overlays[i].uvLowerLeft.x,
  178. overlays[i].uvUpperRight.y - overlays[i].uvLowerLeft.y);
  179. if (overlays[i].Side == SvrOverlay.eSide.Left || overlays[i].Side == SvrOverlay.eSide.Both)
  180. {
  181. leftScreen = Rect.MinMaxRect(
  182. leftCenter.x + eyeExtent.x * eyeRectMin.x,
  183. leftCenter.y + eyeExtent.y * eyeRectMin.y,
  184. leftCenter.x + eyeExtent.x * eyeRectMax.x,
  185. leftCenter.y + eyeExtent.y * eyeRectMax.y);
  186. Graphics.DrawTexture(leftScreen, overlays[i].TexturePtr, textureRect, 0, 0, 0, 0);
  187. }
  188. if (overlays[i].Side == SvrOverlay.eSide.Right || overlays[i].Side == SvrOverlay.eSide.Both)
  189. {
  190. rightScreen = Rect.MinMaxRect(
  191. rightCenter.x + eyeExtent.x * eyeRectMin.x,
  192. rightCenter.y + eyeExtent.y * eyeRectMin.y,
  193. rightCenter.x + eyeExtent.x * eyeRectMax.x,
  194. rightCenter.y + eyeExtent.y * eyeRectMax.y);
  195. Graphics.DrawTexture(rightScreen, overlays[i].TexturePtr, textureRect, 0, 0, 0, 0);
  196. }
  197. }
  198. }
  199. public override int HandShank_Getbond(int lr) { return 17; }
  200. public override void Shutdown()
  201. {
  202. base.Shutdown();
  203. }
  204. }