private static void LoadMaterials(AssetImporterGltfMeshContext context)
 {
     for (int i = 0; i < context.Model.Materials.Length; i++)
     {
         glTFLoader.Schema.Material mat = context.Model.Materials[i];
     }
 }
示例#2
0
        private void InitMaterials()
        {
            // default material is always set at index 0;
            float grey  = 0.8f;
            float alpha = 1.0f;

            gltf.Material m = CreateMaterial("Default material", grey, grey, grey, alpha);
            _materials.Add(m);
        }
示例#3
0
        int CreateSolidColorMaterial(Color4f color)
        {
            glTFLoader.Schema.Material material = new glTFLoader.Schema.Material()
            {
                PbrMetallicRoughness = new MaterialPbrMetallicRoughness()
                {
                    BaseColorFactor = color.ToFloatArray(),
                }
            };

            return(dummy.Materials.AddAndReturnIndex(material));
        }
示例#4
0
        void HandleBaseColor(Rhino.DocObjects.Material rhinoMaterial, glTFLoader.Schema.Material gltfMaterial)
        {
            Rhino.DocObjects.Texture baseColorDoc    = rhinoMaterial.GetTexture(TextureType.PBR_BaseColor);
            Rhino.DocObjects.Texture alphaTextureDoc = rhinoMaterial.GetTexture(TextureType.PBR_Alpha);

            RenderTexture baseColorTexture = rhinoMaterial.RenderMaterial.GetTextureFromUsage(RenderMaterial.StandardChildSlots.PbrBaseColor);
            RenderTexture alphaTexture     = rhinoMaterial.RenderMaterial.GetTextureFromUsage(RenderMaterial.StandardChildSlots.PbrAlpha);

            bool baseColorLinear = baseColorTexture == null ? false : IsLinear(baseColorTexture);

            bool hasBaseColorTexture = baseColorDoc == null ? false : baseColorDoc.Enabled;
            bool hasAlphaTexture     = alphaTextureDoc == null ? false : alphaTextureDoc.Enabled;

            bool baseColorDiffuseAlphaForTransparency = rhinoMaterial.PhysicallyBased.UseBaseColorTextureAlphaForObjectAlphaTransparencyTexture;

            if (!baseColorDiffuseAlphaForTransparency && !hasAlphaTexture)
            {
                gltfMaterial.AlphaMode = glTFLoader.Schema.Material.AlphaModeEnum.OPAQUE;
            }
            else
            {
                gltfMaterial.AlphaMode = glTFLoader.Schema.Material.AlphaModeEnum.BLEND;
            }

            Color4f baseColor = rhinoMaterial.PhysicallyBased.BaseColor;

            if (workflow.PreProcessColors)
            {
                baseColor = Color4f.ApplyGamma(baseColor, workflow.PreProcessGamma);
            }

            if (!hasBaseColorTexture && !hasAlphaTexture)
            {
                gltfMaterial.PbrMetallicRoughness.BaseColorFactor = new float[]
                {
                    baseColor.R,
                    baseColor.G,
                    baseColor.B,
                    (float)rhinoMaterial.PhysicallyBased.Alpha,
                };
            }
            else
            {
                if (!hasAlphaTexture)
                {
                    float alpha = (float)rhinoMaterial.PhysicallyBased.Alpha;
                    alphaTexture = CreateSolidColorRhinoTexture(new Color4f(alpha, alpha, alpha, 1.0f), new Size(256, 256));
                }

                gltfMaterial.PbrMetallicRoughness.BaseColorTexture = CombineBaseColorAndAlphaTexture(baseColorTexture, alphaTexture, baseColorDiffuseAlphaForTransparency, baseColor, baseColorLinear);
            }
        }
