private void CreateMaterial(Material material, int count)
        {
            PmxMaterial pmxMaterial = new PmxMaterial();

            pmxMaterial.Name  = material.name;
            pmxMaterial.NameE = material.name;
            pmxMaterial.Flags = (PmxMaterial.MaterialFlags.DrawBoth | PmxMaterial.MaterialFlags.Shadow | PmxMaterial.MaterialFlags.SelfShadowMap | PmxMaterial.MaterialFlags.SelfShadow);
            if (material.HasProperty("_MainTex"))
            {
                string textureName = material.name;
                if (textureName.Contains("Instance"))
                {
                    textureName = material.name + "_(" + material.GetInstanceID() + ")";
                }
                pmxMaterial.Tex = textureName + ".png";
                Texture mainTexture = material.GetTexture("_MainTex");
                if (mainTexture == null)
                {
                    mainTexture = material.mainTexture;
                }
                if (mainTexture != null && this.SaveTexture)
                {
                    Debug.Log($"Generate Material: {material.name} {mainTexture.name}");
                    TextureBuilder.WriteTextureToFile(Path.Combine(this.ExportFolder, pmxMaterial.Tex), mainTexture);
                }
            }
            if (material.HasProperty("_AmbColor"))
            {
                pmxMaterial.Ambient = new PmxLib.Vector3(material.GetColor("_AmbColor"));
            }
            if (material.HasProperty("_Color"))
            {
                pmxMaterial.Diffuse = new PmxLib.Vector4(material.GetColor("_Color"));
            }
            if (material.HasProperty("_Opacity"))
            {
                pmxMaterial.Diffuse.Alpha = material.GetFloat("_Opacity");
            }
            if (material.HasProperty("_SpecularColor"))
            {
                pmxMaterial.Specular = new PmxLib.Vector3(material.GetColor("_SpecularColor"));
            }
            if (material.HasProperty("_Shininess"))
            {
                pmxMaterial.Power = material.GetFloat("_Shininess");
            }
            if (material.HasProperty("_OutlineColor"))
            {
                pmxMaterial.EdgeSize  = material.GetFloat("_OutlineWidth");
                pmxMaterial.EdgeColor = new PmxLib.Vector4(material.GetColor("_OutlineColor"));
            }
            pmxMaterial.FaceCount = count;
            this.pmxFile.MaterialList.Add(pmxMaterial);
        }
        private string GenerateMaterial(StringBuilder matOutput, List <string> matNameCache, Material material)
        {
            Debug.Log($"Generating material: {material.name}");

            string matRef = material.name;

            if (matRef.Contains("Instance"))
            {
                matRef += "_(" + material.GetInstanceID() + ")";
            }

            if (!matNameCache.Contains(matRef))
            {
                matNameCache.Add(matRef);
                matOutput.AppendLine("newmtl " + matRef);

                if (material.HasProperty("_Color"))
                {
                    Color color = material.color;
                    matOutput.AppendLine("Kd " + color.r + " " + color.g + " " + color.b);
                    float num = Mathf.Lerp(1f, 0f, color.a);
                    matOutput.AppendLine("d " + num);
                }

                if (material.mainTexture != null)
                {
                    Texture mainTex = this.GetMainTex(material);
                    if (mainTex != null)
                    {
                        if (mainTex.wrapMode == TextureWrapMode.Clamp)
                        {
                            matOutput.AppendLine("-clamp on");
                        }

                        Vector2 textureScale = material.GetTextureScale("_MainTex");
                        matOutput.AppendLine("s " + textureScale.x + " " + textureScale.y);
                        matOutput.AppendLine("map_Kd " + matRef + "d.png");
                        if (this.SaveTexture)
                        {
                            TextureBuilder.WriteTextureToFile(Path.Combine(this.ExportFolder, matRef + "d.png"), mainTex);
                        }

                        Texture2D shadowTex = this.GetShadowTex(material);
                        if (shadowTex != null)
                        {
                            matOutput.AppendLine("map_Ka " + matRef + "a.png");
                            if (this.SaveTexture)
                            {
                                TextureBuilder.WriteTextureToFile(Path.Combine(this.ExportFolder, matRef + "a.png"), shadowTex);
                            }
                        }
                    }
                    else
                    {
                        Debug.LogWarning("No texture found for " + matRef);
                    }
                }
                else
                {
                    Debug.Log("No Texture for " + matRef);
                }

                matOutput.AppendLine();
            }

            return(matRef);
        }
 private Texture2D GetShadowTex(Material material)
 {
     if (material.HasProperty("_ShadowTex"))
     {
         Texture texture = material.GetTexture("_ShadowTex");
         return((texture.GetType() != typeof(RenderTexture)) ? (texture as Texture2D) : TextureBuilder.ConvertToTexture2D(texture as RenderTexture));
     }
     return(null);
 }