AcTools.Kn5File.Kn5.ExportCollada_MaterialEffect C# (CSharp) Method

ExportCollada_MaterialEffect() private method

private ExportCollada_MaterialEffect ( XmlWriter xml, Kn5Material material ) : void
xml XmlWriter
material Kn5Material
return void
        private void ExportCollada_MaterialEffect(XmlWriter xml, Kn5Material material) {
            xml.WriteStartElement("effect");
            xml.WriteAttributeStringSafe("id", $"{material.Name}-effect");
            xml.WriteStartElement("profile_COMMON");
            xml.WriteStartElement("technique");
            xml.WriteAttributeString("sid", "common");
            xml.WriteStartElement("phong");
            
            xml.WriteStartElement("emission");
            xml.WriteStartElement("color");
            xml.WriteAttributeString("sid", "emission");
            var ksEmissive = material.GetPropertyByName("ksEmissive");
            xml.WriteString(ksEmissive == null ? "0 0 0 1" :
                    $"{ksEmissive.ValueC[0].ToString(CultureInfo.InvariantCulture)} {ksEmissive.ValueC[1].ToString(CultureInfo.InvariantCulture)} {ksEmissive.ValueC[2].ToString(CultureInfo.InvariantCulture)} 1");
            xml.WriteEndElement();
            xml.WriteEndElement();
            
            xml.WriteStartElement("ambient");
            xml.WriteStartElement("color");
            xml.WriteAttributeString("sid", "ambient");
            var ksAmbient = material.GetPropertyByName("ksAmbient");
            xml.WriteString(ksAmbient == null ? "0.4 0.4 0.4 1" : string.Format("{0} {0} {0} 1", ksAmbient.ValueA.ToString(CultureInfo.InvariantCulture)));
            xml.WriteEndElement();
            xml.WriteEndElement();
            
            xml.WriteStartElement("diffuse");
            var txDiffuse = material.GetMappingByName("txDiffuse");
            if (txDiffuse != null) {
                ExportCollada_MaterialEffectTexture(xml, txDiffuse);
            }

            xml.WriteStartElement("color");
            xml.WriteAttributeString("sid", "diffuse");
            var ksDiffuse = material.GetPropertyByName("ksDiffuse");
            xml.WriteString(ksDiffuse == null ? "0.6 0.6 0.6 1" : string.Format("{0} {0} {0} 1", ksDiffuse.ValueA.ToString(CultureInfo.InvariantCulture)));
            xml.WriteEndElement();
            xml.WriteEndElement();
            
            xml.WriteStartElement("specular");
            xml.WriteStartElement("color");
            xml.WriteAttributeString("sid", "specular");
            var ksSpecular = material.GetPropertyByName("ksSpecular");
            xml.WriteString(ksSpecular == null ? "0.5 0.5 0.5 1" : string.Format("{0} {0} {0} 1", ksSpecular.ValueA.ToString(CultureInfo.InvariantCulture)));
            xml.WriteEndElement();
            xml.WriteEndElement();
            
            xml.WriteStartElement("shininess");
            xml.WriteStartElement("float");
            xml.WriteAttributeString("sid", "shininess");
            xml.WriteString(material.GetPropertyByName("ksSpecularEXP")?.ValueA ?? 50);
            xml.WriteEndElement();
            xml.WriteEndElement();
            
            xml.WriteStartElement("index_of_refraction");
            xml.WriteStartElement("float");
            xml.WriteAttributeString("sid", "index_of_refraction");
            xml.WriteString(1f);
            xml.WriteEndElement();
            xml.WriteEndElement();

            if (material.BlendMode == Kn5MaterialBlendMode.AlphaBlend) {
                xml.WriteStartElement("transparent");
                xml.WriteAttributeString("opaque", "RGB_ZERO");

                if (material.ShaderName == "ksPerPixelNM" || material.ShaderName == "ksPerPixelNM_UV2" ||
                        material.ShaderName == "ksPerPixelMultiMap_AT" || material.ShaderName == "ksPerPixelMultiMap_AT_NMDetail") {
                    var txNormal = material.GetMappingByName("txNormal");
                    if (txNormal != null) {
                        ExportCollada_MaterialEffectTexture(xml, txNormal);
                    }
                } else if (txDiffuse != null) {
                    ExportCollada_MaterialEffectTexture(xml, txDiffuse);
                }
                
                xml.WriteEndElement(); // transparent
            }

            var alpha = 1.0f;
            switch (material.ShaderName) {
                case "ksBrokenGlass":
                    alpha = 0f;
                    break;
                case "ksPerPixelAlpha":
                    alpha = material.GetPropertyByName("alpha")?.ValueA ?? 1f;
                    break;
            }

            if (!Equals(alpha, 1f)) {
                xml.WriteStartElement("transparency");
                xml.WriteStartElement("float");
                xml.WriteAttributeString("sid", "transparency");
                xml.WriteString(alpha);
                xml.WriteEndElement(); // float
                xml.WriteEndElement(); // transparency
            }

            xml.WriteEndElement();
            xml.WriteEndElement();
            xml.WriteEndElement();
            xml.WriteEndElement();
        }