IsLeftHandPrimary() public method

public IsLeftHandPrimary ( ) : bool
return bool
    public override bool ShouldActivateModule()
    {
        if (!base.ShouldActivateModule())
        {
            return(false);
        }

        if (intManager == null)
        {
            intManager = GetInteractionManager();
        }

        //bool shouldActivate |= (InteractionManager.Instance != null && InteractionManager.Instance.IsInteractionInited());
        bool shouldActivate = m_ForceModuleActive || (m_framePressState != PointerEventData.FramePressState.NotChanged);

        if (!shouldActivate && processCursorMovement && intManager &&
            (intManager.IsLeftHandPrimary() || intManager.IsRightHandPrimary()))
        {
            bool bIsLeftHand = intManager.IsLeftHandPrimary();

            // check for cursor pos change
            Vector2 handCursorPos = bIsLeftHand ? intManager.GetLeftHandScreenPos() : intManager.GetRightHandScreenPos();

            if (handCursorPos != m_lastCursorPos)
            {
                m_lastCursorPos = handCursorPos;
                shouldActivate  = true;
            }
        }

        return(shouldActivate);
    }
    void Update()
    {
        if (interactionManager != null && interactionManager.IsInteractionInited())
        {
            Vector3 screenPixelPos = Vector3.zero;

            if (selectedObject == null)
            {
                // no object is currently selected or dragged.
                // check if there is an underlying object to be selected
                if (lastHandEvent == InteractionManager.HandEventType.Grip && screenNormalPos != Vector3.zero)
                {
                    // convert the normalized screen pos to pixel pos
                    screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();

                    screenPixelPos.x = (int)(screenNormalPos.x * (screenCamera ? screenCamera.pixelWidth : Screen.width));
                    screenPixelPos.y = (int)(screenNormalPos.y * (screenCamera ? screenCamera.pixelHeight : Screen.height));
                    Ray ray = screenCamera ? screenCamera.ScreenPointToRay(screenPixelPos) : new Ray();

                    // check for underlying objects
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit))
                    {
                        if (hit.collider.gameObject == gameObject)
                        {
                            selectedObject = gameObject;

                            savedObjectMaterial = selectedObject.GetComponent <Renderer>().material;
                            selectedObject.GetComponent <Renderer>().material = selectedObjectMaterial;
                        }
                    }
                }
            }
            else
            {
                // continue dragging the object
                screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();

                float angleArounfY = screenNormalPos.x * 360f;                  // horizontal rotation
                float angleArounfX = screenNormalPos.y * 360f;                  // vertical rotation

                Vector3    vObjectRotation = new Vector3(-angleArounfX, -angleArounfY, 180f);
                Quaternion qObjectRotation = screenCamera ? screenCamera.transform.rotation * Quaternion.Euler(vObjectRotation) : Quaternion.Euler(vObjectRotation);

                selectedObject.transform.rotation = Quaternion.Slerp(selectedObject.transform.rotation, qObjectRotation, smoothFactor * Time.deltaTime);

                // check if the object (hand grip) was released
                bool isReleased = lastHandEvent == InteractionManager.HandEventType.Release;

                if (isReleased)
                {
                    // restore the object's material and stop dragging the object
                    selectedObject.GetComponent <Renderer>().material = savedObjectMaterial;
                    selectedObject = null;
                }
            }
        }
    }
Beispiel #3
0
    int HoverIndex()
    {
        if (screenCamera == null)
        {
            screenCamera = Camera.main;
        }
        if (interactionManager == null)
        {
            interactionManager = InteractionManager.Instance;
        }

        screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();

        screenPixelPos.x = (int)(screenNormalPos.x * (screenCamera ? screenCamera.pixelWidth : Screen.width));
        screenPixelPos.y = (int)(screenNormalPos.y * (screenCamera ? screenCamera.pixelHeight : Screen.height));

        Debug.Log(screenPixelPos);

        for (int i = 0; i < buttons.Count; i++)
        {
            Button btn = buttons[i];
            if (btn.gameObject.activeInHierarchy && RectTransformUtility.RectangleContainsScreenPoint(btn.image.rectTransform, screenPixelPos, null))
            {
                return(i);
            }
        }
        return(-1);
    }
