Exemple #1
0
    static async Task CustomInstantiation()
    {
#if GLTFAST_4_OR_NEWER
        var gltf = new GLTFast.GltfImport();
#else
        var gltf = new GLTFast.GLTFast();
#endif
        var success = await gltf.Load("file:///path/to/file.gltf");

        if (success)
        {
            // Get the first material
            var material = gltf.GetMaterial();
            Debug.LogFormat("The first material is called {0}", material.name);

            // Instantiate the scene multiple times
#if GLTFAST_4_OR_NEWER
            gltf.InstantiateMainScene(new GameObject("Instance 1").transform);
            gltf.InstantiateMainScene(new GameObject("Instance 2").transform);
            gltf.InstantiateMainScene(new GameObject("Instance 3").transform);
#else
            gltf.InstantiateGltf(new GameObject("Instance 1").transform);
            gltf.InstantiateGltf(new GameObject("Instance 2").transform);
            gltf.InstantiateGltf(new GameObject("Instance 3").transform);
#endif
        }
        else
        {
            Debug.LogError("Loading glTF failed!");
        }
    }
Exemple #2
0
 void Load(IDeferAgent deferAgent = null)
 {
     gLTFastInstance = new GLTFast(this);
     gLTFastInstance.onSettingRequest += OnSettingHeaders;
     gLTFastInstance.onLoadComplete   += OnLoadComplete;
     gLTFastInstance.Load(url, deferAgent, _forceBinary);
 }
Exemple #3
0
    public IEnumerator SampleModelsTestLoadAllGlb()
    {
        foreach (var file in glbFiles)
        {
            var path = string.Format(
#if UNITY_ANDROID && !UNITY_EDITOR
                "{0}"
#else
                "file://{0}"
#endif
                , Path.Combine(Application.streamingAssetsPath, file)
                );

            Debug.LogFormat("Testing {0}", path);

            var www = new WWW(path);
            yield return(www);

            Assert.Null(www.error, www.error);
            var bytes = www.bytes;

            Assert.NotNull(bytes);
            Assert.Greater(bytes.Length, 0);

            var go      = new GameObject();
            var glTFast = new GLTFast.GLTFast();
            var success = glTFast.LoadGlb(bytes, go.transform);
            Assert.True(success);
            yield return(null);

            Object.Destroy(go);
        }
    }
        protected virtual IEnumerator LoadContent(DownloadHandler dlh)
        {
            deferAgent.Reset();
            gLTFastInstance = new GLTFast();

            bool allFine = true;

            LoadContentPrimary(dlh);

            allFine = !gLTFastInstance.LoadingError;

            if (allFine)
            {
                if (deferAgent.ShouldDefer())
                {
                    yield return(null);
                }
                var routineBuffers  = StartCoroutine(gLTFastInstance.WaitForBufferDownloads());
                var routineTextures = StartCoroutine(gLTFastInstance.WaitForTextureDownloads());

                yield return(routineBuffers);

                yield return(routineTextures);
            }

            allFine = !gLTFastInstance.LoadingError;

            if (allFine)
            {
                deferAgent.Reset();
                var prepareRoutine = gLTFastInstance.Prepare();
                while (prepareRoutine.MoveNext())
                {
                    allFine = !gLTFastInstance.LoadingError;
                    if (!allFine)
                    {
                        break;
                    }
                    if (deferAgent.ShouldDefer())
                    {
                        yield return(null);
                    }
                }
            }

            allFine = !gLTFastInstance.LoadingError;
            if (allFine)
            {
                if (deferAgent.ShouldDefer())
                {
                    yield return(null);
                }
                allFine = gLTFastInstance.InstanciateGltf(transform);
            }

            if (onLoadComplete != null)
            {
                onLoadComplete(allFine, frame);
            }
        }
Exemple #5
0
 private void OnDestroy()
 {
     if (gLTFastInstance != null)
     {
         gLTFastInstance.Destroy();
         gLTFastInstance = null;
     }
 }
Exemple #6
0
 protected virtual void OnDestroy()
 {
     if (gLTFastInstance != null)
     {
         gLTFastInstance.Destroy();
         gLTFastInstance = null;
     }
 }
Exemple #7
0
 public void Load(string url = null, IDeferAgent deferAgent = null)
 {
     if (url != null)
     {
         this.url = url;
     }
     gLTFastInstance = new GLTFast(this);
     gLTFastInstance.onLoadComplete += OnLoadComplete;
     gLTFastInstance.Load(this.url, deferAgent);
 }
