Skip to content

Commit 39de868

Browse files
author
ChengNan
committed
Add support to latest SDK
* Set true to useTextureInProject for controller model * Improve extract hand joint data * Update GetPinchRay API to WVR_HandPoseType_Pinch type * Add new symbols to asmdef
1 parent 4453664 commit 39de868

File tree

3 files changed

+152
-22
lines changed

3 files changed

+152
-22
lines changed

Assets/HTC.UnityPlugin/HTC.ViveInputUtility.asmdef

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "HTC.ViveInputUtility",
3+
"rootNamespace": "",
34
"references": [
45
"Unity.XR.Management",
56
"SteamVR",
@@ -25,6 +26,17 @@
2526
"precompiledReferences": [],
2627
"autoReferenced": true,
2728
"defineConstraints": [],
28-
"versionDefines": [],
29+
"versionDefines": [
30+
{
31+
"name": "com.htc.upm.wave.native",
32+
"expression": "4.2.0",
33+
"define": "VIU_WAVE_NATIVE_4_2_OR_NEWER"
34+
},
35+
{
36+
"name": "com.htc.upm.wave.essence",
37+
"expression": "5.4.0",
38+
"define": "VIU_WAVE_ESSENCE_5_4_OR_NEWER"
39+
}
40+
],
2941
"noEngineReferences": false
3042
}

Assets/HTC.UnityPlugin/VRModule/Modules/WaveVRModule.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ public override void UpdateRenderModel()
7979
#endif
8080
GameObject controllerObj = new GameObject("Controller");
8181
controllerObj.transform.SetParent(m_modelObj.transform, false);
82-
controllerObj.AddComponent<Wave.Essence.Controller.Model.RenderModel>();
82+
var rm = controllerObj.AddComponent<Wave.Essence.Controller.Model.RenderModel>();
83+
rm.WhichHand = XR_Hand.Dominant;
84+
#if VIU_WAVE_ESSENCE_5_4_R8_OR_NEWER
85+
rm.useTextureInProject = true;
86+
#endif
8387
controllerObj.AddComponent<Wave.Essence.Controller.Model.ButtonEffect>();
8488
#elif VIU_WAVEXR_ESSENCE_RENDERMODEL
8589
m_modelObj.AddComponent<Wave.Essence.Controller.RenderModel>();
@@ -107,6 +111,9 @@ public override void UpdateRenderModel()
107111
controllerObj.transform.SetParent(m_modelObj.transform, false);
108112
var rm = controllerObj.AddComponent<Wave.Essence.Controller.Model.RenderModel>();
109113
rm.WhichHand = XR_Hand.NonDominant;
114+
#if VIU_WAVE_ESSENCE_5_4_R8_OR_NEWER
115+
rm.useTextureInProject = true;
116+
#endif
110117
var be = controllerObj.AddComponent<Wave.Essence.Controller.Model.ButtonEffect>();
111118
be.HandType = XR_Hand.NonDominant;
112119
#elif VIU_WAVEXR_ESSENCE_RENDERMODEL

Assets/HTC.UnityPlugin/VRModule/Submodules/WaveHandTrackingSubmodule.cs

Lines changed: 131 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#if VIU_WAVEVR_HAND_TRACKING
1111
using Wave.Native;
12+
using Wave.XR.Function;
1213
#endif
1314

1415
namespace HTC.UnityPlugin.VRModuleManagement
@@ -48,6 +49,11 @@ public void Fetch()
4849
VRModuleSettings.showWaveElectronicHandWithController ?
4950
WVR_HandModelType.WVR_HandModelType_WithController : WVR_HandModelType.WVR_HandModelType_WithoutController;
5051

52+
#if VIU_WAVE_NATIVE_4_2_OR_NEWER
53+
private delegate void ConvertHandTrackingDataToUnityDelegate(ref WVR_HandJointData_t src, ref HandJointData26 dest);
54+
private static ConvertHandTrackingDataToUnityDelegate ConvertHandTrackingDataToUnity = null;
55+
#endif
56+
5157
public override bool ShouldActiveModule() { return VRModuleSettings.activateWaveHandTrackingSubmodule; }
5258