示例#5
0
        internal static Dictionary <string, int> AddMaterials(this Gltf gltf, IList <Material> materials)
        {
            var materialDict = new Dictionary <string, int>();
            var newMaterials = new List <glTFLoader.Schema.Material>();
            var matId        = 0;

            foreach (var material in materials)
            {
                if (materialDict.ContainsKey(material.Name))
                {
                    continue;
                }

                var m = new glTFLoader.Schema.Material();
                newMaterials.Add(m);

                m.PbrMetallicRoughness = new MaterialPbrMetallicRoughness();
                m.PbrMetallicRoughness.BaseColorFactor = new[] { material.Color.Red, material.Color.Green, material.Color.Blue, material.Color.Alpha };
                m.PbrMetallicRoughness.MetallicFactor  = 1.0f;

                m.Name = material.Name;

                if (material.Color.Alpha < 1.0)
                {
                    m.AlphaMode = glTFLoader.Schema.Material.AlphaModeEnum.BLEND;
                }
                else
                {
                    m.AlphaMode = glTFLoader.Schema.Material.AlphaModeEnum.OPAQUE;
                }

                m.Extensions = new Dictionary <string, object> {
                    { "KHR_materials_pbrSpecularGlossiness", new Dictionary <string, object> {
                          { "diffuseFactor", new[] { material.Color.Red, material.Color.Green, material.Color.Blue, material.Color.Alpha } },
                          { "specularFactor", new[] { material.SpecularFactor, material.SpecularFactor, material.SpecularFactor } },
                          { "glossinessFactor", material.GlossinessFactor }
                      } }
                };

                materialDict.Add(m.Name, matId);
                matId++;
            }

            gltf.Materials = newMaterials.ToArray();

            return(materialDict);
        }
示例#6
0
 private static gltf.Material CreateMaterial(string name, float red, float greeen, float blue, float alpha)
 {
     gltf.Material m = new glTFLoader.Schema.Material();
     m.Name = name;
     m.PbrMetallicRoughness = new glTFLoader.Schema.MaterialPbrMetallicRoughness()
     {
         BaseColorFactor = new Single[] { red, greeen, blue, alpha },
         MetallicFactor  = 0,
         RoughnessFactor = 1
     };
     m.EmissiveFactor = new Single[] { 0, 0, 0 };
     m.AlphaMode      = (alpha < 1.0f)
         ? glTFLoader.Schema.Material.AlphaModeEnum.BLEND
         : glTFLoader.Schema.Material.AlphaModeEnum.OPAQUE;
     m.AlphaCutoff = 0.5f;
     m.DoubleSided = false;
     return(m);
 }
示例#7
0
        internal static int AddMaterial(this Gltf gltf, string name, float red, float green, float blue, float alpha, float specularFactor, float glossinessFactor)
        {
            var m = new glTFLoader.Schema.Material();

            m.PbrMetallicRoughness = new MaterialPbrMetallicRoughness();
            m.PbrMetallicRoughness.BaseColorFactor = new[] { red, green, blue, alpha };
            m.PbrMetallicRoughness.MetallicFactor  = 1.0f;

            m.Name = name;

            if (alpha < 1.0)
            {
                m.AlphaMode = glTFLoader.Schema.Material.AlphaModeEnum.BLEND;
            }
            else
            {
                m.AlphaMode = glTFLoader.Schema.Material.AlphaModeEnum.OPAQUE;
            }

            m.Extensions = new Dictionary <string, object> {
                { "KHR_materials_pbrSpecularGlossiness", new Dictionary <string, object> {
                      { "diffuseFactor", new[] { red, green, blue, alpha } },
                      { "specularFactor", new[] { specularFactor, specularFactor, specularFactor } },
                      { "glossinessFactor", glossinessFactor }
                  } }
            };

            if (gltf.Materials != null)
            {
                var materials = gltf.Materials.ToList();
                materials.Add(m);
                gltf.Materials = materials.ToArray();
            }
            else
            {
                gltf.Materials = new [] { m };
            }

            return(gltf.Materials.Length - 1);
        }
    private UnityEngine.Material LoadMaterial(glTFLoader.Schema.Material material)
    {
        string techniqueID = material.Technique;

        if (techniqueID != null)
        {
            LoadTechnique(m_model.Techniques[techniqueID]);
        }

        UnityEngine.Material res = new UnityEngine.Material(UnityEngine.Shader.Find(" Diffuse"));
        res.name = material.Name;

        if (material.Values != null)
        {
            if (material.Values.ContainsKey("ambient"))
            {
                JArray ambientArray = material.Values["ambient"] as JArray;
                int[]  ambient      = ambientArray.ToObject <int[]>();
                res.SetColor("_Color", ArrayConverter.ToColor(ambient));
            }
            if (material.Values.ContainsKey("emission"))
            {
                JArray emissionArray = material.Values["emission"] as JArray;
                int[]  emission      = emissionArray.ToObject <int[]>();
                res.SetColor("_EmissionColor", ArrayConverter.ToColor(emission));
            }
            if (material.Values.ContainsKey("diffuse"))
            {
                if (material.Values["diffuse"] is string)
                {
                    string    diffuseTextureID = material.Values["diffuse"] as string;
                    Texture2D texture          = LoadTexture(m_model.Textures[diffuseTextureID]);
                    res.mainTexture = texture;
                }
            }
        }
        return(res);
    }
