예제 #1
0
    private void SpawnMolecule()
    {
        Touch             touch = Input.GetTouch(0);
        TrackableHit      hit;
        TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
                                          TrackableHitFlags.FeaturePointWithSurfaceNormal;

        if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
        {
            // Use hit pose and camera pose to check if hittest is from the
            // back of the plane, if it is, no need to create the anchor.
            if ((hit.Trackable is DetectedPlane) &&
                Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
                            hit.Pose.rotation * Vector3.up) < 0)
            {
                Debug.Log("Hit at back of the current DetectedPlane");
            }
            else
            {
                // Instantiate chemical model at the hit pose.
                var molObj = Instantiate(loadedChemModel, hit.Pose.position, hit.Pose.rotation);
                molObj.transform.Translate(0, 0.5f, 0, Space.World);

                // Compensate for the hitPose rotation facing away from the raycast (i.e. camera).
                molObj.transform.Rotate(0, k_ModelRotation, 0, Space.Self);

                //rotate the loaded molecule

                // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
                // world evolves.
                var anchor = hit.Trackable.CreateAnchor(hit.Pose);

                // Make molecule model a child of the anchor.
                molObj.transform.parent = anchor.transform;

                MoleculeController selectedMolScript = molObj.GetComponent <MoleculeController>();
                selectedMolScript.planePosition = anchor.transform.position;
                selectedMolScript.Highlight();
                selectedMol = selectedMolScript;
            }
        }
    }
    private void SpawnMolecule()
    {
        try
        {
            Touch             touch = Input.GetTouch(0);
            TrackableHit      hit;
            TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
                                              TrackableHitFlags.FeaturePointWithSurfaceNormal;

            if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
            {
                // Use hit pose and camera pose to check if hittest is from the
                // back of the plane, if it is, no need to create the anchor.
                if ((hit.Trackable is DetectedPlane) &&
                    Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
                                hit.Pose.rotation * Vector3.up) < 0)
                {
                    Debug.Log("Hit at back of the current DetectedPlane");
                }
                else
                {
                    // Instantiate chemical model at the hit pose.
                    var molObj = Instantiate(loadedChemModel, hit.Pose.position, hit.Pose.rotation);
                    molObj.SetActive(true);
                    molObj.GetComponentInChildren <MoleculeController>().enabled = true;
                    molObj.transform.GetChild(0).position = hit.Pose.position;

                    Collider[] myColliders    = molObj.GetComponentsInChildren <Collider>();
                    Bounds     moleculeBounds = new Bounds(transform.position, Vector3.zero);
                    foreach (Collider nextCollider in myColliders)
                    {
                        moleculeBounds.Encapsulate(nextCollider.bounds);
                    }

                    molObj.GetComponentInChildren <MoleculeController>().moleculeBounds = moleculeBounds;

                    molObj.transform.Translate(0, molObj.GetComponentInChildren <MoleculeController>().moleculeBounds.extents.y, 0, Space.World);

                    // Compensate for the hitPose rotation facing away from the raycast (i.e. camera).
                    molObj.transform.Rotate(0, k_ModelRotation, 0, Space.Self);

                    //rotate the loaded molecule

                    // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
                    // world evolves.
                    var anchor = hit.Trackable.CreateAnchor(hit.Pose);

                    // Make molecule model a child of the anchor.
                    molObj.transform.parent = anchor.transform;

                    if (selectedMol != null)
                    {
                        selectedMol.Dehighlight();
                    }

                    MoleculeController selectedMolScript = molObj.GetComponentInChildren <MoleculeController>();
                    selectedMolScript.planePosition = anchor.transform.position;
                    selectedMolScript.Highlight();
                    selectedMol      = selectedMolScript;
                    _lastSelectedMol = selectedMol;
                    uIController.SetToggles(selectedMol);
                }
            }
        }

        catch (Exception e)
        {
            _ShowAndroidToastMessage(e.ToString());
        }
    }
    public void Update()
    {
        _UpdateApplicationLifecycle();

        // Hide snackbar when currently tracking at least one plane.
        Session.GetTrackables <DetectedPlane>(m_AllPlanes);
        bool showSearchingUI = true;

        for (int i = 0; i < m_AllPlanes.Count; i++)
        {
            if (m_AllPlanes[i].TrackingState == TrackingState.Tracking)
            {
                showSearchingUI = false;
                break;
            }
        }


        if (showSearchingUI != uIController.SearchingForPlanes)
        {
            if (firstSurface == true)
            {
                ChemviewHelper.ShowAndroidToastMessage("Double tap on a surface to spawn a molecule");
                firstSurface = false;
            }

            uIController.SearchingForPlanes = showSearchingUI;
            SearchingForPlaneUI.GetComponent <SearchingForPlaneController>().MovePosition(showSearchingUI);

            if (showSearchingUI)
            {
                SearchingForPlaneUI.GetComponent <SearchingForPlaneController>().StartMessageCoroutine();
                uIController.FadeOut();
            }

            else
            {
                uIController.FadeIn();
            }
        }

        Touch touch;

        // If the player has not touched the screen, we are done with this update.
        if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
        {
            return;
        }


        if (touch.tapCount == 1 && UserRotating == false)
        {
            if (!EventSystem.current.IsPointerOverGameObject(touch.fingerId))
            {
                Ray        raycast = FirstPersonCamera.ScreenPointToRay(touch.position);
                RaycastHit raycastHit;
                if (Physics.Raycast(raycast, out raycastHit))
                {
                    if (raycastHit.collider.tag == "Molecule" && UserRotating == false)
                    {
                        if (selectedMol == null)
                        {
                            selectedMol            = raycastHit.collider.GetComponentInChildren <MoleculeController>();
                            _lastSelectedMol       = selectedMol;
                            selectedMol.isSelected = true;
                            selectedMol.Highlight();
                            uIController.SetToggles(selectedMol);
                        }

                        else
                        {
                            if (selectedMol != raycastHit.collider.GetComponentInChildren <MoleculeController>())
                            {
                                selectedMol.Dehighlight();
                                UserRotating = false;
                                uIController.TurnOffToggles();

                                selectedMol            = raycastHit.collider.GetComponentInChildren <MoleculeController>();
                                _lastSelectedMol       = selectedMol;
                                selectedMol.isSelected = true;
                                selectedMol.Highlight();
                                uIController.SetToggles(selectedMol);
                            }
                        }
                    }
                }
                else
                {
                    if (selectedMol != null && UserRotating == false)
                    {
                        selectedMol.Dehighlight();
                        MoleculeController selectedMolScript = selectedMol.GetComponentInChildren <MoleculeController>();
                        selectedMolScript.isSelected = false;
                        selectedMol = null;
                        uIController.TurnOffToggles();
                        UserRotating = false;
                    }
                }
                return;
            }
        }

        if (touch.tapCount == 2)
        {
            if (!EventSystem.current.IsPointerOverGameObject(touch.fingerId) && UserRotating == false)
            {
                SpawnMolecule();
                return;
            }
        }
    }
