/// <summary> /// 针对OperateObject的处理 /// </summary> public void OnOperateObjectHandle() { if (OperateObject == null) { return; } switch (Operate.InputHand.HandStatus) { case MInputHandStatus.Grabing: var screenDevice = MUtility.MainWorldToScreenPoint(OperateObject.GrabObject.transform.position); var screenMouse = Operate.InputHand.ScreenPoint; Vector3 vpos = MUtility.MainScreenToWorldPoint(new Vector3(screenMouse.x, screenMouse.y, screenDevice.z)); Vector3 position = vpos - Offset; EventUpdateObject.SendListener(OperateObject.GrabObject, position, OperateObject.GrabObject.transform.rotation, HandIndex); break; case MInputHandStatus.Idle: OperateObject = null; break; } }
/// <summary> /// 计算适口偏移值 /// </summary> /// <param name="mousePosition"></param> /// <param name="grabObject"></param> /// <returns></returns> private Vector3 GetOffsetPosition(Vector3 mousePosition, GameObject grabObject) { var offset = Vector3.zero; Vector3 screenDevice = MUtility.MainWorldToScreenPoint(grabObject.transform.position); Vector3 vPos = MUtility.MainScreenToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, screenDevice.z)); offset = vPos - grabObject.transform.position; return(offset); }
/// <summary> /// 打开 /// </summary> public virtual void OnOpen(int handIndex = 0) { this.handIndex = handIndex; Vector3 screenHand = MOperateManager.GetHandScreenPoint(handIndex); Vector3 screenPosition = MUtility.MainWorldToScreenPoint(GrabObject.transform.position); recordPos = MUtility.MainScreenToWorldPoint(new Vector3(screenHand.x, screenHand.y, screenPosition.z));//手的坐标 recordEuler = space == Space.World ? GrabObject.transform.eulerAngles : GrabObject.transform.localEulerAngles; isActive = true; }
// 单手操作处理 void KinectHandOne() { if (MInputKinect.IsHandActive(0)) { handIndex = 0; } else if (MInputKinect.IsHandActive(1)) { handIndex = 1; } //将他的屏幕坐标传递出去 InputHands[0].OnUpdate(MInputKinect.ScreenHandPostion(handIndex)); if (MInputKinect.HandGrip(handIndex)) { InputHands[0].SetGrip(); } if (MInputKinect.HandRelease(handIndex)) { InputHands[0].SetIdle(); } if (rightOperateObject != null) { switch (InputHands[0].HandStatus) { case MInputHandStatus.Grabing: var screenDevice = MUtility.MainWorldToScreenPoint(rightOperateObject.GrabObject.transform.position); Vector3 screenMouse = InputHands[0].ScreenPoint; Vector3 vPos = MUtility.MainScreenToWorldPoint(new Vector3(screenMouse.x, screenMouse.y, screenDevice.z)); rightOperateObject.GrabObject.transform.position = vPos - rightOffset; //需要处理偏移量 break; case MInputHandStatus.Idle: this.rightOperateObject = null; break; default: break; } } }
IEnumerator OnUpdate(int handIndex) { while (true) { yield return(new WaitForFixedUpdate()); Vector3 screenHand = MOperateManager.GetHandScreenPoint(handIndex); Vector3 screenPosition = MUtility.MainWorldToScreenPoint(GrabObject.transform.position); Vector3 position = MUtility.MainScreenToWorldPoint(new Vector3(screenHand.x, screenHand.y, screenPosition.z)); if (OnCustomizeUpdate != null) { OnCustomizeUpdate.Invoke(GrabObject, position, handIndex); } } }
/// <summary> /// 设置物体被抓取 /// </summary> /// <param name="operate">Operate.</param> /// <param name="handIndex">Hand index.</param> public void SetGrabObject(IOperateObject operate, int handIndex, float cameraRelativeDistance) { if (HandIndex != handIndex) { return; } Vector3 screenPoint = Operate.InputHand.ScreenPoint; OperateObject = operate; Vector3 screenMainCamera = MUtility.MainWorldToScreenPoint(MUtility.MainCamera.transform.position + MUtility.MainCamera.transform.forward * cameraRelativeDistance); Vector3 position = MUtility.MainScreenToWorldPoint(new Vector3(screenPoint.x, screenPoint.y, screenMainCamera.z)); Offset = Vector3.zero; OperateObject.GrabObject.transform.position = position; }
/// <summary> /// 设置物体被抓取 /// </summary> /// <param name="operate"></param> /// <param name="handIndex"></param> /// <param name="cameraRelativeDistance"></param> void SetGrabObject(IOperateObject operate, int handIndex, float cameraRelativeDistance) { if (handIndex != InputHands[0].HandIndex) { return; } //Vector3 screenDevice = MUtility.MainWorldToScreenPoint(operate.GrabObject.transform.position); Vector3 screenpoint = InputHands[0].ScreenPoint; rightOperateObject = operate; Vector3 screenMainCamera = MUtility.MainWorldToScreenPoint(MUtility.MainCamera.transform.position + MUtility.MainCamera.transform.forward * cameraRelativeDistance); Vector3 position = MUtility.MainScreenToWorldPoint(new Vector3(screenpoint.x, screenpoint.y, screenMainCamera.z)); rightOperateObject.GrabObject.transform.position = position; }
public virtual void OnUpdate() { if (!isActive) { return; } Vector3 screenHand = MOperateManager.GetHandScreenPoint(handIndex); Vector3 screenPosition = MUtility.MainWorldToScreenPoint(recordPos); Vector3 handPos = MUtility.MainScreenToWorldPoint(new Vector3(screenHand.x, screenHand.y, screenPosition.z)); //手的坐标 float dis = handPos.x - recordPos.x; //手移动的距离 float ratio = dis * speed; float angle = 0; Vector3 euler = recordEuler; angle = GetAngle(ratio, angle); euler = GetEuler(angle, euler); RotateSelf(euler); }
/// <summary> /// 鼠标控制端的妹帧执行 /// </summary> void OnMouseUpdate() { if (!IsEnable) { return; } //将他的屏幕坐标传递出去 InputHands[0].OnUpdate(Input.mousePosition); #region 标/触摸检测 //触摸 if (!IsMousing && Input.touchCount >= 1) { InputHands[0].SetGrip(); IsTouching = true; observedMode.OnDown(); } if (Input.GetMouseButtonDown(0)) { //鼠标 if (!IsTouching && InputHands[0].IsIdleStatus) { InputHands[0].SetGrip(); IsMousing = true; observedMode.OnDown(); } } if (IsTouching && Input.touchCount == 0) { InputHands[0].SetIdle(); IsTouching = false; ObservedModeUpHandler(); } if (Input.GetMouseButtonUp(0)) { if (IsMousing && !InputHands[0].IsErrorStatus) { InputHands[0].SetIdle(); IsMousing = false; ObservedModeUpHandler(); } } #endregion //不同模式中的不同操作 switch (MSwitchManager.CurrentMode) { case OperateModeType.Rotate: observedMode.OnAchieve(InputHands[0], true); break; case OperateModeType.Zoom: //observedMode.OnAchieve(InputHands[0], false); observedMode.OnZoom(); break; case OperateModeType.Tool: break; default: break; } if (operateObject != null) { switch (InputHands[0].HandStatus) { case MInputHandStatus.Grabing: //需要处理偏移量 var screenDevice = MUtility.MainWorldToScreenPoint(operateObject.GrabObject.transform.position); Vector3 screenMouse = InputHands[0].ScreenPoint; Vector3 vPos = MUtility.MainScreenToWorldPoint(new Vector3(screenMouse.x, screenMouse.y, screenDevice.z)); Vector3 position = vPos - offset; EventUpdateObject.SendListener(operateObject.GrabObject, position, operateObject.GrabObject.transform.rotation, InputHands[0].HandIndex); break; case MInputHandStatus.Idle: this.operateObject = null; break; default: break; } } }
/// <summary> /// 鼠标控制端的妹帧执行 /// </summary> void OnMouseUpdate() { if (!IsEnable) { return; } //将他的屏幕坐标传递出去 InputHands[0].OnUpdate(Input.mousePosition); if (Input.GetMouseButtonDown(0) && InputHands[0].IsIdleStatus) { InputHands[0].SetGrip(); } if (Input.GetMouseButtonUp(0) && !(InputHands[0].IsRotateZoomStatus || InputHands[0].IsErrorStatus)) { InputHands[0].SetIdle(); } #region 旋转 //如果按下右键 if (Input.GetMouseButtonDown(1)) { IsRotateDown = true; IsRotate = false; } if (Input.GetMouseButtonUp(1)) { IsRotateDown = false; //已经存在旋转,并且在集合中记录 if (IsRotate) { EventCameraRotate.SendListener(Vector3.zero); } IsRotate = false; InputHands[0].HandStatus = MInputHandStatus.Idle; } //按住右键旋转 if (IsRotateDown) { //向量的模大于2.0时 if (!IsRotate && InputHands[0].IsIdleStatus && InputHands[0].ScreenVector.magnitude > 2.0f) { //将动作记录到集合中 InputHands[0].HandStatus = MInputHandStatus.Rotate; IsRotate = true; } //已经存在旋转,并且在集合中记录 if (IsRotate) { EventCameraRotate.SendListener(InputHands[0].ScreenVector); } } #endregion #region 缩放 //缩放 if (Input.GetAxis("Mouse ScrollWheel") != 0) { float result = Input.GetAxis("Mouse ScrollWheel"); if (!IsZoom && InputHands[0].IsIdleStatus) { InputHands[0].HandStatus = MInputHandStatus.Zoom; IsZoom = true; } //进行缩放 if (IsZoom) { EventCameraZoom.SendListener(result); } } else { //进行缩放 if (IsZoom) { EventCameraZoom.SendListener(0); IsZoom = false; InputHands[0].HandStatus = MInputHandStatus.Idle; } } #endregion if (operateObject != null) { switch (InputHands[0].HandStatus) { case MInputHandStatus.Grabing: //需要处理偏移量 var screenDevice = MUtility.MainWorldToScreenPoint(operateObject.GrabObject.transform.position); Vector3 screenMouse = InputHands[0].ScreenPoint; Vector3 vPos = MUtility.MainScreenToWorldPoint(new Vector3(screenMouse.x, screenMouse.y, screenDevice.z)); Vector3 position = vPos - offset; EventUpdateObject.SendListener(operateObject.GrabObject, position, operateObject.GrabObject.transform.rotation, InputHands[0].HandIndex); break; case MInputHandStatus.Idle: this.operateObject = null; break; default: break; } } }
/// <summary> /// 鼠标控制端的妹帧执行 /// </summary> void OnMouseUpdate() { //将他的屏幕坐标传递出去 InputHands[0].OnUpdate(Input.mousePosition); if (Input.GetMouseButtonDown(0)) { InputHands[0].SetGrip(); } if (Input.GetMouseButtonUp(0)) { InputHands[0].SetIdle(); } //如果按下右键 if (Input.GetMouseButtonDown(1)) { IsRotateDown = true; IsRotate = false; } if (Input.GetMouseButtonUp(1)) { IsRotateDown = false; //已经存在旋转,并且在集合中记录 if (IsRotate && ActionConstraint.IsBind(ActionConstraint.Camera_Rotate_Action)) { EventCameraRotate.SendListener(Vector3.zero); } IsRotate = false; ActionConstraint.RemoveBind(ActionConstraint.Camera_Rotate_Action); } //按住右键旋转 if (IsRotateDown) { //向量的模大于2.0时 if (!IsRotate && ActionConstraint.BindCount == 0 && InputHands[0].ScreenVector.magnitude > 2.0f) { //将动作记录到集合中 ActionConstraint.AddBind(ActionConstraint.Camera_Rotate_Action); IsRotate = true; } //已经存在旋转,并且在集合中记录 if (IsRotate && ActionConstraint.IsBind(ActionConstraint.Camera_Rotate_Action)) { EventCameraRotate.SendListener(InputHands[0].ScreenVector); } } //缩放 if (Input.GetAxis("Mouse ScrollWheel") != 0) { float result = Input.GetAxis("Mouse ScrollWheel"); if (!IsZoom && ActionConstraint.BindCount == 0) { ActionConstraint.AddBind(ActionConstraint.Camera_Zoom_Action); IsZoom = true; } //进行缩放 if (IsZoom && ActionConstraint.IsBind(ActionConstraint.Camera_Zoom_Action)) { EventCameraZoom.SendListener(result); } } else { //进行缩放 if (IsZoom && ActionConstraint.IsBind(ActionConstraint.Camera_Zoom_Action)) { EventCameraZoom.SendListener(0); } IsZoom = false; if (ActionConstraint.BindCount > 0) { ActionConstraint.RemoveBind(ActionConstraint.Camera_Zoom_Action); } } if (operateObject != null) { switch (InputHands[0].HandStatus) { case MInputHandStatus.Grabing: //需要处理偏移量 var screenDevice = MUtility.MainWorldToScreenPoint(operateObject.GrabObject.transform.position); Vector3 screenMouse = InputHands[0].ScreenPoint; Vector3 vPos = MUtility.MainScreenToWorldPoint(new Vector3(screenMouse.x, screenMouse.y, screenDevice.z)); Vector3 position = vPos - offset; EventUpdateObject.SendListener(operateObject.GrabObject, position, operateObject.GrabObject.transform.rotation, InputHands[0].HandIndex); break; case MInputHandStatus.Idle: this.operateObject = null; break; default: break; } } }
// 双手操作处理 void KinectHandTwo() { //将他的屏幕坐标传递出去 InputHands[0].OnUpdate(MInputKinect.ScreenHandPostion(0)); if (MInputKinect.HandGrip(0)) { InputHands[0].SetGrip(); } if (MInputKinect.HandRelease(0)) { InputHands[0].SetIdle(); } //将他的屏幕坐标传递出去 InputHands[1].OnUpdate(MInputKinect.ScreenHandPostion(1)); if (MInputKinect.HandGrip(1)) { InputHands[1].SetGrip(); } if (MInputKinect.HandRelease(1)) { InputHands[1].SetIdle(); } KinectRotateZoom(); if (rightOperateObject != null) { switch (InputHands[0].HandStatus) { case MInputHandStatus.Grabing: var screenDevice = MUtility.MainWorldToScreenPoint(rightOperateObject.GrabObject.transform.position); Vector3 screenMouse = InputHands[0].ScreenPoint; Vector3 vPos = MUtility.MainScreenToWorldPoint(new Vector3(screenMouse.x, screenMouse.y, screenDevice.z)); //rightOperateObject.GrabObject.transform.position = vPos - rightOffset; Vector3 position = vPos - rightOffset; EventUpdateObject.SendListener(rightOperateObject.GrabObject, position, rightOperateObject.GrabObject.transform.rotation, InputHands[0].HandIndex); //需要处理偏移量 break; case MInputHandStatus.Idle: this.rightOperateObject = null; break; default: break; } } if (leftOperateObject != null) { switch (InputHands[1].HandStatus) { case MInputHandStatus.Grabing: var screenDevice = MUtility.MainWorldToScreenPoint(leftOperateObject.GrabObject.transform.position); Vector3 screenMouse = InputHands[1].ScreenPoint; Vector3 vPos = MUtility.MainScreenToWorldPoint(new Vector3(screenMouse.x, screenMouse.y, screenDevice.z)); //leftOperateObject.GrabObject.transform.position = vPos - leftOffset; Vector3 position = vPos - leftOffset; EventUpdateObject.SendListener(leftOperateObject.GrabObject, position, leftOperateObject.GrabObject.transform.rotation, InputHands[0].HandIndex); //需要处理偏移量 break; case MInputHandStatus.Idle: this.leftOperateObject = null; break; default: break; } } if (kinectHandStartStatus == KinectActiveHandStadus.One) { if (MInputKinect.IsHandActive(0) && !MInputKinect.IsHandActive(1) && isRight == false) { MOperateManager.GetOperateHand(0, OperatePlatform.Kinect).OnEnable(); MOperateManager.GetOperateHand(1, OperatePlatform.Kinect).OnDisable(); isRight = true; isLeft = false; } else if (MInputKinect.IsHandActive(1) && !MInputKinect.IsHandActive(0) && isLeft == false) { MOperateManager.GetOperateHand(1, OperatePlatform.Kinect).OnEnable(); MOperateManager.GetOperateHand(0, OperatePlatform.Kinect).OnDisable(); isLeft = true; isRight = false; } } }