/// <summary> /// When a controller is detected, the model is spawned and the controller object /// is added to the tracking dictionary. /// </summary> /// <param name="sender">The SpatialInteractionManager which sent this event.</param> /// <param name="args">The source event data to be used to set up our controller model.</param> private void SpatialInteractionManager_SourceDetected(SpatialInteractionManager sender, SpatialInteractionSourceEventArgs args) { SpatialInteractionSource source = args.State.Source; // We only want to attempt loading a model if this source is actually a controller. if (source.Kind == SpatialInteractionSourceKind.Controller && controllerDictionary != null && !controllerDictionary.ContainsKey(source.Id)) { SpatialInteractionController controller = source.Controller; if (controller != null) { // Since this is a Unity call and will create a GameObject, this must run on Unity's app thread. UnityEngine.WSA.Application.InvokeOnAppThread(() => { // LoadControllerModel is a coroutine in order to handle/wait for async calls. StartCoroutine(LoadControllerModel(controller, source)); }, false); } } }
private void SpatialInteractionManager_SourceDetected(SpatialInteractionManager sender, SpatialInteractionSourceEventArgs args) { SpatialInteractionSource source = args.State.Source; #if VERBOSE_STATE TraceHelper.LogOnUnityThread("ControllerModelProvider::SourceDetected: " + args.State.Source.Id); #endif if (source.Kind == SpatialInteractionSourceKind.Controller) { SpatialInteractionController controller = source.Controller; if (controller != null) { string productId = MakeProductIdHash(controller, source.Handedness); if (!cachedModels.ContainsKey(productId) && !loadingModels.Contains(productId)) { loadingModels.Add(productId); LoadOffThread(args.State.Source.Id, controller, source.Handedness); } } } }
private IEnumerator LoadControllerModel(SpatialInteractionController controller, SpatialInteractionSource source) { GameObject controllerModelGameObject; if (source.Handedness == SpatialInteractionSourceHandedness.Left && LeftControllerOverride != null) { controllerModelGameObject = Instantiate(LeftControllerOverride); } else if (source.Handedness == SpatialInteractionSourceHandedness.Right && RightControllerOverride != null) { controllerModelGameObject = Instantiate(RightControllerOverride); } else { if (GLTFMaterial == null) { Debug.Log("If using glTF, please specify a material on " + name + "."); yield break; } // This API returns the appropriate glTF file according to the motion controller you're currently using, if supported. IAsyncOperation <IRandomAccessStreamWithContentType> modelTask = controller.TryGetRenderableModelAsync(); if (modelTask == null) { Debug.Log("Model task is null."); yield break; } while (modelTask.Status == AsyncStatus.Started) { yield return(null); } IRandomAccessStreamWithContentType modelStream = modelTask.GetResults(); if (modelStream == null) { Debug.Log("Model stream is null."); yield break; } if (modelStream.Size == 0) { Debug.Log("Model stream is empty."); yield break; } byte[] fileBytes = new byte[modelStream.Size]; using (DataReader reader = new DataReader(modelStream)) { DataReaderLoadOperation loadModelOp = reader.LoadAsync((uint)modelStream.Size); while (loadModelOp.Status == AsyncStatus.Started) { yield return(null); } reader.ReadBytes(fileBytes); } controllerModelGameObject = new GameObject(); GLTFComponentStreamingAssets gltfScript = controllerModelGameObject.AddComponent <GLTFComponentStreamingAssets>(); gltfScript.ColorMaterial = GLTFMaterial; gltfScript.NoColorMaterial = GLTFMaterial; gltfScript.GLTFData = fileBytes; yield return(gltfScript.LoadModel()); } FinishControllerSetup(controllerModelGameObject, source.Handedness.ToString(), source.Id); }
private string MakeProductIdHash(SpatialInteractionController controller, SpatialInteractionSourceHandedness handedness) { return(string.Format("{0}-{1}-{2}-{3}", controller.VendorId, controller.ProductId, controller.Version, handedness).ToLower()); }
public bool Contains(SpatialInteractionController controller, SpatialInteractionSourceHandedness handedness) { string id = MakeProductIdHash(controller, handedness); return(cachedModels.ContainsKey(id)); }