예제 #4
0
    public void Update()
    {
        _UpdateApplicationLifecycle();

        // Hide snackbar when currently tracking at least one plane.
        Session.GetTrackables <DetectedPlane>(m_AllPlanes);
        bool showSearchingUI = true;

        for (int i = 0; i < m_AllPlanes.Count; i++)
        {
            if (m_AllPlanes[i].TrackingState == TrackingState.Tracking)
            {
                showSearchingUI = false;
                break;
            }
        }

        SearchingForPlaneUI.SetActive(showSearchingUI);

        Touch touch;

        // If the player has not touched the screen, we are done with this update.
        if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
        {
            return;
        }


        if (touch.tapCount == 1)
        {
            if (!EventSystem.current.IsPointerOverGameObject(touch.fingerId))
            {
                Ray        raycast = FirstPersonCamera.ScreenPointToRay(touch.position);
                RaycastHit raycastHit;

                if (Physics.Raycast(raycast, out raycastHit))
                {
                    if (raycastHit.collider.tag == "Molecule" && UserRotating == false)
                    {
                        if (selectedMol == null)
                        {
                            selectedMol = raycastHit.collider.GetComponent <MoleculeController>();
                            MoleculeController selectedMolScript = selectedMol.GetComponent <MoleculeController>();
                            selectedMolScript.isSelected = true;
                            selectedMol.Highlight();
                        }

                        else
                        {
                            selectedMol.Dehighlight();
                            selectedMol = raycastHit.collider.GetComponent <MoleculeController>();
                            MoleculeController selectedMolScript = selectedMol.GetComponent <MoleculeController>();
                            selectedMolScript.isSelected = true;
                            selectedMol.Highlight();
                        }
                    }
                }
                else
                {
                    if (selectedMol != null && UserRotating == false)
                    {
                        selectedMol.Dehighlight();
                        MoleculeController selectedMolScript = selectedMol.GetComponent <MoleculeController>();
                        selectedMolScript.isSelected = false;
                        selectedMol = null;
                    }
                }
                return;
            }
        }

        if (touch.tapCount == 2)
        {
            if (!EventSystem.current.IsPointerOverGameObject(touch.fingerId) && UserRotating == false)
            {
                _ShowAndroidToastMessage("mol Spawned");
                SpawnMolecule();
                return;
            }
        }
    }