Exemple #8
0
        protected override IEnumerator LoadContent(DownloadHandler dlh)
        {
            byte[] results = dlh.data;
            gLTFastInstance = new GLTFast();
            var success = gLTFastInstance.LoadGlb(results, transform);

            if (onLoadComplete != null)
            {
                onLoadComplete(success);
            }
            yield break;
        }
Exemple #9
0
    public IEnumerator SampleModelsTestLoadAllGltf()
    {
        yield return(GltfSampleModels.LoadGltfFileUrls());

        foreach (var file in GltfSampleModels.gltfFileUrls)
        {
            var path = string.Format(
#if UNITY_ANDROID && !UNITY_EDITOR
                "{0}"
#else
                "file://{0}"
#endif
                , Path.Combine(Application.streamingAssetsPath, Path.Combine(prefix, file))
                );

            Debug.LogFormat("Testing {0}", path);

            var webRequest = UnityWebRequest.Get(path);
            yield return(webRequest.SendWebRequest());

            Assert.Null(webRequest.error, webRequest.error);
            Assert.IsFalse(webRequest.isNetworkError);
            Assert.IsFalse(webRequest.isHttpError);
            var json = webRequest.downloadHandler.text;

            Assert.NotNull(json);
            Assert.Greater(json.Length, 0);

            var go = new GameObject(GltfSampleModels.GetNameFromPath(file));
            var materialGenerator = new DefaultMaterialGenerator();
            var glTFast           = new GLTFast.GLTFast(materialGenerator);
            glTFast.LoadGltf(json, path);
            Assert.IsFalse(glTFast.LoadingError);

            yield return(glTFast.WaitForBufferDownloads());

            Assert.False(glTFast.LoadingError);
            yield return(glTFast.WaitForTextureDownloads());

            Assert.False(glTFast.LoadingError);
            yield return(glTFast.Prepare());

            Assert.False(glTFast.LoadingError);
            var success = glTFast.InstantiateGltf(go.transform);
            Assert.IsTrue(success);

            yield return(null);

            glTFast.Destroy();
            Object.Destroy(go);
        }
    }
Exemple #10
0
        public static bool LoadGlbFile(string path, Transform parent = null)
        {
            var bytes = File.ReadAllBytes(path);

            if (bytes == null || bytes.Length < 12)
            {
                Debug.LogError("Couldn't load GLB file.");
                return(false);
            }
            var glTFast = new GLTFast();

            return(glTFast.LoadGlb(bytes, parent));
        }
    public IEnumerator SampleModelsTestLoadAllGlb()
    {
        yield return(GltfSampleModels.LoadGlbFileUrls());

        foreach (var file in GltfSampleModels.glbFileUrls)
        {
            var path = string.Format(
#if UNITY_ANDROID && !UNITY_EDITOR
                "{0}"
#else
                "file://{0}"
#endif
                , Path.Combine(Application.streamingAssetsPath, Path.Combine(prefix, file))
                );

            Debug.LogFormat("Testing {0}", path);

            var webRequest = UnityWebRequest.Get(path);
            yield return(webRequest.SendWebRequest());

            Assert.Null(webRequest.error);
            var bytes = webRequest.downloadHandler.data;

            Assert.NotNull(bytes);
            Assert.Greater(bytes.Length, 0);

            var go      = new GameObject();
            var glTFast = new GLTFast.GLTFast();
            glTFast.LoadGlb(bytes, path);
            Assert.False(glTFast.LoadingError);
            yield return(glTFast.WaitForBufferDownloads());

            Assert.False(glTFast.LoadingError);
            yield return(glTFast.WaitForTextureDownloads());

            Assert.False(glTFast.LoadingError);
            yield return(glTFast.Prepare());

            Assert.False(glTFast.LoadingError);
            var success = glTFast.InstanciateGltf(go.transform);
            Assert.True(success);
            yield return(null);

            glTFast.Destroy();
            Object.Destroy(go);
        }
    }
Exemple #12
0
        protected virtual IEnumerator LoadContent(DownloadHandler dlh)
        {
            string json = dlh.text;

            gLTFastInstance = new GLTFast();

            if (gLTFastInstance.LoadGltf(json, url))
            {
                yield return(StartCoroutine(gLTFastInstance.WaitForAllDependencies()));

                var success = gLTFastInstance.InstanciateGltf(transform);

                if (onLoadComplete != null)
                {
                    onLoadComplete(success);
                }
            }
        }
