예제 #1
0
    public void Update()
    {
        //update timer
        _time += Time.deltaTime;

        if (_time > RecordingSystem.FloorScanRate && _searchCompleted == true) //update and check for ongoing search
        {
            //reset timer
            _time = 0;

            //request new floor position from pointcloud
            _searchCompleted = false;
            floorFound = false;
            _tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM);
            _pointCloud.FindFloor();
        }

        // If the point cloud has found the floor, adjust the position accordingly.
        if (_pointCloud.m_floorFound)
        {
            _searchCompleted = true;
            floorFound = true;
            if (transform.position.y != _pointCloud.m_floorPlaneY)
                transform.position = new Vector3(0.0f, _pointCloud.m_floorPlaneY, 0.0f);
        }
    }
    /// <summary>
    /// OnGUI is called for rendering and handling GUI events.
    /// </summary>
    public void OnGUI()
    {
        GUI.color = Color.white;

        if (!m_findingFloor)
        {
            if (GUI.Button(new Rect(Screen.width - 220, 20, 200, 80), "<size=30>Find Floor</size>"))
            {
                if (m_pointCloud == null)
                {
                    Debug.LogError("TangoPointCloud required to find floor.");
                    return;
                }

                m_findingFloor = true;
                m_marker.SetActive(false);
                m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM);
                m_pointCloud.FindFloor();
            }
        }
        else
        {
            GUI.Label(new Rect(0, Screen.height - 50, Screen.width, 50), "<size=30>Searching for floor position. Make sure the floor is visible.</size>");
        }
    }
예제 #3
0
    public void Start()
    {
        _pointCloud = FindObjectOfType<TangoPointCloud>();

        if (_pointCloud == null)
        {
            Debug.LogError("TangoPointCloud is NULL, required to find floor.");
            return;
        }

        _tangoApplication = FindObjectOfType<TangoApplication>();

        if (_tangoApplication == null)
        {
            Debug.LogError("TangoApplication is NULL, required to find floor.");
            return;
        }


        //initialize
        _pointCloud.FindFloor();
    }
    /// <summary>
    /// OnGUI is called for rendering and handling GUI events.
    /// </summary>
    public void OnGUI()
    {
        GUI.skin = mySkin;

        if (!m_findingFloor && !showMarkerMenu)
        {
            if (GUI.Button(new Rect(Screen.width - 220, 20, 200, 80), "<size=30>Find Floor</size>"))
            {
                if (m_pointCloud == null)
                {
                    Debug.LogError("TangoPointCloud required to find floor.");
                    return;
                }

                m_findingFloor = true;
                m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM);
                m_pointCloud.FindFloor();
            }
        }
        else if (showMarkerMenu)
        {
            if (GUI.Button(new Rect(Screen.width - buttonWidth * 6, 20, buttonWidth, 80), "<size=30>Marker 1</size>"))
            {
                PlaceMarker(0);
            }
            if (GUI.Button(new Rect(Screen.width - buttonWidth * 5, 20, buttonWidth, 80), "<size=30>Marker 2</size>"))
            {
                PlaceMarker(1);
            }
            if (GUI.Button(new Rect(Screen.width - buttonWidth * 4, 20, buttonWidth, 80), "<size=30>Marker 3</size>"))
            {
                PlaceMarker(2);
            }
            if (GUI.Button(new Rect(Screen.width - buttonWidth * 3, 20, buttonWidth, 80), "<size=30>Marker 4</size>"))
            {
                PlaceMarker(3);
            }
            if (GUI.Button(new Rect(Screen.width - buttonWidth * 2, 20, buttonWidth, 80), "<size=30>Marker 5</size>"))
            {
                PlaceMarker(4);
            }
            if (GUI.Button(new Rect(Screen.width - buttonWidth, 20, buttonWidth, 80), "<size=30>Marker 6</size>"))
            {
                PlaceMarker(5);
                copyPosition.UpdateTargetTransform(5, 0);
                PlaceMarker(6);
                copyPosition.UpdateTargetTransform(6, 0);
                PlaceMarker(5);
            }


            if (markerCount > 0)
            {
                if (theLatestButton != 2 && theLatestButton != 5 && theLatestButton != 6)
                {
                    hSliderValue     = GUI.HorizontalSlider(new Rect(25, 150, Screen.width / 2, Screen.height / 6), hSliderValue, 0.0F, 360.0F);
                    scaleSliderValue = GUI.HorizontalSlider(new Rect(25, 350, Screen.width / 2, Screen.height / 6), scaleSliderValue, 0F, latestScaleValue * 2f);
                }

                Vector3 newAngle = new Vector3(0, hSliderValue, 0);
                markerDictionary[theLatestButton].transform.eulerAngles = newAngle;

                Vector3 newScale = new Vector3(scaleSliderValue, scaleSliderValue, scaleSliderValue);
                sceneDictionary[theLatestButton].transform.localScale = newScale;

                copyPosition.UpdateTargetTransform(theLatestButton, 2);
            }

            // Confirm Button
            if (showConfirm)
            {
                if (GUI.Button(new Rect(Screen.width - buttonWidth, 100, buttonWidth, 80), "<size=30>OK!</size>"))
                {
                    this.gameObject.SetActive(false);
                    showMarkerMenu = false;
                    ShowOnFinish();
                }
            }
        }
        else
        {
            GUI.Label(new Rect(0, Screen.height - 50, Screen.width, 50), "<size=30>Searching for floor position. Make sure the floor is visible.</size>");
        }
    }
