Ejemplo n.º 1
0
        public async Task <Texture> GetTextureAsync(int textureIndex, bool isSRgb = false)
        {
            int imageIndex = gltf.Textures[textureIndex].Source ?? throw new Exception();

            GltfLoader.Schema.Image image = gltf.Images[imageIndex];

            int        bufferViewIndex = image.BufferView ?? throw new Exception();
            BufferView bufferView      = gltf.BufferViews[bufferViewIndex];

            byte[] currentBuffer = buffers[bufferView.Buffer];

            MemoryStream stream  = new MemoryStream(currentBuffer, bufferView.ByteOffset, bufferView.ByteLength);
            Texture      texture = await Texture.LoadAsync(GraphicsDevice, stream, isSRgb);

            return(texture);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }