Esempio n. 1
0
        private void ExportSpecularGlossiness(AssetContext asset, SpecularGlossinessShaderArguments arguments)
        {
            using (var writer = asset.DestinationFolder.CreateXml(asset.UrhoAssetName))
            {
                if (writer == null)
                {
                    return;
                }
                writer.WriteStartDocument();
                writer.WriteWhitespace(Environment.NewLine);
                writer.WriteStartElement("material");
                writer.WriteWhitespace(Environment.NewLine);

                if (arguments.Diffuse != null)
                {
                    // Albedo
                    if (arguments.PBRSpecular != null)
                    {
                        // Albedo, MetallicGloss
                        if (arguments.Bump != null)
                        {
                            // Albedo, MetallicGloss, Normal
                            if (arguments.Emission)
                            {
                                // Albedo, MetallicGloss, Normal, Emission
                                if (arguments.Transparent)
                                {
                                    WriteTechnique(writer,
                                                   "Techniques/PBR/PBRMetallicRoughDiffNormalSpecEmissiveAlpha.xml");
                                }
                                else
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRMetallicRoughDiffNormalSpecEmissive.xml");
                                }
                                WriteTexture(arguments.Emission, writer, "emissive");
                            }
                            else
                            {
                                // Albedo, MetallicGloss, Normal, No Emission
                                if (arguments.Transparent)
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRMetallicRoughDiffNormalSpecAlpha.xml");
                                }
                                else
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRMetallicRoughDiffNormalSpec.xml");
                                }
                            }

                            WriteTexture(arguments.Bump, writer, "normal");
                        }
                        else
                        {
                            // Albedo, MetallicGloss, No Normal
                            if (arguments.Transparent)
                            {
                                WriteTechnique(writer, "Techniques/PBR/PBRMetallicRoughDiffSpecAlpha.xml");
                            }
                            else
                            {
                                WriteTechnique(writer, "Techniques/PBR/PBRMetallicRoughDiffSpec.xml");
                            }
                        }

                        if (_assets.TryGetTexturePath(arguments.PBRSpecular, out var baseAssetName))
                        {
                            var textureReferences = new TextureReferences(TextureSemantic.PBRSpecularGlossiness, 1.0f,
                                                                          arguments.SmoothnessTextureChannel == SmoothnessTextureChannel.AlbedoAlpha
                                    ? arguments.Diffuse
                                    : arguments.PBRSpecular, arguments.SmoothnessTextureChannel);
                            var textureOutputName =
                                TextureExporter.GetTextureOutputName(baseAssetName, textureReferences);
                            WriteTexture(textureOutputName, writer, "specular");
                        }
                    }
                    else
                    {
                        // Albedo, No MetallicGloss
                        if (arguments.Bump != null)
                        {
                            // Albedo, No MetallicGloss, Normal
                            if (arguments.Emission != null)
                            {
                                // Albedo, No MetallicGloss, Normal, Emission
                                if (arguments.Transparent)
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRDiffNormalEmissiveAlpha.xml");
                                }
                                else
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRDiffNormalEmissive.xml");
                                }
                                WriteTexture(arguments.Emission, writer, "emissive");
                            }
                            else
                            {
                                // Albedo, No MetallicGloss, Normal, No Emission
                                if (arguments.Transparent)
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRDiffNormalAlpha.xml");
                                }
                                else
                                {
                                    WriteTechnique(writer, "Techniques/PBR/PBRDiffNormal.xml");
                                }
                            }

                            WriteTexture(arguments.Bump, writer, "normal");
                        }
                        else
                        {
                            // Albedo, No MetallicGloss, No Normal
                            if (arguments.Transparent)
                            {
                                WriteTechnique(writer, "Techniques/PBR/PBRDiffAlpha.xml");
                            }
                            else
                            {
                                WriteTechnique(writer, "Techniques/PBR/PBRDiff.xml");
                            }
                        }
                    }

                    if (_assets.TryGetTexturePath(arguments.Diffuse, out var diffuseName))
                    {
                        var textureReferences = new TextureReferences(TextureSemantic.PBRDiffuse, 1.0f,
                                                                      arguments.PBRSpecular, arguments.SmoothnessTextureChannel);
                        var textureOutputName = TextureExporter.GetTextureOutputName(diffuseName, textureReferences);
                        WriteTexture(textureOutputName, writer, "diffuse");
                    }
                }
                else
                {
                    // No albedo
                    if (arguments.Transparent)
                    {
                        WriteTechnique(writer, "Techniques/PBR/PBRNoTextureAlpha.xml");
                    }
                    else
                    {
                        WriteTechnique(writer, "Techniques/PBR/PBRNoTexture.xml");
                    }
                }

                WriteParameter(writer, "MatDiffColor", BaseNodeExporter.Format(arguments.DiffuseColor));
                if (arguments.HasEmission)
                {
                    WriteParameter(writer, "MatEmissiveColor", BaseNodeExporter.FormatRGB(arguments.EmissiveColor));
                }
                WriteParameter(writer, "MatEnvMapColor", BaseNodeExporter.FormatRGB(Color.white));
                WriteParameter(writer, "MatSpecColor", BaseNodeExporter.Format(Vector4.zero));
                if (arguments.PBRSpecular != null)
                {
                    WriteParameter(writer, "Roughness", BaseNodeExporter.Format(0));
                    WriteParameter(writer, "Metallic", BaseNodeExporter.Format(0));
                }
                else
                {
                    ////TODO: Evaluate correct parameters:
                    WriteParameter(writer, "Roughness", BaseNodeExporter.Format(0.0f));
                    WriteParameter(writer, "Metallic", BaseNodeExporter.Format(0.0f));
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
Esempio n. 2
0
        private void ExportStandartMaterial(Material material, XmlWriter writer)
        {
            {
                var matEmissionEnabled = material.IsKeywordEnabled("_EMISSION");
                var matDiffColor       = Color.white;
                var matSpecColor       = Color.black;
                var matEmissiveColor   = Color.white;
                var flags = new MaterialFlags();
                flags.hasAlpha    = material.renderQueue == (int)RenderQueue.Transparent;
                flags.hasEmissive = matEmissionEnabled;
                var shader = material.shader;
                for (var i = 0; i < ShaderUtil.GetPropertyCount(shader); i++)
                {
                    var propertyName = ShaderUtil.GetPropertyName(shader, i);
                    var propertyType = ShaderUtil.GetPropertyType(shader, i);
                    if (propertyType == ShaderUtil.ShaderPropertyType.TexEnv)
                    {
                        var texture = material.GetTexture(propertyName);
                        if (texture != null)
                        {
                            switch (propertyName)
                            {
                            case "_Diffuse":
                            case "_Texture":
                            case "_MainTex":
                                flags.hasDiffuse = WriteTexture(texture, writer, "diffuse");
                                break;

                            case "_SpecGlossMap":
                                flags.hasSpecular = WriteTexture(texture, writer, "specular");
                                break;

                            case "_ParallaxMap":
                                break;

                            case "_Normal":
                            case "_BumpMap":
                                flags.hasNormal = WriteTexture(texture, writer, "normal");
                                break;

                            case "_DetailAlbedoMap":
                                break;

                            case "_DetailNormalMap":
                                break;

                            case "_EmissionMap":
                            case "_Emission":
                                flags.hasEmissive &= WriteTexture(texture, writer, "emissive");
                                break;

                            case "_MetallicGlossMap":
                                break;

                            case "_OcclusionMap":
                                break;

                            case "_Overlay":
                                break;

                            case "_Mask":
                                break;

                            case "_DetailMask":
                                break;

                            default:
                                Debug.LogWarning(propertyName);
                                break;
                            }
                        }
                    }
                    else if (propertyType == ShaderUtil.ShaderPropertyType.Color)
                    {
                        var color = material.GetColor(propertyName);
                        switch (propertyName)
                        {
                        case "_FresnelColor":
                            break;

                        case "_MainColor":
                        case "_Color":
                            matDiffColor = color;
                            break;

                        case "_EmissionColor":
                            matEmissiveColor = color;
                            break;

                        case "_SpecColor":
                            matSpecColor = color;
                            break;

                        default:
                            Debug.LogWarning(propertyName);
                            break;
                        }
                    }
                    else if (propertyType == ShaderUtil.ShaderPropertyType.Range)
                    {
                        var value = material.GetFloat(propertyName);
                        switch (propertyName)
                        {
                        case "_Cutoff":
                        case "_Glossiness":
                        case "_GlossMapScale":
                        case "_Parallax":
                        case "_OcclusionStrength":
                        case "_Specular":
                        case "_Gloss":
                        case "_FresnelPower":
                        case "_FresnelExp":
                        case "_Alpha_2":
                        case "_RefractionPower":
                        case "_Metallic":
                            break;

                        case "_Alpha_1":
                            matDiffColor.a = value;
                            break;

                        default:
                            Debug.LogWarning(propertyName);
                            break;
                        }
                    }
                    else if (propertyType == ShaderUtil.ShaderPropertyType.Float)
                    {
                        var value = material.GetFloat(propertyName);
                        switch (propertyName)
                        {
                        case "_SmoothnessTextureChannel":
                        case "_SpecularHighlights":
                        case "_GlossyReflections":
                        case "_BumpScale":
                        case "_DetailNormalMapScale":
                        case "_UVSec":
                        case "_Mode":
                        case "_SrcBlend":
                        case "_DstBlend":
                        case "_ZWrite":
                            break;

                        default:
                            Debug.LogWarning(propertyName);
                            break;
                        }
                    }
                    //else
                    //{
                    //    Debug.LogWarning(propertyName+" of unsupported type "+propertyType);
                    //}
                }
                if (!flags.hasDiffuse)
                {
                    WriteParameter(writer, "\t", "MatDiffColor", BaseNodeExporter.Format(matDiffColor));
                }
                if (!flags.hasSpecular)
                {
                    WriteParameter(writer, "\t", "MatSpecColor", BaseNodeExporter.Format(matSpecColor));
                }
                if (matEmissionEnabled)
                {
                    WriteParameter(writer, "\t", "MatEmissiveColor", BaseNodeExporter.Format(matEmissiveColor));
                }

                writer.WriteWhitespace(Environment.NewLine);
                writer.WriteStartElement("technique");
                var bestTechnique         = Techniques[0];
                var bestTechniqueDistance = bestTechnique.Material - flags;
                foreach (var technique in Techniques)
                {
                    if (technique.Material.Fits(flags))
                    {
                        var d = technique.Material - flags;
                        if (d < bestTechniqueDistance)
                        {
                            bestTechnique         = technique;
                            bestTechniqueDistance = d;
                        }
                    }
                }
                writer.WriteAttributeString("name", "Techniques/" + bestTechnique.Name);
                writer.WriteAttributeString("quality", "0");
                writer.WriteEndElement();
                writer.WriteWhitespace(Environment.NewLine);
            }
        }
Esempio n. 3
0
        private void ExportLegacy(AssetContext asset, LegacyShaderArguments arguments)
        {
            using (var writer = asset.DestinationFolder.CreateXml(asset.UrhoAssetName))
            {
                if (writer == null)
                {
                    return;
                }
                writer.WriteStartDocument();
                writer.WriteWhitespace(Environment.NewLine);

                var flags = new LegacyTechniqueFlags();
                flags.hasAlpha    = arguments.Transparent;
                flags.hasDiffuse  = arguments.Diffuse != null;
                flags.hasEmissive = arguments.Emission != null;
                flags.hasNormal   = arguments.Bump != null;
                flags.hasSpecular = arguments.Specular != null;
                writer.WriteStartElement("material");
                writer.WriteWhitespace(Environment.NewLine);
                {
                    var bestTechnique         = Techniques[0];
                    var bestTechniqueDistance = bestTechnique.Material - flags;
                    foreach (var technique in Techniques)
                    {
                        if (technique.Material.Fits(flags))
                        {
                            var d = technique.Material - flags;
                            if (d < bestTechniqueDistance)
                            {
                                bestTechnique         = technique;
                                bestTechniqueDistance = d;
                            }
                        }
                    }

                    WriteTechnique(writer, "Techniques/" + bestTechnique.Name);
                }
                if (arguments.Diffuse != null)
                {
                    WriteTexture(arguments.Diffuse, writer, "diffuse");
                }
                if (arguments.Specular != null)
                {
                    WriteTexture(arguments.Specular, writer, "specular");
                }
                if (arguments.Bump != null)
                {
                    WriteTexture(arguments.Bump, writer, "normal");
                }
                if (arguments.Emission != null)
                {
                    WriteTexture(arguments.Bump, writer, "emissive");
                }
                WriteParameter(writer, "MatDiffColor", BaseNodeExporter.Format(arguments.DiffColor));
                if (arguments.HasEmission)
                {
                    WriteParameter(writer, "MatEmissiveColor", BaseNodeExporter.FormatRGB(arguments.EmissiveColor));
                }
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }