Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gleblebedev/c01fa7c9a1c5bff273c9cbd3020d3b89 to your computer and use it in GitHub Desktop.
Save gleblebedev/c01fa7c9a1c5bff273c9cbd3020d3b89 to your computer and use it in GitHub Desktop.
Urho3D PBR techniques generator
void Main()
{
string targetDir = @"C:\Urho3D\Urho3D\bin\CoreData\Techniques\PBR\";
foreach (var p in GetPermutations())
{
var name = new StringBuilder();
name.Append("PBR");
if (p.MetallicRough) name.Append("MetallicRough");
if (p.Diff) name.Append("Diff");
if (p.Normal) name.Append("Normal");
if (p.MetallicRough) name.Append("Spec");
if (p.Emissive) name.Append("Emissive");
if (p.AO) name.Append("AO");
var noTextures = !p.MetallicRough && !p.AO && !p.Diff && !p.Emissive && !p.Normal;
if (noTextures) name.Append("NoTexture");
if (p.Alpha) name.Append("Alpha");
name.Append(".xml");
var doc = new XDocument();
var vsdefines = new List<string>();
var psdefines = new List<string>();
if (noTextures)
vsdefines.Add("NOUV");
if (p.Diff)
psdefines.Add("DIFFMAP");
if (p.Normal)
{
vsdefines.Add("NORMALMAP");
psdefines.Add("NORMALMAP");
}
if (p.Emissive)
psdefines.Add("EMISSIVEMAP");
if (p.AO)
{
vsdefines.Add("AO");
psdefines.Add("AO");
}
psdefines.Add("PBR");
psdefines.Add("IBL");
if (p.MetallicRough)
{
psdefines.Add("METALLIC");
psdefines.Add("ROUGHNESS");
}
var technique = new XElement("technique", new XAttribute("vs", "PBRLitSolid"), new XAttribute("ps", "PBRLitSolid"));
if (vsdefines.Count > 0)
{
technique.Add(new XAttribute("vsdefines", string.Join(" ", vsdefines)));
}
if (psdefines.Count > 0)
{
technique.Add(new XAttribute("psdefines", string.Join(" ", psdefines)));
}
var indent = Environment.NewLine+" ";
if (p.Alpha)
{
technique.Add(indent);
technique.Add(new XElement("pass", new XAttribute("name", "alpha"), new XAttribute("depthwrite", "false"), new XAttribute("blend", "alpha")));
technique.Add(indent);
technique.Add(new XElement("pass", new XAttribute("name", "litalpha"), new XAttribute("depthwrite", "false"), new XAttribute("blend", "addalpha")));
technique.Add(indent);
var shadow = new XElement("pass", new XAttribute("name", "shadow"), new XAttribute("vs", "Shadow"), new XAttribute("ps", "Shadow"));
if (p.Normal) shadow.Add(new XAttribute("psexcludes", "PACKEDNORMAL"));
technique.Add(shadow);
}
else
{
technique.Add(indent);
technique.Add(new XElement("pass", new XAttribute("name", "base")));
technique.Add(indent);
technique.Add(new XElement("pass", new XAttribute("name", "light"), new XAttribute("depthtest", "equal"), new XAttribute("depthwrite", "false"), new XAttribute("blend", "add")));
//technique.Add(indent);
//technique.Add(new XElement("pass", new XAttribute("name", "prepass"), new XAttribute("psdefines", "PREPASS")));
technique.Add(indent);
technique.Add(new XElement("pass", new XAttribute("name", "material"), new XAttribute("psdefines", "MATERIAL"), new XAttribute("depthtest", "equal"), new XAttribute("depthwrite", "false")));
technique.Add(indent);
technique.Add(new XElement("pass", new XAttribute("name", "deferred"), new XAttribute("psdefines", "DEFERRED"), new XAttribute("blend", "add")));
technique.Add(indent);
var depth = new XElement("pass", new XAttribute("name", "depth"), new XAttribute("vs", "Depth"), new XAttribute("ps", "Depth"));
if (p.Normal) depth.Add(new XAttribute("psexcludes", "PACKEDNORMAL"));
technique.Add(depth);
technique.Add(indent);
var shadow = new XElement("pass", new XAttribute("name", "shadow"), new XAttribute("vs", "Shadow"), new XAttribute("ps", "Shadow"));
if (p.Normal) shadow.Add(new XAttribute("psexcludes", "PACKEDNORMAL"));
technique.Add(shadow);
}
technique.Add(Environment.NewLine);
doc.Add(technique);
using (var stream = File.Open(Path.Combine(targetDir, name.ToString()), FileMode.Create, FileAccess.Write, FileShare.Read))
{
var buf = new StringBuilder();
using (var writer = new StringWriter(buf))
{
doc.Save(writer, SaveOptions.DisableFormatting);
}
var xml = buf.ToString().Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>","");
var bytes = new UTF8Encoding(false).GetBytes(xml);
stream.Write(bytes, 0, bytes.Length);
}
}
}
IEnumerable<Permutation> GetPermutations()
{
foreach (var mr in GetBoolPermutations())
{
foreach (var d in GetBoolPermutations())
{
foreach (var n in GetBoolPermutations())
{
foreach (var a in GetBoolPermutations())
{
var p = new Permutation()
{
MetallicRough = mr,
Diff = d,
Normal = n,
Alpha = a
};
yield return p;
p.Emissive = true;
yield return p;
p.Emissive = false;
p.AO = true;
yield return p;
}
}
}
}
}
IEnumerable<bool> GetBoolPermutations()
{
yield return false;
yield return true;
}
public class Permutation
{
public bool MetallicRough;
public bool Diff;
public bool Normal;
public bool Emissive;
public bool AO;
public bool Alpha;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment