Skip to content

Commit bea9b06

Browse files
lawwonglawwong
authored andcommitted
Fix Oculus render model not working on OVR plugin v39+
Oculus Avatar SDK (legacy) is removed since Oculus Integration SDK v39 Without OvrAvatar, VIU loads Ovr built-in controller prefab instead
1 parent 572b266 commit bea9b06

7 files changed

Lines changed: 182 additions & 10 deletions

File tree

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,121 @@ private bool IsHand()
248248
return m_index == s_leftHandIndex || m_index == s_rightHandIndex;
249249
}
250250
}
251+
#elif UNITY_2020_3_OR_NEWER && VIU_OCULUSVR_20_0_OR_NEWER
252+
private class RenderModelCreator : RenderModelHook.DefaultRenderModelCreator
253+
{
254+
private static bool s_mgrInit;
255+
private uint m_index = INVALID_DEVICE_INDEX;
256+
private bool m_isLeft;
257+
private bool m_isController;
258+
private bool m_isTrackedHand;
259+
260+
private OVRControllerHelper m_controllerModel;
261+
private OculusHandRenderModel m_trackedHandModel;
262+
263+
public override bool shouldActive
264+
{
265+
get
266+
{
267+
if (!VIUSettings.EnableOculusSDKHandRenderModel && !VIUSettings.EnableOculusSDKControllerRenderModel) { return false; }
268+
if (s_moduleInstance == null) { return false; }
269+
if (!s_moduleInstance.isActivated) { return false; }
270+
return true;
271+
}
272+
}
273+
274+
public override void UpdateRenderModel()
275+
{
276+
if (!ChangeProp.Set(ref m_index, hook.GetModelDeviceIndex())) { return; }
277+
278+
var isValidDevice = VRModule.IsValidDeviceIndex(m_index);
279+
if (!isValidDevice)
280+
{
281+
m_isController = false;
282+
m_isTrackedHand = false;
283+
}
284+
else
285+
{
286+
var dvc = VRModule.GetDeviceState(m_index);
287+
m_isLeft = dvc.deviceModel.IsLeft();
288+
m_isController = dvc.deviceClass == VRModuleDeviceClass.Controller;
289+
m_isTrackedHand = dvc.deviceClass == VRModuleDeviceClass.TrackedHand;
290+
}
291+
292+
if (m_isController && VIUSettings.oculusVRControllerPrefab == null)
293+
{
294+
m_isController = false;
295+
}
296+
297+
if (m_isController)
298+
{
299+
if (!s_mgrInit && !OVRManager.OVRManagerinitialized)
300+
{
301+
var go = new GameObject("OVRManager");
302+
go.transform.SetParent(VRModule.GetInstanceGameObject().transform, false);
303+
go.AddComponent<OVRManager>();
304+
s_mgrInit = true;
305+
}
306+
307+
if (m_controllerModel != null)
308+
{
309+
var modelIsLeft = m_controllerModel.m_controller == OVRInput.Controller.LTouch;
310+
if (modelIsLeft != m_isLeft)
311+
{
312+
Object.Destroy(m_controllerModel.gameObject);
313+
m_controllerModel = null;
314+
}
315+
}
316+
317+
if (m_controllerModel == null)
318+
{
319+
var go = Object.Instantiate(VIUSettings.oculusVRControllerPrefab);
320+
go.name = VIUSettings.oculusVRControllerPrefab.name;
321+
go.transform.SetParent(hook.transform, false);
322+
go.SetActive(false);
323+
m_controllerModel = go.GetComponent<OVRControllerHelper>();
324+
m_controllerModel.m_controller = m_isLeft ? OVRInput.Controller.LTouch : OVRInput.Controller.RTouch;
325+
}
326+
327+
m_controllerModel.gameObject.SetActive(true);
328+
}
329+
else
330+
{
331+
if (m_controllerModel != null)
332+
{
333+
m_controllerModel.gameObject.SetActive(false);
334+
}
335+
}
336+
337+
if (m_isTrackedHand)
338+
{
339+
if (m_trackedHandModel == null)
340+
{
341+
var go = new GameObject(typeof(OculusHandRenderModel).Name);
342+
go.transform.SetParent(hook.transform, false);
343+
go.transform.localRotation *=
344+
Quaternion.Inverse(
345+
m_isLeft ?
346+
Quaternion.LookRotation(Vector3.right, Vector3.down) :
347+
Quaternion.LookRotation(Vector3.left, Vector3.up));
348+
m_trackedHandModel = go.AddComponent<OculusHandRenderModel>();
349+
m_trackedHandModel.Initialize(m_isLeft);
350+
}
351+
352+
m_trackedHandModel.SetHand(m_isLeft);
353+
m_trackedHandModel.gameObject.SetActive(true);
354+
}
355+
else
356+
{
357+
if (m_trackedHandModel != null)
358+
{
359+
m_trackedHandModel.gameObject.SetActive(false);
360+
}
361+
}
362+
363+
UpdateDefaultRenderModel(!m_isController && !m_isTrackedHand);
364+
}
365+
}
251366
#endif
252367

