Esempio n. 1
0
 public KHR_materials_pbrSpecularGlossinessExtension(Color diffuseFactor, TextureInfo diffuseTexture, Vector3 specularFactor, double glossinessFactor, TextureInfo specularGlossinessTexture)
 {
     DiffuseFactor             = diffuseFactor;
     DiffuseTexture            = diffuseTexture;
     SpecularFactor            = specularFactor;
     GlossinessFactor          = glossinessFactor;
     SpecularGlossinessTexture = specularGlossinessTexture;
 }
Esempio n. 2
0
 public static GLTF.Math.Color[] ToNumericsColorLinear(this UnityEngine.Color[] inColorArr)
 {
     GLTF.Math.Color[] outColorArr = new GLTF.Math.Color[inColorArr.Length];
     for (int i = 0; i < inColorArr.Length; ++i)
     {
         outColorArr[i] = inColorArr[i].ToNumericsColorLinear();
     }
     return(outColorArr);
 }
Esempio n. 3
0
 public static UnityEngine.Color ToUnityColor(this GLTF.Math.Color color)
 {
     return(new UnityEngine.Color(color.R, color.G, color.B, color.A));
 }
Esempio n. 4
0
 public static Color ImportColor(GLTF.Math.Color color)
 {
     return(ImportColor(new Color(color.R, color.G, color.B, color.A)));
 }
        private static GLTF.Schema.Material ConvertSeinPBRMaterial(UnityEngine.Material mat, ExporterEntry entry)
        {
            var material = new GLTF.Schema.Material();

            material.Name = mat.name;

            bool isMetal = mat.GetInt("workflow") == 0;
            bool isUnlit = mat.GetInt("unlit") == 1;

            if (!isMetal)
            {
                // special
                entry.AddExtension("KHR_materials_pbrSpecularGlossiness");
                material.Extensions = new Dictionary <string, Extension>();
            }
            else
            {
                material.PbrMetallicRoughness = new PbrMetallicRoughness();
            }
            bool hasTransparency = ProcessTransparency(mat, material);

            if (isUnlit || isMetal)
            {
                if (mat.GetTexture("_baseColorMap") != null)
                {
                    var id = entry.SaveTexture((Texture2D)mat.GetTexture("_baseColorMap"), hasTransparency);
                    material.PbrMetallicRoughness.BaseColorTexture = new TextureInfo {
                        Index = id
                    };
                }

                if (mat.GetColor("_baseColor") != null)
                {
                    Color c = mat.GetColor("_baseColor");
                    material.PbrMetallicRoughness.BaseColorFactor = Utils.ExportColor(c);
                }
            }

            if (isUnlit)
            {
                if (material.Extensions == null)
                {
                    material.Extensions = new Dictionary <string, Extension>();
                }
                ExtensionManager.Serialize(ExtensionManager.GetExtensionName(typeof(KHR_materials_unlitExtensionFactory)), entry, material.Extensions);
            }
            else if (isMetal)
            {
                bool hasPBRMap = mat.GetTexture("_metallicMap") != null;
                if (hasPBRMap)
                {
                    Texture2D metallicTexture  = (Texture2D)mat.GetTexture("_metallicMap");
                    Texture2D roughnessTexture = (Texture2D)mat.GetTexture("_roughnessMap");
                    Texture2D occlusion        = (Texture2D)mat.GetTexture("_occlusionMap");

                    var metalRoughTextureAo = CreateOcclusionMetallicRoughnessTexture(
                        ref metallicTexture, ref roughnessTexture, ref occlusion
                        );
                    var assetPath = AssetDatabase.GetAssetPath(metallicTexture);
                    var ext       = Path.GetExtension(assetPath);
                    var id        = entry.SaveTexture(metalRoughTextureAo, hasTransparency, assetPath.Replace(ext, "-orm") + ext);
                    material.PbrMetallicRoughness.MetallicRoughnessTexture = new TextureInfo {
                        Index = id
                    };

                    if (occlusion != null)
                    {
                        material.OcclusionTexture = new OcclusionTextureInfo
                        {
                            Index    = id,
                            Strength = mat.GetFloat("_occlusionStrength")
                        };
                    }
                }

                material.PbrMetallicRoughness.MetallicFactor  = mat.GetFloat("_metallic");
                material.PbrMetallicRoughness.RoughnessFactor = mat.GetFloat("_roughness");
            }
            else
            {
                TextureInfo specGlossMap = null;
                TextureInfo diffuseMap   = null;
                var         diffuseColor = new GLTF.Math.Color();

                if (mat.GetTexture("_baseColorMap") != null)
                {
                    var id = entry.SaveTexture((Texture2D)mat.GetTexture("_baseColorMap"), hasTransparency);
                    diffuseMap = new TextureInfo {
                        Index = id
                    };
                }

                if (mat.GetColor("_baseColor") != null)
                {
                    Color c = mat.GetColor("_baseColor");
                    diffuseColor = Utils.ExportColor(c);
                }

                bool hasPBRMap = mat.GetTexture("_specularGlossinessMap") != null;

                if (hasPBRMap)
                {
                    specGlossMap = new TextureInfo {
                        Index = entry.SaveTexture((Texture2D)mat.GetTexture("_specularGlossinessMap"), true)
                    };
                }

                var specularFactor   = hasPBRMap ? Color.white : (Color)Utils.ExportColorVec4(mat.GetColor("_specular"));
                var glossinessFactor = hasPBRMap ? 1.0f : mat.GetFloat("_glossiness");

                if (material.Extensions == null)
                {
                    material.Extensions = new Dictionary <string, Extension>();
                }
                material.Extensions.Add(
                    "KHR_materials_pbrSpecularGlossiness",
                    new KHR_materials_pbrSpecularGlossinessExtension(
                        diffuseColor, diffuseMap,
                        new GLTF.Math.Vector3(specularFactor.r, specularFactor.g, specularFactor.b),
                        glossinessFactor, specGlossMap
                        )
                    );

                Texture2D occlusion = (Texture2D)mat.GetTexture("_occlusionMap");
                if (occlusion != null)
                {
                    material.OcclusionTexture = new OcclusionTextureInfo
                    {
                        Index    = entry.SaveTexture((Texture2D)mat.GetTexture("_occlusionMap"), false),
                        Strength = mat.GetFloat("_occlusionStrength")
                    };
                }
            }

            if (mat.GetTexture("_normalMap") != null)
            {
                material.NormalTexture = new NormalTextureInfo
                {
                    Index = entry.SaveTexture((Texture2D)mat.GetTexture("_normalMap"), false),
                };
            }

            if (mat.GetTexture("_emissionMap") != null)
            {
                material.EmissiveTexture = new TextureInfo
                {
                    Index = entry.SaveTexture((Texture2D)mat.GetTexture("_emissionMap"), false),
                };
            }

            var emissive = mat.GetColor("_emission");

            if (!emissive.Equals(new Color(0, 0, 0)))
            {
                material.EmissiveFactor = Utils.ExportColor(emissive);
            }

            if (mat.GetInt("envReflection") != (int)SeinPBRShaderGUI.EnvReflection.Off || (ExporterSettings.Lighting.ambient && (RenderSettings.ambientMode == UnityEngine.Rendering.AmbientMode.Skybox || RenderSettings.ambientMode == UnityEngine.Rendering.AmbientMode.Trilight)))
            {
                if (material.Extensions == null)
                {
                    material.Extensions = new Dictionary <string, Extension>();
                }
                ExtensionManager.Serialize(ExtensionManager.GetExtensionName(typeof(Sein_imageBasedLightingExtensionFactory)), entry, material.Extensions, mat);
            }

            return(material);
        }
Esempio n. 6
0
        public static UnityEngine.Color ToUnityColorLinear(this GLTF.Math.Color color)
        {
            var c = new UnityEngine.Color(color.R, color.G, color.B, color.A);

            return(c);
        }
Esempio n. 7
0
 public static Color ToUnityColorRaw(this GLTF.Math.Color color)
 {
     return(new Color(color.R, color.G, color.B, color.A));
 }