Skip to content

Commit

Permalink
Add basic VR haptic events and hook up articulating_arm grab events
Browse files Browse the repository at this point in the history
  • Loading branch information
xthexder committed Sep 12, 2024
1 parent c388a93 commit ee12107
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 9 deletions.
15 changes: 15 additions & 0 deletions assets/scenes/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,21 @@
"prefab": "template"
}
},
{
"name": "mirrorcube4",
"transform": {
"parent": "table.surface",
"rotate": [80, 0, 1, 0],
"scale": 0.1,
"translate": [0.45, 0, 0.55]
},
"script": {
"parameters": {
"source": "mirrorcube"
},
"prefab": "template"
}
},
{
"name": "duck",
"transform": {
Expand Down
5 changes: 3 additions & 2 deletions assets/scenes/templates/articulating_arm.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@
"translate": [0, 0.1043, 0]
}
}],
"signal_output": {
"value": 1
"signal_output": {},
"signal_bindings": {
"value": "(arm.Knob/interact_holds + arm.Ball_1/interact_holds) == 0"
},
"event_input": {},
"event_bindings": {
Expand Down
10 changes: 8 additions & 2 deletions assets/scenes/vr.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@
"torque_limit": 15.0
}
}
]
],
"event_bindings": {
"/physics/collision/force_found": "output:haptics/actions/main/out/lefthand_touch"
}
},
{
"name": "right_hand_skeleton",
Expand Down Expand Up @@ -139,7 +142,10 @@
"torque_limit": 15.0
}
}
]
],
"event_bindings": {
"/physics/collision/force_found": "output:haptics/actions/main/out/righthand_touch"
}
},
{
"name": "laser_pointer",
Expand Down
5 changes: 4 additions & 1 deletion src/scripts/scripts/InteractionScripts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ namespace sp::scripts {
ph.group = PhysicsGroup::HeldObject;
}

bool newRenderOutline = !disabled && (!grabEntities.empty() || !pointEntities.empty());
bool newRenderOutline = enableInteraction && (!grabEntities.empty() || !pointEntities.empty());
if (renderOutline != newRenderOutline) {
for (auto &e : lock.EntitiesWith<Renderable>()) {
if (!e.Has<TransformTree, Renderable>(lock)) continue;
Expand All @@ -172,6 +172,9 @@ namespace sp::scripts {
}
renderOutline = newRenderOutline;
}

SignalRef(ent, "interact_holds").SetValue(lock, enableInteraction ? (double)grabEntities.size() : 0.0f);
SignalRef(ent, "interact_points").SetValue(lock, enableInteraction ? (double)pointEntities.size() : 0.0f);
}
};
StructMetadata MetadataInteractiveObject(typeid(InteractiveObject),
Expand Down
36 changes: 33 additions & 3 deletions src/xr/openvr/InputBindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ namespace sp::xr {
}

GetSceneManager().QueueActionAndBlock(SceneAction::ApplySystemScene,
"vr_input",
"vr_io",
[this](ecs::Lock<ecs::AddRemove> lock, std::shared_ptr<Scene> scene) {
auto outputEnt = scene->NewSystemEntity(lock, scene, outputEntity.Name());
outputEnt.Set<ecs::EventInput>(lock);
for (auto &actionSet : actionSets) {
for (auto &action : actionSet.actions) {
if (action.type == Action::DataType::Pose || action.type == Action::DataType::Skeleton) {
Expand All @@ -120,14 +122,26 @@ namespace sp::xr {
action.poseEntity = ecs::Name("input", inputName);
auto ent = scene->NewSystemEntity(lock, scene, action.poseEntity.Name());
ent.Set<ecs::TransformTree>(lock);
} else if (action.type == Action::DataType::Haptic) {
if (!action.eventQueue) action.eventQueue = ecs::EventQueue::New();
}
}
}
});
ecs::QueueTransaction<ecs::Write<ecs::EventInput>>([this](auto &lock) {
ecs::Entity ent = outputEntity.Get(lock);
auto &eventInput = ent.Get<ecs::EventInput>(lock);

for (auto &actionSet : actionSets) {
for (auto &action : actionSet.actions) {
if (action.eventQueue) eventInput.Register(lock, action.eventQueue, action.name);
}
}
});
}

InputBindings::~InputBindings() {
GetSceneManager().QueueActionAndBlock(SceneAction::RemoveScene, "vr_input");
GetSceneManager().QueueActionAndBlock(SceneAction::RemoveScene, "vr_io");
}

void InputBindings::Frame() {
Expand Down Expand Up @@ -434,14 +448,30 @@ namespace sp::xr {
break;
}
}
if (action.type == Action::DataType::Haptic) {
ecs::Event event;
while (ecs::EventInput::Poll(lock, action.eventQueue, event)) {
float &amplitude = std::get<float>(event.data);
error = vr::VRInput()->TriggerHapticVibrationAction(action.handle,
0.0f, // start delay
0.1f, // duration
100.0f, // frequency
std::clamp(amplitude, 0.0f, 1.0f), // amplitude
0);
if (error != vr::EVRInputError::VRInputError_None) break;
}
Assertf(error == vr::EVRInputError::VRInputError_None,
"Failed to send OpenVR haptic action: %s",
action.name);
}
}
}
}