253368
#if VIU_OCULUSVR

Assets/HTC.UnityPlugin/VRModule/VRModuleDeviceState.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ public static bool IsRight(this VRModuleDeviceModel deviceModel)
436436
switch (deviceModel)
437437
{
438438
case VRModuleDeviceModel.OculusTouchRight:
439+
case VRModuleDeviceModel.OculusQuest2ControllerRight:
439440
case VRModuleDeviceModel.KnucklesRight:
440441
case VRModuleDeviceModel.WMRControllerRight:
441442
case VRModuleDeviceModel.ViveCosmosControllerRight:
@@ -457,6 +458,7 @@ public static bool IsLeft(this VRModuleDeviceModel deviceModel)
457458
switch (deviceModel)
458459
{
459460
case VRModuleDeviceModel.OculusTouchLeft:
461+
case VRModuleDeviceModel.OculusQuest2ControllerLeft:
460462
case VRModuleDeviceModel.KnucklesLeft:
461463
case VRModuleDeviceModel.WMRControllerLeft:
462464
case VRModuleDeviceModel.ViveCosmosControllerLeft:

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Editor/VRPlatformSettings/OculusGoSettings.cs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,13 @@ public static partial class VIUSettingsEditor
424424
#else
425425
AndroidSdkVersions.AndroidApiLevel21;
426426
#endif
427+
public static readonly Version oculusVRPlugin_warpperVersion =
428+
#if VIU_OCULUSVR
429+
OVRPlugin.wrapperVersion;
430+
#else
431+
new System.Version(0, 0, 0);
432+
#endif
433+
public static readonly Version oculusVRPlugin_v39_warpperVersion = new Version(1, 71, 0);
427434

428435
public static bool canSupportOculusGo
429436
{
@@ -684,6 +691,7 @@ public override void OnPreferenceGUI()
684691
// Hand tracking support
685692
const string enableHandTrackingTitle = "Enable Oculus Hand Tracking";
686693
const string enableHandRenderModelTitle = "Enable Oculus Tracked Hand Render Model";
694+
687695
#if VIU_OCULUSVR_20_0_OR_NEWER
688696
{
689697
var oldEnableHandTracking = VIUSettings.activateOculusVRModule && oculusProjectConfig.handTrackingSupport != OVRProjectConfig.HandTrackingSupport.ControllersOnly;
@@ -735,20 +743,28 @@ public override void OnPreferenceGUI()
735743
}
736744
#endif
737745

738-
#pragma warning disable 0162
739746
// Controller Render Model
740747
const string enableControllerRenderModelTitle = "Enable Oculus Controller Render Model";
741748
const string enableControllerRenderModelSkeletonTitle = "Enable Hand Attached to Oculus Controller Render Model";
742-
if (OculusVRExtension.VIUOvrAvatar.SUPPORTED)
749+
750+
if (OculusVRExtension.VIUOvrAvatar.SUPPORTED || oculusVRPlugin_warpperVersion >= oculusVRPlugin_v39_warpperVersion)
743751
{
744752
var oldValue = VIUSettings.activateOculusVRModule && VIUSettings.EnableOculusSDKControllerRenderModel;
745753
var newValue = EditorGUILayout.ToggleLeft(new GUIContent(enableControllerRenderModelTitle, VIUSettings.ENABLE_OCULUS_SDK_CONTROLLER_RENDER_MODEL_TOOLTIP), oldValue);
754+
755+
if (!oldValue && newValue && !VRModule.isOculusVRAvatarSupported && FindControllerRenderModelPrefab() == null)
756+
{
757+
Debug.LogError("missing \"OVRControllerPrefab\"");
758+
newValue = false;
759+
}
760+
746761
if (newValue)
747762
{
748763
if (!oldValue)
749764
{
750765
VIUSettings.activateOculusVRModule = true;
751766
VIUSettings.EnableOculusSDKControllerRenderModel = true;
767+
VIUSettings.oculusVRControllerPrefab = FindControllerRenderModelPrefab();
752768
}
753769
}
754770
else
@@ -759,15 +775,17 @@ public override void OnPreferenceGUI()
759775
}
760776
}
761777

762-
if (newValue)
778+
if (newValue && VRModule.isOculusVRAvatarSupported)
763779
{
764780
VIUSettings.EnableOculusSDKControllerRenderModelSkeleton = EditorGUILayout.ToggleLeft(new GUIContent(enableControllerRenderModelSkeletonTitle, VIUSettings.ENABLE_OCULUS_SDK_CONTROLLER_RENDER_MODEL_SKELETON_TOOLTIP), VIUSettings.EnableOculusSDKControllerRenderModelSkeleton);
765781
}
766782
else
767783
{
784+
var tooltip = VRModule.isOculusVRAvatarSupported ? VIUSettings.ENABLE_OCULUS_SDK_CONTROLLER_RENDER_MODEL_SKELETON_TOOLTIP : "Currently only support lagacy OVR Avatar in Oculus Integration SDK v38 or before";
785+
768786
var wasGUIEnabled = GUI.enabled;
769787
GUI.enabled = false;
770-
EditorGUILayout.ToggleLeft(new GUIContent(enableControllerRenderModelSkeletonTitle, VIUSettings.ENABLE_OCULUS_SDK_CONTROLLER_RENDER_MODEL_SKELETON_TOOLTIP), false);
788+
EditorGUILayout.ToggleLeft(new GUIContent(enableControllerRenderModelSkeletonTitle, tooltip), false);
771789
GUI.enabled = wasGUIEnabled;
772790
}
773791
}
@@ -788,7 +806,6 @@ public override void OnPreferenceGUI()
788806

789807
GUI.enabled = wasGUIEnabled;
790808
}
791-
#pragma warning restore 0162
792809

