Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/ShaderRipper.Tests/ShaderLabTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;
using ShaderRipper.Data;
using ShaderRipper.Emit;
using ShaderRipper.Game;

namespace ShaderRipper.Tests;

Expand Down Expand Up @@ -339,3 +340,58 @@ public void FoldDoesNotConsumeADeclarationItCannotProveIsAMatrix()
}
}

/// <summary>
/// Unity's <c>ColorWriteMask</c> packs bits as Alpha=1, Blue=2, Green=4, Red=8.
/// </summary>
[TestFixture]
public class ColorMaskTests
{
[TestCase(0f, "ColorMask 0")]
[TestCase(1f, "ColorMask A")]
[TestCase(2f, "ColorMask B")]
[TestCase(4f, "ColorMask G")]
[TestCase(8f, "ColorMask R")]
[TestCase(14f, "ColorMask RGB")]
[TestCase(12f, "ColorMask RG")]
[TestCase(10f, "ColorMask RB")]
[TestCase(9f, "ColorMask RA")]
public void DescribeRenderStateDecodesUnityColorWriteMaskBits(float colMask, string expectedLine)
{
var pass = new Pass
{
Blends = { new BlendState { ColMask = colMask } },
};

var states = GameExport.DescribeRenderState(pass);

Assert.That(states, Does.Contain(expectedLine));
}

[Test]
public void DescribeRenderStateOmitsDefaultFullColorMask()
{
var pass = new Pass
{
Blends = { new BlendState { ColMask = 15f } },
};

var states = GameExport.DescribeRenderState(pass);

Assert.That(states.Any(s => s.StartsWith("ColorMask")), Is.False);
}

[Test]
public void DescribeRenderStatePreservesPropertyDrivenColorMask()
{
var pass = new Pass
{
Blends = { new BlendState { ColMask = 15f, ColMaskName = "_ColorMask" } },
};

var states = GameExport.DescribeRenderState(pass);

Assert.That(states, Does.Contain("ColorMask [_ColorMask]"));
}
}


10 changes: 5 additions & 5 deletions src/ShaderRipper/Game/GameExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2849,17 +2849,17 @@ private static string N(float f) =>
private static string Named(string propertyName, string literal) =>
string.IsNullOrEmpty(propertyName) ? literal : $"[{propertyName}]";

/// <summary><c>ColorMask</c> letters for a four-bit mask (1 = R, 2 = G, 4 = B, 8 = A).</summary>
/// <summary><c>ColorMask</c> letters for a four-bit mask (1 = A, 2 = B, 4 = G, 8 = R, matching Unity's ColorWriteMask).</summary>
private static string ColourMaskLetters(float mask)
{
int m = (int)mask;
if (m == 0) return "0";
if (m == 15) return "RGBA";
var sb = new System.Text.StringBuilder(4);
if ((m & 1) != 0) sb.Append('R');
if ((m & 2) != 0) sb.Append('G');
if ((m & 4) != 0) sb.Append('B');
if ((m & 8) != 0) sb.Append('A');
if ((m & 8) != 0) sb.Append('R');
if ((m & 4) != 0) sb.Append('G');
if ((m & 2) != 0) sb.Append('B');
if ((m & 1) != 0) sb.Append('A');
return sb.ToString();
}

Expand Down