5359
protected override void OnActivated()
@@ -173,7 +179,7 @@ public static bool TryGetLeftPinchRay(out Vector3 origin, out Vector3 direction)
173179
Coordinate.GetVectorFromGL(pinch.pinch.origin, out origin);
174180
Coordinate.GetVectorFromGL(pinch.pinch.direction, out direction);
175181

176-
return pinch.state.type != WVR_HandPoseType.WVR_HandPoseType_Invalid;
182+
return pinch.state.type == WVR_HandPoseType.WVR_HandPoseType_Pinch;
177183
}
178184

179185
public static bool TryGetRightPinchRay(out Vector3 origin, out Vector3 direction)
@@ -190,7 +196,43 @@ public static bool TryGetRightPinchRay(out Vector3 origin, out Vector3 direction
190196
Coordinate.GetVectorFromGL(pinch.pinch.origin, out origin);
191197
Coordinate.GetVectorFromGL(pinch.pinch.direction, out direction);
192198

193-
return pinch.state.type != WVR_HandPoseType.WVR_HandPoseType_Invalid;
199+
return pinch.state.type == WVR_HandPoseType.WVR_HandPoseType_Pinch;
200+
}
201+
202+
public static bool TryGetLeftHandScale(out Vector3 scale)
203+
{
204+
if (!trackingActivator.isLeftValid)
205+
{
206+
scale = Vector3.one;
207+
208+
return false;
209+
}
210+
211+
var scaleGL = trackingActivator.getLeftHandData.scale;
212+
213+
scale.x = scaleGL.v0;
214+
scale.y = scaleGL.v1;
215+
scale.z = scaleGL.v2;
216+
217+
return true;
218+
}
219+
220+
public static bool TryGetRightHandScale(out Vector3 scale)
221+
{
222+
if (!trackingActivator.isRightValid)
223+
{
224+
scale = Vector3.one;
225+
226+
return false;
227+
}
228+
229+
var scaleGL = trackingActivator.getRightHandData.scale;
230+
231+
scale.x = scaleGL.v0;
232+
scale.y = scaleGL.v1;
233+
scale.z = scaleGL.v2;
234+
235+
return true;
194236
}
195237