Beispiel #4
0
    void Update()
    {
        if (interactionManager != null && interactionManager.IsInteractionInited())
        {
            lastScreenPixelPos = screenPixelPos;

            // convert the normalized screen pos to pixel pos
            screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();

            screenPixelPos.x = (int)(screenNormalPos.x * (screenCamera ? screenCamera.pixelWidth : Screen.width));
            screenPixelPos.y = (int)(screenNormalPos.y * (screenCamera ? screenCamera.pixelHeight : Screen.height));

            //print(lastHandEvent);

            if (lastHandEvent != InteractionManager.HandEventType.Grip && nowHandEvent == InteractionManager.HandEventType.Grip)
            {
                rotation.x = (screenPixelPos.x - lastScreenPixelPos.x) * 0.0005f;
                rotation.y = (screenPixelPos.y - lastScreenPixelPos.y) * 0.000001f;

                print(rotation);
                if (rotation.x != 0)
                {
                    if (GameObject.FindGameObjectWithTag("model") != null)
                    {
                        GameObject.FindGameObjectWithTag("model").GetComponent <ModelViewer>().BeginDrag();
                        GameObject.FindGameObjectWithTag("model").GetComponent <ModelViewer>().Rotate(rotation);
                    }
                }
            }
        }
    }
    public override void ActivateModule()
    {
        base.ActivateModule();

        m_IntManager     = InteractionManager.Instance;
        m_isLeftHandDrag = m_IntManager.IsLeftHandPrimary();

        var toSelect = eventSystem.currentSelectedGameObject;

        if (toSelect == null)
        {
            toSelect = eventSystem.firstSelectedGameObject;
        }

        eventSystem.SetSelectedGameObject(toSelect, GetBaseEventData());
    }
    private void CheckGrippedCursorPosition()
    {
        if (intManager)
        {
            bool bIsLeftHand = intManager.IsLeftHandPrimary();

            // check for gripped hand
            bool bHandGrip = bIsLeftHand ? m_leftHandGrip : m_rightHandGrip;

            // check for cursor pos change
            Vector2 cursorNormalPos = bIsLeftHand ? intManager.GetLeftHandScreenPos() : intManager.GetRightHandScreenPos();

            if (bHandGrip && cursorNormalPos != (Vector2)m_screenNormalPos)
            {
                // emulate new press
                m_framePressState = PointerEventData.FramePressState.Pressed;
                m_screenNormalPos = cursorNormalPos;
            }
        }
    }
