How to access files in the Hololens 3D object Folder and get it to the apllication at run time

0

I've been looking for an SDK that can access the internal folder (3D model folder) of the HoloLens and load it into a running application  and we've tried a lot of links to no avail. Can anyone help us solve this problem?

3d-model c# hololens internals
2021-11-24 06:35:33
1

0

Your question is extremely broad but to be honest UWP is a tricky subject. I'm typing this on my phone though but I hope it helps you getting started.


First of all: The Hololens uses UWP.

For doing file IO in UWP applications you need to use a special c# API which only works asynchronous! So get familiar with this concept and the keywords async, await and Task before you start!

Further note that most of Unity's API can only be used on the Unity main thread! Therefore you will need a dedicated class that enables to receive asynchronous Actions and dispatch them into the next Unity main thread Update call via a ConcurrentQueue like e.g.

using System;
using System.Collections.Concurrent;
using UnityEngine;

public class MainThreadDispatcher : MonoBehaviour
{
    private static MainThreadDispatcher _instance;

    public static MainThreadDispatcher Instance
    {
        get
        {
            if (_instance) return _instance;

            _instance = FindObjectOfType<MainThreadDispatcher>();

            if (_instance) return _instance;

            _instance = new GameObject(nameof(MainThreadDispatcher)).AddComponent<MainThreadDispatcher>();

            return _instance;
        }
    }

    private readonly ConcurrentQueue<Action> _actions = new ConcurrentQueue<Action>();

    private void Awake()
    {
        if (_instance && _instance != this)
        {
            Destroy(gameObject);
            return;
        }

        _instance = this;
        DontDestroyOnLoad(gameObject);
    }

    public void DoInMainThread(Action action)
    {
        _actions.Enqueue(action);
    }

    private void Update()
    {
        while (_actions.TryDequeue(out var action))
        {
            action?.Invoke();
        }
    }
}

Now this said you are most probably looking for Windows.Storage.KnownFolders.Objects3D which is a Windows.Storage.StorageFolder.

Here you will use GetFileAsync in order to get a Windows.Storage.StorageFile.

Then you use Windows.Storage.FileIO.ReadBufferAsync in order to read content of this file into an IBuffer.

And finally you can use ToArray in order to get the raw byte[] out of it.

After all this you have to dispatch the result back into the Unity main thread in order to be able to use it there (or continue with the import process in another async way).

You can try and use something like

using System;
using System.IO;
using System.Threading.Tasks;

#if WINDOWS_UWP // We only have these namespaces if on an UWP device
using Windows.Storage;
using System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions;
#endif

public static class Example
{
    // Instead of directly returning byte[] you will need to use a callback
    public static void GetFileContent(string filePath, Action<byte[]> onSuccess)
    {
        Task.Run(async () => await FileLoadingTask(filePath, onSuccess));
    }
    
    private static async Task FileLoadingTask(string filePath, Action<byte[]> onSuccess)
    {
#if WINDOWS_UWP
        // Get the 3D Objects folder
        var folder = Windows.Storage.KnownFolders.Objects3D;
        // get a file within it
        var file = await folder.GetFileAsync(filePath);

        // read the content into a buffer
        var buffer = await Windows.Storage.FileIO.ReadBufferAsync(file);
        // get a raw byte[]
        var bytes = buffer.ToArray();
#else
        // as a fallback and for testing in the Editor use he normal FileIO
        var folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "3D Objects");
        var fullFilePath = Path.Combine(folderPath, filePath);
        var bytes = await File.ReadAllBytesAsync(fullFilePath);
#endif

        // finally dispatch the callback action into the Unity main thread
        MainThreadDispatcher.Instance.DoInMainThread(() => onSuccess?.Invoke(bytes));
    }
}

What you then do with the returned byte[] is up to you! There are many loader implementations and libraries online for the different file types.


Further reading:

2021-11-24 09:34:08

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................