196238
private enum FeatureActivity
@@ -346,6 +388,9 @@ private class TrackingActivator
346388
private static WVR_Pose_t[] s_NaturalHandJointsPoseLeft;
347389
private static WVR_HandJointData_t m_NaturalHandJointDataRight = new WVR_HandJointData_t();
348390
private static WVR_Pose_t[] s_NaturalHandJointsPoseRight;
391+
#if VIU_WAVE_NATIVE_4_2_OR_NEWER
392+
private static HandJointData26 jointData26 = new HandJointData26();
393+
#endif
349394
private static int[] intJointMappingArray;
350395
private static byte[] jointValidFlagArrayBytes;
351396
private static uint jointCount;
@@ -412,6 +457,18 @@ private static Func<WVR_Result> GenerateInitializer()
412457
{
413458
return () =>
414459
{
460+
#if VIU_WAVE_NATIVE_4_2_OR_NEWER
461+
var ptr = FunctionsHelper.GetFuncPtr("ConvertHandTrackingDataToUnity");
462+
if (ptr != IntPtr.Zero)
463+
{
464+
ConvertHandTrackingDataToUnity = Marshal.GetDelegateForFunctionPointer<ConvertHandTrackingDataToUnityDelegate>(ptr);
465+
}
466+
else
467+
{
468+
ConvertHandTrackingDataToUnity = null;
469+
}
470+
#endif
471+
415472
var result = Interop.WVR_GetHandJointCount(preferredTrackerType, ref jointCount);
416473
if (result != WVR_Result.WVR_Success) return result;
417474

@@ -423,18 +480,22 @@ private static Func<WVR_Result> GenerateInitializer()
423480
ref s_NaturalHandJointsPoseLeft,
424481
ref s_NaturalHandJointsPoseRight,
425482
jointCount);
426-
var trackerInfoResult = Interop.WVR_GetHandTrackerInfo(preferredTrackerType, ref trackerInfo);
427-
var hasTrackerInfo = ExtractHandTrackerInfo(trackerInfo, ref s_NaturalHandJoints, ref s_NaturalHandJointsFlag);
428-
//if (hasTrackerInfo)
429-
//{
430-
// for (int i = 0; i < trackerInfo.jointCount; i++)
431-
// {
432-
// Debug.Log("GetHandTrackerInfo() "
433-
// + "joint count: " + trackerInfo.jointCount
434-
// + ", s_NaturalHandJoints[" + i + "] = " + s_NaturalHandJoints[i]
435-
// + ", s_NaturalHandJointsFlag[" + i + "] = " + s_NaturalHandJointsFlag[i]);
436-
// }
437-
//}
483+
var trackerInfoResult = Interop.WVR_GetHandTrackerInfo(preferredTrackerType, ref trackerInfo) == WVR_Result.WVR_Success ? true : false;
484+
if (trackerInfoResult)
485+
{
486+
var hasTrackerInfo = ExtractHandTrackerInfo(trackerInfo, ref s_NaturalHandJoints, ref s_NaturalHandJointsFlag);
487+
//if (hasTrackerInfo)
488+
//{
489+
// for (int i = 0; i < trackerInfo.jointCount; i++)
490+
// {
491+
// Debug.Log("GetHandTrackerInfo() "
492+
// + "joint count: " + trackerInfo.jointCount
493+
// + ", s_NaturalHandJoints[" + i + "] = " + s_NaturalHandJoints[i]
494+
// + ", s_NaturalHandJointsFlag[" + i + "] = " + s_NaturalHandJointsFlag[i]);
495+
// }
496+
//}
497+
}
498+
438499
return default(WVR_Result);
439500
};
440501
}
@@ -473,6 +534,10 @@ public bool TryFetchData(WVR_PoseOriginModel originModel)
473534

474535
public WVR_HandPoseState_t getRightPinchData { get { return pinchData.right; } }
475536