示例#9
0
        internal static Dictionary <string, int> AddMaterials(this Gltf gltf, IList <Material> materials, List <byte> buffer, List <BufferView> bufferViews)
        {
            var materialDict = new Dictionary <string, int>();
            var newMaterials = new List <glTFLoader.Schema.Material>();

            var textureDict = new Dictionary <string, int>(); // the name of the texture image, the id of the texture
            var textures    = new List <glTFLoader.Schema.Texture>();

            var images   = new List <glTFLoader.Schema.Image>();
            var samplers = new List <glTFLoader.Schema.Sampler>();

            var matId     = 0;
            var texId     = 0;
            var imageId   = 0;
            var samplerId = 0;

            foreach (var material in materials)
            {
                if (materialDict.ContainsKey(material.Name))
                {
                    continue;
                }

                var m = new glTFLoader.Schema.Material();
                newMaterials.Add(m);

                m.PbrMetallicRoughness = new MaterialPbrMetallicRoughness();
                m.PbrMetallicRoughness.BaseColorFactor = material.Color.ToArray();
                m.PbrMetallicRoughness.MetallicFactor  = 1.0f;
                m.DoubleSided = material.DoubleSided;
                m.Name        = material.Name;

                if (material.Unlit)
                {
                    m.Extensions = new Dictionary <string, object> {
                        { "KHR_materials_unlit", new Dictionary <string, object> {
                          } }
                    };
                }
                else
                {
                    m.Extensions = new Dictionary <string, object> {
                        { "KHR_materials_pbrSpecularGlossiness", new Dictionary <string, object> {
                              { "diffuseFactor", new[] { material.Color.Red, material.Color.Green, material.Color.Blue, material.Color.Alpha } },
                              { "specularFactor", new[] { material.SpecularFactor, material.SpecularFactor, material.SpecularFactor } },
                              { "glossinessFactor", material.GlossinessFactor }
                          } }
                    };
                }

                if (material.Texture != null && File.Exists(material.Texture))
                {
                    // Add the texture
                    var ti = new TextureInfo();
                    m.PbrMetallicRoughness.BaseColorTexture = ti;
                    ti.Index    = texId;
                    ti.TexCoord = 0;
                    ((Dictionary <string, object>)m.Extensions["KHR_materials_pbrSpecularGlossiness"])["diffuseTexture"] = ti;

                    if (textureDict.ContainsKey(material.Texture))
                    {
                        ti.Index = textureDict[material.Texture];
                    }
                    else
                    {
                        var tex = new Texture();
                        textures.Add(tex);

                        var image = new glTFLoader.Schema.Image();

                        using (var ms = new MemoryStream())
                        {
                            // Flip the texture image vertically
                            // to align with OpenGL convention.
                            // 0,1  1,1
                            // 0,0  1,0
                            using (var texImage = SixLabors.ImageSharp.Image.Load(material.Texture))
                            {
                                texImage.Mutate(x => x.Flip(FlipMode.Vertical));
                                texImage.Save(ms, new SixLabors.ImageSharp.Formats.Png.PngEncoder());
                            }
                            var imageData = ms.ToArray();
                            image.BufferView = AddBufferView(bufferViews, 0, buffer.Count, imageData.Length, null, null);
                            buffer.AddRange(imageData);
                        }

                        while (buffer.Count % 4 != 0)
                        {
                            // Console.WriteLine("Padding...");
                            buffer.Add(0);
                        }

                        image.MimeType = glTFLoader.Schema.Image.MimeTypeEnum.image_png;
                        tex.Source     = imageId;
                        images.Add(image);

                        var sampler = new Sampler();
                        sampler.MagFilter = Sampler.MagFilterEnum.LINEAR;
                        sampler.MinFilter = Sampler.MinFilterEnum.LINEAR;
                        sampler.WrapS     = Sampler.WrapSEnum.REPEAT;
                        sampler.WrapT     = Sampler.WrapTEnum.REPEAT;
                        tex.Sampler       = samplerId;
                        samplers.Add(sampler);

                        textureDict.Add(material.Texture, texId);

                        texId++;
                        imageId++;
                        samplerId++;
                    }
                }

                if (material.Color.Alpha < 1.0)
                {
                    m.AlphaMode = glTFLoader.Schema.Material.AlphaModeEnum.BLEND;
                }
                else
                {
                    m.AlphaMode = glTFLoader.Schema.Material.AlphaModeEnum.OPAQUE;
                }

                materialDict.Add(m.Name, matId);
                matId++;
            }

            if (materials.Count > 0)
            {
                gltf.Materials = newMaterials.ToArray();
            }
            if (textures.Count > 0)
            {
                gltf.Textures = textures.ToArray();
            }
            if (images.Count > 0)
            {
                gltf.Images = images.ToArray();
            }
            if (samplers.Count > 0)
            {
                gltf.Samplers = samplers.ToArray();
            }

            return(materialDict);
        }
