private IEnumerator LoadControllerModel(InteractionSource source) { loadingControllers.Add(source.id); GameObject controllerModelGameObject; if (source.handedness == InteractionSourceHandedness.Left && LeftControllerOverride != null) { controllerModelGameObject = Instantiate(LeftControllerOverride); } else if (source.handedness == InteractionSourceHandedness.Right && RightControllerOverride != null) { controllerModelGameObject = Instantiate(RightControllerOverride); } else { #if !UNITY_EDITOR if (GLTFMaterial == null) { Debug.Log("If using glTF, please specify a material on " + name + "."); loadingControllers.Remove(source.id); yield break; } // This API returns the appropriate glTF file according to the motion controller you're currently using, if supported. IAsyncOperation <IRandomAccessStreamWithContentType> modelTask = source.TryGetRenderableModelAsync(); if (modelTask == null) { Debug.Log("Model task is null."); loadingControllers.Remove(source.id); yield break; } while (modelTask.Status == AsyncStatus.Started) { yield return(null); } IRandomAccessStreamWithContentType modelStream = modelTask.GetResults(); if (modelStream == null) { Debug.Log("Model stream is null."); loadingControllers.Remove(source.id); yield break; } if (modelStream.Size == 0) { Debug.Log("Model stream is empty."); loadingControllers.Remove(source.id); 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()); #else loadingControllers.Remove(source.id); yield break; #endif } FinishControllerSetup(controllerModelGameObject, source.handedness.ToString(), source.id); }
private async void CreateControllerModelFromPlatformSDK(InteractionSource interactionSource) { Debug.Log("Trying to load controller model from platform SDK"); byte[] fileBytes = null; #if WINDOWS_UWP var controllerModelStream = await interactionSource.TryGetRenderableModelAsync(); if (controllerModelStream == null || controllerModelStream.Size == 0) { Debug.LogError("Failed to obtain controller model from driver"); } else { fileBytes = new byte[controllerModelStream.Size]; using (DataReader reader = new DataReader(controllerModelStream)) { await reader.LoadAsync((uint)controllerModelStream.Size); reader.ReadBytes(fileBytes); } } #endif GameObject gltfGameObject = null; if (fileBytes != null) { var gltfObject = GltfUtility.GetGltfObjectFromGlb(fileBytes); gltfGameObject = await gltfObject.ConstructAsync(); if (gltfGameObject != null) { var visualizationProfile = GetControllerVisualizationProfile(); if (visualizationProfile != null) { var visualizationType = visualizationProfile.GetControllerVisualizationTypeOverride(GetType(), ControllerHandedness); if (visualizationType != null) { // Set the platform controller model to not be destroyed when the source is lost. It'll be disabled instead, // and re-enabled when the same controller is re-detected. if (gltfGameObject.AddComponent(visualizationType.Type) is IMixedRealityControllerPoseSynchronizer visualizer) { visualizer.DestroyOnSourceLost = false; } TryAddControllerModelToSceneHierarchy(gltfGameObject); controllerDictionary.Add(GenerateKey(interactionSource), gltfGameObject); } else { Debug.LogError("Controller visualization type not defined for controller visualization profile"); UnityEngine.Object.Destroy(gltfGameObject); gltfGameObject = null; } } else { Debug.LogError("Failed to obtain a controller visualization profile"); } } } failedToObtainControllerModel = (gltfGameObject == null); if (failedToObtainControllerModel) { Debug.LogWarning("Failed to create controller model from driver, defaulting to BaseController behavior"); TryRenderControllerModel(GetType(), InputSourceType.Controller); } }
private IEnumerator LoadSourceControllerModel(InteractionSource source) { byte[] fileBytes; GameObject controllerModelGameObject; if (GLTFMaterial == null) { Debug.Log("If using glTF, please specify a material on " + name + "."); yield break; } #if !UNITY_EDITOR // This API returns the appropriate glTF file according to the motion controller you're currently using, if supported. IAsyncOperation <IRandomAccessStreamWithContentType> modelTask = source.TryGetRenderableModelAsync(); if (modelTask == null) { Debug.Log("Model task is null; loading alternate."); LoadAlternateControllerModel(source); yield break; } while (modelTask.Status == AsyncStatus.Started) { yield return(null); } IRandomAccessStreamWithContentType modelStream = modelTask.GetResults(); if (modelStream == null) { Debug.Log("Model stream is null; loading alternate."); LoadAlternateControllerModel(source); yield break; } if (modelStream.Size == 0) { Debug.Log("Model stream is empty; loading alternate."); LoadAlternateControllerModel(source); yield break; } 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); } #else IntPtr controllerModel = new IntPtr(); uint outputSize = 0; if (TryGetMotionControllerModel(source.id, out outputSize, out controllerModel)) { fileBytes = new byte[Convert.ToInt32(outputSize)]; Marshal.Copy(controllerModel, fileBytes, 0, Convert.ToInt32(outputSize)); } else { Debug.Log("Unable to load controller models; loading alternate."); LoadAlternateControllerModel(source); yield break; } #endif controllerModelGameObject = new GameObject { name = "glTFController" }; controllerModelGameObject.transform.Rotate(0, 180, 0); var sceneImporter = new GLTFSceneImporter( "", new MemoryStream(fileBytes, 0, fileBytes.Length, false, true), controllerModelGameObject.transform ); sceneImporter.SetShaderForMaterialType(GLTFSceneImporter.MaterialType.PbrMetallicRoughness, GLTFMaterial.shader); sceneImporter.SetShaderForMaterialType(GLTFSceneImporter.MaterialType.KHR_materials_pbrSpecularGlossiness, GLTFMaterial.shader); sceneImporter.SetShaderForMaterialType(GLTFSceneImporter.MaterialType.CommonConstant, GLTFMaterial.shader); yield return(sceneImporter.Load()); FinishControllerSetup(controllerModelGameObject, source.handedness, GenerateKey(source)); }
private IEnumerator LoadSourceControllerModel(InteractionSource source) { byte[] fileBytes; GameObject controllerModelGameObject; if (GLTFMaterial == null) { Debug.Log("If using glTF, please specify a material on " + name + "."); yield break; } #if !UNITY_EDITOR // This API returns the appropriate glTF file according to the motion controller you're currently using, if supported. IAsyncOperation <IRandomAccessStreamWithContentType> modelTask = source.TryGetRenderableModelAsync(); if (modelTask == null) { Debug.Log("Model task is null; loading alternate."); LoadAlternateControllerModel(source); yield break; } while (modelTask.Status == AsyncStatus.Started) { yield return(null); } IRandomAccessStreamWithContentType modelStream = modelTask.GetResults(); if (modelStream == null) { Debug.Log("Model stream is null; loading alternate."); LoadAlternateControllerModel(source); yield break; } if (modelStream.Size == 0) { Debug.Log("Model stream is empty; loading alternate."); LoadAlternateControllerModel(source); yield break; } 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); } #else IntPtr controllerModel = new IntPtr(); uint outputSize = 0; if (TryGetMotionControllerModel(source.id, out outputSize, out controllerModel)) { fileBytes = new byte[Convert.ToInt32(outputSize)]; Marshal.Copy(controllerModel, fileBytes, 0, Convert.ToInt32(outputSize)); } else { Debug.Log("Unable to load controller models; loading alternate."); LoadAlternateControllerModel(source); yield break; } #endif controllerModelGameObject = new GameObject { name = "glTFController" }; GLTFComponentStreamingAssets gltfScript = controllerModelGameObject.AddComponent <GLTFComponentStreamingAssets>(); gltfScript.ColorMaterial = GLTFMaterial; gltfScript.NoColorMaterial = GLTFMaterial; gltfScript.GLTFData = fileBytes; yield return(gltfScript.LoadModel()); FinishControllerSetup(controllerModelGameObject, source.handedness, GenerateKey(source)); }
private IEnumerator LoadSourceControllerModel(InteractionSource source) { //throw new NotImplementedException(); byte[] fileBytes = null; UnityEngine.Material GLTFMaterial = new UnityEngine.Material(Shader.Find("Standard")); #if !UNITY_EDITOR // This API returns the appropriate glTF file according to the motion controller you're currently using, if supported. IAsyncOperation <IRandomAccessStreamWithContentType> modelTask = source.TryGetRenderableModelAsync(); if (modelTask == null) { Debug.Log("Model task is null; loading alternate."); LoadAlternateControllerModel(source); yield break; } while (modelTask.Status == AsyncStatus.Started) { yield return(null); } IRandomAccessStreamWithContentType modelStream = modelTask.GetResults(); if (modelStream == null) { Debug.Log("Model stream is null; loading alternate."); LoadAlternateControllerModel(source); yield break; } if (modelStream.Size == 0) { Debug.Log("Model stream is empty; loading alternate."); LoadAlternateControllerModel(source); yield break; } 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); } #else IntPtr controllerModel = new IntPtr(); uint outputSize = 0; try { if (TryGetMotionControllerModel(source.id, out outputSize, out controllerModel)) { fileBytes = new byte[Convert.ToInt32(outputSize)]; Marshal.Copy(controllerModel, fileBytes, 0, Convert.ToInt32(outputSize)); } else { Debug.Log("Unable to load controller models; loading alternate."); LoadAlternateControllerModel(source); yield break; } } catch (Exception e) { loading = false; } #endif RenderModel = new GameObject { name = source.handedness + "-glTFController" }; GLTFComponentStreamingAssets gltfScript = RenderModel.AddComponent <GLTFComponentStreamingAssets>(); gltfScript.ColorMaterial = GLTFMaterial; gltfScript.NoColorMaterial = GLTFMaterial; gltfScript.GLTFData = fileBytes; yield return(gltfScript.LoadModel()); RenderModelInitialized = true; loading = false; FinishControllerSetup(RenderModel, source.handedness, source.vendorId + "/" + source.productId + "/" + source.productVersion + "/" + source.handedness); }
private IEnumerator LoadControllerModel(InteractionSource source) { GameObject controllerModelGameObject = null; GameObject parentGameObject = null; GameObject rootGameObject = null; if (source.handedness == InteractionSourceHandedness.Left) { rootGameObject = leftControllerParent; parentGameObject = leftModelParent; } else if (source.handedness == InteractionSourceHandedness.Right) { rootGameObject = rightControllerParent; parentGameObject = rightModelParent; } Debug.Assert(rootGameObject != null); rootGameObject.SetActive(true); if (source.handedness == InteractionSourceHandedness.Left && LeftControllerOverride != null) { if (leftControllerModelGameObject == null) { leftControllerModelGameObject = Instantiate(LeftControllerOverride); } controllerModelGameObject = leftControllerModelGameObject; } else if (source.handedness == InteractionSourceHandedness.Right && RightControllerOverride != null) { if (rightControllerModelGameObject == null) { rightControllerModelGameObject = Instantiate(RightControllerOverride); } controllerModelGameObject = rightControllerModelGameObject; } else { #if !UNITY_EDITOR bool requireNewModel = false; if (source.handedness == InteractionSourceHandedness.Left && leftControllerModelGameObject == null) { requireNewModel = true; } if (source.handedness == InteractionSourceHandedness.Right && rightControllerModelGameObject == null) { requireNewModel = true; } if (requireNewModel) { 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 = source.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()); // 캐싱 if (source.handedness == InteractionSourceHandedness.Left) { leftControllerModelGameObject = controllerModelGameObject; } else if (source.handedness == InteractionSourceHandedness.Right) { rightControllerModelGameObject = controllerModelGameObject; } } else { if (source.handedness == InteractionSourceHandedness.Left) { controllerModelGameObject = leftControllerModelGameObject; } else if (source.handedness == InteractionSourceHandedness.Right) { controllerModelGameObject = rightControllerModelGameObject; } } #else // 더미 컨트롤러를 만들었기때문에 여기에는 진입하지 않는다 Debug.Assert(false); yield break; #endif } /* * 사용 가능한 mesh renderer 목록 * 8개 * * New Game Object/GLTFScene/GLTFNode/GLTFNode/GLTFNode/GLTFNode/GLTFNode/GLTFNode/GLTFMesh/Primitive * New Game Object/GLTFScene/GLTFNode/GLTFNode/HOME/VALUE/GLTFNode/GLTFMesh/Primitive * New Game Object/GLTFScene/GLTFNode/GLTFNode/MENU/VALUE/GLTFNode/GLTFMesh/Primitive * New Game Object/GLTFScene/GLTFNode/GLTFNode/GRASP/VALUE/GLTFNode/GLTFMesh/Primitive * New Game Object/GLTFScene/GLTFNode/GLTFNode/THUMBSTICK_PRESS/VALUE/THUMBSTICK_X/VALUE/THUMBSTICK_Y/VALUE/GLTFNode/GLTFMesh/Primitive * New Game Object/GLTFScene/GLTFNode/GLTFNode/SELECT/VALUE/GLTFNode/GLTFMesh/Primitive * New Game Object/GLTFScene/GLTFNode/GLTFNode/TOUCHPAD_PRESS/VALUE/TOUCHPAD_PRESS_X/VALUE/TOUCHPAD_PRESS_Y/VALUE/TOUCHPAD_TOUCH_X/GLTFNode/GLTFMesh/Primitive * New Game Object/GLTFScene/GLTFNode/GLTFNode/GLTFNode/GLTFMesh/Primitive * * var renderers = controllerModelGameObject.GetComponentsInChildren<Renderer>(); * Debug.Log(renderers.Length); * for (var i = 0 ; i < renderers.Length; i++) { * var r = renderers[i]; * var n = SDK_WindowsMRController.GetElemPath(r.gameObject, null); * Debug.LogFormat("{0} {1} {2}", i, n, r.GetType().ToString()); * } */ var info = FinishControllerSetup(rootGameObject, parentGameObject, controllerModelGameObject, source.handedness.ToString(), source.id); if (source.handedness == InteractionSourceHandedness.Left) { info.PointingTransform = leftPointer; _cachedLeftMotionController = info; _cachedLeftMotionControllerID = source.id; _cachedLeftMotionControllerSource = source; } else if (source.handedness == InteractionSourceHandedness.Right) { info.PointingTransform = rightPointer; _cachedRightMotionController = info; _cachedRightMotionControllerID = source.id; _cachedRightMotionControllerSource = source; } yield break; }