537+
public WVR_HandJointData_t getLeftHandData { get { return trackingData.left; } }
538+
539+
public WVR_HandJointData_t getRightHandData { get { return trackingData.right; } }
540+
476541
public void UpdateJoints(IVRModuleDeviceStateRW state, bool isLeft)
477542
{
478543
var data = isLeft ? trackingData.left : trackingData.right;
@@ -576,14 +641,15 @@ private static void InitializeHandTrackerData(
576641
handTrackerData.right = handJointDataRight;
577642
}
578643

644+
private static int size_of_wvr_pose_t = Marshal.SizeOf<WVR_Pose_t>();
645+
579646
private static void InitializeHandJointData(ref WVR_HandJointData_t handJointData, ref WVR_Pose_t[] jointsPose, uint count)
580647
{
581648
handJointData.isValidPose = false;
582649
handJointData.confidence = 0;
583650
handJointData.jointCount = count;
584651

585-
WVR_Pose_t wvr_pose_type = default(WVR_Pose_t);
586-
handJointData.joints = Marshal.AllocHGlobal(Marshal.SizeOf(wvr_pose_type) * (int)count);
652+
handJointData.joints = Marshal.AllocHGlobal(size_of_wvr_pose_t * (int)count);
587653

588654
jointsPose = new WVR_Pose_t[count];
589655

@@ -597,7 +663,7 @@ private static void InitializeHandJointData(ref WVR_HandJointData_t handJointDat
597663
{
598664
IntPtr wvr_pose_ptr = new IntPtr(offset);
599665
Marshal.StructureToPtr(jointsPose[i], wvr_pose_ptr, false);
600-
offset += Marshal.SizeOf(wvr_pose_type);
666+
offset += size_of_wvr_pose_t;
601667
}
602668
}
603669

@@ -657,6 +723,19 @@ private static bool ExtractHandTrackerInfo(WVR_HandTrackerInfo_t handTrackerInfo
657723

658724
private static bool ExtractHandTrackerData(WVR_HandTrackingData_t handTrackerData, ref WVR_Pose_t[] handJointsPoseLeft, ref WVR_Pose_t[] handJointsPoseRight)
659725
{
726+
#if VIU_WAVE_NATIVE_4_2_OR_NEWER
727+
if (handTrackerData.left.jointCount == 26 &&
728+
handTrackerData.right.jointCount == 26 &&
729+
handJointsPoseLeft.Length == 26 &&
730+
handJointsPoseRight.Length == 26 &&
731+
ConvertHandTrackingDataToUnity != null)
732+
{
733+
ExtractHandJointData2(ref handTrackerData.left, ref handJointsPoseLeft);
734+
ExtractHandJointData2(ref handTrackerData.right, ref handJointsPoseRight);
735+
return true;
736+
}
737+
#endif
738+
660739
if (!ExtractHandJointData(handTrackerData.left, ref handJointsPoseLeft))
661740
return false;
662741
if (!ExtractHandJointData(handTrackerData.right, ref handJointsPoseRight))
@@ -680,8 +759,6 @@ private static bool ExtractHandJointData(WVR_HandJointData_t handJointData, ref
680759
jointsPose = new WVR_Pose_t[handJointData.jointCount];
681760
}
682761

683-
WVR_Pose_t wvr_pose_type = default(WVR_Pose_t);
684-
685762
int offset = 0;
686763
for (int i = 0; i < jointsPose.Length; i++)
687764
{
@@ -690,11 +767,45 @@ private static bool ExtractHandJointData(WVR_HandJointData_t handJointData, ref
690767
else
691768
jointsPose[i] = (WVR_Pose_t)Marshal.PtrToStructure(new IntPtr(handJointData.joints.ToInt64() + offset), typeof(WVR_Pose_t));
692769

693-
offset += Marshal.SizeOf(wvr_pose_type);
770+
offset += size_of_wvr_pose_t;
694771
}
695772

696773
return true;
697774
}
775+
776+
#if VIU_WAVE_NATIVE_4_2_OR_NEWER
777+
private static void ExtractHandJointData2(ref WVR_HandJointData_t jd, ref WVR_Pose_t[] poses)
778+
{
779+
ConvertHandTrackingDataToUnity(ref jd, ref jointData26);
780+
781+
poses[0] = jointData26.j00;
782+
poses[1] = jointData26.j01;
783+
poses[2] = jointData26.j02;
784+
poses[3] = jointData26.j03;
785+
poses[4] = jointData26.j04;
786+
poses[5] = jointData26.j05;
787+
poses[6] = jointData26.j06;
788+
poses[7] = jointData26.j07;
789+
poses[8] = jointData26.j08;
790+
poses[9] = jointData26.j09;
791+
poses[10] = jointData26.j10;
792+
poses[11] = jointData26.j11;
793+
poses[12] = jointData26.j12;
794+
poses[13] = jointData26.j13;
795+
poses[14] = jointData26.j14;
796+
poses[15] = jointData26.j15;
797+
poses[16] = jointData26.j16;
798+
poses[17] = jointData26.j17;
799+
poses[18] = jointData26.j18;
800+
poses[19] = jointData26.j19;
801+
poses[20] = jointData26.j20;
802+
poses[21] = jointData26.j21;
803+
poses[22] = jointData26.j22;
804+
poses[23] = jointData26.j23;
805+
poses[24] = jointData26.j24;
806+
poses[25] = jointData26.j25;
807+
}
808+
#endif
698809
}
699810

700811
private class GestureActivator

0 commit comments

Comments
 (0)