/// <summary> /// 计算适口偏移值 /// </summary> /// <returns>The offset position.</returns> /// <param name="handPosition">Hand position.</param> /// <param name="grabObject">Grab object.</param> public static Vector3 GetOffsetPosition(Vector3 handPosition, GameObject grabObject) { var offset = Vector3.zero; Vector3 screenDevice = MUtility.MainWorldToScreenPoint(grabObject.transform.position); Vector3 vPos = MUtility.MainScreenToWorldPoint(new Vector3(handPosition.x, handPosition.y, screenDevice.z)); offset = vPos - grabObject.transform.position; return(offset); }
public static void SetObjectGrab(GameObject target, int handIndex = 0) { var operate = GetOperateHand(handIndex); if (operate == null) { return; } Vector3 tempPos = MUtility.MainWorldToScreenPoint(target.transform.position - new Vector3(0, 0, MUtility.MainCamera.transform.position.z)); tempPos = MUtility.MainScreenToWorldPoint(tempPos); operate.SetObjectGrab(target, tempPos.z); }
/// <summary> /// 返回是否在屏幕内 /// </summary> /// <param name="thingAttach"></param> /// <returns></returns> public static bool AttachThingPosInCamera(Transform thingAttach, Vector2 xlimits, Vector2 ylimits) { Transform camTransform = MUtility.MainCamera.transform; Vector3 dir = (thingAttach.position - camTransform.position).normalized; float dot = Vector3.Dot(camTransform.forward, dir); //判断物体是否在相机前面 Vector2 screenPos = MUtility.MainWorldToScreenPoint(thingAttach.position); if (screenPos.x < xlimits.y && screenPos.x > xlimits.x && screenPos.y < ylimits.y && screenPos.y > ylimits.x && dot > 0) { return(true); } else { return(false); } }
/// <summary> /// 是否存在区域内 /// </summary> /// <param name="transform">指定物体</param> /// <param name="handIndex"></param> /// <returns></returns> public static bool IsAreaContains(Transform transform, int handIndex) { //if (!KinectTransfer.IsHandActive(handIndex)) return false; try { //获取到此时手的屏幕坐标屏幕坐标 Vector3 screenHandPoint = MOperateManager.GetHandScreenPoint(handIndex); Vector3 screenPoint = MUtility.UIWorldToScreenPoint(transform.position); //根据自身此时的屏幕坐标,去算区域 RectTransform rectTransform = transform.GetComponent <RectTransform>(); return(ScreenPointContains(screenPoint, rectTransform.sizeDelta, screenHandPoint)); } catch (Exception) { return(false); //throw new Exception("手势可能没激活,如果是在编辑器上遇到此问题,不用理会"); } }