Exemplo n.º 1
0
        private GameObject CreateGLTFScene(string projectFilePath)
        {
            ILoader fileLoader = new FileLoader(Path.GetDirectoryName(projectFilePath));

            using (var stream = File.OpenRead(projectFilePath))
            {
                GLTFRoot gLTFRoot;
                GLTFParser.ParseJson(stream, out gLTFRoot);
                var loader = new GLTFSceneImporter(gLTFRoot, fileLoader, null, stream);
                loader.MaximumLod      = _maximumLod;
                loader.IsMultithreaded = true;

                loader.LoadSceneAsync().Wait();
                return(loader.LastLoadedScene);
            }
        }
Exemplo n.º 2
0
        private GameObject CreateGLTFScene(string projectFilePath)
        {
            var importOptions = new ImportOptions
            {
                DataLoader = new FileLoader(Path.GetDirectoryName(projectFilePath)),
            };

            using (var stream = File.OpenRead(projectFilePath))
            {
                GLTFRoot gLTFRoot;
                GLTFParser.ParseJson(stream, out gLTFRoot);
                stream.Position = 0;                 // Make sure the read position is changed back to the beginning of the file
                var loader = new GLTFSceneImporter(gLTFRoot, stream, importOptions);
                loader.MaximumLod      = _maximumLod;
                loader.IsMultithreaded = true;

                loader.LoadSceneAsync().Wait();
                return(loader.LastLoadedScene);
            }
        }
Exemplo n.º 3
0
        // todo undo
#if !WINDOWS_UWP
        private async Task Start()
        {
            var         fullPath0 = Application.streamingAssetsPath + Path.DirectorySeparatorChar + asset0Path;
            IDataLoader loader0   = new FileLoader(URIHelper.GetDirectoryName(fullPath0));

            var         fullPath1 = Application.streamingAssetsPath + Path.DirectorySeparatorChar + asset1Path;
            IDataLoader loader1   = new FileLoader(URIHelper.GetDirectoryName(fullPath1));

            var asset0Stream = await loader0.LoadStreamAsync(Path.GetFileName(asset0Path));

            GLTFRoot asset0Root;

            GLTFParser.ParseJson(asset0Stream, out asset0Root);

            var asset1Stream = await loader1.LoadStreamAsync(Path.GetFileName(asset1Path));

            GLTFRoot asset1Root;

            GLTFParser.ParseJson(asset1Stream, out asset1Root);

            string newPath = "../../" + URIHelper.GetDirectoryName(asset0Path);

            int previousBufferCount  = asset1Root.Buffers.Count;
            int previousImageCount   = asset1Root.Images.Count;
            int previousSceneCounter = asset1Root.Scenes.Count;

            GLTFHelpers.MergeGLTF(asset1Root, asset0Root);

            for (int i = previousBufferCount; i < asset1Root.Buffers.Count; ++i)
            {
                GLTF.Schema.GLTFBuffer buffer = asset1Root.Buffers[i];
                if (!URIHelper.IsBase64Uri(buffer.Uri))
                {
                    buffer.Uri = newPath + buffer.Uri;
                }
            }

            for (int i = previousImageCount; i < asset1Root.Images.Count; ++i)
            {
                GLTFImage image = asset1Root.Images[i];
                if (!URIHelper.IsBase64Uri(image.Uri))
                {
                    image.Uri = newPath + image.Uri;
                }
            }

            foreach (NodeId node in asset1Root.Scenes[asset0Root.Scene.Id + previousSceneCounter].Nodes)
            {
                node.Value.Translation.X += 5f;
                asset1Root.Scene.Value.Nodes.Add(node);
            }
            GLTFSceneImporter importer = new GLTFSceneImporter(
                asset1Root,
                null,
                new ImportOptions
            {
                DataLoader           = loader1,
                AsyncCoroutineHelper = gameObject.AddComponent <AsyncCoroutineHelper>()
            });

            importer.MaximumLod      = MaximumLod;
            importer.IsMultithreaded = Multithreaded;
            await importer.LoadSceneAsync(-1);
        }
Exemplo n.º 4
0
        public async Task Load()
        {
            var importOptions = new ImportOptions
            {
                AsyncCoroutineHelper = gameObject.GetComponent <AsyncCoroutineHelper>() ?? gameObject.AddComponent <AsyncCoroutineHelper>()
            };

            GLTFSceneImporter sceneImporter = null;

            try
            {
                Factory = Factory ?? ScriptableObject.CreateInstance <DefaultImporterFactory>();

                if (UseStream)
                {
                    string fullPath;
                    if (AppendStreamingAssets)
                    {
                        // 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.
                        fullPath = Path.Combine(Application.streamingAssetsPath, GLTFUri.TrimStart(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }));
                    }
                    else
                    {
                        fullPath = GLTFUri;
                    }
                    string directoryPath = URIHelper.GetDirectoryName(fullPath);
                    importOptions.DataLoader = new FileLoader(directoryPath);
                    sceneImporter            = Factory.CreateSceneImporter(
                        Path.GetFileName(GLTFUri),
                        importOptions
                        );
                }
                else
                {
                    string directoryPath = URIHelper.GetDirectoryName(GLTFUri);
                    importOptions.DataLoader = new WebRequestLoader(directoryPath);

                    sceneImporter = Factory.CreateSceneImporter(
                        URIHelper.GetFileFromUri(new Uri(GLTFUri)),
                        importOptions
                        );
                }

                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;
                    }
                }

                print("model loaded with vertices: " + sceneImporter.Statistics.VertexCount.ToString() + ", triangles: " + sceneImporter.Statistics.TriangleCount.ToString());
                LastLoadedScene = sceneImporter.LastLoadedScene;

                Animations = sceneImporter.LastLoadedScene.GetComponents <Animation>();

                if (PlayAnimationOnLoad && Animations.Any())
                {
                    Animations.FirstOrDefault().Play();
                }
            }
            finally
            {
                if (importOptions.DataLoader != null)
                {
                    sceneImporter?.Dispose();
                    sceneImporter            = null;
                    importOptions.DataLoader = null;
                }

                if (onLoadFinished != null)
                {
                    onLoadFinished.Invoke(this);
                }

                if (loadingIndicaor)
                {
                    loadingIndicaor.SetActive(false);
                }
            }
        }