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); }
public async Task <IList <MaterialAttributes> > GetMaterialAttributesAsync() { List <MaterialAttributes> materials = new List <MaterialAttributes>(gltf.Materials.Length); for (int i = 0; i < gltf.Materials.Length; i++) { MaterialAttributes material = await GetMaterialAttributesAsync(i); materials.Add(material); } return(materials); }
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)); }
/// <summary> /// Initializes a new instance of the <see cref="MaterialAsset"/> class. /// </summary> public MaterialAsset() { Attributes = new MaterialAttributes(); Layers = new MaterialBlendLayers(); Parameters = new ParameterCollection(); }
/// <summary> /// Initializes a new instance of the <see cref="MaterialAsset"/> class. /// </summary> public MaterialAsset() { Attributes = new MaterialAttributes(); Layers = new MaterialBlendLayers(); }
public async Task <MaterialAttributes> GetMaterialAttributesAsync(int materialIndex) { Material material = gltf.Materials[materialIndex]; int?diffuseTextureIndex = material.PbrMetallicRoughness.BaseColorTexture?.Index; int?metallicRoughnessTextureIndex = material.PbrMetallicRoughness.MetallicRoughnessTexture?.Index; int?normalTextureIndex = material.NormalTexture?.Index; if (!diffuseTextureIndex.HasValue) { if (material.Extensions.TryGetValue("KHR_materials_pbrSpecularGlossiness", out object value) && value is JsonElement element) { JsonProperty diffuseTextureProperty = element.EnumerateObject().FirstOrDefault(p => p.Name == "diffuseTexture"); if (!EqualityComparer <JsonProperty> .Default.Equals(diffuseTextureProperty, default)) { diffuseTextureIndex = diffuseTextureProperty.Value.EnumerateObject().First(p => p.Name == "index").Value.GetInt32(); } } } int?specularGlossinessTextureIndex = null; if (!metallicRoughnessTextureIndex.HasValue) { if (material.Extensions.TryGetValue("KHR_materials_pbrSpecularGlossiness", out object value) && value is JsonElement element) { JsonProperty specularGlossinessTextureProperty = element.EnumerateObject().FirstOrDefault(p => p.Name == "specularGlossinessTexture"); if (!EqualityComparer <JsonProperty> .Default.Equals(specularGlossinessTextureProperty, default)) { specularGlossinessTextureIndex = specularGlossinessTextureProperty.Value.EnumerateObject().First(p => p.Name == "index").Value.GetInt32(); } } } MaterialAttributes materialAttributes = new MaterialAttributes(); if (diffuseTextureIndex.HasValue) { Texture diffuseTexture = await GetTextureAsync(diffuseTextureIndex.Value, true); materialAttributes.Diffuse = new MaterialDiffuseMapFeature(new ComputeTextureColor(diffuseTexture)); } else { float[] baseColor = material.PbrMetallicRoughness.BaseColorFactor; Vector4 color = new Vector4(baseColor[0], baseColor[1], baseColor[2], baseColor[3]); materialAttributes.Diffuse = new MaterialDiffuseMapFeature(new ComputeColor(color)); } if (metallicRoughnessTextureIndex.HasValue) { Texture metallicRoughnessTexture = await GetTextureAsync(metallicRoughnessTextureIndex.Value); materialAttributes.MicroSurface = new MaterialRoughnessMapFeature(new ComputeTextureScalar(metallicRoughnessTexture, ColorChannel.G)); materialAttributes.Specular = new MaterialMetalnessMapFeature(new ComputeTextureScalar(metallicRoughnessTexture, ColorChannel.B)); } else if (specularGlossinessTextureIndex.HasValue) { Texture specularGlossinessTexture = await GetTextureAsync(specularGlossinessTextureIndex.Value); materialAttributes.MicroSurface = new MaterialRoughnessMapFeature(new ComputeTextureScalar(specularGlossinessTexture, ColorChannel.A)) { Invert = true }; materialAttributes.Specular = new MaterialSpecularMapFeature(new ComputeTextureColor(specularGlossinessTexture)); } else { float roughness = material.PbrMetallicRoughness.RoughnessFactor; float metalness = material.PbrMetallicRoughness.MetallicFactor; materialAttributes.MicroSurface = new MaterialRoughnessMapFeature(new ComputeScalar(roughness)); materialAttributes.Specular = new MaterialMetalnessMapFeature(new ComputeScalar(metalness)); } if (normalTextureIndex.HasValue) { Texture normalTexture = await GetTextureAsync(normalTextureIndex.Value); materialAttributes.Surface = new MaterialNormalMapFeature(new ComputeTextureColor(normalTexture)); } return(materialAttributes); }
public async Task <MaterialAttributes> GetMaterialAttributesAsync(int materialIndex) { Material material = gltf.Materials[materialIndex]; int?diffuseTextureIndex = material.PbrMetallicRoughness.BaseColorTexture?.Index; int?metallicRoughnessTextureIndex = material.PbrMetallicRoughness.MetallicRoughnessTexture?.Index; int?normalTextureIndex = material.NormalTexture?.Index; if (!diffuseTextureIndex.HasValue) { if (material.Extensions?.FirstOrDefault().Value is Newtonsoft.Json.Linq.JObject jObject && jObject.TryGetValue("diffuseTexture", out Newtonsoft.Json.Linq.JToken token)) { if (token.FirstOrDefault(t => (t as Newtonsoft.Json.Linq.JProperty)?.Name == "index") is Newtonsoft.Json.Linq.JProperty indexToken) { diffuseTextureIndex = (int)indexToken.Value; } } } int?specularGlossinessTextureIndex = null; if (!metallicRoughnessTextureIndex.HasValue) { if (material.Extensions?.FirstOrDefault().Value is Newtonsoft.Json.Linq.JObject jObject && jObject.TryGetValue("specularGlossinessTexture", out Newtonsoft.Json.Linq.JToken token)) { if (token.FirstOrDefault(t => (t as Newtonsoft.Json.Linq.JProperty)?.Name == "index") is Newtonsoft.Json.Linq.JProperty indexToken) { specularGlossinessTextureIndex = (int)indexToken.Value; } } } MaterialAttributes materialAttributes = new MaterialAttributes(); if (diffuseTextureIndex.HasValue) { Texture diffuseTexture = await GetTextureAsync(diffuseTextureIndex.Value); materialAttributes.Diffuse = new MaterialDiffuseMapFeature(new ComputeTextureColor(diffuseTexture)); } else { float[] baseColor = material.PbrMetallicRoughness.BaseColorFactor; Vector4 color = new Vector4(baseColor[0], baseColor[1], baseColor[2], baseColor[3]); materialAttributes.Diffuse = new MaterialDiffuseMapFeature(new ComputeColor(color)); } if (metallicRoughnessTextureIndex.HasValue) { Texture metallicRoughnessTexture = await GetTextureAsync(metallicRoughnessTextureIndex.Value); materialAttributes.MicroSurface = new MaterialRoughnessMapFeature(new ComputeTextureScalar(metallicRoughnessTexture, ColorChannel.G)); materialAttributes.Specular = new MaterialMetalnessMapFeature(new ComputeTextureScalar(metallicRoughnessTexture, ColorChannel.B)); } else if (specularGlossinessTextureIndex.HasValue) { Texture specularGlossinessTexture = await GetTextureAsync(specularGlossinessTextureIndex.Value); materialAttributes.MicroSurface = new MaterialRoughnessMapFeature(new ComputeTextureScalar(specularGlossinessTexture, ColorChannel.A)) { Invert = true }; materialAttributes.Specular = new MaterialSpecularMapFeature(new ComputeTextureColor(specularGlossinessTexture)); } else { float roughness = material.PbrMetallicRoughness.RoughnessFactor; float metalness = material.PbrMetallicRoughness.MetallicFactor; materialAttributes.MicroSurface = new MaterialRoughnessMapFeature(new ComputeScalar(roughness)); materialAttributes.Specular = new MaterialMetalnessMapFeature(new ComputeScalar(metalness)); } if (normalTextureIndex.HasValue) { Texture normalTexture = await GetTextureAsync(normalTextureIndex.Value); materialAttributes.Surface = new MaterialNormalMapFeature(new ComputeTextureColor(normalTexture)); } return(materialAttributes); }
public async Task When_serializing_and_then_deserializing_should_produce_equivalent(string texture, TextureMapType textureMapType, MaterialAttributes materialAttributes) { var chunk = new TextureMapChunk { ParentId = 123, Id = 456, MapChannel = 2, MapType = textureMapType, Attributes = materialAttributes, CreationTime = new DateTime(2008, 8, 13, 16, 50, 13), Texture = texture, TgaTextureSize = 789 }; using (var ms = new MemoryStream()) { await chunk.SerializeAsync(ms, false); ms.Position = 0; // Act var deserializedChunk = new TextureMapChunk(); await deserializedChunk.DeserializeAsync(ms, false); // Assert deserializedChunk.Should().BeEquivalentTo(chunk); ms.Should().BeEof(); } }