/// <summary> /// Attempts to load the controller model render settings from the <see cref="MixedRealityControllerVisualizationProfile"/> /// to render the controllers in the scene. /// </summary> /// <param name="controllerType">The controller type.</param> /// <param name="glbData">The raw binary glb data of the controller model, typically loaded from the driver.</param> /// <param name="useAlternatePoseAction">Should the visualizer be assigned the alternate pose actions?</param> /// <returns>True, if controller model is being properly rendered.</returns> /// <remarks> /// (Given a user can, have no system default and override specific controller types with a system default, OR, enable a system system default but override that default for specific controllers) /// Flow is as follows: /// 1. Check if either there is a global setting for an system override and if there is a specific customization for that controller type /// 2. If either the there is a system data and either the /// /// </remarks> internal async Task TryRenderControllerModelAsync(Type controllerType, byte[] glbData = null, bool useAlternatePoseAction = false) { if (controllerType == null) { Debug.LogError("Unknown type of controller, cannot render"); return; } var visualizationProfile = MixedRealityToolkit.Instance.ActiveProfile.InputSystemProfile.ControllerVisualizationProfile; if (visualizationProfile == null) { Debug.LogError("Missing ControllerVisualizationProfile!"); return; } if (!visualizationProfile.RenderMotionControllers) { return; } GltfObject gltfObject = null; // If a specific controller template exists, check if it wants to override the global model, or use the system default specifically (in case global default is not used) bool useSystemDefaultModels = visualizationProfile.GetControllerModelOverride(controllerType, ControllerHandedness, out var controllerModel); // If an override is not configured for defaults and has no model, then use the system default check if (!useSystemDefaultModels && controllerModel == null) { useSystemDefaultModels = visualizationProfile.UseDefaultModels; } // if we have model data from the platform and the controller has been configured to use the default model, attempt to load the controller model from glbData. if (glbData != null && useSystemDefaultModels) { gltfObject = GltfUtility.GetGltfObjectFromGlb(glbData); await gltfObject.ConstructAsync(); controllerModel = gltfObject.GameObjectReference; } // If we didn't get an override model, and we didn't load the driver model, // then get the global controller model for each hand. if (controllerModel == null) { switch (ControllerHandedness) { case Handedness.Left when visualizationProfile.GlobalLeftHandModel != null: controllerModel = visualizationProfile.GlobalLeftHandModel; break; case Handedness.Right when visualizationProfile.GlobalRightHandModel != null: controllerModel = visualizationProfile.GlobalRightHandModel; break; } } // If we've got a controller model, then place it in the scene and get/attach the visualizer. if (controllerModel != null) { //If the model was loaded from a system template if (useSystemDefaultModels && gltfObject != null) { controllerModel.name = $"{controllerType.Name}_Visualization"; controllerModel.transform.SetParent(MixedRealityToolkit.CameraSystem?.CameraRig.PlayspaceTransform); var visualizationType = visualizationProfile.GetControllerVisualizationTypeOverride(controllerType, ControllerHandedness) ?? visualizationProfile.ControllerVisualizationType; controllerModel.AddComponent(visualizationType.Type); Visualizer = controllerModel.GetComponent <IMixedRealityControllerVisualizer>(); } //If the model was a prefab else { var controllerObject = UnityEngine.Object.Instantiate(controllerModel, MixedRealityToolkit.CameraSystem?.CameraRig.PlayspaceTransform); controllerObject.name = $"{controllerType.Name}_Visualization"; Visualizer = controllerObject.GetComponent <IMixedRealityControllerVisualizer>(); } //If a visualizer exists, set it up and bind it to the controller if (Visualizer != null) { Visualizer.Controller = this; SetupController(Visualizer); } else { Debug.LogWarning($"Failed to attach a valid IMixedRealityControllerVisualizer to {controllerType.Name}"); } } if (Visualizer == null) { Debug.LogError("Failed to render controller model!"); } void SetupController(IMixedRealityControllerVisualizer visualizer) { if (!useAlternatePoseAction && visualizationProfile.TryGetControllerPose(controllerType, ControllerHandedness, out MixedRealityInputAction poseAction)) { visualizer.UseSourcePoseData = false; visualizer.PoseAction = poseAction; } else if (useAlternatePoseAction && visualizationProfile.TryGetControllerPoseOverride(controllerType, ControllerHandedness, out MixedRealityInputAction altPoseAction)) { visualizer.UseSourcePoseData = false; visualizer.PoseAction = altPoseAction; } else if (visualizationProfile.GlobalPointerPose != MixedRealityInputAction.None) { visualizer.UseSourcePoseData = false; visualizer.PoseAction = visualizationProfile.GlobalPointerPose; } else { Debug.LogError("Failed to get pose actions for controller visual."); } } }
/// <summary> /// Constructs the glTF Object. /// </summary> /// <param name="gltfObject"></param> /// <returns>The new <see cref="GameObject"/> of the final constructed <see cref="GltfScene"/></returns> public static async void Construct(this GltfObject gltfObject) { await gltfObject.ConstructAsync(); }
/// <summary> /// Attempts to load the controller model render settings from the <see cref="MixedRealityControllerVisualizationProfile"/> /// to render the controllers in the scene. /// </summary> /// <param name="glbData">The raw binary glb data of the controller model, typically loaded from the driver.</param> /// <param name="useAlternatePoseAction">Should the visualizer be assigned the alternate pose actions?</param> public async Task TryRenderControllerModelAsync(byte[] glbData = null, bool useAlternatePoseAction = false) { if (visualizationProfile.IsNull()) { Debug.LogWarning($"Missing {nameof(visualizationProfile)} for {GetType().Name}"); return; } GltfObject gltfObject = null; GameObject controllerModel = null; // if we have model data from the platform and the controller has been configured to use the default model, attempt to load the controller model from glbData. if (glbData != null) { gltfObject = GltfUtility.GetGltfObjectFromGlb(glbData); await gltfObject.ConstructAsync(); controllerModel = gltfObject.GameObjectReference; } // If we didn't get an override model, and we didn't load the driver model, // then get the global controller model for each hand. if (controllerModel.IsNull()) { switch (ControllerHandedness) { case Handedness.Left when !visualizationProfile.LeftHandModel.IsNull(): controllerModel = visualizationProfile.LeftHandModel; break; case Handedness.Right when !visualizationProfile.LeftHandModel.IsNull(): controllerModel = visualizationProfile.LeftHandModel; break; } } // If we've got a controller model, then place it in the scene and get/attach the visualizer. if (!controllerModel.IsNull()) { var playspaceTransform = MixedRealityToolkit.CameraSystem != null ? MixedRealityToolkit.CameraSystem.MainCameraRig.PlayspaceTransform : CameraCache.Main.transform.parent; // If the model was loaded from a system template if (gltfObject != null) { controllerModel.name = $"{GetType().Name}_Visualization"; controllerModel.transform.SetParent(playspaceTransform); var visualizationType = visualizationProfile.ControllerVisualizationType; controllerModel.AddComponent(visualizationType.Type); Visualizer = controllerModel.GetComponent <IMixedRealityControllerVisualizer>(); } // If the model was a prefab else { var controllerObject = Object.Instantiate(controllerModel, playspaceTransform) as GameObject; Debug.Assert(controllerObject != null); controllerObject.name = $"{GetType().Name}_Visualization"; Visualizer = controllerObject.GetComponent <IMixedRealityControllerVisualizer>(); } // If a visualizer exists, set it up and bind it to the controller if (Visualizer != null) { Visualizer.Controller = this; SetupController(Visualizer); } else { Debug.LogWarning($"Failed to attach a valid {nameof(IMixedRealityControllerVisualizer)} to {GetType().Name}"); } } if (Visualizer == null) { Debug.LogError("Failed to render controller model!"); } void SetupController(IMixedRealityControllerVisualizer visualizer) { if (useAlternatePoseAction) { visualizer.UseSourcePoseData = visualizationProfile.AlternatePointerPose == MixedRealityInputAction.None; visualizer.PoseAction = visualizationProfile.AlternatePointerPose; } else { visualizer.UseSourcePoseData = visualizationProfile.PointerPose == MixedRealityInputAction.None; visualizer.PoseAction = visualizationProfile.PointerPose; } } }