if (missingEntities) {
ZoneScopedN("InputBindings::AddMissingEntities");
GetSceneManager().QueueActionAndBlock(SceneAction::ApplySystemScene,
"vr_input",
"vr_io",
[this](ecs::Lock<ecs::AddRemove> lock, std::shared_ptr<Scene> scene) {
for (auto &actionSet : actionSets) {
for (auto &action : actionSet.actions) {
Expand Down
2 changes: 2 additions & 0 deletions src/xr/openvr/InputBindings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace sp::xr {
ecs::EntityRef poseEntity;
std::vector<ecs::EntityRef> boneEntities;
std::vector<vr::BoneIndex_t> boneHierarchy;
ecs::EventQueueRef eventQueue;
DataType type;

Action() {}
Expand All @@ -59,5 +60,6 @@ namespace sp::xr {
};

std::vector<ActionSet> actionSets;
ecs::EntityRef outputEntity = ecs::Name("output", "haptics");
};
} // namespace sp::xr
12 changes: 11 additions & 1 deletion src/xr/openvr/actions.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
"name": "/actions/main/in/lefthand_anim",
"type": "skeleton",
"skeleton": "/skeleton/hand/left"
},
{
"name": "/actions/main/out/righthand_touch",
"type": "vibration"
},
{
"name": "/actions/main/out/lefthand_touch",
"type": "vibration"
}
],
"action_sets": [
Expand All @@ -98,7 +106,9 @@
"/actions/main/in/righthand": "Right Hand",
"/actions/main/in/lefthand": "Left Hand",
"/actions/main/in/righthand_anim": "Right Hand Skeleton",
"/actions/main/in/lefthand_anim": "Left Hand Skeleton"
"/actions/main/in/lefthand_anim": "Left Hand Skeleton",
"/actions/main/out/righthand_touch": "Right Hand Touch Haptics",
"/actions/main/out/lefthand_touch": "Left Hand Touch Haptics"
}
]
}
10 changes: 10 additions & 0 deletions src/xr/openvr/sp_bindings_knuckles.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
"app_key" : "system.generated.sp-vk.exe",
"bindings" : {
"/actions/main" : {
"haptics" : [
{
"output" : "/actions/main/out/lefthand_touch",
"path" : "/user/hand/left/output/haptic"
},
{
"output" : "/actions/main/out/righthand_touch",
"path" : "/user/hand/right/output/haptic"
}
],
"poses" : [
{
"output" : "/actions/main/in/righthand",
Expand Down

0 comments on commit ee12107

Please sign in to comment.