示例#10
0
        public int AddMaterial()
        {
            // Prep
            glTFLoader.Schema.Material material = new glTFLoader.Schema.Material()
            {
                Name = rhinoMaterial.Name,
                PbrMetallicRoughness = new MaterialPbrMetallicRoughness(),
            };

            if (!rhinoMaterial.IsPhysicallyBased)
            {
                rhinoMaterial.ToPhysicallyBased();
            }

            // Textures
            Rhino.DocObjects.Texture metallicTexture  = rhinoMaterial.PhysicallyBased.GetTexture(TextureType.PBR_Metallic);
            Rhino.DocObjects.Texture roughnessTexture = rhinoMaterial.PhysicallyBased.GetTexture(TextureType.PBR_Roughness);
            Rhino.DocObjects.Texture normalTexture    = rhinoMaterial.PhysicallyBased.GetTexture(TextureType.Bump);
            Rhino.DocObjects.Texture occlusionTexture = rhinoMaterial.PhysicallyBased.GetTexture(TextureType.PBR_AmbientOcclusion);
            Rhino.DocObjects.Texture emissiveTexture  = rhinoMaterial.PhysicallyBased.GetTexture(TextureType.PBR_Emission);

            HandleBaseColor(rhinoMaterial, material);

            if (metallicTexture != null || roughnessTexture != null)
            {
                material.PbrMetallicRoughness.MetallicRoughnessTexture = AddMetallicRoughnessTexture(rhinoMaterial);

                material.PbrMetallicRoughness.MetallicFactor  = 1.0f;
                material.PbrMetallicRoughness.RoughnessFactor = 1.0f;
            }
            else
            {
                material.PbrMetallicRoughness.MetallicFactor  = (float)rhinoMaterial.PhysicallyBased.Metallic;
                material.PbrMetallicRoughness.RoughnessFactor = (float)rhinoMaterial.PhysicallyBased.Roughness;
            }

            if (normalTexture != null && normalTexture.Enabled)
            {
                material.NormalTexture = AddTextureNormal(normalTexture);
            }

            if (occlusionTexture != null && occlusionTexture.Enabled)
            {
                material.OcclusionTexture = AddTextureOcclusion(occlusionTexture.FileReference.FullPath);
            }

            if (emissiveTexture != null && emissiveTexture.Enabled)
            {
                material.EmissiveTexture = AddTexture(emissiveTexture.FileReference.FullPath);

                float emissionMultiplier = 1.0f;

                var param = rhinoMaterial.RenderMaterial.GetParameter("emission-multiplier");

                if (param != null)
                {
                    emissionMultiplier = (float)Convert.ToDouble(param);
                }

                material.EmissiveFactor = new float[]
                {
                    emissionMultiplier,
                    emissionMultiplier,
                    emissionMultiplier,
                };
            }
            else
            {
                material.EmissiveFactor = new float[]
                {
                    rhinoMaterial.PhysicallyBased.Emission.R,
                    rhinoMaterial.PhysicallyBased.Emission.G,
                    rhinoMaterial.PhysicallyBased.Emission.B,
                };
            }

            return(dummy.Materials.AddAndReturnIndex(material));
        }