793810
// Custom Android manifest
794811
EditorGUILayout.BeginHorizontal();
@@ -826,10 +843,17 @@ public override void OnPreferenceGUI()
826843
}
827844
s_guiChanged |= EditorGUI.EndChangeCheck();
828845
}
846+
847+
if (!support || !VIUSettings.EnableOculusSDKControllerRenderModel)
848+
{
849+
VIUSettings.oculusVRControllerPrefab = null;
850+
}
829851
}
830852

831853
public void OnPreprocessBuild(BuildTarget target, string path)
832854
{
855+
VIUSettings.oculusVRControllerPrefab = support && VIUSettings.EnableOculusSDKControllerRenderModel ? FindControllerRenderModelPrefab() : null;
856+
833857
if (!support) { return; }
834858

835859
if (File.Exists(VIUSettings.oculusVRAndroidManifestPath))
@@ -845,6 +869,8 @@ public void OnPreprocessBuild(BuildTarget target, string path)
845869
#if UNITY_2018_1_OR_NEWER
846870
public void OnPreprocessBuild(BuildReport report)
847871
{
872+
VIUSettings.oculusVRControllerPrefab = support && VIUSettings.EnableOculusSDKControllerRenderModel ? FindControllerRenderModelPrefab() : null;
873+
848874
if (!support) { return; }
849875

850876
if (File.Exists(VIUSettings.oculusVRAndroidManifestPath))
@@ -872,6 +898,27 @@ public void OnPreprocessBuild(BuildReport report)
872898
}
873899
}
874900
}
901+
#endif
902+
#if UNITY_2020_3_OR_NEWER && VIU_OCULUSVR_20_0_OR_NEWER
903+
private GameObject FindControllerRenderModelPrefab()
904+
{
905+
var results = AssetDatabase.FindAssets("OVRControllerPrefab t:Prefab");
906+
if (results != null || results.Length > 0)
907+
{
908+
foreach (var result in results)
909+
{
910+
var go = AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(result));
911+
if (go != null && go.TryGetComponent<OVRControllerHelper>(out _))
912+
{
913+
VIUSettings.oculusVRControllerPrefab = go;
914+
return go;
915+
}
916+
}
917+
}
918+
return null;
919+
}
920+
#else
921+
private GameObject FindControllerRenderModelPrefab() { return null; }
875922
#endif
876923
}
877924
}

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Editor/VRPlatformSettings/OculusSettings.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ public OculusRecommendedSettings()
4343
recommendedValue = true,
4444
});
4545

46+
#if VIU_OCULUSVR_20_0_OR_NEWER
47+
// Oculus Avatar SDK (legacy) is removed since Oculus Integration SDK v39
4648
Add(new VIUVersionCheck.RecommendedSetting<bool>()
4749
{
4850
settingTitle = "Add Missing Assembly Definitions in Oculus SDK",
49-
skipCheckFunc = () => { return !VRModule.isOculusVRPluginDetected; },
51+
skipCheckFunc = () => { return VIUSettingsEditor.oculusVRPlugin_warpperVersion >= VIUSettingsEditor.oculusVRPlugin_v39_warpperVersion; },
5052
currentValueFunc = () => { return VRModule.isOculusVRAvatarSupported; },
5153
setValueFunc = v =>
5254
{
@@ -69,6 +71,7 @@ public OculusRecommendedSettings()
6971
},
7072
recommendedValue = true,
7173
});
74+
#endif
7275
}
7376

