public async Task <bool> LoadObjectModelsAsync(string modelPath)
        {
            IObjectAnchorsService objectAnchorsService = ObjectAnchorsService.GetService();

            Debug.Log($"{Application.persistentDataPath} {objectAnchorsService != null}");
            string[] ouFiles = Directory.GetFiles(modelPath, "*.ou", SearchOption.AllDirectories);
            foreach (var file in ouFiles)
            {
                // Represent a model by TrackableObject, and load its model into OU service.
                var trackableObject = new TrackableObjectData();

                trackableObject.ModelFilePath = file.Replace('/', '\\');
                string appPath = Application.persistentDataPath.Replace('/', '\\');
                if (trackableObject.ModelFilePath.Contains(appPath))
                {
                    trackableObject.ModelId = await objectAnchorsService.AddObjectModelAsync(trackableObject.ModelFilePath);
                }
                else
                {
#if WINDOWS_UWP
                    byte[] buffer = await ReadFileBytesAsync(trackableObject.ModelFilePath);

                    trackableObject.ModelId = await objectAnchorsService.AddObjectModelAsync(buffer);
#endif // WINDOWS_UWP
                }

                if (trackableObject.ModelId != Guid.Empty)
                {
                    trackableObject.ModelMesh = new Mesh();
                    await trackableObject.ModelMesh.SetFromObjectModel(trackableObject.ModelId);

                    Debug.Log($"mesh has {trackableObject.ModelMesh.triangles.Length} indices");

                    trackableObject.logicalBoundingBox = objectAnchorsService.GetModelBoundingBox(trackableObject.ModelId);
                    _trackableObjects.Add(trackableObject);
                    _modelIdToTrackableObject.Add(trackableObject.ModelId, trackableObject);

                    Debug.Log($"Loaded Model\n{trackableObject}");
                }
                else
                {
                    Debug.LogError($"failed to load model {trackableObject.ModelFilePath}");
                }
            }
            if (_trackableObjects.Count > 0)
            {
                ModelsLoaded?.Invoke(this, EventArgs.Empty);
            }
            return(_trackableObjects.Count > 0);
        }
    private async void Start()
    {
        try
        {
            await _objectAnchorsService.InitializeAsync();
        }
        catch (System.ArgumentException ex)
        {
#if WINDOWS_UWP
            string message = ex.Message;
            Windows.Foundation.IAsyncOperation <Windows.UI.Popups.IUICommand> dialog = null;
            UnityEngine.WSA.Application.InvokeOnUIThread(() => dialog = new Windows.UI.Popups.MessageDialog(message, "Invalid account information").ShowAsync(), true);
            await dialog;
#elif UNITY_EDITOR
            UnityEditor.EditorUtility.DisplayDialog("Invaild account information", ex.Message, "OK");
#endif // WINDOWS_UWP
            throw ex;
        }

        TextLogger.Log($"Object search initialized.");

        foreach (var file in FileHelper.GetFilesInDirectory(Application.persistentDataPath, "*.ou"))
        {
            TextLogger.Log($"Loading model ({Path.GetFileNameWithoutExtension(file)})");

            await _objectAnchorsService.AddObjectModelAsync(file.Replace('/', '\\'));
        }

#if WINDOWS_UWP
        StorageFolder objects3d = KnownFolders.Objects3D;
        foreach (string filePath in FileHelper.GetFilesInDirectory(objects3d.Path, "*.ou"))
        {
            TextLogger.Log($"Loading model ({Path.GetFileNameWithoutExtension(filePath)})");

            byte[] buffer = await ReadFileBytesAsync(filePath);

            await _objectAnchorsService.AddObjectModelAsync(buffer);
        }
#endif

        _objectQueries = InitializeObjectQueries();

        if (IsDiagnosticsCaptureEnabled)
        {
            TextLogger.Log($"Start capture diagnostics.");

            _objectAnchorsService.StartDiagnosticsSession();
        }
    }
    private async void Start()
    {
        await _objectAnchorsService.InitializeAsync();

        TextLogger.Log($"Object search initialized.");

        foreach (var file in FileHelper.GetFilesInDirectory(Application.persistentDataPath, "*.ou"))
        {
            TextLogger.Log($"Loading model ({Path.GetFileNameWithoutExtension(file)})");

            await _objectAnchorsService.AddObjectModelAsync(file.Replace('/', '\\'));
        }

        if (IsDiagnosticsCaptureEnabled)
        {
            TextLogger.Log($"Start capture diagnostics.");

            _objectAnchorsService.StartDiagnosticsSession();
        }
    }
Example #4
0
        public async Task <bool> LoadObjectModelsAsync(string modelPath)
        {
            IObjectAnchorsService objectAnchorsService = ObjectAnchorsService.GetService();

            Debug.Log($"{Application.persistentDataPath} {objectAnchorsService != null}");
            string[] ouFiles = Directory.GetFiles(modelPath, "*.ou", SearchOption.AllDirectories);
            foreach (var file in ouFiles)
            {
                // Represent a model by TrackableObject, and load its model into OU service.
                var trackableObject = new TrackableObjectData();

                trackableObject.ModelFilePath = file.Replace('/', '\\');

                trackableObject.ModelId = await objectAnchorsService.AddObjectModelAsync(trackableObject.ModelFilePath);

                if (trackableObject.ModelId != Guid.Empty)
                {
                    // Query the default coverage threshold from this object model.
                    ObjectQuery query = objectAnchorsService.CreateObjectQuery(trackableObject.ModelId);
                    trackableObject.MinSurfaceCoverageFromObjectModel = query.MinSurfaceCoverage;

                    trackableObject.ModelMesh          = GenerateMesh(trackableObject.ModelId);
                    trackableObject.logicalBoundingBox = objectAnchorsService.GetModelBoundingBox(trackableObject.ModelId);
                    _trackableObjects.Add(trackableObject);
                    _modelIdToTrackableObject.Add(trackableObject.ModelId, trackableObject);

                    Debug.Log($"Loaded Model\n{trackableObject}");
                }
                else
                {
                    Debug.LogError($"failed to load model {trackableObject.ModelFilePath}");
                }
            }
            if (_trackableObjects.Count > 0)
            {
                ModelsLoaded?.Invoke(this, EventArgs.Empty);
            }
            return(_trackableObjects.Count > 0);
        }