예제 #1
0
    /**
     * This function is called after all normal Update() functions have finished.
     * We set touchDownUI to true in GizmoControl when the OnClick event handler.
     * This setup relies on OnClick event handlers happening in a different stage of
     * execution from the Update() and LateUpdate() functions.
     */
    private void LateUpdate()
    {
        ResetState();
#if UNITY_EDITOR
        MouseUpdate();
#else
        TouchUpdate();
#endif
        if (touchDown)
        {
            foreach (RectTransform rt in uiRectTransforms)
            {
                if (BBUtils.IsPointInRT(position, rt))
                {
                    touchDownUI = true;
                    break;
                }
            }
        }
    }
예제 #2
0
    private void Update()
    {
        if (InputManager.instance.touchDown)
        {
            Ray        ray = Camera.main.ScreenPointToRay(InputManager.instance.position);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, GizmoControl.GIZMO_LAYER_MASK, QueryTriggerInteraction.Collide))
            {
                storedGizmoObj        = hit.collider.gameObject;
                storedPosition        = storedGizmoObj.transform.position;
                storedInversePosition = transform.position - (storedPosition - transform.position);
                storedMode            = BBUtils.FindModeFromObj(objModeMapping, hit.collider.gameObject);
                BBUtils.GetProjectedPosition(InputManager.instance.position, transform.position, storedMode, out storedProjectedPosition, transform);
                storedMat = storedGizmoObj.GetComponent <Renderer>().material;
                storedGizmoObj.GetComponent <Renderer>().material = selectedMat;
                scaling = true;
            }
        }
        else if (InputManager.instance.touching && scaling)
        {
            Vector3 targetProjectedPosition;
            bool    success = BBUtils.GetProjectedPosition(InputManager.instance.position, transform.position, storedMode, out targetProjectedPosition, transform);
            if (success)
            {
                Vector3 newPosition = storedPosition + (targetProjectedPosition - storedProjectedPosition);
                if ((newPosition - storedPosition).sqrMagnitude > GizmoControl.MAX_DISTANCE * GizmoControl.MAX_DISTANCE)
                {
                    newPosition = storedPosition + (newPosition - storedPosition).normalized * GizmoControl.MAX_DISTANCE;
                }

                transform.position           = (newPosition + storedInversePosition) / 2;
                linkedObj.transform.position = transform.position;
                float   extent       = Vector3.Distance(newPosition, storedInversePosition);
                Vector3 currentScale = linkedObj.transform.localScale;
                switch (storedMode)
                {
                case Mode.X:
                    linkedObj.transform.localScale = new Vector3(extent, currentScale.y, currentScale.z);
                    break;

                case Mode.Y:
                    linkedObj.transform.localScale = new Vector3(currentScale.x, extent, currentScale.z);
                    break;

                case Mode.Z:
                    linkedObj.transform.localScale = new Vector3(currentScale.x, currentScale.y, extent);
                    break;
                }

                SetGizmoLocations();
            }
        }
        else if (InputManager.instance.touchUp && scaling)
        {
            storedMode = Mode.NONE;
            storedGizmoObj.GetComponent <Renderer>().material = storedMat;
            storedGizmoObj = null;
            storedMat      = null;
            scaling        = false;
        }

        ResizeGizmo();
    }
