Exemple #1
0
    void Update()
    {
        if (!Input.GetMouseButtonDown(0))
        {
            return;
        }

        RaycastHit raycastHit;
        // Ray ray = Camera.main.ScreenPointToRay(touch.position);
        Ray  ray   = Camera.main.ScreenPointToRay(Input.mousePosition);
        bool found = Physics.Raycast(ray, out raycastHit, 1000f);

        Debug.Log("found=" + found);

        if (found)
        {
            if (raycastHit.transform != null)
            {
                Debug.Log("raycastHit.transform=" + raycastHit.transform);
                PlaneAttachment a = raycastHit.transform.gameObject.GetComponent <PlaneAttachment>();
                Debug.Log("a.name=" + a.name);
                if (andys.Contains(a))
                {
                    Debug.Log("andys.Remove(a)");
                    andys.Remove(a);
                    Transform holder = a.transform.parent;
                    Debug.Log("holder.name=" + holder.name);
                    // Transform anchor = holder.parent;
                    Destroy(holder.gameObject);
                    return;
                }
            }
        }
    }
Exemple #2
0
    void Start()
    {
        Transform holder = new GameObject("holder").transform;

        holder.position = new Vector3(0f, -.1f, 1f);
        PlaneAttachment andy = Instantiate(m_andyAndroidPrefab, holder.position, Quaternion.identity, holder).GetComponent <PlaneAttachment>();

        andys.Add(andy);
    }
Exemple #3
0
    public void Update()
    {
        _QuitOnConnectionErrors();

        // The tracking state must be FrameTrackingState.Tracking in order to access the Frame.
        if (Frame.TrackingState != FrameTrackingState.Tracking)
        {
            const int LOST_TRACKING_SLEEP_TIMEOUT = 15;
            Screen.sleepTimeout = LOST_TRACKING_SLEEP_TIMEOUT;
            return;
        }

        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        Frame.GetNewPlanes(ref m_newPlanes);

        // Iterate over planes found in this frame and instantiate corresponding GameObjects to visualize them.
        for (int i = 0; i < m_newPlanes.Count; i++)
        {
            // Instantiate a plane visualization prefab and set it to track the new plane. The transform is set to
            // the origin with an identity rotation since the mesh for our prefab is updated in Unity World
            // coordinates.
            GameObject planeObject = Instantiate(m_trackedPlanePrefab, Vector3.zero, Quaternion.identity,
                                                 transform);
            planeObject.GetComponent <TrackedPlaneVisualizer>().SetTrackedPlane(m_newPlanes[i]);

            // Apply a random color and grid rotation.
            planeObject.GetComponent <Renderer>().material.SetColor("_GridColor", m_planeColors[Random.Range(0,
                                                                                                             m_planeColors.Length - 1)]);
            planeObject.GetComponent <Renderer>().material.SetFloat("_UvRotation", Random.Range(0.0f, 360.0f));
        }

        foreach (GameObject go in visualizedPlanes)
        {
            Destroy(go);
        }
        visualizedPlanes.Clear();


        // Disable the snackbar UI when no planes are valid.
        bool showSearchingUI = true;

        Frame.GetAllPlanes(ref m_allPlanes);
        for (int i = 0; i < m_allPlanes.Count; i++)
        {
            if (m_allPlanes[i].IsValid)
            {
                showSearchingUI = false;
                // break;
                GameObject planeVisualizer = Instantiate(planeVisualizerPrefab, m_allPlanes[i].Position, m_allPlanes[i].Rotation);
                planeVisualizer.transform.localScale = new Vector3(m_allPlanes[i].Bounds.x, 1f, m_allPlanes[i].Bounds.y);
                visualizedPlanes.Add(planeVisualizer);
            }
        }

        m_searchingForPlaneUI.SetActive(showSearchingUI);

        Touch touch;

        if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
        {
            UpdateLastAndy();
            return;
        }

        RaycastHit raycastHit;
        Ray        ray   = Camera.main.ScreenPointToRay(touch.position);
        bool       found = Physics.Raycast(ray, out raycastHit, 1000f, andyLayermask);

        if (found)
        {
            if (raycastHit.transform != null)
            {
                PlaneAttachment a = raycastHit.transform.gameObject.GetComponent <PlaneAttachment>();
                if (andys.Contains(a))
                {
                    andys.Remove(a);
                    Transform holder = a.transform.parent;
                    // Transform anchor = holder.parent;
                    Destroy(holder.gameObject);
                    return;
                }
            }
        }

        TrackableHit     hit;
        TrackableHitFlag raycastFilter = TrackableHitFlag.PlaneWithinBounds | TrackableHitFlag.PlaneWithinPolygon;

        if (Session.Raycast(m_firstPersonCamera.ScreenPointToRay(touch.position), raycastFilter, out hit))
        {
            // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
            // world evolves.
            var anchor = Session.CreateAnchor(hit.Point, Quaternion.identity);

            // Intanstiate a holder object as a child of the anchor; it's transform will now benefit
            // from the anchor's tracking.
            Transform holder = new GameObject("holder").transform;
            holder.SetParent(anchor.transform, false);

            PlaneAttachment andy = Instantiate(m_andyAndroidPrefab, hit.Point, Quaternion.identity,
                                               holder).GetComponent <PlaneAttachment>();
            andys.Add(andy);

            // Andy should look at the camera but still be flush with the plane.
            andy.transform.LookAt(m_firstPersonCamera.transform);
            andy.transform.rotation = Quaternion.Euler(0.0f,
                                                       andy.transform.rotation.eulerAngles.y, andy.transform.rotation.z);

            // Use a plane attachment component to maintain Andy's y-offset from the plane
            // (occurs after anchor updates).
            andy.Attach(hit.Plane);
        }
    }