Beispiel #7
0
    void Update()
    {
        // get the interaction manager instance
        if (manager == null)
        {
            manager = InteractionManager.Instance;
        }

        if (manager != null && manager.IsInteractionInited())
        {
            Vector3 screenNormalPos = Vector3.zero;
            Vector3 screenPixelPos  = Vector3.zero;

            if (draggedObject == null)
            {
                // if there is a hand grip, select the underlying object and start dragging it.
                if (manager.IsLeftHandPrimary())
                {
                    // if the left hand is primary, check for left hand grip
                    if (manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Grip)
                    {
                        isLeftHandDrag  = true;
                        screenNormalPos = manager.GetLeftHandScreenPos();
                    }
                }
                else if (manager.IsRightHandPrimary())
                {
                    // if the right hand is primary, check for right hand grip
                    if (manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Grip)
                    {
                        isLeftHandDrag  = false;
                        screenNormalPos = manager.GetRightHandScreenPos();
                    }
                }

                // check if there is an underlying object to be selected
                if (screenNormalPos != Vector3.zero)
                {
                    // convert the normalized screen pos to pixel pos
                    screenPixelPos.x = (int)(screenNormalPos.x * Camera.main.pixelWidth);
                    screenPixelPos.y = (int)(screenNormalPos.y * Camera.main.pixelHeight);

                    Vector2    point       = Camera.main.ScreenToWorldPoint(screenPixelPos);
                    Collider2D collision2d = Physics2D.OverlapPoint(point);

                    if (collision2d)
                    {
                        RaycastHit2D hit = Physics2D.Raycast(point, -Vector2.up);
                        if (hit)
                        {
                            foreach (GameObject obj in draggableObjects)
                            {
                                if (hit.collider.gameObject == obj)
                                {
                                    // an object was hit by the ray. select it and start drgging
                                    draggedObject = obj;
                                    draggedObject.GetComponent <FruitController>().OnGrabbedKinect();
                                    Vector2 draggedObjectPos = new Vector2(draggedObject.transform.position.x, draggedObject.transform.position.y);
                                    draggedObjectOffset = hit.point - draggedObjectPos;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                // continue dragging the object
                screenNormalPos = isLeftHandDrag ? manager.GetLeftHandScreenPos() : manager.GetRightHandScreenPos();

                // convert the normalized screen pos to 3D-world pos
                screenPixelPos.x = (int)(screenNormalPos.x * Camera.main.pixelWidth);
                screenPixelPos.y = (int)(screenNormalPos.y * Camera.main.pixelHeight);

                if (screenPixelPos.y < Camera.main.pixelHeight / 4)
                {
                    screenPixelPos.y = Camera.main.pixelHeight / 4;
                }

                Vector2 newObjectPos = Camera.main.ScreenToWorldPoint(screenPixelPos) - draggedObjectOffset;
                draggedObject.transform.position = Vector3.Lerp(draggedObject.transform.position, newObjectPos, dragSpeed * Time.deltaTime);

                // check if the object (hand grip) was released
                bool isReleased = isLeftHandDrag ? (manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Release) :
                                  (manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Release);

                if (isReleased)
                {
                    draggedObject.GetComponent <FruitController>().OnReleasedKinect();
                    draggedObject = null;
                }
            }
        }
    }
    void Update()
    {
        if (resetObjects && draggedObject == null)
        {
            // reset the objects as needed
            resetObjects = false;
            ResetObjects();
        }

        // get the interaction manager instance
        if (manager == null)
        {
            manager = InteractionManager.Instance;
        }

        if (manager != null && manager.IsInteractionInited())
        {
            Vector3 screenNormalPos = Vector3.zero;
            Vector3 screenPixelPos  = Vector3.zero;

            if (draggedObject == null)
            {
                // if there is a hand grip, select the underlying object and start dragging it.
                if (manager.IsLeftHandPrimary())
                {
                    // if the left hand is primary, check for left hand grip
                    if (manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Grip)
                    {
                        isLeftHandDrag  = true;
                        screenNormalPos = manager.GetLeftHandScreenPos();
                    }
                }
                else if (manager.IsRightHandPrimary())
                {
                    // if the right hand is primary, check for right hand grip
                    if (manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Grip)
                    {
                        isLeftHandDrag  = false;
                        screenNormalPos = manager.GetRightHandScreenPos();
                    }
                }

                // check if there is an underlying object to be selected
                if (screenNormalPos != Vector3.zero)
                {
                    // convert the normalized screen pos to pixel pos
                    screenPixelPos.x = (int)(screenNormalPos.x * Camera.main.pixelWidth);
                    screenPixelPos.y = (int)(screenNormalPos.y * Camera.main.pixelHeight);
                    Ray ray = Camera.main.ScreenPointToRay(screenPixelPos);

                    // check if there is an underlying objects
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit))
                    {
                        foreach (GameObject obj in draggableObjects)
                        {
                            if (hit.collider.gameObject == obj)
                            {
                                // an object was hit by the ray. select it and start drgging
                                draggedObject       = obj;
                                draggedObjectDepth  = draggedObject.transform.position.z - Camera.main.transform.position.z;
                                draggedObjectOffset = hit.point - draggedObject.transform.position;

                                // set selection material
                                draggedObjectMaterial = draggedObject.GetComponent <Renderer>().material;
                                draggedObject.GetComponent <Renderer>().material = selectedObjectMaterial;

                                // stop using gravity while dragging object
                                draggedObject.GetComponent <Rigidbody>().useGravity = false;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                // continue dragging the object
                screenNormalPos = isLeftHandDrag ? manager.GetLeftHandScreenPos() : manager.GetRightHandScreenPos();

                // convert the normalized screen pos to 3D-world pos
                screenPixelPos.x = (int)(screenNormalPos.x * Camera.main.pixelWidth);
                screenPixelPos.y = (int)(screenNormalPos.y * Camera.main.pixelHeight);
                screenPixelPos.z = screenNormalPos.z + draggedObjectDepth;

                Vector3 newObjectPos = Camera.main.ScreenToWorldPoint(screenPixelPos) - draggedObjectOffset;
                draggedObject.transform.position = Vector3.Lerp(draggedObject.transform.position, newObjectPos, dragSpeed * Time.deltaTime);

                // check if the object (hand grip) was released
                bool isReleased = isLeftHandDrag ? (manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Release) :
                                  (manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Release);

                if (isReleased)
                {
                    // restore the object's material and stop dragging the object
                    draggedObject.GetComponent <Renderer>().material = draggedObjectMaterial;

                    if (useGravity)
                    {
                        // add gravity to the object
                        draggedObject.GetComponent <Rigidbody>().useGravity = true;
                    }

                    draggedObject = null;
                }
            }
        }
    }
Beispiel #9
0
    void Update()
    {
        if (interactionManager != null && interactionManager.IsInteractionInited())
        {
            if (resetObjects && draggedObject == null)
            {
                // reset the objects as needed
                resetObjects = false;
                ResetObjects();
            }

            if (draggedObject == null)
            {
                // no object is currently selected or dragged.
                bool bHandIntAllowed = (leftHandInteraction && interactionManager.IsLeftHandPrimary()) || (rightHandInteraction && interactionManager.IsRightHandPrimary());

                // check if there is an underlying object to be selected
                if (lastHandEvent == InteractionManager.HandEventType.Grip && bHandIntAllowed)
                {
                    // convert the normalized screen pos to pixel pos
                    screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();

                    screenPixelPos.x = (int)(screenNormalPos.x * (screenCamera ? screenCamera.pixelWidth : Screen.width));
                    screenPixelPos.y = (int)(screenNormalPos.y * (screenCamera ? screenCamera.pixelHeight : Screen.height));
                    Ray ray = screenCamera ? screenCamera.ScreenPointToRay(screenPixelPos) : new Ray();

                    // check if there is an underlying objects
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit))
                    {
                        foreach (GameObject obj in draggableObjects)
                        {
                            if (hit.collider.gameObject == obj)
                            {
                                // an object was hit by the ray. select it and start drgging
                                draggedObject         = obj;
                                draggedObjectOffset   = hit.point - draggedObject.transform.position;
                                draggedObjectOffset.z = 0;                                 // don't change z-pos

                                draggedNormalZ = (minZ + screenNormalPos.z * (maxZ - minZ)) -
                                                 draggedObject.transform.position.z;                        // start from the initial hand-z

                                // set selection material
                                draggedObjectMaterial = draggedObject.GetComponent <Renderer>().material;
                                draggedObject.GetComponent <Renderer>().material = selectedObjectMaterial;

                                // stop using gravity while dragging object
                                draggedObject.GetComponent <Rigidbody>().useGravity = false;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                bool bHandIntAllowed = (leftHandInteraction && interactionManager.IsLeftHandPrimary()) || (rightHandInteraction && interactionManager.IsRightHandPrimary());

                Debug.Log("hover");

                if (bHandIntAllowed)
                {
                    // continue dragging the object
                    screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();

                    // convert the normalized screen pos to 3D-world pos
                    screenPixelPos.x = (int)(screenNormalPos.x * (screenCamera ? screenCamera.pixelWidth : Screen.width));
                    screenPixelPos.y = (int)(screenNormalPos.y * (screenCamera ? screenCamera.pixelHeight : Screen.height));
                    //screenPixelPos.z = screenNormalPos.z + draggedObjectDepth;
                    screenPixelPos.z = (minZ + screenNormalPos.z * (maxZ - minZ)) - draggedNormalZ -
                                       (screenCamera ? screenCamera.transform.position.z : 0f);

                    newObjectPos = screenCamera.ScreenToWorldPoint(screenPixelPos) - draggedObjectOffset;
                    draggedObject.transform.position = Vector3.Lerp(draggedObject.transform.position, newObjectPos, dragSpeed * Time.deltaTime);

                    // check if the object (hand grip) was released
                    bool isReleased = lastHandEvent == InteractionManager.HandEventType.Release;

                    if (isReleased)
                    {
                        Debug.Log("dilepas");
                        // restore the object's material and stop dragging the object
                        draggedObject.GetComponent <Renderer>().material = draggedObjectMaterial;

                        if (useGravity)
                        {
                            // add gravity to the object
                            draggedObject.GetComponent <Rigidbody>().useGravity = true;
                        }

                        draggedObject = null;
                    }
                }
            }
        }
    }
Beispiel #10
0
    void Update()
    {
        manager = InteractionManager.Instance;

        if (manager != null && manager.IsInteractionInited())
        {
            Vector3 screenNormalPos = Vector3.zero;
            Vector3 screenPixelPos  = Vector3.zero;

            if (selectedObject == null)
            {
                // no object is currently selected or dragged.
                // if there is a hand grip, try to select the underlying object and start dragging it.
                if (manager.IsLeftHandPrimary())
                {
                    // if the left hand is primary, check for left hand grip
                    if (manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Grip)
                    {
                        isLeftHandDrag  = true;
                        screenNormalPos = manager.GetLeftHandScreenPos();
                    }
                }
                else if (manager.IsRightHandPrimary())
                {
                    // if the right hand is primary, check for right hand grip
                    if (manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Grip)
                    {
                        isLeftHandDrag  = false;
                        screenNormalPos = manager.GetRightHandScreenPos();
                    }
                }

                // check if there is an underlying object to be selected
                if (screenNormalPos != Vector3.zero)
                {
                    // convert the normalized screen pos to pixel pos
                    screenPixelPos.x = (int)(screenNormalPos.x * Camera.main.pixelWidth);
                    screenPixelPos.y = (int)(screenNormalPos.y * Camera.main.pixelHeight);
                    Ray ray = Camera.main.ScreenPointToRay(screenPixelPos);

                    // check for underlying objects
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit))
                    {
                        if (hit.collider.gameObject == gameObject)
                        {
                            selectedObject = gameObject;

                            savedObjectMaterial = selectedObject.GetComponent <Renderer>().material;
                            selectedObject.GetComponent <Renderer>().material = selectedObjectMaterial;
                        }
                    }
                }
            }
            else
            {
                // continue dragging the object
                screenNormalPos = isLeftHandDrag ? manager.GetLeftHandScreenPos() : manager.GetRightHandScreenPos();

                float   angleArounfY      = screenNormalPos.x * 360f;           // horizontal rotation
                float   angleArounfX      = screenNormalPos.y * 360f;           // vertical rotation
                Vector3 newObjectRotation = new Vector3(-angleArounfX, -angleArounfY, 180);

                selectedObject.transform.rotation = Quaternion.Slerp(selectedObject.transform.rotation, Quaternion.Euler(newObjectRotation), smoothFactor * Time.deltaTime);

                // check if the object (hand grip) was released
                bool isReleased = isLeftHandDrag ? (manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Release) :
                                  (manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Release);

                if (isReleased)
                {
                    // restore the object's material and stop dragging the object
                    selectedObject.GetComponent <Renderer>().material = savedObjectMaterial;
                    selectedObject = null;
                }
            }
        }
    }
Beispiel #11
0
    void Update()
    {
        // by default set the main-camera to be screen-camera
        if (screenCamera == null)
        {
            screenCamera = Camera.main;
        }
        // get the interaction manager instance
        if (interactionManager == null)
        {
            interactionManager = InteractionManager.Instance;
        }
        // get all buttons in scene
        btns = GameObject.FindObjectsOfType <Button>();

        if (interactionManager != null && interactionManager.IsInteractionInited())
        {
            // convert the normalized screen pos to pixel pos
            screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();

            screenPixelPos.x = (int)(screenNormalPos.x * (screenCamera ? screenCamera.pixelWidth : Screen.width));
            screenPixelPos.y = (int)(screenNormalPos.y * (screenCamera ? screenCamera.pixelHeight : Screen.height));

            if (lastOnButton == null)
            {
                foreach (Button btn in btns)
                {
                    if (RectTransformUtility.RectangleContainsScreenPoint(btn.image.rectTransform, screenPixelPos, null))
                    {
                        lastOnButton    = btn;
                        btn.image.color = btn_focused;
                        break;
                    }
                }
            }
            else
            {
                if (!RectTransformUtility.RectangleContainsScreenPoint(lastOnButton.image.rectTransform, screenPixelPos, null))
                {
                    lastOnButton.image.color = btn_normal;
                    lastOnButton             = null;
                    return;
                }
                if (lastHandEvent != InteractionManager.HandEventType.Grip && nowHandEvent == InteractionManager.HandEventType.Grip)
                {
                    if (lastOnButton && lastGripTime < 0)
                    {
                        lastGripTime = Time.time;
                    }
                    if (lastOnButton == null)
                    {
                        lastGripTime  = -1;
                        lastHandEvent = InteractionManager.HandEventType.Grip;
                    }
                    if (lastGripTime > 0 && Time.time - lastGripTime > gripDelay)
                    {
                        lastOnButton.image.color = btn_pressed;
                        lastOnButton.onClick.Invoke();
                        lastHandEvent = InteractionManager.HandEventType.Grip;

                        lastGripTime = -1;
                    }
                }
                else if (lastHandEvent == InteractionManager.HandEventType.Grip && nowHandEvent != InteractionManager.HandEventType.Grip)
                {
                    lastOnButton.image.color = btn_focused;
                }
            }
        }
    }
Beispiel #12
0
    // Update is called once per frame
    void Update()
    {
        //如果交互管理器不为空,并且已经初始化
        if (interactionManager != null && interactionManager.IsInteractionInited())
        {
            // check if there is an underlying object to be selected    检查是否存在要选择的基础对象
            if (lastHandEvent == InteractionManager.HandEventType.Grip && screenNormalPos != Vector3.zero)
            {
                // convert the normalized screen pos to pixel pos   将标准化屏幕POS转换为像素POS
                screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();

                screenPixelPos.x = (int)(screenNormalPos.x * (screenCamera ? screenCamera.pixelWidth : Screen.width));
                screenPixelPos.y = (int)(screenNormalPos.y * (screenCamera ? screenCamera.pixelHeight : Screen.height));
                Ray ray = screenCamera ? screenCamera.ScreenPointToRay(screenPixelPos) : new Ray();

                game.transform.position = Camera.main.ScreenToWorldPoint(screenPixelPos);
                // check if there is an underlying objects  检查是否存在底层对象
                RaycastHit hit;

                Debug.Log("握拳");
                Debug.Log(screenPixelPos);
                //udpClient.SocketSend(screenPixelPos.x + "," + screenPixelPos.y);
                //udpClient.SocketSend(screenNormalPos.x + "," + screenNormalPos.y);
                //udpClient.SocketSend(screenNormalPos.x.ToString("f2") + "," + screenNormalPos.y.ToString("f2"));//f2保留两位小数
                //udpClient.SocketSend("anXia");
                if (Physics.Raycast(ray, out hit))
                {
                    Debug.Log("位置");
                    Debug.Log(hit.point);

                    /*foreach (GameObject obj in draggableObjects)
                     * {
                     *  if (hit.collider.gameObject == obj)
                     *  {
                     *      // an object was hit by the ray. select it and start drgging
                     *      draggedObject = obj;
                     *      draggedObjectOffset = hit.point - draggedObject.transform.position;
                     *      draggedObjectOffset.z = 0; // don't change z-pos
                     *
                     *      draggedNormalZ = (minZ + screenNormalPos.z * (maxZ - minZ)) -
                     *          draggedObject.transform.position.z; // start from the initial hand-z
                     *
                     *      // set selection material
                     *      draggedObjectMaterial = draggedObject.GetComponent<Renderer>().material;
                     *      draggedObject.GetComponent<Renderer>().material = selectedObjectMaterial;
                     *
                     *      // stop using gravity while dragging object
                     *      draggedObject.GetComponent<Rigidbody>().useGravity = false;
                     *      break;
                     *  }
                     * }*/
                }
            }
            else
            {
                // continue dragging the object 继续拖动对象
                screenNormalPos = interactionManager.IsLeftHandPrimary() ? interactionManager.GetLeftHandScreenPos() : interactionManager.GetRightHandScreenPos();

                // convert the normalized screen pos to 3D-world pos    将标准化的屏幕POS转换为3D世界POS
                screenPixelPos.x = (int)(screenNormalPos.x * (screenCamera ? screenCamera.pixelWidth : Screen.width));
                screenPixelPos.y = (int)(screenNormalPos.y * (screenCamera ? screenCamera.pixelHeight : Screen.height));

                /*//screenPixelPos.z = screenNormalPos.z + draggedObjectDepth;
                 * screenPixelPos.z = (minZ + screenNormalPos.z * (maxZ - minZ)) - draggedNormalZ -
                 *  (screenCamera ? screenCamera.transform.position.z : 0f);*/


                //Debug.Log("手掌");
                //Debug.Log(screenPixelPos);
                //udpClient.SocketSend(screenPixelPos.x + "," + screenPixelPos.y);
                //udpClient.SocketSend(screenNormalPos.x + "," + screenNormalPos.y);
                //udpClient.SocketSend(screenNormalPos.x.ToString("f2") + "," + screenNormalPos.y.ToString("f2"));
                //udpClient.SocketSend("taiQi");
                //newObjectPos = screenCamera.ScreenToWorldPoint(screenPixelPos) - draggedObjectOffset;

                // check if the object (hand grip) was released

                /*bool isReleased = lastHandEvent == InteractionManager.HandEventType.Release;
                 *
                 * if (isReleased)
                 * {
                 *  // restore the object's material and stop dragging the object
                 *  draggedObject.GetComponent<Renderer>().material = draggedObjectMaterial;
                 *
                 *  if (useGravity)
                 *  {
                 *      // add gravity to the object
                 *      draggedObject.GetComponent<Rigidbody>().useGravity = true;
                 *  }
                 *
                 *  draggedObject = null;
                 * }*/
            }
        }
    }
Beispiel #13
0
    void Update()
    {
        manager = InteractionManager.Instance;

        if (manager != null && manager.IsInteractionInited())
        {
            Vector3 screenNormalPos = Vector3.zero;
            Vector3 screenPixelPos  = Vector3.zero;

            if (draggedObject == null)
            {
                // no object is currently selected or dragged.
                // if there is a hand grip, try to select the underlying object and start dragging it.
                if (manager.IsLeftHandPrimary())
                {
                    // if the left hand is primary, check for left hand grip
                    if (manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Grip)
                    {
                        isLeftHandDrag  = true;
                        screenNormalPos = manager.GetLeftHandScreenPos();
                    }
                }
                else if (manager.IsRightHandPrimary())
                {
                    // if the right hand is primary, check for right hand grip
                    if (manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Grip)
                    {
                        isLeftHandDrag  = false;
                        screenNormalPos = manager.GetRightHandScreenPos();
                    }
                }

                // check if there is an underlying object to be selected
                if (screenNormalPos != Vector3.zero)
                {
                    // convert the normalized screen pos to pixel pos
                    screenPixelPos.x = (int)(screenNormalPos.x * Camera.main.pixelWidth);
                    screenPixelPos.y = (int)(screenNormalPos.y * Camera.main.pixelHeight);
                    Ray ray = Camera.main.ScreenPointToRay(screenPixelPos);

                    // check for underlying objects
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit))
                    {
                        foreach (GameObject obj in draggableObjects)
                        {
                            if (hit.collider.gameObject == obj)
                            {
                                // an object was hit by the ray. select it and start drgging
                                draggedObject       = obj;
                                draggedObjectDepth  = draggedObject.transform.position.z - Camera.main.transform.position.z;
                                draggedObjectOffset = hit.point - draggedObject.transform.position;

                                // set selection material
                                draggedObjectMaterial           = draggedObject.renderer.material;
                                draggedObject.renderer.material = selectedObjectMaterial;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                // continue dragging the object
                screenNormalPos = isLeftHandDrag ? manager.GetLeftHandScreenPos() : manager.GetRightHandScreenPos();

//				// check if there is pull-gesture
//				bool isPulled = isLeftHandDrag ? manager.IsLeftHandPull(true) : manager.IsRightHandPull(true);
//				if(isPulled)
//				{
//					// set object depth to its original depth
//					draggedObjectDepth = -Camera.main.transform.position.z;
//				}

                // convert the normalized screen pos to 3D-world pos
                screenPixelPos.x = (int)(screenNormalPos.x * Camera.main.pixelWidth);
                screenPixelPos.y = (int)(screenNormalPos.y * Camera.main.pixelHeight);
                screenPixelPos.z = screenNormalPos.z + draggedObjectDepth;

                Vector3 newObjectPos = Camera.main.ScreenToWorldPoint(screenPixelPos) - draggedObjectOffset;
                draggedObject.transform.position = Vector3.Lerp(draggedObject.transform.position, newObjectPos, dragSpeed * Time.deltaTime);

                // check if the object (hand grip) was released
                bool isReleased = isLeftHandDrag ? (manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Release) :
                                  (manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Release);

                if (isReleased)
                {
                    // restore the object's material and stop dragging the object
                    draggedObject.renderer.material = draggedObjectMaterial;
                    draggedObject = null;
                }
            }
        }
    }
	void Update() 
	{
		manager = InteractionManager.Instance;

		if(manager != null && manager.IsInteractionInited())
		{
			Vector3 screenNormalPos = Vector3.zero;
			Vector3 screenPixelPos = Vector3.zero;

			if(selectedObject == null)
			{
				// no object is currently selected or dragged.
				// if there is a hand grip, try to select the underlying object and start dragging it.
				if(manager.IsLeftHandPrimary())
				{
					// if the left hand is primary, check for left hand grip
					if(manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Grip)
					{
						isLeftHandDrag = true;
						screenNormalPos = manager.GetLeftHandScreenPos();
					}
				}
				else if(manager.IsRightHandPrimary())
				{
					// if the right hand is primary, check for right hand grip
					if(manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Grip)
					{
						isLeftHandDrag = false;
						screenNormalPos = manager.GetRightHandScreenPos();
					}
				}
				
				// check if there is an underlying object to be selected
				if(screenNormalPos != Vector3.zero)
				{
					// convert the normalized screen pos to pixel pos
					screenPixelPos.x = (int)(screenNormalPos.x * Camera.main.pixelWidth);
					screenPixelPos.y = (int)(screenNormalPos.y * Camera.main.pixelHeight);
					Ray ray = Camera.main.ScreenPointToRay(screenPixelPos);
					
					// check for underlying objects
					RaycastHit hit;
					if(Physics.Raycast(ray, out hit))
					{
						if(hit.collider.gameObject == gameObject)
						{
							selectedObject = gameObject;
						
							savedObjectMaterial = selectedObject.GetComponent<Renderer>().material;
							selectedObject.GetComponent<Renderer>().material = selectedObjectMaterial;
						}
					}
				}
				
			}
			else
			{
				// continue dragging the object
				screenNormalPos = isLeftHandDrag ? manager.GetLeftHandScreenPos() : manager.GetRightHandScreenPos();

				float angleArounfY = screenNormalPos.x * 360f;  // horizontal rotation
				float angleArounfX = screenNormalPos.y * 360f;  // vertical rotation
				Vector3 newObjectRotation = new Vector3(-angleArounfX,-angleArounfY,180);

				selectedObject.transform.rotation = Quaternion.Slerp(selectedObject.transform.rotation, Quaternion.Euler(newObjectRotation), smoothFactor * Time.deltaTime);

				// check if the object (hand grip) was released
				bool isReleased = isLeftHandDrag ? (manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Release) :
					(manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Release);

				if(isReleased)
				{
					// restore the object's material and stop dragging the object
					selectedObject.GetComponent<Renderer>().material = savedObjectMaterial;
					selectedObject = null;
				}
			}
		}
	}
Beispiel #15
0
    void Update()
    {
        manager = InteractionManager.Instance;

        if(manager != null && manager.IsInteractionInited())
        {
            Vector3 screenNormalPos = Vector3.zero;
            Vector3 screenPixelPos = Vector3.zero;

            if(draggedObject == null)
            {
                // no object is currently selected or dragged.
                // if there is a hand grip, try to select the underlying object and start dragging it.
                if(manager.IsLeftHandPrimary())
                {
                    // if the left hand is primary, check for left hand grip
                    if(manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Grip)
                    {
                        isLeftHandDrag = true;
                        screenNormalPos = manager.GetLeftHandScreenPos();
                    }
                }
                else if(manager.IsRightHandPrimary())
                {
                    // if the right hand is primary, check for right hand grip
                    if(manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Grip)
                    {
                        isLeftHandDrag = false;
                        screenNormalPos = manager.GetRightHandScreenPos();
                    }
                }

                // check if there is an underlying object to be selected
                if(screenNormalPos != Vector3.zero)
                {
                    // convert the normalized screen pos to pixel pos
                    screenPixelPos.x = (int)(screenNormalPos.x * Camera.main.pixelWidth);
                    screenPixelPos.y = (int)(screenNormalPos.y * Camera.main.pixelHeight);
                    Ray ray = Camera.main.ScreenPointToRay(screenPixelPos);

                    // check for underlying objects
                    RaycastHit hit;
                    if(Physics.Raycast(ray, out hit))
                    {
                        foreach(GameObject obj in draggableObjects)
                        {
                            if(hit.collider.gameObject == obj)
                            {
                                // an object was hit by the ray. select it and start drgging
                                draggedObject = obj;
                                draggedObjectDepth = draggedObject.transform.position.z - Camera.main.transform.position.z;
                                draggedObjectOffset = hit.point - draggedObject.transform.position;

                                // set selection material
                                draggedObjectMaterial = draggedObject.renderer.material;
                                draggedObject.renderer.material = selectedObjectMaterial;
                                break;
                            }
                        }
                    }
                }

            }
            else
            {
                // continue dragging the object
                screenNormalPos = isLeftHandDrag ? manager.GetLeftHandScreenPos() : manager.GetRightHandScreenPos();

        //				// check if there is pull-gesture
        //				bool isPulled = isLeftHandDrag ? manager.IsLeftHandPull(true) : manager.IsRightHandPull(true);
        //				if(isPulled)
        //				{
        //					// set object depth to its original depth
        //					draggedObjectDepth = -Camera.main.transform.position.z;
        //				}

                // convert the normalized screen pos to 3D-world pos
                screenPixelPos.x = (int)(screenNormalPos.x * Camera.main.pixelWidth);
                screenPixelPos.y = (int)(screenNormalPos.y * Camera.main.pixelHeight);
                screenPixelPos.z = screenNormalPos.z + draggedObjectDepth;

                Vector3 newObjectPos = Camera.main.ScreenToWorldPoint(screenPixelPos) - draggedObjectOffset;
                draggedObject.transform.position = Vector3.Lerp(draggedObject.transform.position, newObjectPos, dragSpeed * Time.deltaTime);

                // check if the object (hand grip) was released
                bool isReleased = isLeftHandDrag ? (manager.GetLastLeftHandEvent() == InteractionManager.HandEventType.Release) :
                    (manager.GetLastRightHandEvent() == InteractionManager.HandEventType.Release);

                if(isReleased)
                {
                    // restore the object's material and stop dragging the object
                    draggedObject.renderer.material = draggedObjectMaterial;
                    draggedObject = null;
                }
            }
        }
    }