예제 #3
0
    void Update()
    {
        if (InputManager.instance.touchDown)
        {
            Ray          ray = Camera.main.ScreenPointToRay(InputManager.instance.position);
            RaycastHit[] hits;
            hits = Physics.RaycastAll(ray, Mathf.Infinity, GizmoControl.GIZMO_LAYER_MASK, QueryTriggerInteraction.Collide);
            foreach (RaycastHit hit in hits)
            {
                Mode mode = BBUtils.FindModeFromObj(objModeMapping, hit.collider.gameObject);
                if (mode == Mode.X || mode == Mode.Y || mode == Mode.Z)
                {
                    storedMode     = mode;
                    storedGizmoObj = hit.collider.gameObject;
                    break;
                }
                else if (mode != Mode.NONE)
                {
                    storedMode     = mode;
                    storedGizmoObj = hit.collider.gameObject;
                }
            }

            if (storedMode != Mode.NONE)
            {
                storedPosition = transform.position;
                BBUtils.GetProjectedPosition(InputManager.instance.position, transform.position, storedMode, out storedProjectedPosition, transform);
                storedMat = BBUtils.ChangeSiblingMaterial(storedGizmoObj, selectedMat);
                moving    = true;
            }
        }
        else if (InputManager.instance.touching && moving)
        {
            Vector3 targetProjectedPosition;
            bool    success = BBUtils.GetProjectedPosition(InputManager.instance.position, transform.position, storedMode, out targetProjectedPosition, transform);
            if (success)
            {
                Vector3 newPosition = storedPosition + (targetProjectedPosition - storedProjectedPosition);
                if ((newPosition - storedPosition).sqrMagnitude <= GizmoControl.MAX_DISTANCE * GizmoControl.MAX_DISTANCE)
                {
                    transform.position = newPosition;
                }
                else
                {
                    transform.position = storedPosition + (newPosition - storedPosition).normalized * GizmoControl.MAX_DISTANCE;
                }
            }
        }
        else if (InputManager.instance.touchUp && moving)
        {
            storedMode = Mode.NONE;
            BBUtils.ChangeSiblingMaterial(storedGizmoObj, storedMat);
            storedGizmoObj = null;
            moving         = false;
        }

        if (linkedObj != null)
        {
            linkedObj.transform.position = transform.position;
        }

        ResizeGizmo();
    }
예제 #4
0
    void Update()
    {
        if (InputManager.instance.touchDown)
        {
            Ray        ray = Camera.main.ScreenPointToRay(InputManager.instance.position);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, GizmoControl.GIZMO_LAYER_MASK, QueryTriggerInteraction.Collide))
            {
                storedGizmoObj   = hit.collider.gameObject;
                startingRotation = transform.rotation;

                // Project collison hit point to plane of rotation
                planeNormal = storedGizmoObj.transform.TransformDirection(Vector3.up);
                Vector3 origin3D = BBUtils.GetPointToPlaneClosestPoint(hit.point, transform.position, planeNormal);

                // Compute tangent 3D line
                Vector3 dir3D = Vector3.Cross(origin3D - transform.position, planeNormal).normalized;

                // Compute 2D line by projecting 2 points on the tangent3D line to the plane
                Vector3 origin2D = Camera.main.WorldToScreenPoint(origin3D);
                Vector3 dir2D    = (Camera.main.WorldToScreenPoint(origin3D + dir3D) - origin2D).normalized;

                // Strip out the 3rd element to the 2D line
                lineOrigin = origin2D;
                lineDir    = dir2D;

                storedMat = BBUtils.ChangeSiblingMaterial(storedGizmoObj, selectedMat);
                rotating  = true;
            }
        }
        else if (InputManager.instance.touching && rotating)
        {
            // Project touch position to line
            Vector2 pos = BBUtils.ProjectPointToLine(InputManager.instance.position, lineOrigin, lineDir);

            // Compute rotation amount based off of distance from lineOrigin
            float distance = Vector2.Distance(pos, lineOrigin);
            if ((pos.x - lineOrigin.x) / lineDir.x < 0)
            {
                distance *= -1;
            }
            float angle = distance * -sensitivity;

            // Rotate obj from its starting rotation by theta
            transform.rotation = startingRotation;
            transform.RotateAround(transform.position, planeNormal, angle);
        }
        else if (InputManager.instance.touchUp && rotating)
        {
            BBUtils.ChangeSiblingMaterial(storedGizmoObj, storedMat);
            storedGizmoObj = null;
            storedMat      = null;
            rotating       = false;
        }

        if (linkedObj != null)
        {
            linkedObj.transform.rotation = transform.rotation;
        }

        ResizeGizmo();
    }