Skip to content

Commit

Permalink
Rework alpha function (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-HyperCake committed Mar 12, 2024
1 parent ec209f9 commit 51bea4a
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 77 deletions.
46 changes: 28 additions & 18 deletions Smash Forge/Filetypes/Application/MaterialXML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ private static void WriteMatAttributes(XmlDocument doc, Nud.Material mat, XmlNod
AddUintAttribute(doc, "flags", mat.Flags, matNode, true);
AddIntAttribute(doc, "srcFactor", mat.SrcFactor, matNode, false);
AddIntAttribute(doc, "dstFactor", mat.DstFactor, matNode, false);
AddIntAttribute(doc, "AlphaFunc", mat.AlphaFunction, matNode, false);
AddIntAttribute(doc, "AlphaTest", mat.AlphaTest, matNode, false);
AddIntAttribute(doc, "RefAlpha", mat.RefAlpha, matNode, false);
AddIntAttribute(doc, "cullmode", mat.CullMode, matNode, true);
AddIntAttribute(doc, "alphaFunc", mat.AlphaFunc, matNode, true);
AddIntAttribute(doc, "alphaRef", mat.RefAlpha, matNode, false);
AddIntAttribute(doc, "cullMode", mat.CullMode, matNode, true);
AddIntAttribute(doc, "zbuffoff", mat.ZBufferOffset, matNode, false);
}

Expand Down Expand Up @@ -114,13 +113,14 @@ private static void WriteMatParams(XmlDocument doc, Nud.Material mat, XmlNode ma
}
else
{
int count = 0;
foreach (float f in mat.GetPropertyValues(materialProperty))
float[] values = mat.GetPropertyValues(materialProperty);
// Only print 4 values, to avoid lots of trailing zeroes.
for (int count = 0, max = Math.Min(4, values.Length);;)
{
// Only print 4 values and avoids tons of trailing 0's.
if (count <= 4)
paramnode.InnerText += f.ToString() + " ";
count += 1;
paramnode.InnerText += values[count++].ToString("G9");
if (count >= max)
break;
paramnode.InnerText += " ";
}

}
Expand Down Expand Up @@ -252,7 +252,7 @@ private static void ReadMaterials(List<Nud.Material> materialList, XmlNode polyN

private static void ReadAttributes(XmlNode materialNode, Nud.Material material)
{
int value = 0;
int value;
foreach (XmlAttribute attribute in materialNode.Attributes)
{
switch (attribute.Name)
Expand All @@ -270,18 +270,18 @@ private static void ReadAttributes(XmlNode materialNode, Nud.Material material)
int.TryParse(attribute.Value, out value);
material.DstFactor = value;
break;
case "AlphaFunc":
int.TryParse(attribute.Value, out value);
material.AlphaFunction = value;
break;
case "AlphaTest":
int.TryParse(attribute.Value, out value);
material.AlphaTest = value;
case "alphaFunc":
int.TryParse(attribute.Value, NumberStyles.HexNumber, null, out value);
material.AlphaFunc = value;
break;
// "RefAlpha" was the old name, still supported for historical compatibility with old XMLs.
case "alphaRef":
case "RefAlpha":
int.TryParse(attribute.Value, out value);
material.RefAlpha = value;
break;
// "cullmode" was the old name, still supported
case "cullMode":
case "cullmode":
int.TryParse(attribute.Value, NumberStyles.HexNumber, null, out value);
material.CullMode = value;
Expand All @@ -290,6 +290,16 @@ private static void ReadAttributes(XmlNode materialNode, Nud.Material material)
int.TryParse(attribute.Value, out value);
material.ZBufferOffset = value;
break;

// No longer used, but still supported.
case "AlphaFunc":
int.TryParse(attribute.Value, out value);
material.AlphaFunction = value;
break;
case "AlphaTest":
int.TryParse(attribute.Value, out value);
material.AlphaTest = value;
break;
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions Smash Forge/Filetypes/Models/Nuds/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ public uint Flags
public int DstFactor { get; set; }
public int SrcFactor { get; set; }

public int AlphaTest { get; set; }
public int AlphaFunction { get; set; }
// AlphaTest and AlphaFunction are deprecated. Use AlphaFunc instead.
public int AlphaFunc = 0;
public int AlphaTest {
get { return AlphaFunc >> 8; }
set { AlphaFunc = value << 8; }
}
public int AlphaFunction {
get { return AlphaFunc & 0xFF; }
set { AlphaFunc = value & 0xFF; }
}
public int RefAlpha { get; set; }

public int CullMode { get; set; }
Expand Down
7 changes: 2 additions & 5 deletions Smash Forge/Filetypes/Models/Nuds/NUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,7 @@ public static List<Material> ReadMaterials(FileData d, PolyData p, int nameOffse
m.SrcFactor = d.ReadUShort();
ushort texCount = d.ReadUShort();
m.DstFactor = d.ReadUShort();
m.AlphaTest = d.ReadByte();
m.AlphaFunction = d.ReadByte();
m.AlphaFunc = d.ReadUShort();
m.RefAlpha = d.ReadUShort();
m.CullMode = d.ReadUShort();
d.Skip(4); // unknown
Expand Down Expand Up @@ -1559,9 +1558,7 @@ public static int[] WriteMaterial(FileOutput d, List<Material> materials, FileOu
d.WriteShort(mat.SrcFactor);
d.WriteShort(mat.textures.Count);
d.WriteShort(mat.DstFactor);
d.WriteByte(mat.AlphaTest);
d.WriteByte(mat.AlphaFunction);

d.WriteShort(mat.AlphaFunc);
d.WriteShort(mat.RefAlpha);
d.WriteShort(mat.CullMode);
d.WriteInt(0); // unknown
Expand Down
28 changes: 19 additions & 9 deletions Smash Forge/Filetypes/Models/Nuds/NudEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ namespace SmashForge.Filetypes.Models.Nuds
{
public static class NudEnums
{
public enum AlphaTest
{
Enabled = 0x02,
Disabled = 0x00
}

public static readonly Dictionary<int, BlendingFactor> srcFactorByMatValue = new Dictionary<int, BlendingFactor>()
{
{ 0x00, BlendingFactor.One },
Expand Down Expand Up @@ -59,9 +53,25 @@ public enum AlphaTest

public static readonly Dictionary<int, AlphaFunction> alphaFunctionByMatValue = new Dictionary<int, AlphaFunction>()
{
{ 0x0, AlphaFunction.Never },
{ 0x4, AlphaFunction.Gequal },
{ 0x6, AlphaFunction.Gequal },
// OpenGL (used in NDP3 games like Smash)
{ 0x200, AlphaFunction.Never },
{ 0x201, AlphaFunction.Less },
{ 0x202, AlphaFunction.Equal },
{ 0x203, AlphaFunction.Lequal },
{ 0x204, AlphaFunction.Greater },
{ 0x205, AlphaFunction.Notequal },
{ 0x206, AlphaFunction.Gequal },
{ 0x207, AlphaFunction.Always },

// Direct3D (used in NDWD games like Pokken)
{ 1, AlphaFunction.Never },
{ 2, AlphaFunction.Less },
{ 3, AlphaFunction.Equal },
{ 4, AlphaFunction.Lequal },
{ 5, AlphaFunction.Greater },
{ 6, AlphaFunction.Notequal },
{ 7, AlphaFunction.Gequal },
{ 8, AlphaFunction.Always }
};

public static readonly Dictionary<int, TextureWrapMode> wrapModeByMatValue = new Dictionary<int, TextureWrapMode>()
Expand Down
10 changes: 4 additions & 6 deletions Smash Forge/Filetypes/Models/Nuds/NudRenderMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,13 @@ private void SetFaceCulling(Nud.Material material)

private void SetAlphaTesting(Nud.Material material)
{

bool enabled = (material.AlphaTest == (int)NudEnums.AlphaTest.Enabled);

AlphaFunction alphaFunc = AlphaFunction.Always;
if (NudEnums.alphaFunctionByMatValue.ContainsKey(material.AlphaFunction))
alphaFunc = NudEnums.alphaFunctionByMatValue[material.AlphaFunction];

float refAlpha = material.RefAlpha / 255.0f;

bool enabled = NudEnums.alphaFunctionByMatValue.ContainsKey(material.AlphaFunc);
if (enabled)
alphaFunc = NudEnums.alphaFunctionByMatValue[material.AlphaFunc];

renderSettings.alphaTestSettings = new AlphaTestSettings(enabled, alphaFunc, refAlpha);
}

Expand Down
14 changes: 0 additions & 14 deletions Smash Forge/GUI/Editors/NUDMaterialEditor.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 20 additions & 23 deletions Smash Forge/GUI/Editors/NUDMaterialEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,24 @@ public partial class NudMaterialEditor : DockContent

public static Dictionary<int, string> alphaFuncByMatValue = new Dictionary<int, string>()
{
{ 0x00, "Never"},
{ 0x04, "Gequal Ref Alpha"},
{ 0x06, "Gequal Ref Alpha + ???"}
{ 0x000, "None (Always)" },
{ 0x200, "Never" },
{ 0x201, "Less (<)" },
{ 0x202, "Equal (==)" },
{ 0x203, "Less or equal (<=)" },
{ 0x204, "Greater (>)" },
{ 0x205, "Not equal (!=)" },
{ 0x206, "Greater or equal (>=)" },
{ 0x207, "Always" },
// Direct3D
{ 1, "(Pokkén) Never" },
{ 2, "(Pokkén) Less (<)" },
{ 3, "(Pokkén) Equal (==)" },
{ 4, "(Pokkén) Less or equal (<=)" },
{ 5, "(Pokkén) Greater (>)" },
{ 6, "(Pokkén) Not equal (!=)" },
{ 7, "(Pokkén) Greater or equal (>=)" },
{ 8, "(Pokkén) Always" }
};

public static Dictionary<int, string> mapModeByMatValue = new Dictionary<int, string>()
Expand Down Expand Up @@ -263,18 +278,14 @@ public void FillForm()

private void InitializeComboBoxes(Nud.Material mat)
{
alphaFuncComboBox.SelectedItem = alphaFuncByMatValue[mat.AlphaFunction];
alphaFuncComboBox.SelectedItem = alphaFuncByMatValue[mat.AlphaFunc];
cullModeComboBox.SelectedItem = cullModeByMatValue[mat.CullMode];
}

private void InitializeCheckBoxes(Nud.Material mat)
{
shadowCB.Checked = mat.HasShadow;
GlowCB.Checked = mat.Glow;

alphaTestCB.Checked = mat.AlphaTest == (int)NudEnums.AlphaTest.Enabled;
// Enable/disable extra controls.
alphaTestCB_CheckedChanged(null, null);
}

private void InitializeTextBoxes(Nud.Material mat)
Expand Down Expand Up @@ -377,7 +388,7 @@ private void AlphaFuncCB_SelectedIndexChanged(object sender, EventArgs e)
{
if (alphaFuncByMatValue[i].Equals(alphaFuncComboBox.SelectedItem))
{
currentMaterialList[currentMatIndex].AlphaFunction = i;
currentMaterialList[currentMatIndex].AlphaFunc = i;
break;
}
}
Expand Down Expand Up @@ -1024,20 +1035,6 @@ private void param4TrackBar_Leave(object sender, EventArgs e)
enableParam4SliderUpdates = true;
}

private void alphaTestCB_CheckedChanged(object sender, EventArgs e)
{
if (alphaTestCB.Checked)
currentMaterialList[currentMatIndex].AlphaTest = (int)NudEnums.AlphaTest.Enabled;
else
currentMaterialList[currentMatIndex].AlphaTest = (int)NudEnums.AlphaTest.Disabled;

// Only enable extra settings when alpha testing is enabled.
alphaFuncRefPanel.Visible = alphaTestCB.Checked;

// Force all flow layouts to rescale children.
GuiTools.ScaleControlsHorizontallyToLayoutWidth(generalFlowLayout);
}

private void flagsButton_Click(object sender, EventArgs e)
{
flagsPanel.Visible = !flagsPanel.Visible;
Expand Down

0 comments on commit 51bea4a

Please sign in to comment.