Azure / azure-spatial-anchors-samples

Azure Spatial Anchors sample code
Other
293 stars 137 forks source link

Inconsistency when locating the same anchor multiple times #360

Open Connor-Kerwin opened 2 years ago

Connor-Kerwin commented 2 years ago

Description

Locating an anchor twice results in the second anchor locate result being invalid. It seems as though the anchor cache does not return a valid anchor when you relocate. I'm wondering if it's a package bug, because If I set my AnchorLocateCriteria BypassCache value to true, the issue goes away.

Steps to reproduce the issue

Expected behavior

I'd expect that the second locate operation would result in the anchor being positioned at the same location as the first anchor.

Development information (please complete the following information)

AR Device information (please complete the following information):

Additional context

I did some additional debugging, printing out the LocalAnchor pointer values; they had different values. I'm not sure if this is expected:

[TRACE] Received located event for ID:'66f03ce1-634d-42bd-bd86-19ddfdfda66a' PTR:'1858822145808' [TRACE] Received located event for ID:'66f03ce1-634d-42bd-bd86-19ddfdfda66a' PTR:'1858813819056'

Varnauld commented 2 years ago

Thank you for submitting this issue (internal tracking n# 40431033) - @XiangyuL-Microsoft will be reaching out soon, thank you

XiangyuL-Microsoft commented 2 years ago

@Connor-Kerwin

Hi, is you app built upon Unity or not? Is it possible to get some debugging log during the reproducing? Is it possible to share some code or project with us?

Thank you very much.

Connor-Kerwin commented 2 years ago

@XiangyuL-Microsoft

I am using Unity. Unfortunately, I'm unable to provide the exact source code.

I attempted to write up a new script with just the behaviour which causes my issue, however I am running into another issue now. Could I ask if this script should work? I'm getting anchors locating under the floor in the distance instead of where It's expected to be... The script below is similar to how I'm currently creating and using ASA anchors.

To expand a little on what we do in our project, we make the CloudNativeAnchors and bind them to CloudSpatialAnchors ourselves using CloudToNative, but we also destroy the CloudNativeAnchors, which I think also destroys the ARAnchor which ASA uses. Is it safe to just destroy the CloudNativeAnchor like this? Do you recommend a good way to locally delete an anchor?

using System;
using UnityEngine;
using Microsoft.Azure.SpatialAnchors;
using Microsoft.Azure.SpatialAnchors.Unity;

public class ASAIssueExample : MonoBehaviour
{
    private CloudNativeAnchor instance;
    private CloudSpatialAnchorWatcher watcher;

    public CloudNativeAnchor Prefab;
    public string AnchorId;
    public SpatialAnchorManager AnchorManager;

    protected virtual void Start()
    {
        AnchorManager.SessionStarted += Event_OnSessionStarted;
        AnchorManager.AnchorLocated += Event_OnAnchorLocated;
    }

    public void LocateProcess()
    {
        // Create criteria for the anchor we want
        var criteria = new AnchorLocateCriteria
        {
            Identifiers = new string[] { AnchorId }
        };

        Debug.Log("[TRACE] Starting watcher");

        // Start watching for the anchor
        watcher = AnchorManager.Session.CreateWatcher(criteria);
    }

    private void Event_OnAnchorLocated(object sender, AnchorLocatedEventArgs args)
    {
        var anchor = args.Anchor;
        UnityDispatcher.InvokeOnAppThread(() =>
        {
            OnAnchorLocated(anchor);
        });
    }

    private void OnAnchorLocated(CloudSpatialAnchor anchor)
    {
        try
        {
            Debug.Log($"[TRACE] Anchor {anchor.Identifier} located");
            Debug.Log("[TRACE] Creating native anchor");

            // Create a cloud native anchor
            instance = Instantiate<CloudNativeAnchor>(Prefab);

            // Bind the cloud anchor to the native anchor
            instance.CloudToNative(anchor);

            Debug.Log($"[TRACE] Anchor bound to native ptr {anchor.LocalAnchor}");
        }
        catch (Exception e)
        {
            Debug.LogException(e);
        }
    }

    private void Event_OnSessionStarted(object sender, EventArgs e)
    {
        Debug.Log("[TRACE] Session Started");

        // Fire off the process
        LocateProcess();
    }

    // NOTE: This is not called by the example, we do a credential injection flow
    // to prevent having the keys in the app, this is invoked elsewhere.
    public void StartSession()
    {
        _ = AnchorManager.StartSessionAsync();
    }
}

I get this result in my log, but the anchor appears under the floor. Weirdly, it's consistently incorrect, through restarting the app, it appears in the same incorrect position way under the floor. I can also confirm that this issue occurs on multiple devices, to rule out the device being the issue.

StartSessionAsync called with but no session. Creating one.
[TRACE] Session Started
[TRACE] Starting watcher
[TRACE] Anchor 44f6c38d-1a16-4a55-84d4-e75275f2cb53 located
[TRACE] Creating native anchor
[TRACE] Anchor bound to native ptr 1693714914640
XiangyuL-Microsoft commented 2 years ago

@Connor-Kerwin

We just released a new version of SDK 2.13.0. Could you please try this new version of SDK to see if the problem still existing? Thank you very much.

Varnauld commented 1 year ago

Hello @Connor-Kerwin - were you able to resolve this with the update in July? If not, would you have some further feedback you could share please? Alternatively, if the issue is resolved, may I close it? thank you