Esempio n. 1
0
        private static Schedulable <GameObject> LoadVrmAsyncInternal(VRMImporterContext ctx, bool show)
        {
            var schedulable = Schedulable.Create();

            return(schedulable
                   .AddTask(Scheduler.ThreadPool, () =>
            {
                return glTF_VRM_Material.Parse(ctx.Json);
            })
                   .ContinueWith(Scheduler.MainThread, x =>
            {
                // material function
                ctx.CreateMaterial = VRMImporter.GetMaterialFunc(x);
            })
                   .OnExecute(Scheduler.ThreadPool, parent =>
            {
                // textures
                for (int i = 0; i < ctx.GLTF.textures.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.MainThread,
                                   () =>
                    {
                        var texture = new TextureItem(ctx.GLTF, index);
                        texture.Process(ctx.GLTF, ctx.Storage);
                        return texture;
                    })
                    .ContinueWith(Scheduler.ThreadPool, x => ctx.Textures.Add(x));
                }
            })
                   .ContinueWithCoroutine(Scheduler.MainThread, () => LoadMaterials(ctx))
                   .OnExecute(Scheduler.ThreadPool, parent =>
            {
                // meshes
                for (int i = 0; i < ctx.GLTF.meshes.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.ThreadPool,
                                   () => gltfImporter.ReadMesh(ctx, index))
                    .ContinueWith(Scheduler.MainThread, x => gltfImporter.BuildMesh(ctx, x))
                    .ContinueWith(Scheduler.ThreadPool, x => ctx.Meshes.Add(x))
                    ;
                }
            })
                   .ContinueWithCoroutine(Scheduler.MainThread, () => LoadNodes(ctx))
                   .ContinueWithCoroutine(Scheduler.MainThread, () => BuildHierarchy(ctx))
                   .ContinueWith(Scheduler.CurrentThread, _ => VRMImporter.OnLoadModel(ctx))
                   .ContinueWith(Scheduler.CurrentThread,
                                 _ =>
            {
                ctx.Root.name = "VRM";

                if (show)
                {
                    ctx.ShowMeshes();
                }

                return ctx.Root;
            }));
        }
