Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Making OculusVR support WebXR gamepad.
Browse files Browse the repository at this point in the history
  • Loading branch information
daoshengmu committed Dec 23, 2019
1 parent c7f86ed commit 2abdffd
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static void vrPrefsWorkAround(Context aContext, Bundle aExtras) {
Log.i(LOGTAG, "Creating file: " + prefFileName);
try (FileOutputStream out = new FileOutputStream(prefFileName)) {
out.write("pref(\"dom.vr.enabled\", true);\n".getBytes());
out.write("pref(\"dom.vr.webxr.enabled\", true);\n".getBytes());
out.write("pref(\"dom.vr.external.enabled\", true);\n".getBytes());
out.write("pref(\"webgl.enable-surface-texture\", true);\n".getBytes());
// Enable MultiView draft extension
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,9 @@ BrowserWorld::Draw() {
m.externalVR->PullBrowserState();
m.externalVR->SetHapticState(m.controllers);

const uint64_t frameId = m.externalVR->GetFrameId();
m.controllers->SetFrameId(frameId);

m.CheckExitImmersive();
if (m.splashAnimation) {
DrawSplashAnimation();
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/cpp/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ Controller::operator=(const Controller& aController) {
leftHanded = aController.leftHanded;
inDeadZone = aController.inDeadZone;
lastHoverEvent = aController.lastHoverEvent;
profile = aController.profile;
selectActionStartFrameId = aController.selectActionStartFrameId;
selectActionStartFrameId = aController.selectActionStopFrameId;
squeezeActionStartFrameId = aController.squeezeActionStartFrameId;
squeezeActionStopFrameId = aController.squeezeActionStopFrameId;
return *this;
}

Expand Down Expand Up @@ -102,6 +107,10 @@ Controller::Reset() {
leftHanded = false;
inDeadZone = true;
lastHoverEvent = 0.0;
selectActionStartFrameId = 0;
selectActionStopFrameId = 0;
squeezeActionStartFrameId = 0;
squeezeActionStopFrameId = 0;
}

vrb::Vector Controller::StartPoint() const {
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/cpp/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Device.h"
#include "vrb/Forward.h"
#include "vrb/Matrix.h"
#include "moz_external_vr.h"

namespace crow {

Expand Down Expand Up @@ -61,6 +62,12 @@ struct Controller {
double lastHoverEvent;
device::CapabilityFlags deviceCapabilities;

std::string profile;
uint64_t selectActionStartFrameId;
uint64_t selectActionStopFrameId;
uint64_t squeezeActionStartFrameId;
uint64_t squeezeActionStopFrameId;

vrb::Vector StartPoint() const;
vrb::Vector Direction() const;

Expand Down
71 changes: 71 additions & 0 deletions app/src/main/cpp/ControllerContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ struct ControllerContainer::State {
GeometryPtr beamModel;
bool visible = false;
vrb::Color pointerColor;
uint64_t immersiveFrameId;
uint64_t lastImmersiveFrameId;

void Initialize(vrb::CreationContextPtr& aContext) {
context = aContext;
root = Toggle::Create(aContext);
visible = true;
pointerColor = vrb::Color(1.0f, 1.0f, 1.0f, 1.0f);
immersiveFrameId = 0;
lastImmersiveFrameId = 0;
}

bool Contains(const int32_t aControllerIndex) {
Expand Down Expand Up @@ -380,6 +384,63 @@ ControllerContainer::GetHapticFeedback(const int32_t aControllerIndex, uint64_t
aPulseIntensity = m.list[aControllerIndex].pulseIntensity;
}

void
ControllerContainer::SetProfile(const int32_t aControllerIndex, const std::string& aProfile) {
if (!m.Contains(aControllerIndex)) {
return;
}

m.list[aControllerIndex].profile = aProfile;
}

void
ControllerContainer::SetSelectActionStart(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex) || !m.immersiveFrameId) {
return;
}

if (m.list[aControllerIndex].selectActionStopFrameId >=
m.list[aControllerIndex].selectActionStartFrameId) {
m.list[aControllerIndex].selectActionStartFrameId = m.immersiveFrameId;
}
}

void
ControllerContainer::SetSelectActionStop(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex) || !m.lastImmersiveFrameId) {
return;
}

if (m.list[aControllerIndex].selectActionStartFrameId >
m.list[aControllerIndex].selectActionStopFrameId) {
m.list[aControllerIndex].selectActionStopFrameId = m.lastImmersiveFrameId;
}
}

void
ControllerContainer::SetSqueezeActionStart(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex) || !m.immersiveFrameId) {
return;
}

if (m.list[aControllerIndex].squeezeActionStopFrameId >=
m.list[aControllerIndex].squeezeActionStartFrameId) {
m.list[aControllerIndex].squeezeActionStartFrameId = m.immersiveFrameId;
}
}

void
ControllerContainer::SetSqueezeActionStop(const int32_t aControllerIndex) {
if (!m.Contains(aControllerIndex) || !m.lastImmersiveFrameId) {
return;
}

if (m.list[aControllerIndex].squeezeActionStartFrameId >
m.list[aControllerIndex].squeezeActionStopFrameId) {
m.list[aControllerIndex].squeezeActionStopFrameId = m.lastImmersiveFrameId;
}
}

void
ControllerContainer::SetLeftHanded(const int32_t aControllerIndex, const bool aLeftHanded) {
if (!m.Contains(aControllerIndex)) {
Expand Down Expand Up @@ -442,6 +503,16 @@ ControllerContainer::SetVisible(const bool aVisible) {
}
}

void
ControllerContainer::SetFrameId(const uint64_t aFrameId) {
if (m.immersiveFrameId) {
m.lastImmersiveFrameId = aFrameId ? aFrameId : m.immersiveFrameId;
} else {
m.lastImmersiveFrameId = 0;
}
m.immersiveFrameId = aFrameId;
}

ControllerContainer::ControllerContainer(State& aState, vrb::CreationContextPtr& aContext) : m(aState) {
m.Initialize(aContext);
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/cpp/ControllerContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ class ControllerContainer : public crow::ControllerDelegate {
uint32_t GetHapticCount(const int32_t aControllerIndex) override;
void SetHapticFeedback(const int32_t aControllerIndex, const uint64_t aInputFrameID, const float aPulseDuration, const float aPulseIntensity) override;
void GetHapticFeedback(const int32_t aControllerIndex, uint64_t &aInputFrameID, float& aPulseDuration, float& aPulseIntensity) override;
void SetProfile(const int32_t aControllerIndex, const std::string& aProfile) override;
void SetSelectActionStart(const int32_t aControllerIndex) override;
void SetSelectActionStop(const int32_t aControllerIndex) override;
void SetSqueezeActionStart(const int32_t aControllerIndex) override;
void SetSqueezeActionStop(const int32_t aControllerIndex) override;
void SetLeftHanded(const int32_t aControllerIndex, const bool aLeftHanded) override;
void SetTouchPosition(const int32_t aControllerIndex, const float aTouchX, const float aTouchY) override;
void EndTouch(const int32_t aControllerIndex) override;
void SetScrolledDelta(const int32_t aControllerIndex, const float aScrollDeltaX, const float aScrollDeltaY) override;
void SetPointerColor(const vrb::Color& color) const;
void SetVisible(const bool aVisible);
void SetFrameId(const uint64_t aFrameId);
protected:
struct State;
ControllerContainer(State& aState, vrb::CreationContextPtr& aContext);
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/cpp/ControllerDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "GestureDelegate.h"

#include <memory>
#include <string>

namespace crow {

Expand Down Expand Up @@ -46,6 +47,11 @@ class ControllerDelegate {
virtual uint32_t GetHapticCount(const int32_t aControllerIndex) = 0;
virtual void SetHapticFeedback(const int32_t aControllerIndex, const uint64_t aInputFrameID, const float aPulseDuration, const float aPulseIntensity) = 0;
virtual void GetHapticFeedback(const int32_t aControllerIndex, uint64_t & aInputFrameID, float& aPulseDuration, float& aPulseIntensity) = 0;
virtual void SetProfile(const int32_t aControllerIndex, const std::string& aProfile) = 0;
virtual void SetSelectActionStart(const int32_t aControllerIndex) = 0;
virtual void SetSelectActionStop(const int32_t aControllerIndex) = 0;
virtual void SetSqueezeActionStart(const int32_t aControllerIndex) = 0;
virtual void SetSqueezeActionStop(const int32_t aControllerIndex) = 0;
virtual void SetLeftHanded(const int32_t aControllerIndex, const bool aLeftHanded) = 0;
virtual void SetTouchPosition(const int32_t aControllerIndex, const float aTouchX, const float aTouchY) = 0;
virtual void EndTouch(const int32_t aControllerIndex) = 0;
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/cpp/ExternalVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ ExternalVR::SetSourceBrowser(VRBrowserType aBrowser) {
m.SetSourceBrowser(aBrowser);
}

uint64_t
ExternalVR::GetFrameId() const {
return m.lastFrameId;
}

void
ExternalVR::SetCompositorEnabled(bool aEnabled) {
if (aEnabled == m.compositorEnabled) {
Expand Down Expand Up @@ -431,6 +436,17 @@ ExternalVR::PushFramePoses(const vrb::Matrix& aHeadTransform, const std::vector<
vrb::Vector position(controller.transformMatrix.GetTranslation());
memcpy(&(immersiveController.pose.position), position.Data(), sizeof(immersiveController.pose.position));
}

immersiveController.targetRayMode = mozilla::gfx::TargetRayMode::TrackedPointer;
immersiveController.mappingType = mozilla::gfx::GamepadMappingType ::XRStandard;
if (controller.profile.size() > mozilla::gfx::kProfileNameListMaxLen) {
VRB_ERROR("controller profile size is over than kProfileNameListMaxLen.");
}
memcpy(&immersiveController.profiles, controller.profile.data(), mozilla::gfx::kProfileNameListMaxLen);
immersiveController.selectActionStartFrameId = controller.selectActionStartFrameId;
immersiveController.selectActionStopFrameId = controller.selectActionStopFrameId;
immersiveController.squeezeActionStartFrameId = controller.squeezeActionStartFrameId;
immersiveController.squeezeActionStopFrameId = controller.squeezeActionStopFrameId;
}

m.system.sensorState.timestamp = aTimestamp;
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/ExternalVR.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ExternalVR : public ImmersiveDisplay {
void SetHapticState(ControllerContainerPtr aControllerContainer) const;
void StopPresenting();
void SetSourceBrowser(VRBrowserType aBrowser);
uint64_t GetFrameId() const;
ExternalVR();
~ExternalVR() = default;
protected:
Expand Down
23 changes: 23 additions & 0 deletions app/src/oculusvr/cpp/DeviceDelegateOculusVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,12 +955,14 @@ struct DeviceDelegateOculusVR::State {
controllerName, beamTransform);
controller->SetButtonCount(controllerState.index, 6);
controller->SetHapticCount(controllerState.index, 1);
controller->SetProfile(controllerState.index, "oculus-touch-v2");
} else {
// Oculus Go only has one kind of controller model.
controller->CreateController(controllerState.index, 0, "Oculus Go Controller");
controller->SetButtonCount(controllerState.index, 2);
// Oculus Go has no haptic feedback.
controller->SetHapticCount(controllerState.index, 0);
controller->SetProfile(controllerState.index, "oculus-go");
}
controllerState.created = true;
}
Expand Down Expand Up @@ -1069,6 +1071,16 @@ struct DeviceDelegateOculusVR::State {
// This is always false in Oculus Browser.
const bool thumbRest = false;
controller->SetButtonState(controllerState.index, ControllerDelegate::BUTTON_OTHERS, 5, thumbRest, thumbRest);

if (gripPressed) {
if (renderMode == device::RenderMode::Immersive) {
controller->SetSqueezeActionStart(controllerState.index);
} else {
controller->SetSqueezeActionStop(controllerState.index);
}
} else {
controller->SetSqueezeActionStop(controllerState.index);
}
} else {
triggerPressed = (controllerState.inputState.Buttons & ovrButton_A) != 0;
triggerTouched = triggerPressed;
Expand Down Expand Up @@ -1098,6 +1110,17 @@ struct DeviceDelegateOculusVR::State {
controller->SetButtonState(controllerState.index, ControllerDelegate::BUTTON_TOUCHPAD, 0, trackpadPressed, trackpadTouched);

controller->SetAxes(controllerState.index, axes, kNumAxes);

if (triggerPressed) {
if (renderMode == device::RenderMode::Immersive) {
controller->SetSelectActionStart(controllerState.index);
}
else {
controller->SetSelectActionStop(controllerState.index);
}
} else {
controller->SetSelectActionStop(controllerState.index);
}
if (controller->GetHapticCount(controllerState.index)) {
UpdateHaptics(controllerState);
}
Expand Down

0 comments on commit 2abdffd

Please sign in to comment.