google-ar / arcore-unity-sdk

ARCore SDK for Unity
https://developers.google.com/ar
Other
1.4k stars 402 forks source link

Random deactivation of gameobject during loss of image tracking #714

Open synthercat opened 4 years ago

synthercat commented 4 years ago

What is going on: Eventually... when tracking is lost... sometimes(!) for just one frame, your anchored game-object might go inactive.

This was tricky to pinpoin but it gave me a 6 hour madness as I had a OnDisable() method executing without anything calling it until I've build a test script and confirmed it.

Here is how to reproduce:

  1. Have a cube deactivated in the hierarchy
  2. Use my sloppy script that will test the scene just fine, drag that deativated cube in debugGameobject
  3. Run on device having the device report to the console (development / script debugging)
  4. It doesn't always fail but I found that by doing this trick 2 or 3 times it does fail

ARCore BUG

I found ways to overcome this and optimize the hell out of my AR app but this shouldn't have happened in the first place.

synthercat commented 4 years ago

I forgot to paste this

//-----------------------------------------------------------------------
// <copyright file="AugmentedImageExampleController.cs" company="Google">
//
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
//-----------------------------------------------------------------------

namespace GoogleARCore.Examples.AugmentedImage
{
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using GoogleARCore;
    using UnityEngine;
    using UnityEngine.UI;

    /// <summary>
    /// Controller for AugmentedImage example.
    /// </summary>
    /// <remarks>
    /// In this sample, we assume all images are static or moving slowly with
    /// a large occupation of the screen. If the target is actively moving,
    /// we recommend to check <see cref="AugmentedImage.TrackingMethod"/> and
    /// render only when the tracking method equals to
    /// <see cref="AugmentedImageTrackingMethod"/>.<c>FullTracking</c>.
    /// See details in <a href="https://developers.google.com/ar/develop/c/augmented-images/">
    /// Recognize and Augment Images</a>
    /// </remarks>
    public class AugmentedImageExampleControllerDEBUG : MonoBehaviour
    {
        //DEBUGGER
        private bool m_KeepRunning = true;
        public GameObject debugGameobject;

        private List<AugmentedImage> m_TempAugmentedImages = new List<AugmentedImage>();

        /// <summary>
        /// The Unity Awake() method.
        /// </summary>
        public void Awake()
        {
            // Enable ARCore to target 60fps camera capture frame rate on supported devices.
            // Note, Application.targetFrameRate is ignored when QualitySettings.vSyncCount != 0.
            Application.targetFrameRate = 60;
            Screen.sleepTimeout = SleepTimeout.NeverSleep;
        }

        /// <summary>
        /// The Unity Update method.
        /// </summary>
        public void Update()
        {
            Debug.Log(
                $"*debuger activeSelf = {debugGameobject.activeSelf} & " +
                $"activeInHierarchy = {debugGameobject.activeInHierarchy}");
            //Debug.LogWarning("gameO parent = " + debugGameobject.transform.parent.name);

            // Exit the app when the 'back' button is pressed.
            if (Input.GetKey(KeyCode.Escape))
            {
                Application.Quit();
            }

            if (m_KeepRunning)
            {
                print("in keep");
                // Get updated augmented images for this frame.
                Session.GetTrackables<AugmentedImage>(
                    m_TempAugmentedImages, TrackableQueryFilter.Updated);

                if (m_TempAugmentedImages.Count > 0)
                {
                    if (m_TempAugmentedImages[0].TrackingState == TrackingState.Tracking)
                    {
                        print("Assining");
                        // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                        Anchor anchor = m_TempAugmentedImages[0].CreateAnchor(m_TempAugmentedImages[0].CenterPose);
                        debugGameobject.transform.position = anchor.transform.position;
                        debugGameobject.transform.parent = anchor.transform;
                        debugGameobject.SetActive(true);
                        m_KeepRunning = false;
                    }
                }
            }
        }
    }
}