forked from LeenkxTeam/LNXSDK
Patch_1
This commit is contained in:
@ -3,6 +3,14 @@ package iron.system;
|
||||
class Time {
|
||||
public static var scale = 1.0;
|
||||
|
||||
// TODO: VR Frame Time Override - used to sync physics with VR headset refresh rate
|
||||
#if lnx_vr
|
||||
public static var vrFrameTime: Float = -1.0; // VR frame time in seconds (-1 = not in VR)
|
||||
static var lastVRFrameTime: Float = 0.0;
|
||||
static var vrFrameCount: Int = 0;
|
||||
static var normalModeLogged: Bool = false;
|
||||
#end
|
||||
|
||||
static var frequency: Null<Int> = null;
|
||||
static function initFrequency() {
|
||||
frequency = kha.Display.primary != null ? kha.Display.primary.frequency : 60;
|
||||
@ -47,6 +55,24 @@ class Time {
|
||||
}
|
||||
|
||||
public static function update() {
|
||||
#if lnx_vr
|
||||
// TODO: use VR frame time when in VR present mode to sync physics with headset refresh
|
||||
if (vrFrameTime >= 0.0) {
|
||||
if (lastVRFrameTime > 0.0) {
|
||||
_delta = vrFrameTime - lastVRFrameTime;
|
||||
} else {
|
||||
_delta = 1.0 / 90.0; // Default to 90Hz for first VR frame
|
||||
}
|
||||
lastVRFrameTime = vrFrameTime;
|
||||
|
||||
return;
|
||||
} else {
|
||||
if (!normalModeLogged) {
|
||||
normalModeLogged = true;
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
||||
_delta = realTime() - lastTime;
|
||||
lastTime = realTime();
|
||||
}
|
||||
|
||||
138
leenkx/Sources/iron/system/VRController.hx
Normal file
138
leenkx/Sources/iron/system/VRController.hx
Normal file
@ -0,0 +1,138 @@
|
||||
package iron.system;
|
||||
|
||||
#if lnx_vr
|
||||
import iron.math.Vec4;
|
||||
import iron.math.Quat;
|
||||
|
||||
class VRController {
|
||||
|
||||
public static var leftHandPosition: Vec4 = new Vec4();
|
||||
public static var leftHandRotation: Quat = new Quat();
|
||||
public static var rightHandPosition: Vec4 = new Vec4();
|
||||
public static var rightHandRotation: Quat = new Quat();
|
||||
|
||||
public static var leftHandActive: Bool = false;
|
||||
public static var rightHandActive: Bool = false;
|
||||
|
||||
public static var leftThumbstickX: Float = 0.0;
|
||||
public static var leftThumbstickY: Float = 0.0;
|
||||
public static var rightThumbstickX: Float = 0.0;
|
||||
public static var rightThumbstickY: Float = 0.0;
|
||||
|
||||
public static var leftTrigger: Float = 0.0;
|
||||
public static var rightTrigger: Float = 0.0;
|
||||
public static var leftGrip: Float = 0.0;
|
||||
public static var rightGrip: Float = 0.0;
|
||||
|
||||
public static var leftButtonX: Bool = false;
|
||||
public static var leftButtonY: Bool = false;
|
||||
public static var rightButtonA: Bool = false;
|
||||
public static var rightButtonB: Bool = false;
|
||||
|
||||
public static var debugLog:Bool = false;
|
||||
|
||||
public static function enableDebug() {
|
||||
debugLog = true;
|
||||
}
|
||||
|
||||
public static function disableDebug() {
|
||||
debugLog = false;
|
||||
}
|
||||
|
||||
public static function updatePoses() {
|
||||
var vr: kha.js.vr.VrInterface = cast kha.vr.VrInterface.instance;
|
||||
if (vr == null || !vr.IsPresenting()) {
|
||||
if (debugLog) trace("[VRController] Not presenting or VR null");
|
||||
leftHandActive = false;
|
||||
rightHandActive = false;
|
||||
return;
|
||||
}
|
||||
|
||||
untyped window._vrControllerFrame = (untyped window._vrControllerFrame || 0) + 1;
|
||||
|
||||
leftHandActive = false;
|
||||
rightHandActive = false;
|
||||
|
||||
leftButtonX = false;
|
||||
leftButtonY = false;
|
||||
rightButtonA = false;
|
||||
rightButtonB = false;
|
||||
|
||||
var refSpace = untyped vr.xrRefSpace;
|
||||
if (vr.currentInputSources == null || vr.currentFrame == null || refSpace == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var inputSources = vr.currentInputSources;
|
||||
var frame = vr.currentFrame;
|
||||
|
||||
var sourceCount:Int = untyped inputSources.length;
|
||||
|
||||
for (i in 0...sourceCount) {
|
||||
var inputSource = untyped inputSources[i];
|
||||
if (inputSource == null) continue;
|
||||
|
||||
var handedness = untyped inputSource.handedness; // "left", "right", or "none"
|
||||
|
||||
var gripSpace = untyped inputSource.gripSpace;
|
||||
var targetRaySpace = untyped inputSource.targetRaySpace;
|
||||
// use targetRaySpace first laser/pointer and fall back to gripSpace
|
||||
var space = (targetRaySpace != null) ? targetRaySpace : gripSpace;
|
||||
|
||||
if (space == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var pose = untyped frame.getPose(space, refSpace);
|
||||
if (pose == null || pose.transform == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var transform = pose.transform;
|
||||
var pos = transform.position;
|
||||
var orient = transform.orientation;
|
||||
|
||||
if (handedness == "left") {
|
||||
leftHandPosition.set(pos.x, pos.y, pos.z);
|
||||
leftHandRotation.set(orient.x, orient.y, orient.z, orient.w);
|
||||
leftHandActive = true;
|
||||
|
||||
var gamepad = untyped inputSource.gamepad;
|
||||
if (gamepad != null) {
|
||||
// [0]=thumbstickX [1]=thumbstickY [2]=touchpadX [3]=touchpadY
|
||||
if (gamepad.axes != null && gamepad.axes.length >= 2) {
|
||||
leftThumbstickX = gamepad.axes[0];
|
||||
leftThumbstickY = gamepad.axes[1];
|
||||
}
|
||||
// [0]=trigger [1]=grip [4]=X [5]=Y
|
||||
if (gamepad.buttons != null) {
|
||||
if (gamepad.buttons.length > 0) leftTrigger = gamepad.buttons[0].value;
|
||||
if (gamepad.buttons.length > 1) leftGrip = gamepad.buttons[1].value;
|
||||
if (gamepad.buttons.length > 4) leftButtonX = gamepad.buttons[4].pressed;
|
||||
if (gamepad.buttons.length > 5) leftButtonY = gamepad.buttons[5].pressed;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (handedness == "right") {
|
||||
rightHandPosition.set(pos.x, pos.y, pos.z);
|
||||
rightHandRotation.set(orient.x, orient.y, orient.z, orient.w);
|
||||
rightHandActive = true;
|
||||
|
||||
var gamepad = untyped inputSource.gamepad;
|
||||
if (gamepad != null) {
|
||||
if (gamepad.axes != null && gamepad.axes.length >= 2) {
|
||||
rightThumbstickX = gamepad.axes[0];
|
||||
rightThumbstickY = gamepad.axes[1];
|
||||
}
|
||||
if (gamepad.buttons != null) {
|
||||
if (gamepad.buttons.length > 0) rightTrigger = gamepad.buttons[0].value;
|
||||
if (gamepad.buttons.length > 1) rightGrip = gamepad.buttons[1].value;
|
||||
if (gamepad.buttons.length > 4) rightButtonA = gamepad.buttons[4].pressed;
|
||||
if (gamepad.buttons.length > 5) rightButtonB = gamepad.buttons[5].pressed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#end
|
||||
Reference in New Issue
Block a user