コード例 #1
0
        public async override Task CreateAssetAsync(Material material, IServiceProvider services)
        {
            IContentManager      contentManager       = services.GetRequiredService <IContentManager>();
            ShaderContentManager shaderContentManager = services.GetRequiredService <ShaderContentManager>();
            GraphicsDevice       device = services.GetRequiredService <GraphicsDevice>();

            if (device is null)
            {
                throw new InvalidOperationException();
            }

            MaterialDescriptor descriptor;

            if (string.IsNullOrEmpty(Source))
            {
                descriptor = new MaterialDescriptor {
                    Id = Id, Attributes = Attributes
                };
            }
            else
            {
                string path      = Source;
                int    index     = GetIndex(ref path);
                string extension = Path.GetExtension(path);

                if (extension == ".glb")
                {
                    using Stream stream = await contentManager.RootFolder.OpenStreamForReadAsync(path);

                    GltfModelLoader modelLoader = await GltfModelLoader.CreateAsync(device, stream);

                    MaterialAttributes materialAttributes = await modelLoader.GetMaterialAttributesAsync(index);

                    // TODO: Combine material attributes.

                    descriptor = new MaterialDescriptor {
                        Id = Id, Attributes = materialAttributes
                    };
                }
                else
                {
                    throw new NotSupportedException("This file type is not supported.");
                }
            }

            material.Passes.Clear();

            material.Descriptor = descriptor;

            MaterialGeneratorContext context = new MaterialGeneratorContext(device, material, shaderContentManager);
            await MaterialGenerator.GenerateAsync(descriptor, context);
        }
コード例 #2
0
        public async override Task CreateAssetAsync(Model model, IServiceProvider services)
        {
            IContentManager      contentManager       = services.GetRequiredService <IContentManager>();
            ShaderContentManager shaderContentManager = services.GetRequiredService <ShaderContentManager>();
            GraphicsDevice       device = services.GetRequiredService <GraphicsDevice>();

            if (device is null)
            {
                throw new InvalidOperationException();
            }

            string extension = Path.GetExtension(Source);

            if (extension == ".glb")
            {
                model.Materials.Clear();
                model.Meshes.Clear();


                using Stream stream = await contentManager.RootFolder.OpenStreamForReadAsync(Source);

                GltfModelLoader modelLoader = await GltfModelLoader.CreateAsync(device, stream);

                var meshes = await modelLoader.GetMeshesAsync();

                foreach (Mesh mesh in meshes)
                {
                    model.Meshes.Add(mesh);
                }

                if (Materials.Count > 0)
                {
                    foreach (Material material in Materials)
                    {
                        model.Materials.Add(material);
                    }
                }
                else
                {
                    foreach (MaterialAttributes attributes in await modelLoader.GetMaterialAttributesAsync())
                    {
                        Material material = await Material.CreateAsync(device, new MaterialDescriptor { Id = Id, Attributes = attributes }, shaderContentManager);

                        model.Materials.Add(material);
                    }
                }
            }
            else
            {
                throw new NotSupportedException("This file type is not supported.");
            }
        }
コード例 #3
0
        public async override Task <object> CreateAssetAsync(IServiceProvider services)
        {
            IContentManager contentManager = services.GetRequiredService <IContentManager>();
            GraphicsDevice  device         = services.GetRequiredService <GraphicsDevice>();

            if (device is null)
            {
                throw new InvalidOperationException();
            }

            MaterialDescriptor descriptor;

            if (string.IsNullOrEmpty(Source))
            {
                descriptor = new MaterialDescriptor {
                    Id = Id, Attributes = Attributes
                };
            }
            else
            {
                string path      = Source;
                int    index     = GetIndex(ref path);
                string extension = Path.GetExtension(path);

                if (extension == ".glb")
                {
                    using Stream stream = await contentManager.FileProvider.OpenStreamAsync(path, FileMode.Open, FileAccess.Read);

                    GltfModelLoader modelLoader = await GltfModelLoader.CreateAsync(device, stream);

                    MaterialAttributes materialAttributes = await modelLoader.GetMaterialAttributesAsync(index);

                    // TODO: Combine material attributes.

                    descriptor = new MaterialDescriptor {
                        Id = Id, Attributes = materialAttributes
                    };
                }
                else
                {
                    throw new NotSupportedException("This file type is not supported.");
                }
            }

            return(await Material.CreateAsync(device, descriptor, contentManager));
        }