Skip to content

Commit

Permalink
support various legacy VRCFT setups
Browse files Browse the repository at this point in the history
  • Loading branch information
galister committed Jul 30, 2024
1 parent e8b6ba6 commit 1aaac2e
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 91 deletions.
15 changes: 8 additions & 7 deletions src/core/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use log::debug;
use rosc::{OscBundle, OscMessage, OscPacket, OscType};

use super::{INPUT_PREFIX, PARAM_PREFIX};
Expand All @@ -24,37 +23,39 @@ impl AvatarBundle for OscBundle {
}
}
fn send_parameter(&mut self, name: &str, value: OscType) {
debug!("Sending parameter {} = {:?}", name, value);
log::trace!("Sending parameter {} = {:?}", name, value);
self.content.push(OscPacket::Message(OscMessage {
addr: format!("{}{}", PARAM_PREFIX, name),
args: vec![value],
}));
}
fn send_tracking(&mut self, addr: &str, args: Vec<OscType>) {
debug!("Sending tracking {} = {:?}", addr, args);
log::trace!("Sending tracking {} = {:?}", addr, args);
self.content.push(OscPacket::Message(OscMessage {
addr: addr.to_string(),
args,
}));
}
fn send_input_axis(&mut self, name: &str, value: f32) {
debug!("Sending input axis {} = {:?}", name, value);
log::trace!("Sending input axis {} = {:?}", name, value);
self.content.push(OscPacket::Message(OscMessage {
addr: format!("{}{}", INPUT_PREFIX, name),
args: vec![OscType::Float(value)],
}));
}
fn send_input_button(&mut self, name: &str, value: bool) {
debug!("Sending input button {} = {:?}", name, value);
log::trace!("Sending input button {} = {:?}", name, value);
self.content.push(OscPacket::Message(OscMessage {
addr: format!("{}{}", INPUT_PREFIX, name),
args: vec![OscType::Bool(value)],
}));
}
fn send_chatbox_message(&mut self, message: String, open_keyboard: bool, play_sound: bool) {
debug!(
log::trace!(
"Sending chatbox message {} (kbd: {:?}, sfx: {:?})",
message, open_keyboard, play_sound
message,
open_keyboard,
play_sound
);
self.content.insert(
0,
Expand Down
7 changes: 3 additions & 4 deletions src/core/ext_storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{fs::File, time::Instant};

use log::{debug, info};
use rosc::{OscBundle, OscType};

use super::{bundle::AvatarBundle, folders::CONFIG_DIR};
Expand Down Expand Up @@ -41,7 +40,7 @@ impl ExtStorage {

fn save(&mut self) {
self.last_save = Instant::now();
info!("Saving ExtStorage to {}", &self.path);
log::info!("Saving ExtStorage to {}", &self.path);
File::create(&self.path)
.ok()
.and_then(|file| serde_json::to_writer(file, &self.data).ok());
Expand Down Expand Up @@ -95,13 +94,13 @@ impl ExtStorage {
}
if self.ext_index != 0 {
self.int_index = 0;
debug!("ExtIndex {}", self.ext_index);
log::trace!("ExtIndex {}", self.ext_index);
return;
}

if let Some(value) = self.next() {
self.last_tick = Instant::now();
debug!("Sending {} {}", self.int_index, value);
log::trace!("Sending {} {}", self.int_index, value);

bundle.send_parameter("IntIndex", OscType::Int(self.int_index as _));
bundle.send_parameter("IntValue", OscType::Float(value));
Expand Down
19 changes: 10 additions & 9 deletions src/core/ext_tracking/alvr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,23 @@ impl AlvrReceiver {
}

if let Some(head) = new_data.head {
let rot: glam::Quat = unsafe { std::mem::transmute(head.orientation) };
let pos: glam::Vec3 = unsafe { std::mem::transmute(head.position) };
state.tracking.head = glam::Affine3A::from_rotation_translation(rot, pos);
state.tracking.head =
glam::Affine3A::from_rotation_translation(head.orientation, head.position);
state.tracking.last_received = Instant::now();
}

if let Some(left_hand) = new_data.hands[0] {
let rot: glam::Quat = unsafe { std::mem::transmute(left_hand.orientation) };
let pos: glam::Vec3 = unsafe { std::mem::transmute(left_hand.position) };
state.tracking.left_hand = glam::Affine3A::from_rotation_translation(rot, pos);
state.tracking.left_hand = glam::Affine3A::from_rotation_translation(
left_hand.orientation,
left_hand.position,
);
}

if let Some(right_hand) = new_data.hands[1] {
let rot: glam::Quat = unsafe { std::mem::transmute(right_hand.orientation) };
let pos: glam::Vec3 = unsafe { std::mem::transmute(right_hand.position) };
state.tracking.right_hand = glam::Affine3A::from_rotation_translation(rot, pos);
state.tracking.right_hand = glam::Affine3A::from_rotation_translation(
right_hand.orientation,
right_hand.position,
);
}
}

Expand Down
134 changes: 70 additions & 64 deletions src/core/ext_tracking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{array, str::FromStr, sync::Arc};
use once_cell::sync::Lazy;
use regex::Regex;
use rosc::{OscBundle, OscType};
use strum::EnumCount;
use sranipal::SRanipalExpression;

#[cfg(feature = "alvr")]
use self::alvr::AlvrReceiver;
Expand All @@ -27,6 +27,7 @@ mod alvr;
mod babble;
#[cfg(any(feature = "alvr", feature = "wivrn"))]
mod face2_fb;
mod sranipal;
pub mod unified;
#[cfg(feature = "wivrn")]
mod wivrn;
Expand Down Expand Up @@ -80,7 +81,7 @@ impl ExtTracking {
last_value: 0.,
last_bits: [false; 8],
};
params[UnifiedExpressions::COUNT + (e as usize)] = Some(new);
params[e as usize] = Some(new);
}

for e in default_unified.into_iter() {
Expand Down Expand Up @@ -139,72 +140,77 @@ impl ExtTracking {
self.data.apply_to_bundle(&mut self.params, bundle);
}

pub fn osc_json(&mut self, root_node: &OscJsonNode) {
pub fn osc_json(&mut self, avatar_node: &OscJsonNode) {
self.params.iter_mut().for_each(|p| *p = None);

let Some(parameters) = avatar_node.get("parameters") else {
log::warn!("oscjson: Could not read /avatar/parameters");
return;
};

self.process_node_recursive("parameters", parameters);
self.print_params();
}
fn process_node_recursive(&mut self, name: &str, node: &OscJsonNode) -> Option<()> {
static FT_PARAMS_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^(.+?)(Negative|\d+)?$").unwrap());

self.params.iter_mut().for_each(|p| *p = None);
if let Some(contents) = node.contents.as_ref() {
log::debug!("Checking {}", name);
for (name, node) in contents.iter() {
let _ = self.process_node_recursive(name, node);
}
return None;
}

let _x: Option<()> = root_node
.get("parameters")
.and_then(|parameters| parameters.get("FT"))
.and_then(|ft| ft.get("v2"))
.and_then(|v2| {
v2.contents.as_ref()?.iter().for_each(|(name, node)| {
if let Some(m) = FT_PARAMS_REGEX.captures(name) {
let main: Arc<str> = m[1].into();

let Some(idx) = UnifiedExpressions::from_str(&main)
.map(|e| e as usize)
.or_else(|_| {
CombinedExpression::from_str(&main)
.map(|e| UnifiedExpressions::COUNT + (e as usize))
})
.ok()
else {
log::warn!("Unknown expression: {}", &main);
return;
};

let create = self.params[idx].is_none();

if create {
let new = MysteryParam {
name: main.clone(),
main_address: None,
addresses: array::from_fn(|_| None),
neg_address: None,
num_bits: 0,
last_value: 0.,
last_bits: [false; 8],
};
self.params[idx] = Some(new);
};

let stored = self.params[idx].as_mut().unwrap();
match m.get(2).map(|s| s.as_str()) {
Some("Negative") => {
let addr = &node.full_path.as_ref()[super::PARAM_PREFIX.len()..];
stored.neg_address = Some(addr.into());
}
Some(digit) => {
let digit = digit.parse::<f32>().unwrap();
let idx = digit.log2() as usize;
let addr = &node.full_path.as_ref()[super::PARAM_PREFIX.len()..];
stored.num_bits = stored.num_bits.max(idx + 1);
stored.addresses[idx] = Some(addr.into());
}
None => {
let addr = &node.full_path.as_ref()[super::PARAM_PREFIX.len()..];
stored.main_address = Some(addr.into());
}
}
}
});

None
});
self.print_params();
if let Some(m) = FT_PARAMS_REGEX.captures(name) {
let main: Arc<str> = m[1].into();

log::debug!("Param: {}", name);
let idx = UnifiedExpressions::from_str(&main)
.map(|e| e as usize)
.or_else(|_| CombinedExpression::from_str(&main).map(|e| e as usize))
.or_else(|_| SRanipalExpression::from_str(&main).map(|e| e as usize))
.ok()?;

log::debug!("Match: {}", name);

let create = self.params[idx].is_none();

if create {
let new = MysteryParam {
name: main.clone(),
main_address: None,
addresses: array::from_fn(|_| None),
neg_address: None,
num_bits: 0,
last_value: 0.,
last_bits: [false; 8],
};
self.params[idx] = Some(new);
};

let stored = self.params[idx].as_mut().unwrap();
match m.get(2).map(|s| s.as_str()) {
Some("Negative") => {
let addr = &node.full_path.as_ref()[super::PARAM_PREFIX.len()..];
stored.neg_address = Some(addr.into());
}
Some(digit) => {
let digit = digit.parse::<f32>().unwrap();
let idx = digit.log2() as usize;
let addr = &node.full_path.as_ref()[super::PARAM_PREFIX.len()..];
stored.num_bits = stored.num_bits.max(idx + 1);
stored.addresses[idx] = Some(addr.into());
}
None => {
let addr = &node.full_path.as_ref()[super::PARAM_PREFIX.len()..];
stored.main_address = Some(addr.into());
}
}
}
None
}

fn print_params(&self) {
Expand Down
49 changes: 49 additions & 0 deletions src/core/ext_tracking/sranipal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use strum::{EnumCount, EnumIter, EnumString, IntoStaticStr};

use super::unified::{CombinedExpression, UnifiedExpressions};

#[allow(unused)]
#[repr(usize)]
#[derive(Debug, Clone, Copy, EnumIter, EnumCount, EnumString, IntoStaticStr)]
pub enum SRanipalExpression {
LeftEyeX = UnifiedExpressions::EyeLeftX as _,
RightEyeX = UnifiedExpressions::EyeRightX as _,
EyesY = UnifiedExpressions::EyeY as _,

EyeLeftWide = UnifiedExpressions::EyeWideLeft as _,
EyeRightWide = UnifiedExpressions::EyeWideRight as _,
EyeLeftBlink = UnifiedExpressions::EyeClosedLeft as _,
EyeRightBlink = UnifiedExpressions::EyeClosedRight as _,
EyeLeftSqueeze = UnifiedExpressions::EyeSquintLeft as _,
EyeRightSqueeze = UnifiedExpressions::EyeSquintRight as _,
CheekSuck = UnifiedExpressions::CheekSuckLeft as _,
MouthApeShape = UnifiedExpressions::MouthClosed as _,
MouthUpperInside = CombinedExpression::LipSuckUpper as usize,
MouthLowerInside = CombinedExpression::LipSuckLower as usize,
MouthUpperOverturn = CombinedExpression::LipFunnelUpper as usize,
MouthLowerOverturn = CombinedExpression::LipFunnelLower as usize,
MouthPout = CombinedExpression::LipPucker as usize,
MouthLowerOverlay = UnifiedExpressions::MouthRaiserLower as _,
TongueLongStep1 = UnifiedExpressions::TongueOut as _,
/* duplicate names
CheekPuffLeft = UnifiedExpressions::CheekPuffLeft as _,
CheekPuffRight = UnifiedExpressions::CheekPuffRight as _,
JawLeft = UnifiedExpressions::JawLeft as _,
JawRight = UnifiedExpressions::JawRight as _,
JawForward = UnifiedExpressions::JawForward as _,
JawOpen = UnifiedExpressions::JawOpen as _,
MouthSmileLeft = CombinedExpression::MouthSmileLeft as usize,
MouthSmileRight = CombinedExpression::MouthSmileRight as usize,
MouthSadLeft = CombinedExpression::MouthSadLeft as usize,
MouthSadRight = CombinedExpression::MouthSadRight as usize,
MouthUpperUpLeft = UnifiedExpressions::MouthUpperUpLeft as _,
MouthUpperUpRight = UnifiedExpressions::MouthUpperUpRight as _,
MouthLowerDownLeft = UnifiedExpressions::MouthLowerDownLeft as _,
MouthLowerDownRight = UnifiedExpressions::MouthLowerDownRight as _,
TongueUp = UnifiedExpressions::TongueUp as _,
TongueDown = UnifiedExpressions::TongueDown as _,
TongueLeft = UnifiedExpressions::TongueLeft as _,
TongueRight = UnifiedExpressions::TongueRight as _,
TongueRoll = UnifiedExpressions::TongueRoll as _,
*/
}
10 changes: 5 additions & 5 deletions src/core/ext_tracking/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl UnifiedShapeAccessors for UnifiedShapes {

#[inline(always)]
fn getc(&self, exp: CombinedExpression) -> f32 {
self[UnifiedExpressions::COUNT + exp as usize]
self[exp as usize]
}

#[inline(always)]
Expand All @@ -43,7 +43,7 @@ impl UnifiedShapeAccessors for UnifiedShapes {

#[inline(always)]
fn setc(&mut self, exp: CombinedExpression, value: f32) {
self[UnifiedExpressions::COUNT + exp as usize] = value;
self[exp as usize] = value;
}
}

Expand Down Expand Up @@ -78,7 +78,7 @@ impl UnifiedTrackingData {

#[inline(always)]
pub fn getc(&self, exp: CombinedExpression) -> f32 {
self.shapes[UnifiedExpressions::COUNT + exp as usize]
self.shapes[exp as usize]
}

#[inline(always)]
Expand All @@ -88,7 +88,7 @@ impl UnifiedTrackingData {

#[inline(always)]
pub fn setc(&mut self, exp: CombinedExpression, value: f32) {
self.shapes[UnifiedExpressions::COUNT + exp as usize] = value;
self.shapes[exp as usize] = value;
}

pub fn calc_combined(&mut self, state: &mut AppState) {
Expand Down Expand Up @@ -593,7 +593,7 @@ pub enum UnifiedExpressions {
#[repr(usize)]
#[derive(Debug, Clone, Copy, EnumIter, EnumCount, EnumString, IntoStaticStr)]
pub enum CombinedExpression {
EyeLidLeft,
EyeLidLeft = UnifiedExpressions::COUNT,
EyeLidRight,
EyeLid,
EyeSquint,
Expand Down
4 changes: 2 additions & 2 deletions src/core/ext_tracking/wivrn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(super) struct WivrnReceiver {

impl WivrnReceiver {
pub fn new() -> Self {
let (sender, receiver) = std::sync::mpsc::sync_channel(8);
let (sender, receiver) = std::sync::mpsc::sync_channel(32);
Self {
sender,
receiver,
Expand Down Expand Up @@ -105,7 +105,7 @@ fn wivrn_receive(sender: SyncSender<Box<WivrnTrackingData>>) {
};

if let Err(e) = sender.try_send(Box::new(data)) {
log::debug!("Failed to send tracking message: {}", e);
log::trace!("Failed to send tracking message: {}", e);
}
}
}
Expand Down

0 comments on commit 1aaac2e

Please sign in to comment.