Exemple #13
0
    async Task CustomDeferAgent()
    {
        // Recommended: Use a common defer agent across multiple GLTFast instances!

        // For a stable frame rate:
        IDeferAgent deferAgent = gameObject.AddComponent <TimeBudgetPerFrameDeferAgent>();

        // Or for faster loading:
        deferAgent = new UninterruptedDeferAgent();

        var tasks = new List <Task>();

        foreach (var url in manyUrls)
        {
#if GLTFAST_4_OR_NEWER
            var gltf = new GLTFast.GltfImport(null, deferAgent);
#else
            var gltf = new GLTFast.GLTFast(null, deferAgent);
#endif
            var task = gltf.Load(url).ContinueWith(
                t => {
                if (t.Result)
                {
#if GLTFAST_4_OR_NEWER
                    gltf.InstantiateMainScene(transform);
                    for (int sceneId = 0; sceneId < gltf.sceneCount; sceneId++)
                    {
                        gltf.InstantiateScene(transform, sceneId);
                    }
#else
                    gltf.InstantiateGltf(transform);
#endif
                }
            },
                TaskScheduler.FromCurrentSynchronizationContext()
                );
            tasks.Add(task);
        }

        await Task.WhenAll(tasks);
    }
Exemple #14
0
        IEnumerator LoadRoutine()
        {
            UnityWebRequest www = UnityWebRequest.Get(url);

            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.LogError(www.error);
            }
            else
            {
                // Or retrieve results as binary data
                byte[] results = www.downloadHandler.data;
                gLTFastInstance = new GLTFast(results, transform);
                if (onLoadComplete != null)
                {
                    onLoadComplete();
                }
            }
            loadRoutine = null;
        }
Exemple #15
0
 void Load(IDownloadProvider downloadProvider = null, IDeferAgent deferAgent = null)
 {
     gLTFastInstance = new GLTFast(this, downloadProvider, deferAgent);
     gLTFastInstance.onLoadComplete += OnLoadComplete;
     gLTFastInstance.Load(url);
 }
Exemple #16
0
 /// <summary>
 /// Method for manual loading with custom <see cref="IDownloadProvider"/> and <see cref="IDeferAgent"/>.
 /// </summary>
 /// <param name="url">URL of the glTF file.</param>
 /// <param name="downloadProvider">Download Provider for custom loading (e.g. caching or HTTP authorization)</param>
 /// <param name="deferAgent">Defer Agent takes care of interrupting the
 /// loading procedure in order to keep the frame rate responsive.</param>
 public virtual async Task <bool> Load(string url, IDownloadProvider downloadProvider = null, IDeferAgent deferAgent = null, IMaterialGenerator materialGenerator = null)
 {
     gLTFastInstance = new GLTFast(downloadProvider, deferAgent, materialGenerator);
     return(await gLTFastInstance.Load(url));
 }
Exemple #17
0
 protected override void LoadContentPrimary(GLTFast gLTFastInstance, DownloadHandler dlh, string url)
 {
     byte[] results = dlh.data;
     gLTFastInstance.LoadGlb(results, url);
 }
Exemple #18
0
        protected virtual void LoadContentPrimary(GLTFast gLTFastInstance, DownloadHandler dlh, string url)
        {
            string json = dlh.text;

            gLTFastInstance.LoadGltf(json, url, urlFunc);
        }
Exemple #19
0
 /// <summary>
 /// Method for manual loading with custom <see cref="IDownloadProvider"/> and <see cref="IDeferAgent"/>.
 /// </summary>
 /// <param name="url">URL of the glTF file.</param>
 /// <param name="downloadProvider">Download Provider for custom loading (e.g. caching or HTTP authorization)</param>
 /// <param name="deferAgent">Defer Agent takes care of interrupting the
 /// loading procedure in order to keep the frame rate responsive.</param>
 public virtual void Load(string url, IDownloadProvider downloadProvider = null, IDeferAgent deferAgent = null, IMaterialGenerator materialGenerator = null)
 {
     gLTFastInstance = new GLTFast(this, downloadProvider, deferAgent, materialGenerator);
     gLTFastInstance.onLoadComplete += OnLoadComplete;
     gLTFastInstance.Load(url);
 }