private MyMotionControllerInfo FinishControllerSetup(GameObject rootGameObject, GameObject parentGameObject, GameObject controllerModelGameObject, string handedness, uint id) { var defaultPos = controllerModelGameObject.transform.localPosition; var defaultRot = controllerModelGameObject.transform.localRotation; //parentGameObject.transform.parent = transform; controllerModelGameObject.transform.parent = parentGameObject.transform; controllerModelGameObject.transform.localPosition = defaultPos; controllerModelGameObject.transform.localRotation = defaultRot; var newControllerInfo = new MyMotionControllerInfo() { ControllerParent = rootGameObject, ModelParent = parentGameObject, ControllerModelGameObject = controllerModelGameObject, }; if (AnimateControllerModel) { newControllerInfo.LoadInfo(controllerModelGameObject.GetComponentsInChildren <Transform>(), this); } controllerDictionary.Add(id, newControllerInfo); return(newControllerInfo); }
/// <summary> /// When a controller is lost, the model is destroyed and the controller object /// is removed from the tracking dictionary. /// </summary> /// <param name="obj">The source event args to be used to determine the controller model to be removed.</param> private void InteractionManager_InteractionSourceLost(InteractionSourceLostEventArgs obj) { InteractionSource source = obj.state.source; if (source.kind == InteractionSourceKind.Controller) { MyMotionControllerInfo controller; if (controllerDictionary != null && controllerDictionary.TryGetValue(source.id, out controller)) { controllerDictionary.Remove(source.id); // 컨트롤러 객체 재사용하고싶으니 파괴하진 않는다 controller.ControllerParent.SetActive(false); // reset cache if (source.handedness == InteractionSourceHandedness.Left) { _cachedLeftMotionController = null; _cachedLeftMotionControllerID = uint.MaxValue; _cachedLeftMotionControllerSource = new InteractionSource(); } else if (source.handedness == InteractionSourceHandedness.Right) { _cachedRightMotionController = null; _cachedRightMotionControllerID = uint.MaxValue; _cachedRightMotionControllerSource = new InteractionSource(); } } } }
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; }