Esempio n. 2
0
        protected virtual Schedulable <Unit> LoadAsync()
        {
            return
                (Schedulable.Create()
                 .AddTask(Scheduler.ThreadPool, () =>
            {
                m_materialFactory.Prepare(GLTF);
            })
                 .ContinueWithCoroutine(Scheduler.ThreadPool, () => m_materialFactory.TexturesProcessOnAnyThread(GLTF, Storage))
                 .ContinueWithCoroutine(Scheduler.MainThread, () => m_materialFactory.TexturesProcessOnMainThread(GLTF))
                 .ContinueWithCoroutine(Scheduler.MainThread, () => m_materialFactory.LoadMaterials(GLTF))
                 .OnExecute(Scheduler.ThreadPool, parent =>
            {
                // UniGLTF does not support draco
                // https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_draco_mesh_compression/README.md#conformance
                if (GLTF.extensionsRequired.Contains("KHR_draco_mesh_compression"))
                {
                    throw new UniGLTFNotSupportedException("draco is not supported");
                }

                // meshes
                var meshImporter = new MeshImporter();
                for (int i = 0; i < GLTF.meshes.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.ThreadPool,
                                   () =>
                    {
                        using (MeasureTime("ReadMesh"))
                        {
                            return meshImporter.ReadMesh(this, index);
                        }
                    })
                    .ContinueWithCoroutine <MeshWithMaterials>(Scheduler.MainThread, x => BuildMesh(x, index))
                    .ContinueWith(Scheduler.ThreadPool, x => Meshes.Add(x))
                    ;
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, LoadNodes)
                 .ContinueWithCoroutine(Scheduler.MainThread, BuildHierarchy)
                 .ContinueWith(Scheduler.MainThread, _ =>
            {
                using (MeasureTime("AnimationImporter"))
                {
                    AnimationImporter.Import(this);
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, OnLoadModel)
                 .ContinueWith(Scheduler.CurrentThread,
                               _ =>
            {
                if (m_showSpeedLog)
                {
                    Debug.Log(GetSpeedLog());
                }
                return Unit.Default;
            }));
        }
Esempio n. 3
0
        private static Schedulable <GameObject> LoadVrmAsyncInternal(VRMImporterContext ctx)
        {
            var schedulable = Schedulable.Create();

            return(schedulable
                   .AddTask(Scheduler.ThreadPool, () =>
            {
                ctx.GLTF.baseDir = Path.GetDirectoryName(ctx.Path);
                return Unit.Default;
            })
                   .ContinueWith(Scheduler.ThreadPool, _ =>
            {
                return glTF_VRM_Material.Parse(ctx.Json);
            })
                   .ContinueWith(Scheduler.MainThread, x =>
            {
                // material function
                ctx.CreateMaterial = VRMImporter.GetMaterialFunc(x);
            })
                   .OnExecute(Scheduler.ThreadPool, parent =>
            {
                // textures
                for (int i = 0; i < ctx.GLTF.textures.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.MainThread,
                                   () =>
                    {
                        var texture = new TextureItem(ctx.GLTF, index);
                        texture.Process();
                        return texture;
                    })
                    .ContinueWith(Scheduler.ThreadPool, x => ctx.Textures.Add(x));
                }
            })
                   .ContinueWithCoroutine(Scheduler.MainThread, () => LoadMaterials(ctx))
                   .OnExecute(Scheduler.ThreadPool, parent =>
            {
                // meshes
                for (int i = 0; i < ctx.GLTF.meshes.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.ThreadPool,
                                   () => gltfImporter.ReadMesh(ctx, index))
                    .ContinueWith(Scheduler.MainThread, x => gltfImporter.BuildMesh(ctx, x))
                    .ContinueWith(Scheduler.ThreadPool, x => ctx.Meshes.Add(x))
                    ;
                }
            })
                   .ContinueWithCoroutine(Scheduler.MainThread, () => LoadNodes(ctx))
                   .ContinueWithCoroutine(Scheduler.MainThread, () => BuildHierarchy(ctx))
                   .ContinueWith(Scheduler.MainThread, _ => VRMImporter.OnLoadModel(ctx))
                   .ContinueWith(Scheduler.MainThread,
                                 _ =>
            {
                /*
                 * Debug.LogFormat("task end: {0}/{1}/{2}/{3}",
                 *  ctx.Textures.Count,
                 *  ctx.Materials.Count,
                 *  ctx.Meshes.Count,
                 *  ctx.Nodes.Count
                 *  );
                 */
                ctx.Root.name = Path.GetFileNameWithoutExtension(ctx.Path);

                // 非表示のメッシュを表示する
                ctx.ShowMeshes();

                return ctx.Root;
            }));
        }
        protected virtual Schedulable <Unit> LoadAsync()
        {
            return
                (Schedulable.Create()
                 .AddTask(Scheduler.ThreadPool, () =>
            {
                if (m_textures.Count == 0)
                {
                    //
                    // runtime
                    //
                    CreateTextureItems();
                }
                else
                {
                    //
                    // already CreateTextures(by assetPostProcessor or editor menu)
                    //
                }
            })
                 .ContinueWithCoroutine(Scheduler.ThreadPool, TexturesProcessOnAnyThread)
                 .ContinueWithCoroutine(Scheduler.MainThread, TexturesProcessOnMainThread)
                 .ContinueWithCoroutine(Scheduler.MainThread, LoadMaterials)
                 .OnExecute(Scheduler.ThreadPool, parent =>
            {
                // UniGLTF does not support draco
                // https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_draco_mesh_compression/README.md#conformance
                if (GLTF.extensionsRequired.Contains("KHR_draco_mesh_compression"))
                {
                    throw new UniGLTFNotSupportedException("draco is not supported");
                }

                // meshes
                var meshImporter = new MeshImporter();
                for (int i = 0; i < GLTF.meshes.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.ThreadPool,
                                   () =>
                    {
                        using (MeasureTime("ReadMesh"))
                        {
                            return meshImporter.ReadMesh(this, index);
                        }
                    })
                    .ContinueWith(Scheduler.MainThread, x =>
                    {
                        using (MeasureTime("BuildMesh"))
                        {
                            var meshWithMaterials = MeshImporter.BuildMesh(this, x);

                            var mesh = meshWithMaterials.Mesh;

                            // mesh name
                            if (string.IsNullOrEmpty(mesh.name))
                            {
                                mesh.name = string.Format("UniGLTF import#{0}", i);
                            }
                            var originalName = mesh.name;
                            for (int j = 1; Meshes.Any(y => y.Mesh.name == mesh.name); ++j)
                            {
                                mesh.name = string.Format("{0}({1})", originalName, j);
                            }

                            return meshWithMaterials;
                        }
                    })
                    .ContinueWith(Scheduler.ThreadPool, x => Meshes.Add(x))
                    ;
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, LoadNodes)
                 .ContinueWithCoroutine(Scheduler.MainThread, BuildHierarchy)
                 .ContinueWith(Scheduler.MainThread, _ =>
            {
                using (MeasureTime("AnimationImporter"))
                {
                    AnimationImporter.ImportAnimation(this);
                }
            })
                 .ContinueWith(Scheduler.CurrentThread,
                               _ =>
            {
                OnLoadModel();
                if (m_showSpeedLog)
                {
                    Debug.Log(GetSpeedLog());
                }
                return Unit.Default;
            }));
        }
Esempio n. 5
0
        public static void LoadVrmAsync(VRMImporterContext ctx, ArraySegment <Byte> chunkData, Action <GameObject> onLoaded)
        {
            var schedulable = Schedulable.Create();

            schedulable
            .AddTask(MainThreadDispatcher.Instance.ThreadScheduler, () =>
            {
                ctx.GLTF.baseDir = Path.GetDirectoryName(ctx.Path);
                foreach (var buffer in ctx.GLTF.buffers)
                {
                    buffer.OpenStorage(ctx.GLTF.baseDir, chunkData);
                }
                return(Unit.Default);
            })
            .ContinueWith(MainThreadDispatcher.Instance.ThreadScheduler, _ =>
            {
                return(glTF_VRM_Material.Parse(ctx.Json));
            })
            .ContinueWith(MainThreadDispatcher.Instance.UnityScheduler, x =>
            {
                // material function
                ctx.CreateMaterial = VRMImporter.GetMaterialFunc(x);
            })
            .OnExecute(MainThreadDispatcher.Instance.UnityScheduler, parent =>
            {
                // textures
                for (int i = 0; i < ctx.GLTF.textures.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(MainThreadDispatcher.Instance.UnityScheduler,
                                   () => gltfImporter.ImportTexture(ctx.GLTF, index))
                    .ContinueWith(MainThreadDispatcher.Instance.ThreadScheduler, x => ctx.Textures.Add(x));
                }
            })
            .ContinueWithCoroutine(MainThreadDispatcher.Instance.UnityScheduler, () => LoadMaterials(ctx))
            .OnExecute(MainThreadDispatcher.Instance.UnityScheduler, parent =>
            {
                // meshes
                for (int i = 0; i < ctx.GLTF.meshes.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(MainThreadDispatcher.Instance.ThreadScheduler,
                                   () => gltfImporter.ReadMesh(ctx, index))
                    .ContinueWith(MainThreadDispatcher.Instance.UnityScheduler, x => gltfImporter.BuildMesh(ctx, x))
                    .ContinueWith(MainThreadDispatcher.Instance.ThreadScheduler, x => ctx.Meshes.Add(x))
                    ;
                }
            })
            .ContinueWithCoroutine(MainThreadDispatcher.Instance.UnityScheduler, () => LoadNodes(ctx))
            .ContinueWithCoroutine(MainThreadDispatcher.Instance.UnityScheduler, () => BuildHierarchy(ctx))
            .ContinueWith(MainThreadDispatcher.Instance.UnityScheduler, _ => VRMImporter.OnLoadModel(ctx))
            .Subscribe(MainThreadDispatcher.Instance.UnityScheduler,
                       _ =>
            {
                /*
                 * Debug.LogFormat("task end: {0}/{1}/{2}/{3}",
                 *  ctx.Textures.Count,
                 *  ctx.Materials.Count,
                 *  ctx.Meshes.Count,
                 *  ctx.Nodes.Count
                 *  );
                 */
                ctx.Root.name = Path.GetFileNameWithoutExtension(ctx.Path);

                // 非表示のメッシュを表示する
                ctx.ShowMeshes();

                onLoaded(ctx.Root);
            }, ex =>
            {
                Debug.LogError(ex);
            })
            ;
        }
Esempio n. 6
0
        protected virtual Schedulable <Unit> LoadAsync()
        {
            return
                (Schedulable.Create()
                 .AddTask(Scheduler.ThreadPool, () =>
            {
                if (m_textures.Count == 0)
                {
                    //
                    // runtime
                    //
                    CreateTextureItems();
                }
                else
                {
                    //
                    // already CreateTextures(by assetPostProcessor or editor menu)
                    //
                }
            })
                 .ContinueWithCoroutine(Scheduler.ThreadPool, () =>
            {
                using (MeasureTime("TexturesProcessOnAnyThread"))
                {
                    return TexturesProcessOnAnyThread();
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, () =>
            {
                using (MeasureTime("TexturesProcessOnMainThread"))
                {
                    return TexturesProcessOnMainThread();
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, () =>
            {
                using (MeasureTime("LoadMaterials"))
                {
                    return LoadMaterials();
                }
            })
                 .OnExecute(Scheduler.ThreadPool, parent =>
            {
                if (GLTF.meshes
                    .SelectMany(x => x.primitives)
                    .Any(x => x.extensions.KHR_draco_mesh_compression != null))
                {
                    throw new UniGLTFNotSupportedException("draco is not supported");
                }

                // meshes
                var meshImporter = new MeshImporter();
                for (int i = 0; i < GLTF.meshes.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.ThreadPool,
                                   () =>
                    {
                        using (MeasureTime("ReadMesh"))
                        {
                            return meshImporter.ReadMesh(this, index);
                        }
                    })
                    .ContinueWith(Scheduler.MainThread, x =>
                    {
                        using (MeasureTime("BuildMesh"))
                        {
                            var meshWithMaterials = MeshImporter.BuildMesh(this, x);

                            var mesh = meshWithMaterials.Mesh;

                            // mesh name
                            if (string.IsNullOrEmpty(mesh.name))
                            {
                                mesh.name = string.Format("UniGLTF import#{0}", i);
                            }
                            var originalName = mesh.name;
                            for (int j = 1; Meshes.Any(y => y.Mesh.name == mesh.name); ++j)
                            {
                                mesh.name = string.Format("{0}({1})", originalName, j);
                            }

                            return meshWithMaterials;
                        }
                    })
                    .ContinueWith(Scheduler.ThreadPool, x => Meshes.Add(x))
                    ;
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, () =>
            {
                using (MeasureTime("LoadNodes"))
                {
                    return LoadNodes();
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, () =>
            {
                using (MeasureTime("BuildHierarchy"))
                {
                    return BuildHierarchy();
                }
            })
                 .ContinueWith(Scheduler.MainThread, _ =>
            {
                AnimationImporter.ImportAnimation(this);
            })
                 .ContinueWith(Scheduler.CurrentThread,
                               _ =>
            {
                OnLoadModel();
                Debug.Log(GetSpeedLog());
                return Unit.Default;
            }));
        }
Esempio n. 7
0
        private static Schedulable <GameObject> LoadVrmAsyncInternal(VRMImporterContext ctx, bool show)
        {
            return(Schedulable.Create()
                   .AddTask(Scheduler.ThreadPool, () =>
            {
                using (ctx.MeasureTime("glTF_VRM_Material.Parse"))
                {
                    return glTF_VRM_Material.Parse(ctx.Json);
                }
            })
                   .ContinueWith(Scheduler.MainThread, gltfMaterials =>
            {
                using (ctx.MeasureTime("new VRMMaterialImporter"))
                {
                    ctx.MaterialImporter = new VRMMaterialImporter(ctx, gltfMaterials);
                }
            })
                   .OnExecute(Scheduler.ThreadPool, parent =>
            {
                // textures
                for (int i = 0; i < ctx.GLTF.textures.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.MainThread,
                                   () =>
                    {
                        using (ctx.MeasureTime("texture.Process"))
                        {
                            var texture = new TextureItem(ctx.GLTF, index);
                            texture.Process(ctx.GLTF, ctx.Storage);
                            return texture;
                        }
                    })
                    .ContinueWith(Scheduler.ThreadPool, x => ctx.AddTexture(x));
                }
            })
                   .ContinueWithCoroutine(Scheduler.MainThread, () => LoadMaterials(ctx))
                   .OnExecute(Scheduler.ThreadPool, parent =>
            {
                // meshes
                for (int i = 0; i < ctx.GLTF.meshes.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.ThreadPool,
                                   () =>
                    {
                        using (ctx.MeasureTime("ReadMesh"))
                        {
                            return gltfImporter.ReadMesh(ctx, index);
                        }
                    })
                    .ContinueWith(Scheduler.MainThread, x =>
                    {
                        using (ctx.MeasureTime("BuildMesh"))
                        {
                            return gltfImporter.BuildMesh(ctx, x);
                        }
                    })
                    .ContinueWith(Scheduler.ThreadPool, x => ctx.Meshes.Add(x))
                    ;
                }
            })
                   .ContinueWithCoroutine(Scheduler.MainThread, () =>
            {
                using (ctx.MeasureTime("LoadNodes"))
                {
                    return LoadNodes(ctx);
                }
            })
                   .ContinueWithCoroutine(Scheduler.MainThread, () =>
            {
                using (ctx.MeasureTime("BuildHierarchy"))
                {
                    return BuildHierarchy(ctx);
                }
            })
                   .ContinueWith(Scheduler.CurrentThread, _ =>
            {
                //using (ctx.MeasureTime("OnLoadModel"))
                {
                    return VRMImporter.OnLoadModel(ctx);
                }
            })
                   .ContinueWith(Scheduler.CurrentThread,
                                 _ =>
            {
                ctx.Root.name = "VRM";

                if (show)
                {
                    ctx.ShowMeshes();
                }

                Debug.Log(ctx.GetSpeedLog());
                return ctx.Root;
            }));
        }
Esempio n. 8
0
 protected override Schedulable <Unit> LoadAsync()
 {
     return(Schedulable.Create()
            .AddTask(Scheduler.ThreadPool, () =>
     {
         using (MeasureTime("glTF_VRM_Material.Parse"))
         {
             return glTF_VRM_Material.Parse(Json);
         }
     })
            .ContinueWith(Scheduler.MainThread, gltfMaterials =>
     {
         using (MeasureTime("new VRMMaterialImporter"))
         {
             SetMaterialImporter(new VRMMaterialImporter(this, gltfMaterials));
         }
     })
            .OnExecute(Scheduler.ThreadPool, parent =>
     {
         // textures
         for (int i = 0; i < GLTF.textures.Count; ++i)
         {
             var index = i;
             parent.AddTask(Scheduler.MainThread,
                            () =>
             {
                 using (MeasureTime("texture.Process"))
                 {
                     var texture = new TextureItem(index);
                     texture.Process(GLTF, Storage);
                     return texture;
                 }
             })
             .ContinueWith(Scheduler.ThreadPool, x => AddTexture(x));
         }
     })
            .ContinueWithCoroutine(Scheduler.MainThread, () => LoadMaterials())
            .OnExecute(Scheduler.ThreadPool, parent =>
     {
         // meshes
         var meshImporter = new MeshImporter();
         for (int i = 0; i < GLTF.meshes.Count; ++i)
         {
             var index = i;
             parent.AddTask(Scheduler.ThreadPool,
                            () =>
             {
                 using (MeasureTime("ReadMesh"))
                 {
                     return meshImporter.ReadMesh(this, index);
                 }
             })
             .ContinueWith(Scheduler.MainThread, x =>
             {
                 using (MeasureTime("BuildMesh"))
                 {
                     return MeshImporter.BuildMesh(this, x);
                 }
             })
             .ContinueWith(Scheduler.ThreadPool, x => Meshes.Add(x))
             ;
         }
     })
            .ContinueWithCoroutine(Scheduler.MainThread, () =>
     {
         using (MeasureTime("LoadNodes"))
         {
             return LoadNodes();
         }
     })
            .ContinueWithCoroutine(Scheduler.MainThread, () =>
     {
         using (MeasureTime("BuildHierarchy"))
         {
             return BuildHierarchy();
         }
     })
            .ContinueWith(Scheduler.CurrentThread, _ =>
     {
         //using (MeasureTime("OnLoadModel"))
         {
             OnLoadModel();
             return Unit.Default;
         }
     })
            .ContinueWith(Scheduler.CurrentThread,
                          _ =>
     {
         Root.name = "VRM";
         Debug.Log(GetSpeedLog());
         return Unit.Default;
     }));
 }