Exemplo n.º 1
0
    public void HandleAnchorsChanged(MLXRSession.AnchorsUpdatedEventArgs e)
    {
        foreach (MLXRAnchor anchor in e.added)
        {
            Debug.Log("PCF: ADD " + anchor.id);
            string anchorString = anchor.id.ToString();
            if (!PcfPoseLookup.ContainsKey(anchorString))
            {
                PcfPoseLookup[anchorString] = (new PcfPoseData()
                {
                    pcfId = anchorString,
                    position = anchor.pose.position,
                    rotation = anchor.pose.rotation
                });
            }

            if (displayDebugVisuals)
            {
                Pose            pose      = anchor.pose;
                PCFAnchorVisual newVisual = Instantiate(AnchorVisualPrefab, pose.position, pose.rotation);
                newVisual.transform.parent = visualParent.transform;
                newVisual.GetComponent <PCFAnchorVisual>().Anchor = anchor;
            }
        }

        foreach (MLXRAnchor anchor in e.removed)
        {
            Debug.Log("PCF: REMOVE " + anchor.id);
            string anchorString = anchor.id.ToString();
            if (PcfPoseLookup.ContainsKey(anchorString))
            {
                PcfPoseLookup.Remove(anchorString);
            }

            if (displayDebugVisuals)
            {
                foreach (var visual in visualParent.GetComponentsInChildren <PCFAnchorVisual>())
                {
                    if (visual.Anchor.id.Equals(anchor.id))
                    {
                        DestroyImmediate(visual.gameObject);
                    }
                }
            }
        }

        foreach (MLXRAnchor anchor in e.updated)
        {
            string anchorString = anchor.id.ToString();
            if (PcfPoseLookup.ContainsKey(anchorString))
            {
                PcfPoseLookup[anchorString].position = anchor.pose.position;
                PcfPoseLookup[anchorString].rotation = anchor.pose.rotation;
            }

            if (displayDebugVisuals)
            {
                foreach (var visual in visualParent.GetComponentsInChildren <PCFAnchorVisual>())
                {
                    if (visual.Anchor.id.Equals(anchor.id))
                    {
                        Pose pose = anchor.pose;
                        visual.gameObject.transform.position = pose.position;
                        visual.gameObject.transform.rotation = pose.rotation;
                        Debug.LogFormat("PCF Updated anchor: was {0}, is now {1}", visual.Anchor.ToString(), anchor.ToString());
                        visual.Anchor = anchor;
                    }
                }
            }
        }
    }
Exemplo n.º 2
0
        public void HandleAnchorsChanged(MLXRSession.AnchorsUpdatedEventArgs e)
        {
            List <MLXRAnchor> updated = new List <MLXRAnchor>(e.updated);

            foreach (MLXRAnchor anchor in e.added)
            {
                // The session may have restarted and we may have gotten an anchor we knew about already, so
                // add it to the updated list instead.
                if (anchorGameObjects.TryGetValue(anchor.id, out AnchorObject anchorObject))
                {
                    Debug.LogFormat("Anchor ID {0} was added, but it already had a visual; Updating instead.", anchor.id);
                    updated.Add(anchor);
                    continue;
                }

                // Create a new GameObject, and insert it to the PCFId -> GameObject map
                Pose       pose      = anchor.pose;
                GameObject newVisual = Instantiate(AnchorVisual, pose.position, pose.rotation);
                newVisual.transform.parent = childContainer.transform;
                TextMesh tm         = newVisual.GetComponentInChildren <TextMesh>();
                string   anchorText = MakeAnchorString(anchor);
                tm.text = anchorText;

                anchorGameObjects.Add(anchor.id, new AnchorObject {
                    anchor = anchor, gameObject = newVisual
                });

                Debug.LogFormat("Added anchor: {0}", anchorText);
            }

            foreach (MLXRAnchor anchor in e.removed)
            {
                // Look up the GameObject in the map, and remove it
                if (anchorGameObjects.TryGetValue(anchor.id, out AnchorObject anchorObject))
                {
                    DestroyImmediate(anchorObject.gameObject);
                    anchorGameObjects.Remove(anchor.id);
                }
                else
                {
                    Debug.LogWarningFormat("Anchor ID {0} removed, but no visual was found.", anchor.id);
                    continue;
                }
            }

            foreach (MLXRAnchor anchor in updated)
            {
                // Look up the GameObject in the map, and reset the pose!
                if (anchorGameObjects.TryGetValue(anchor.id, out AnchorObject anchorObject))
                {
                    Pose pose = anchor.pose;
                    anchorObject.gameObject.transform.position = pose.position;
                    anchorObject.gameObject.transform.rotation = pose.rotation;
                    TextMesh tm         = anchorObject.gameObject.GetComponentInChildren <TextMesh>();
                    string   anchorText = MakeAnchorString(anchor);
                    tm.text = anchorText;

                    Debug.LogFormat("Updated anchor:\n{0}\nNow:\n{1}", MakeAnchorString(anchorObject.anchor), anchorText);

                    anchorObject.anchor          = anchor;
                    anchorGameObjects[anchor.id] = anchorObject;
                }
                else
                {
                    Debug.LogWarningFormat("Anchor ID {0} updated, but no visual was found.", anchor.id);
                }
            }

            if (AnchorIdTestDropdown)
            {
                AnchorIdTestDropdown.ClearOptions();
                AnchorIdTestDropdown.AddOptions(anchorGameObjects.Keys.Select(x => x.ToString()).ToList());
            }
        }