oculusVR.tscript 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // Only load these functions if an Oculus VR device is present
  23. if(!isFunction(isOculusVRDeviceActive))
  24. return;
  25. function setupOculusActionMaps()
  26. {
  27. if (isObject(OculusWarningMap))
  28. return;
  29. new ActionMap(OculusWarningMap);
  30. new ActionMap(OculusCanvasMap);
  31. OculusWarningMap.bind(keyboard, space, dismissOculusVRWarnings);
  32. OculusCanvasMap.bind( mouse, xaxis, oculusYaw );
  33. OculusCanvasMap.bind( mouse, yaxis, oculusPitch );
  34. OculusCanvasMap.bind( mouse, button0, oculusClick );
  35. }
  36. function oculusYaw(%val)
  37. {
  38. OculusCanvas.cursorNudge(%val * 0.10, 0);
  39. }
  40. function oculusPitch(%val)
  41. {
  42. OculusCanvas.cursorNudge(0, %val * 0.10);
  43. }
  44. function oculusClick(%active)
  45. {
  46. OculusCanvas.cursorClick(0, %active);
  47. }
  48. function GuiOffscreenCanvas::checkCursor(%this)
  49. {
  50. %count = %this.getCount();
  51. for(%i = 0; %i < %count; %i++)
  52. {
  53. %control = %this.getObject(%i);
  54. if ((%control.noCursor $= "") || !%control.noCursor)
  55. {
  56. %this.cursorOn();
  57. return true;
  58. }
  59. }
  60. // If we get here, every control requested a hidden cursor, so we oblige.
  61. %this.cursorOff();
  62. return false;
  63. }
  64. function GuiOffscreenCanvas::pushDialog(%this, %ctrl, %layer, %center)
  65. {
  66. Parent::pushDialog(%this, %ctrl, %layer, %center);
  67. %cursorVisible = %this.checkCursor();
  68. if (%cursorVisible)
  69. {
  70. echo("OffscreenCanvas visible");
  71. OculusCanvasMap.pop();
  72. OculusCanvasMap.push();
  73. }
  74. else
  75. {
  76. echo("OffscreenCanvas not visible");
  77. OculusCanvasMap.pop();
  78. }
  79. }
  80. function GuiOffscreenCanvas::popDialog(%this, %ctrl)
  81. {
  82. Parent::popDialog(%this, %ctrl);
  83. %cursorVisible = %this.checkCursor();
  84. if (%cursorVisible)
  85. {
  86. echo("OffscreenCanvas visible");
  87. OculusCanvasMap.pop();
  88. OculusCanvasMap.push();
  89. }
  90. else
  91. {
  92. echo("OffscreenCanvas not visible");
  93. OculusCanvasMap.pop();
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. function oculusSensorMetricsCallback()
  98. {
  99. return ovrDumpMetrics(0);
  100. }
  101. //-----------------------------------------------------------------------------
  102. function onOculusStatusUpdate(%status)
  103. {
  104. $LastOculusTrackingState = %status;
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Call this function from createCanvas() to have the Canvas attach itself
  108. // to the Rift's display. The Canvas' window will still open on the primary
  109. // display if that is different from the Rift, but it will move to the Rift
  110. // when it goes full screen. If the Rift is not connected then nothing
  111. // will happen.
  112. function pointCanvasToOculusVRDisplay()
  113. {
  114. $pref::Video::displayOutputDevice = getOVRHMDDisplayDeviceName(0);
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Call this function from GameConnection::initialControlSet() just before
  118. // your "Canvas.setContent(PlayGui);" call, or at any time you wish to switch
  119. // to a side-by-side rendering and the appropriate barrel distortion. This
  120. // will turn on side-by-side rendering and tell the GameConnection to use the
  121. // Rift as its display device.
  122. // Parameters:
  123. // %gameConnection - The client GameConnection instance
  124. // %trueStereoRendering - If true will enable stereo rendering with an eye
  125. // offset for each viewport. This will render each frame twice. If false
  126. // then a pseudo stereo rendering is done with only a single render per frame.
  127. function enableOculusVRDisplay(%gameConnection, %trueStereoRendering)
  128. {
  129. setOVRHMDAsGameConnectionDisplayDevice(%gameConnection);
  130. PlayGui.renderStyle = "stereo side by side";
  131. setOptimalOVRCanvasSize(Canvas);
  132. if (!isObject(OculusCanvas))
  133. {
  134. new GuiOffscreenCanvas(OculusCanvas) {
  135. targetSize = "512 512";
  136. targetName = "oculusCanvas";
  137. dynamicTarget = true;
  138. };
  139. }
  140. if (!isObject(OculusVROverlay))
  141. {
  142. exec("core/vr/guis/oculusVROverlay.gui");
  143. }
  144. OculusCanvas.setContent(OculusVROverlay);
  145. OculusCanvas.setCursor(DefaultCursor);
  146. PlayGui.setStereoGui(OculusCanvas);
  147. OculusCanvas.setCursorPos("128 128");
  148. OculusCanvas.cursorOff();
  149. $GameCanvas = OculusCanvas;
  150. %ext = Canvas.getExtent();
  151. $OculusMouseScaleX = 512.0 / 1920.0;
  152. $OculusMouseScaleY = 512.0 / 1060.0;
  153. //$gfx::wireframe = true;
  154. // Reset all sensors
  155. ovrResetAllSensors();
  156. }
  157. // Call this function when ever you wish to turn off the stereo rendering
  158. // and barrel distortion for the Rift.
  159. function disableOculusVRDisplay(%gameConnection)
  160. {
  161. OculusCanvas.popDialog();
  162. OculusWarningMap.pop();
  163. $GameCanvas = Canvas;
  164. if (isObject(gameConnection))
  165. {
  166. %gameConnection.clearDisplayDevice();
  167. }
  168. PlayGui.renderStyle = "standard";
  169. }
  170. // Helper function to set the standard Rift control scheme. You could place
  171. // this function in GameConnection::initialControlSet() at the same time
  172. // you call enableOculusVRDisplay().
  173. function setStandardOculusVRControlScheme(%gameConnection)
  174. {
  175. if($OculusVR::SimulateInput)
  176. {
  177. // We are simulating a HMD so allow the mouse and gamepad to control
  178. // both yaw and pitch.
  179. %gameConnection.setControlSchemeParameters(true, true, true);
  180. }
  181. else
  182. {
  183. // A HMD is connected so have the mouse and gamepad only add to yaw
  184. %gameConnection.setControlSchemeParameters(true, true, false);
  185. }
  186. }
  187. //-----------------------------------------------------------------------------
  188. // Helper function to set the resolution for the Rift.
  189. // Parameters:
  190. // %fullscreen - If true then the display will be forced to full screen. If
  191. // pointCanvasToOculusVRDisplay() was called before the Canvas was created, then
  192. // the full screen display will appear on the Rift.
  193. function setVideoModeForOculusVRDisplay(%fullscreen)
  194. {
  195. %res = getOVRHMDResolution(0);
  196. Canvas.setVideoMode(%res.x, %res.y, %fullscreen, 32, 4);
  197. }
  198. //-----------------------------------------------------------------------------
  199. // Reset all Oculus Rift sensors. This will make the Rift's current heading
  200. // be considered the origin.
  201. function resetOculusVRSensors()
  202. {
  203. ovrResetAllSensors();
  204. }
  205. function dismissOculusVRWarnings(%value)
  206. {
  207. //if (%value)
  208. //{
  209. ovrDismissWarnings();
  210. OculusWarningMap.pop();
  211. //}
  212. }