예제 #1
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();
    }
예제 #2
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();
    }