Skip to content

Instantly share code, notes, and snippets.

@jamesu
Created June 14, 2016 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesu/ec9deeb12529a41f57b11eefa5fbad63 to your computer and use it in GitHub Desktop.
Save jamesu/ec9deeb12529a41f57b11eefa5fbad63 to your computer and use it in GitHub Desktop.
Current openvr base code for Torque3D
// Only load these functions if an Oculus VR device is present
if(!isFunction(OpenVRIsCompiledIn))
return;
$Video::VREnabled = false;
function setupOpenVRActionMaps()
{
new ActionMap(VRCanvasMap);
VRCanvasMap.bind( mouse, xaxis, vrCanvasYaw );
VRCanvasMap.bind( mouse, yaxis, vrCanvasPitch );
VRCanvasMap.bind( mouse, button0, vrCanvasClick );
}
function setupDefaultOpenVRBinds(%moveMap)
{
%moveMap.bind( openvr, opvr_sensorrot0, OVRSensorRotAA );
%moveMap.bind( openvr, opvr_sensorrot1, OVRSensorRotAA1 );
%moveMap.bind( openvr, opvr_sensorrot2, OVRSensorRotAA2 );
%moveMap.bind( openvr, opvr_sensorpos0, OVRSensorRotPos );
%moveMap.bind( openvr, opvr_sensorpos1, OVRSensorRotPos1 );
%moveMap.bind( openvr, opvr_sensorpos2, OVRSensorRotPos2 );
}
function OVRSensorRotPos(%x, %y, %z)
{
echo("OVRSensorRotPos ",%x,",",%y,",",%z);
$mvPosX0 = %x;
$mvPosY0 = %y;
$mvPosZ0 = %z;
}
function OVRSensorRotPos1(%x, %y, %z)
{
echo("OVRSensorRotPos1 ",%x,",",%y,",",%z);
$mvPosX1 = %x;
$mvPosY1 = %y;
$mvPosZ1 = %z;
}
function OVRSensorRotPos2(%x, %y, %z)
{
echo("OVRSensorRotPos2 ",%x,",",%y,",",%z);
$mvPosX2 = %x;
$mvPosY2 = %y;
$mvPosZ2 = %z;
}
function OVRSensorRotAA(%x, %y, %z, %a)
{
echo("OVRSensorRotAA ",%x,",",%y,",",%z,",",%a);
$mvRotX0 = %x;
$mvRotY0 = %y;
$mvRotZ0 = %z;
$mvRotA0 = %a;
}
function OVRSensorRotAA1(%x, %y, %z, %a)
{
echo("OVRSensorRotAA1 ",%x,",",%y,",",%z,",",%a);
$mvRotX1 = %x;
$mvRotY1 = %y;
$mvRotZ1 = %z;
$mvRotA1 = %a;
}
function OVRSensorRotAA2(%x, %y, %z, %a)
{
echo("OVRSensorRotAA2 ",%x,",",%y,",",%z,",",%a);
$mvRotX2 = %x;
$mvRotY2 = %y;
$mvRotZ2 = %z;
$mvRotA2 = %a;
}
function vrCanvasYaw(%val)
{
VRCanvas.cursorNudge(%val * 0.10, 0);
}
function vrCanvasPitch(%val)
{
VRCanvas.cursorNudge(0, %val * 0.10);
}
function vrCanvasClick(%active)
{
VRCanvas.cursorClick(0, %active);
}
function GuiOffscreenCanvas::checkCursor(%this)
{
%count = %this.getCount();
for(%i = 0; %i < %count; %i++)
{
%control = %this.getObject(%i);
if ((%control.noCursor $= "") || !%control.noCursor)
{
%this.cursorOn();
return true;
}
}
// If we get here, every control requested a hidden cursor, so we oblige.
%this.cursorOff();
return false;
}
function GuiOffscreenCanvas::pushDialog(%this, %ctrl, %layer, %center)
{
Parent::pushDialog(%this, %ctrl, %layer, %center);
%cursorVisible = %this.checkCursor();
if (%cursorVisible)
{
echo("OffscreenCanvas visible");
VRCanvasMap.pop();
VRCanvasMap.push();
}
else
{
echo("OffscreenCanvas not visible");
VRCanvasMap.pop();
}
}
function GuiOffscreenCanvas::popDialog(%this, %ctrl)
{
Parent::popDialog(%this, %ctrl);
%cursorVisible = %this.checkCursor();
if (%cursorVisible)
{
echo("OffscreenCanvas visible");
VRCanvasMap.pop();
VRCanvasMap.push();
}
else
{
echo("OffscreenCanvas not visible");
VRCanvasMap.pop();
}
}
//-----------------------------------------------------------------------------
function oculusSensorMetricsCallback()
{
return ovrDumpMetrics(0);
}
//-----------------------------------------------------------------------------
// Call this function from createCanvas() to have the Canvas attach itself
// to the Rift's display. The Canvas' window will still open on the primary
// display if that is different from the Rift, but it will move to the Rift
// when it goes full screen. If the Rift is not connected then nothing
// will happen.
function pointCanvasToVRDisplay()
{
$Video::forceDisplayAdapter = OpenVR::getDisplayDeviceId();
}
//-----------------------------------------------------------------------------
// Call this function from GameConnection::initialControlSet() just before
// your "Canvas.setContent(PlayGui);" call, or at any time you wish to switch
// to a side-by-side rendering and the appropriate barrel distortion. This
// will turn on side-by-side rendering and tell the GameConnection to use the
// Rift as its display device.
// Parameters:
// %gameConnection - The client GameConnection instance
// %trueStereoRendering - If true will enable stereo rendering with an eye
// offset for each viewport. This will render each frame twice. If false
// then a pseudo stereo rendering is done with only a single render per frame.
function enableOpenVRDisplay(%gameConnection, %trueStereoRendering)
{
$mvRotIsEuler0 = false;
$OculusVR::GenerateAngleAxisRotationEvents = true;
$OculusVR::GenerateEulerRotationEvents = false;
%oculus = 0;
OpenVR::setHMDAsGameConnectionDisplayDevice(%gameConnection);
PlayGui.renderStyle = "stereo separate";
//PlayGui.renderStyle = "stereo side by side";
VRSetupOverlay();
//$gfx::wireframe = true;
// Reset all sensors
OpenVR::resetSensors();
setupDefaultOpenVRBinds(MoveMap);
}
function VRSetupOverlay()
{
if (isObject(TestOpenVROverlayCanvas))
return;
if (!isObject(VROverlay))
{
exec("art/gui/VROverlay.gui");
}
if (!isObject(TestOpenVROverlayCanvas))
{
new OpenVROverlay(TestOpenVROverlayCanvas) {
internalName = "torque.testoffscreencanvas";
targetSize = "640 480";
dynamicTarget = true;
overlayType = "Dashboard";
transformType = "Absolute";
transformPosition = "0 -4 0";
transformRotation = "0 0 1 0";
trackingOrigin = "Seated";
inputMethod = "Mouse";
};
}
TestOpenVROverlayCanvas.setContent(VROverlay);
TestOpenVROverlayCanvas.setCursor(DefaultCursor);
$GameCanvas = TestOpenVROverlayCanvas;
TestOpenVROverlayCanvas.showOverlay();
}
// Call this function when ever you wish to turn off the stereo rendering
// and barrel distortion for the Rift.
function disableOpenVRDisplay(%gameConnection)
{
VRCanvas.popDialog();
$GameCanvas = Canvas;
if (isObject(gameConnection))
{
%gameConnection.clearDisplayDevice();
}
PlayGui.renderStyle = "standard";
}
// Helper function to set the standard Rift control scheme. You could place
// this function in GameConnection::initialControlSet() at the same time
// you call enableOculusVRDisplay().
function setStandardOpenVRControlScheme(%gameConnection)
{
$mvRotX0 = 0;
$mvRotY0 = 0;
$mvRotZ0 = 1;
$mvRotA0 = 0;
if($OpenVR::SimulateInput)
{
// We are simulating a HMD so allow the mouse and gamepad to control
// both yaw and pitch.
%gameConnection.setControlSchemeParameters(true, true, true);
}
else
{
// A HMD is connected so have the mouse and gamepad only add to yaw
%gameConnection.setControlSchemeParameters(true, true, false);
}
}
//-----------------------------------------------------------------------------
// Helper function to set the resolution for the Rift.
// Parameters:
// %fullscreen - If true then the display will be forced to full screen. If
// pointCanvasToOculusVRDisplay() was called before the Canvas was created, then
// the full screen display will appear on the Rift.
function setVideoModeForOculusVRDisplay(%fullscreen)
{
}
//-----------------------------------------------------------------------------
// Reset all Oculus Rift sensors. This will make the Rift's current heading
// be considered the origin.
function resetOpenVRSensors()
{
OpenVR::resetSensors();
}
// GuiTSCtrl optimal render style helper
function GuiTSCtrl::setOptimalRenderStyle()
{
if ($Video::VREnabled)
{
%openvrEnabled = OpenVR::isDeviceActive();
%useSBS = false;
if (%openvrEnabled && %useSBS)
{
echo("VR: Using stereo side-by-side view");
%this.renderStyle = "stereo side by side";
return;
}
else if (%openvrEnabled && !%useSBS)
{
echo("VR: Using stereo separate view");
%this.renderStyle = "stereo separate";
return;
}
}
echo("VR: using non-vr render style");
%this.renderStyle = "standard";
}
function testControllers()
{
%controllerIds = OpenVR::getControllerDeviceIndexes(Controller);
%leftController = getWord(%controllerIds, 0);
%rightController = getWord(%controllerIds, 1);
if (%leftController !$= "")
{
new OpenVRTrackedObject(OVRLeftController) {
deviceIndex = %leftController;
datablock = DefaultOpenVRController;
mappedMoveIndex = 1;
};
LocalClientConnection.getControlObject().mountObject(OVRLeftController, -1);
OVRLeftController.setModelName(OpenVR::getControllerModel(OVRLeftController.deviceIndex));
}
if (%rightController !$= "")
{
new OpenVRTrackedObject(OVRRightController) {
deviceIndex = %rightController;
datablock = DefaultOpenVRController;
mappedMoveIndex = 2;
};
LocalClientConnection.getControlObject().mountObject(OVRRightController, -1);
OVRRightController.setModelName(OpenVR::getControllerModel(OVRRightController.deviceIndex));
}
OpenVR::mapDeviceToEvent(0, 0); // HMD
OpenVR::mapDeviceToEvent(1, 1); // Left Controller
OpenVR::mapDeviceToEvent(2, 2); // Right Controller
LocalClientConnection.player.setVRControllers(OVRLeftController, OVRRightController);
setupDefaultOpenVRBinds(MoveMap);
}
// OpenVR datablocks for controller shape override
datablock OpenVRTrackedObjectData(DefaultOpenVRController)
{
//shape = "";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment