Exemple #1
0
        IEnumerator BuildMesh(MeshImporter.MeshContext x, int i)
        {
            using (MeasureTime("BuildMesh"))
            {
                MeshWithMaterials meshWithMaterials;
                if (EnableLoadBalancing)
                {
                    var buildMesh = MeshImporter.BuildMeshCoroutine(MaterialFactory, x);
                    yield return(buildMesh);

                    meshWithMaterials = buildMesh.Current as MeshWithMaterials;
                }
                else
                {
                    meshWithMaterials = MeshImporter.BuildMesh(MaterialFactory, 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);
                }

                yield return(meshWithMaterials);
            }
        }
Exemple #2
0
        protected virtual async Task LoadGeometryAsync(IAwaitCaller awaitCaller, Func <string, IDisposable> MeasureTime)
        {
            var inverter = InvertAxis.Create();

            var meshImporter = new MeshImporter();

            for (int i = 0; i < GLTF.meshes.Count; ++i)
            {
                var index = i;
                using (MeasureTime("ReadMesh"))
                {
                    var x = meshImporter.ReadMesh(GLTF, index, inverter);
                    var y = await BuildMeshAsync(awaitCaller, MeasureTime, x, index);

                    Meshes.Add(y);
                }
            }

            using (MeasureTime("LoadNodes"))
            {
                for (int i = 0; i < GLTF.nodes.Count; i++)
                {
                    Nodes.Add(NodeImporter.ImportNode(GLTF.nodes[i], i).transform);
                }
            }
            await awaitCaller.NextFrame();

            using (MeasureTime("BuildHierarchy"))
            {
                var nodes = new List <NodeImporter.TransformWithSkin>();
                for (int i = 0; i < Nodes.Count; ++i)
                {
                    nodes.Add(NodeImporter.BuildHierarchy(GLTF, i, Nodes, Meshes));
                }

                NodeImporter.FixCoordinate(GLTF, nodes, inverter);

                // skinning
                for (int i = 0; i < nodes.Count; ++i)
                {
                    NodeImporter.SetupSkinning(GLTF, nodes, i, inverter);
                }

                if (Root == null)
                {
                    Root = new GameObject("GLTF");
                }
                if (GLTF.rootnodes != null)
                {
                    // connect root
                    foreach (var x in GLTF.rootnodes)
                    {
                        var t = nodes[x].Transform;
                        t.SetParent(Root.transform, false);
                    }
                }
            }
            await awaitCaller.NextFrame();
        }
Exemple #3
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;
            }));
        }
        IEnumerator LoadMeshes()
        {
            var meshImporter = new MeshImporter();

            for (int i = 0; i < GLTF.meshes.Count; ++i)
            {
                var meshContext       = meshImporter.ReadMesh(this, i);
                var meshWithMaterials = MeshImporter.BuildMesh(this, meshContext);
                var mesh = meshWithMaterials.Mesh;
                if (string.IsNullOrEmpty(mesh.name))
                {
                    mesh.name = string.Format("UniGLTF import#{0}", i);
                }
                Meshes.Add(meshWithMaterials);

                yield return(null);
            }
        }
        async Task <MeshWithMaterials> BuildMeshAsync(IAwaitCaller awaitCaller, Func <string, IDisposable> MeasureTime, MeshImporter.MeshContext x, int i)
        {
            using (MeasureTime("BuildMesh"))
            {
                var meshWithMaterials = await MeshImporter.BuildMeshAsync(awaitCaller, MaterialFactory.GetMaterial, 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);
            }
        }
        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;
            }));
        }
        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;
            }));
        }
        public static void Load(ImporterContext ctx)
        {
            // textures
            if (ctx.GLTF.textures != null)
            {
                for (int i = 0; i < ctx.GLTF.textures.Count; ++i)
                {
                    var item = new TextureItem(ctx.GLTF, i, ctx.TextureBaseDir);
                    ctx.AddTexture(item);
                }
            }
            foreach (var x in ctx.GetTextures())
            {
                x.Process(ctx.GLTF, ctx.Storage);
            }

            // materials
            if (ctx.MaterialImporter == null)
            {
                ctx.MaterialImporter = new MaterialImporter(new ShaderStore(ctx), ctx);
            }

            if (ctx.GLTF.materials == null || !ctx.GLTF.materials.Any())
            {
                // no material
                ctx.AddMaterial(ctx.MaterialImporter.CreateMaterial(0, null));
            }
            else
            {
                for (int i = 0; i < ctx.GLTF.materials.Count; ++i)
                {
                    var index    = i;
                    var material = ctx.MaterialImporter.CreateMaterial(index, ctx.GLTF.materials[i]);
                    ctx.AddMaterial(material);
                }
            }

            // meshes
            if (ctx.GLTF.meshes
                .SelectMany(x => x.primitives)
                .Any(x => x.extensions.KHR_draco_mesh_compression != null))
            {
                throw new UniGLTFNotSupportedException("draco is not supported");
            }

            var meshImporter = new MeshImporter();

            for (int i = 0; i < ctx.GLTF.meshes.Count; ++i)
            {
                var meshContext       = meshImporter.ReadMesh(ctx, i);
                var meshWithMaterials = BuildMesh(ctx, meshContext);

                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; ctx.Meshes.Any(x => x.Mesh.name == mesh.name); ++j)
                {
                    mesh.name = string.Format("{0}({1})", originalName, j);
                }

                ctx.Meshes.Add(meshWithMaterials);
            }

            // nodes
            ctx.Nodes.AddRange(ctx.GLTF.nodes.Select(x => ImportNode(x).transform));

            var nodes = ctx.Nodes.Select((x, i) => BuildHierarchy(ctx, i)).ToList();

            gltfImporter.FixCoordinate(ctx, nodes);

            // skinning
            for (int i = 0; i < nodes.Count; ++i)
            {
                gltfImporter.SetupSkinning(ctx, nodes, i);
            }

            // connect root
            ctx.Root = new GameObject("_root_");
            foreach (var x in ctx.GLTF.rootnodes)
            {
                var t = nodes[x].Transform;
                t.SetParent(ctx.Root.transform, false);
            }

            ImportAnimation(ctx);

            //Debug.LogFormat("Import {0}", ctx.Path);
        }
Exemple #9
0
        protected virtual async Task LoadGeometryAsync(IAwaitCaller awaitCaller, Func <string, IDisposable> MeasureTime)
        {
            var inverter = InvertAxis.Create();

            var meshImporter = new MeshImporter();

            if (GLTF.meshes.Count > 0)
            {
                for (var i = 0; i < GLTF.meshes.Count; ++i)
                {
                    var index = i;
                    using (MeasureTime("ReadMesh"))
                    {
                        var meshContext = await awaitCaller.Run(() => meshImporter.ReadMesh(Data, index, inverter));

                        var meshWithMaterials = await BuildMeshAsync(awaitCaller, MeasureTime, meshContext, index);

                        Meshes.Add(meshWithMaterials);
                    }
                }

                await awaitCaller.NextFrame();
            }

            if (GLTF.nodes.Count > 0)
            {
                using (MeasureTime("LoadNodes"))
                {
                    Profiler.BeginSample("ImporterContext.LoadNodes");
                    for (var i = 0; i < GLTF.nodes.Count; i++)
                    {
                        Nodes.Add(NodeImporter.ImportNode(GLTF.nodes[i], i).transform);
                    }
                    Profiler.EndSample();
                }

                await awaitCaller.NextFrame();
            }

            using (MeasureTime("BuildHierarchy"))
            {
                var nodes = new List <NodeImporter.TransformWithSkin>();
                if (Nodes.Count > 0)
                {
                    Profiler.BeginSample("NodeImporter.BuildHierarchy");
                    for (var i = 0; i < Nodes.Count; ++i)
                    {
                        nodes.Add(NodeImporter.BuildHierarchy(GLTF, i, Nodes, Meshes));
                    }
                    Profiler.EndSample();

                    await awaitCaller.NextFrame();
                }

                NodeImporter.FixCoordinate(GLTF, nodes, inverter);

                // skinning
                if (nodes.Count > 0)
                {
                    Profiler.BeginSample("NodeImporter.SetupSkinning");
                    for (var i = 0; i < nodes.Count; ++i)
                    {
                        NodeImporter.SetupSkinning(Data, nodes, i, inverter);
                    }
                    Profiler.EndSample();

                    await awaitCaller.NextFrame();
                }

                if (Root == null)
                {
                    Root = new GameObject("GLTF");
                }
                if (GLTF.rootnodes != null)
                {
                    // connect root
                    foreach (var x in GLTF.rootnodes)
                    {
                        var t = nodes[x].Transform;
                        t.SetParent(Root.transform, false);
                    }
                }
            }
            await awaitCaller.NextFrame();
        }