11//========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
22
3+ using HTC . UnityPlugin . Utility ;
34using HTC . UnityPlugin . Vive ;
45using System . Collections . Generic ;
56using System . Text . RegularExpressions ;
1213using UnityEngine . XR . Management ;
1314using UnityEngine . SpatialTracking ;
1415using System ;
16+ #if VIU_WAVEXR_ESSENCE_RENDERMODEL
17+ using Wave . Essence ;
18+ #endif
1519#endif
1620
1721namespace HTC . UnityPlugin . VRModuleManagement
@@ -31,6 +35,9 @@ public sealed class UnityXRModule : VRModule.ModuleBase
3135
3236 public override int moduleIndex { get { return ( int ) VRModuleSelectEnum . UnityXR ; } }
3337
38+ public const string WAVE_XR_LOADER_NAME = "Wave XR Loader" ;
39+ public const string WAVE_XR_LOADER_CLASS_NAME = "WaveXRLoader" ;
40+
3441#if UNITY_2019_3_OR_NEWER && VIU_XR_GENERAL_SETTINGS
3542 private class CameraCreator : VRCameraHook . CameraCreator
3643 {
@@ -45,6 +52,69 @@ public override void CreateCamera(VRCameraHook hook)
4552 }
4653 }
4754
55+ [ RenderModelHook . CreatorPriorityAttirbute ( 0 ) ]
56+ private class RenderModelCreator : RenderModelHook . DefaultRenderModelCreator
57+ {
58+ private uint m_index = INVALID_DEVICE_INDEX ;
59+
60+ public override bool shouldActive { get { return s_moduleInstance == null ? false : s_moduleInstance . isActivated ; } }
61+
62+ public override void UpdateRenderModel ( )
63+ {
64+ #if VIU_WAVEXR_ESSENCE_RENDERMODEL
65+ if ( HasActiveLoader ( WAVE_XR_LOADER_NAME ) )
66+ {
67+ if ( ! ChangeProp . Set ( ref m_index , hook . GetModelDeviceIndex ( ) ) ) { return ; }
68+ if ( VRModule . IsValidDeviceIndex ( m_index ) && m_index == VRModule . GetRightControllerDeviceIndex ( ) )
69+ {
70+ var go = new GameObject ( "Model" ) ;
71+ go . transform . SetParent ( hook . transform , false ) ;
72+ go . AddComponent < Wave . Essence . Controller . RenderModel > ( ) ;
73+ go . AddComponent < Wave . Essence . Controller . ButtonEffect > ( ) ;
74+ go . AddComponent < Wave . Essence . Controller . ShowIndicator > ( ) ;
75+ }
76+ else if ( VRModule . IsValidDeviceIndex ( m_index ) && m_index == VRModule . GetLeftControllerDeviceIndex ( ) )
77+ {
78+ var go = new GameObject ( "Model" ) ;
79+ go . transform . SetParent ( hook . transform , false ) ;
80+ var rm = go . AddComponent < Wave . Essence . Controller . RenderModel > ( ) ;
81+ rm . transform . gameObject . SetActive ( false ) ;
82+ rm . WhichHand = XR_Hand . NonDominant ;
83+ rm . transform . gameObject . SetActive ( true ) ;
84+ var be = go . AddComponent < Wave . Essence . Controller . ButtonEffect > ( ) ;
85+ be . transform . gameObject . SetActive ( false ) ;
86+ be . HandType = XR_Hand . NonDominant ;
87+ be . transform . gameObject . SetActive ( true ) ;
88+ go . AddComponent < Wave . Essence . Controller . ShowIndicator > ( ) ;
89+ }
90+ else
91+ {
92+ // deacitvate object for render model
93+ if ( m_model != null )
94+ {
95+ m_model . gameObject . SetActive ( false ) ;
96+ }
97+ }
98+ }
99+ else
100+ {
101+ base . UpdateRenderModel ( ) ;
102+ }
103+ #else
104+ base . UpdateRenderModel ( ) ;
105+ #endif
106+ }
107+
108+ public override void CleanUpRenderModel ( )
109+ {
110+ if ( m_model != null )
111+ {
112+ UnityEngine . Object . Destroy ( m_model ) ;
113+ m_model = null ;
114+ }
115+ }
116+ }
117+
48118 private class HapticVibrationState
49119 {
50120 public uint deviceIndex ;
@@ -188,6 +258,15 @@ public override void BeforeRenderUpdate()
188258
189259 SetupKnownDeviceModel ( currState ) ;
190260
261+ if ( ( device . characteristics & InputDeviceCharacteristics . Left ) > 0u )
262+ {
263+ m_leftHandedDeviceIndex = deviceIndex ;
264+ }
265+ else if ( ( device . characteristics & InputDeviceCharacteristics . Right ) > 0u )
266+ {
267+ m_rightHandedDeviceIndex = deviceIndex ;
268+ }
269+
191270 Debug . LogFormat ( "Device connected: {0} / {1} / {2} / {3} / {4} / {5} ({6})" , deviceIndex , currState . deviceClass , currState . deviceModel , currState . modelNumber , currState . serialNumber , device . name , device . characteristics ) ;
192271 }
193272
@@ -203,13 +282,20 @@ public override void BeforeRenderUpdate()
203282 }
204283 }
205284
206- UpdateHandHeldDeviceIndex ( ) ;
285+ // UpdateHandHeldDeviceIndex();
207286
208287 deviceIndex = 0u ;
209288 // reset all devices that is not connected in this frame
210- while ( TryGetValidDeviceState ( deviceIndex ++ , out prevState , out currState ) )
289+ while ( TryGetValidDeviceState ( deviceIndex , out prevState , out currState ) )
211290 {
212- if ( ! currState . isConnected ) { currState . Reset ( ) ; }
291+ if ( ! currState . isConnected )
292+ {
293+ currState . Reset ( ) ;
294+ if ( deviceIndex == m_leftHandedDeviceIndex ) { m_leftHandedDeviceIndex = VRModule . INVALID_DEVICE_INDEX ; }
295+ else if ( deviceIndex == m_rightHandedDeviceIndex ) { m_rightHandedDeviceIndex = VRModule . INVALID_DEVICE_INDEX ; }
296+ }
297+
298+ ++ deviceIndex ;
213299 }
214300
215301 ProcessConnectedDeviceChanged ( ) ;
0 commit comments