示例#1
0
        public async Task Load()
        {
            asyncCoroutineHelper = gameObject.AddComponent <AsyncCoroutineHelper>();
            GLTFSceneImporter sceneImporter = null;
            ILoader           loader        = null;

            try
            {
                if (UseStream)
                {
                    // Path.Combine treats paths that start with the separator character
                    // as absolute paths, ignoring the first path passed in. This removes
                    // that character to properly handle a filename written with it.
                    GLTFUri = GLTFUri.TrimStart(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
                    string fullPath      = Path.Combine(Application.streamingAssetsPath, GLTFUri);
                    string directoryPath = URIHelper.GetDirectoryName(fullPath);
                    loader        = new FileLoader(directoryPath);
                    sceneImporter = new GLTFSceneImporter(
                        Path.GetFileName(GLTFUri),
                        loader,
                        asyncCoroutineHelper
                        );
                }
                else
                {
                    string directoryPath = URIHelper.GetDirectoryName(GLTFUri);
                    loader = new WebRequestLoader(directoryPath, asyncCoroutineHelper);

                    sceneImporter = new GLTFSceneImporter(
                        URIHelper.GetFileFromUri(new Uri(GLTFUri)),
                        loader,
                        asyncCoroutineHelper
                        );
                }

                sceneImporter.SceneParent      = gameObject.transform;
                sceneImporter.Collider         = Collider;
                sceneImporter.MaximumLod       = MaximumLod;
                sceneImporter.Timeout          = Timeout;
                sceneImporter.isMultithreaded  = Multithreaded;
                sceneImporter.CustomShaderName = shaderOverride ? shaderOverride.name : null;

                await sceneImporter.LoadSceneAsync(-1);

                // Override the shaders on all materials if a shader is provided
                if (shaderOverride != null)
                {
                    Renderer[] renderers = gameObject.GetComponentsInChildren <Renderer>();
                    foreach (Renderer renderer in renderers)
                    {
                        renderer.sharedMaterial.shader = shaderOverride;
                    }
                }
            }
            finally
            {
                if (loader != null)
                {
                    sceneImporter?.Dispose();
                    sceneImporter = null;
                    loader        = null;
                }
            }
        }
示例#2
0
        public async Task Load()
        {
            asyncCoroutineHelper = gameObject.GetComponent <AsyncCoroutineHelper>() ?? gameObject.AddComponent <AsyncCoroutineHelper>();
            GLTFSceneImporter sceneImporter = null;
            ILoader           loader        = null;

            try
            {
                if (UseStream)
                {
                    // Path.Combine treats paths that start with the separator character
                    // as absolute paths, ignoring the first path passed in. This removes
                    // that character to properly handle a filename written with it.
                    GLTFUri = GLTFUri.TrimStart(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
                    string fullPath;
                    if (AppendStreamingAssets)
                    {
                        fullPath = Path.Combine(Application.streamingAssetsPath, GLTFUri);
                    }
                    else
                    {
                        fullPath = GLTFUri;
                    }
                    string directoryPath = URIHelper.GetDirectoryName(fullPath);
                    loader        = new FileLoader(directoryPath);
                    sceneImporter = new GLTFSceneImporter(
                        Path.GetFileName(GLTFUri),
                        loader,
                        asyncCoroutineHelper
                        );
                }
                else
                {
                    string directoryPath = URIHelper.GetDirectoryName(GLTFUri);
                    loader = new WebRequestLoader(directoryPath);

                    sceneImporter = new GLTFSceneImporter(
                        URIHelper.GetFileFromUri(new Uri(GLTFUri)),
                        loader,
                        asyncCoroutineHelper
                        );
                }

                sceneImporter.SceneParent      = gameObject.transform;
                sceneImporter.Collider         = Collider;
                sceneImporter.MaximumLod       = MaximumLod;
                sceneImporter.Timeout          = Timeout;
                sceneImporter.IsMultithreaded  = Multithreaded;
                sceneImporter.CustomShaderName = shaderOverride ? shaderOverride.name : null;

                if (MaterialsOnly)
                {
                    var mat = await sceneImporter.LoadMaterialAsync(0);

                    var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    cube.transform.SetParent(gameObject.transform);
                    var renderer = cube.GetComponent <Renderer>();
                    renderer.sharedMaterial = mat;
                }
                else
                {
                    await sceneImporter.LoadSceneAsync();
                }

                // Override the shaders on all materials if a shader is provided
                if (shaderOverride != null)
                {
                    Renderer[] renderers = gameObject.GetComponentsInChildren <Renderer>();
                    foreach (Renderer renderer in renderers)
                    {
                        renderer.sharedMaterial.shader = shaderOverride;
                    }
                }

                if (PlayAnimationOnLoad)
                {
                    Animation[] animations = sceneImporter.LastLoadedScene.GetComponents <Animation>();
                    foreach (Animation anim in animations)
                    {
                        anim.Play();
                    }
                }
            }
            finally
            {
                if (loader != null)
                {
                    sceneImporter?.Dispose();
                    sceneImporter = null;
                    loader        = null;
                }
            }
        }
示例#3
0
 public abstract GLTFSceneImporter CreateSceneImporter(string gltfFileName, ILoader externalDataLoader, AsyncCoroutineHelper asyncCoroutineHelper);
示例#4
0
        public IEnumerator LoadAssetCoroutine()
        {
            if (!string.IsNullOrEmpty(GLTFUri))
            {
                if (VERBOSE)
                {
                    Debug.Log("LoadAssetCoroutine() GLTFUri ->" + GLTFUri);
                }

                asyncCoroutineHelper = gameObject.GetComponent <AsyncCoroutineHelper>() ?? gameObject.AddComponent <AsyncCoroutineHelper>();

                sceneImporter = null;
                ILoader loader = null;

                Destroy(loadedAssetRootGameObject);

                try
                {
                    if (UseStream)
                    {
                        // Path.Combine treats paths that start with the separator character
                        // as absolute paths, ignoring the first path passed in. This removes
                        // that character to properly handle a filename written with it.
                        GLTFUri = GLTFUri.TrimStart(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
                        string fullPath      = Path.Combine(Application.streamingAssetsPath, GLTFUri);
                        string directoryPath = URIHelper.GetDirectoryName(fullPath);
                        loader        = new FileLoader(directoryPath);
                        sceneImporter = new GLTFSceneImporter(
                            Path.GetFileName(GLTFUri),
                            loader,
                            asyncCoroutineHelper
                            );
                    }
                    else
                    {
                        loader = new WebRequestLoader("");

                        if (OnWebRequestStartEvent != null)
                        {
                            (loader as WebRequestLoader).OnLoadStreamStart += OnWebRequestStartEvent;
                        }

                        sceneImporter = new GLTFSceneImporter(
                            GLTFUri,
                            loader,
                            asyncCoroutineHelper
                            );
                    }

                    if (sceneImporter.CreatedObject != null)
                    {
                        Destroy(sceneImporter.CreatedObject);
                    }

                    sceneImporter.SceneParent                     = gameObject.transform;
                    sceneImporter.Collider                        = Collider;
                    sceneImporter.maximumLod                      = MaximumLod;
                    sceneImporter.Timeout                         = Timeout;
                    sceneImporter.isMultithreaded                 = Multithreaded;
                    sceneImporter.useMaterialTransition           = UseVisualFeedback;
                    sceneImporter.CustomShaderName                = shaderOverride ? shaderOverride.name : null;
                    sceneImporter.LoadingTextureMaterial          = LoadingTextureMaterial;
                    sceneImporter.initialVisibility               = initialVisibility;
                    sceneImporter.addMaterialsToPersistentCaching = addMaterialsToPersistentCaching;

                    float time = Time.realtimeSinceStartup;

                    queueCount++;

                    state = State.QUEUED;

                    Func <bool> funcTestDistance = () => TestDistance();
                    yield return(new WaitUntil(funcTestDistance));

                    queueCount--;
                    totalDownloadedCount++;

                    IncrementDownloadCount();

                    state = State.DOWNLOADING;

                    yield return(sceneImporter.LoadScene(-1));

                    state = State.COMPLETED;

                    DecrementDownloadCount();

                    // Override the shaders on all materials if a shader is provided
                    if (shaderOverride != null)
                    {
                        Renderer[] renderers = gameObject.GetComponentsInChildren <Renderer>();
                        foreach (Renderer renderer in renderers)
                        {
                            renderer.sharedMaterial.shader = shaderOverride;
                        }
                    }
                }
                finally
                {
                    if (loader != null)
                    {
                        if (sceneImporter == null)
                        {
                            Debug.Log("sceneImporter is null, could be due to an invalid URI.", this);
                        }
                        else
                        {
                            loadedAssetRootGameObject = sceneImporter.CreatedObject;

                            sceneImporter?.Dispose();
                            sceneImporter = null;
                        }

                        if (OnWebRequestStartEvent != null)
                        {
                            (loader as WebRequestLoader).OnLoadStreamStart -= OnWebRequestStartEvent;
                            OnWebRequestStartEvent = null;
                        }

                        loader = null;
                    }

                    alreadyLoadedAsset = true;
                    OnFinishedLoadingAsset?.Invoke();
                }
            }
            else
            {
                Debug.Log("couldn't load GLTF because url is empty");
            }

            CoroutineStarter.Stop(loadingRoutine);
            loadingRoutine = null;
            Destroy(loadingPlaceholder);
            Destroy(this);
        }
示例#5
0
 public override GLTFSceneImporter CreateSceneImporter(string gltfFileName, ILoader externalDataLoader, AsyncCoroutineHelper asyncCoroutineHelper)
 {
     return(new GLTFSceneImporter(gltfFileName, externalDataLoader, asyncCoroutineHelper));
 }