예제 #5
0
    /// <summary>
    /// Update is called once per frame.
    /// </summary>
    public void Update()
    {
        if (Input.GetKey(KeyCode.Escape))
        {
            // This is a fix for a lifecycle issue where calling
            // Application.Quit() here, and restarting the application
            // immediately results in a deadlocked app.
            AndroidHelper.AndroidQuit();
        }

        if (isEnabled)
        {
            pollTimer += Time.deltaTime;

            //poll floor every 2 seconds
            if (pollTimer > 2)
            {
                pollTimer = 0;

                if (m_pointCloud == null)
                {
                    Debug.LogError("TangoPointCloud required to find floor.");
                    return;
                }

                m_findingFloor = true;
                //m_marker.SetActive(false);
                m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM);
                m_pointCloud.FindFloor();
            }

            //check if already found
            if (!m_findingFloor)
            {
                return;
            }

            // If the point cloud floor has found a new floor, place the marker at the found y position.
            if (m_pointCloudFloor.m_floorFound && m_pointCloud.m_floorFound)
            {
                m_findingFloor = false;

                // Place the marker at the center of the screen at the found floor height.
                Vector3    target;
                RaycastHit hitInfo;
                if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2.0f, Screen.height / 2.0f)), out hitInfo))
                {
                    //create marker
                    var marker = GameObject.Instantiate(m_marker);
                    marker.transform.parent = gameObject.transform;

                    // Limit distance of the marker position from the camera to the camera's far clip plane. This makes sure that the marker
                    // is visible on screen when the floor is found.
                    Vector3 cameraBase = new Vector3(Camera.main.transform.position.x, hitInfo.point.y, Camera.main.transform.position.z);
                    //target = cameraBase + Vector3.ClampMagnitude(hitInfo.point - cameraBase, Camera.main.farClipPlane * 0.9f);
                    marker.transform.position = cameraBase;
                }
                //else
                //{
                // If no raycast hit, place marker in the camera's forward direction.
                //Vector3 dir = new Vector3(Camera.main.transform.forward.x, 0.0f, Camera.main.transform.forward.z);
                //target = dir.normalized * (Camera.main.farClipPlane * 0.9f);
                //target.y = m_pointCloudFloor.transform.position.y;
                //}

                //marker.transform.position = target;
                //AndroidHelper.ShowAndroidToastMessage(string.Format("Floor found. Unity world height = {0}", m_pointCloudFloor.transform.position.y.ToString()));
            }
        }
    }
    /// <summary>
    /// Wait for the next depth update, then find the plane at the touch position.
    /// </summary>
    /// <returns>Coroutine IEnumerator.</returns>
    /// <param name="touchPosition">Touch position to find a plane at.</param>
    /// <param name="doorCorridorPosition">Door position where to put the door marker.</param>
    /// <param name="wall">Touch position to find a plane at.</param>
    /// <param name="doorCorridorBool">True if we are putting a door marker.</param>
    private IEnumerator _WaitForDepthAndFindPlane(Vector2 touchPosition, Vector3 doorCorridorPosition = new Vector3(), Wall wall = null,
                                                  bool doorCorridorBool = false)
    {
        if (!doorCorridorBool)
        {
            if (m_currentMarkType == 1 || m_currentMarkType == 2)
            {
                AndroidHelper.ShowAndroidToastMessage("Door or corridors must be on walls");
                yield break;
            }
        }

        m_findPlaneWaitingForDepth = true;

        // Turn on the camera and wait for a single depth update.
        m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM);
        while (m_findPlaneWaitingForDepth)
        {
            yield return(null);
        }

        m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.DISABLED);

        // Find the plane.
        Camera  cam = Camera.main;
        Vector3 planeCenter;
        Plane   plane;

        // If I'm selecting the floor, set the floor plane
        if (m_currentMarkType == 3)
        {
            if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
            {
                yield break;
            }
            m_pointCloud.FindFloor();
            floor = new Floor(plane, planeCenter);
            Debug.Log("Height of the floor " + planeCenter.y);
            AndroidHelper.ShowAndroidToastMessage("Floor found!");
            yield break;
        }
        else if (!m_pointCloud.m_floorFound)
        {
            AndroidHelper.ShowAndroidToastMessage("Please, select first the floor");
            yield break;
        }

        // Find me the point I touched on screen on world coordinates if I'm not putting a door...
        if (!doorCorridorBool)
        {
            int i = m_pointCloud.FindClosestPoint(cam, touchPosition, 10);
            if (i < 0)
            {
                yield break;
            }
            planeCenter = m_pointCloud.m_points [i];

            // Put the marker on the floor height
            planeCenter.y = floor.floorCenter.y;
        }         //... else my door position is already set as parameter.
        else
        {
            planeCenter = doorCorridorPosition;
        }

        // Ensure the location is always facing the camera.  This is like a LookRotation, but for the Y axis.
        Vector3 up = floor.floorPlane.normal;
        Vector3 forward;

        if (Vector3.Angle(floor.floorPlane.normal, cam.transform.forward) < 175)
        {
            Vector3 right = Vector3.Cross(up, cam.transform.forward).normalized;
            forward = Vector3.Cross(right, up).normalized;
        }
        else
        {
            // Normal is nearly parallel to camera look direction, the cross product would have too much
            // floating point error in it.
            forward = Vector3.Cross(up, cam.transform.right);
        }

        // Instantiate marker object.
        newMarkObject = Instantiate(m_markPrefabs [m_currentMarkType],
                                    planeCenter,
                                    Quaternion.LookRotation(forward, up)) as GameObject;
        newMarkObject.tag = "CornersAndWalls";

        ARMarker markerScript = newMarkObject.GetComponent <ARMarker> ();

        markerScript.m_type      = m_currentMarkType;
        markerScript.m_timestamp = (float)m_poseController.LastPoseTimestamp;

        Matrix4x4 uwTDevice = Matrix4x4.TRS(m_poseController.transform.position,
                                            m_poseController.transform.rotation,
                                            Vector3.one);
        Matrix4x4 uwTMarker = Matrix4x4.TRS(newMarkObject.transform.position,
                                            newMarkObject.transform.rotation,
                                            Vector3.one);

        markerScript.m_deviceTMarker = Matrix4x4.Inverse(uwTDevice) * uwTMarker;

        m_markerList.Add(newMarkObject);

        prevTouchPos     = null;
        selectedLine     = null;
        m_selectedMarker = null;

        switch (m_currentMarkType)
        {
        // Corner case.
        case 0:
            corners.Add(new Corner(newMarkObject));
            break;

        // Door case.
        case 1:
            TouchScreenKeyboard kb = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, false, false, false, false,
                                                              "Write the name for this destination marker");
            while (!kb.done && !kb.wasCanceled)
            {
                yield return(null);
            }
            newMarkObject.name = kb.text;
            doors.Add(new Corner(newMarkObject, wall));
            break;

        default:
            Debug.Log("No valid point inserted.");
            break;
        }
    }