7477
private static bool SafeCopy(string src, string dst)

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Misc/OculusVRExtension/OculusHandRenderModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace HTC.UnityPlugin.Vive.OculusVRExtension
1010
public class OculusHandRenderModel : MonoBehaviour
1111
{
1212
#if VIU_OCULUSVR_20_0_OR_NEWER
13-
public const bool SUPPORTED = true;
13+
public static readonly bool SUPPORTED = true;
1414
private bool m_isLeftHand;
1515
private OVRHand m_ovrHand;
1616
private OVRSkeleton m_ovrSkeleton;
@@ -111,7 +111,7 @@ private void UpdateOvrMesh()
111111
}
112112
}
113113
#else
114-
public const bool SUPPORTED = false;
114+
public static readonly bool SUPPORTED = false;
115115
public void Initialize(bool isLeftHand) { }
116116
public void SetHand(bool isLeftHand) { }
117117
#endif

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Misc/OculusVRExtension/VIUOvrAvatar.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public enum OVRControllerType
2525
}
2626

2727
#if VIU_OCULUSVR_AVATAR
28-
public const bool SUPPORTED = true;
28+
public static readonly bool SUPPORTED = true;
2929
#if UNITY_ANDROID && !UNITY_EDITOR
3030
public const bool USE_MOBILE_TEXTURE_FORMAT = true;
3131
#else
@@ -395,7 +395,7 @@ private void UntrackLoadingId(ulong id)
395395
}
396396
}
397397
#else
398-
public const bool SUPPORTED = false;
398+
public static readonly bool SUPPORTED = false;
399399
#endif
400400
}
401401
}

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/VIUSettingsPartials/OculusSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ public partial class VIUSettings : ScriptableObject
2626
[SerializeField, Tooltip(ENABLE_OCULUS_SDK_CONTROLLER_RENDER_MODEL_SKELETON_TOOLTIP)]
2727
private bool m_enableOculusSDKControllerRenderModelSkeleton = ENABLE_OCULUS_SDK_CONTROLLER_RENDER_MODEL_SKELETON_DEFAULT_VALUE;
2828

29+
[SerializeField]
30+
private GameObject m_oculusVRControllerPrefab;
31+
2932
public static bool activateOculusVRModule { get { return Instance == null ? ACTIVATE_OCULUS_VR_MODULE_DEFAULT_VALUE : s_instance.m_activateOculusVRModule; } set { if (Instance != null) { Instance.m_activateOculusVRModule = value; } } }
3033
public static string oculusVRAndroidManifestPath { get { return Instance == null ? "" : s_instance.m_oculusVRAndroidManifestPath; } set { if (Instance != null) { Instance.m_oculusVRAndroidManifestPath = value; } } }
3134
public static bool EnableOculusSDKHandRenderModel { get { return Instance == null ? ENABLE_OCULUS_SDK_HAND_RENDER_MODEL_DEFAULT_VALUE : s_instance.m_enableOculusSDKHandRenderModel; } set { if (Instance != null) { Instance.m_enableOculusSDKHandRenderModel = value; } } }
3235
public static bool EnableOculusSDKControllerRenderModel { get { return Instance == null ? ENABLE_OCULUS_SDK_CONTROLLER_RENDER_MODEL_DEFAULT_VALUE : s_instance.m_enableOculusSDKControllerRenderModel; } set { if (Instance != null) { Instance.m_enableOculusSDKControllerRenderModel = value; } } }
3336
public static bool EnableOculusSDKControllerRenderModelSkeleton { get { return Instance == null ? ENABLE_OCULUS_SDK_CONTROLLER_RENDER_MODEL_SKELETON_DEFAULT_VALUE : s_instance.m_enableOculusSDKControllerRenderModelSkeleton; } set { if (Instance != null) { Instance.m_enableOculusSDKControllerRenderModelSkeleton = value; } } }
37+
38+
public static GameObject oculusVRControllerPrefab { get { return Instance == null ? null : Instance.m_oculusVRControllerPrefab; } set { if (Instance != null) { Instance.m_oculusVRControllerPrefab = value; } } }
3439
}
3540
}

0 commit comments

Comments
 (0)