Skip to content

Commit 9826d36

Browse files
committed
Fix distance biases and add option for atlas compression
- Fix distance biases because some colors of text could still turn into quads at a distance. - Add option to compress font atlas with BC7 compression if people really care about the extra few MB of VRAM. - Change _TextColor param on test mesh shader to _Color to match the normal shader. - Add support for text color to the text mesh shader
1 parent 7d295fe commit 9826d36

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

Merlin/Scripts/MSDFAtlasGenerator.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class MSDFAtlasGenerator : EditorWindow
3636
public Font FontToConvert = null;
3737

3838
public Texture2D AtlasToSave = null;
39+
public bool UseTextureCompression = false;
3940

4041
private const string MSDFGenPath = "Assets/Merlin/MSDF/bin/msdfgen.exe";
4142
private const string MSDFTempPath = "Assets/Merlin/MSDF/gen/glyph{0}.png";
@@ -53,6 +54,13 @@ void OnGUI()
5354

5455
FontToConvert = (Font)EditorGUILayout.ObjectField("Font Asset:", FontToConvert, typeof(Font), false);
5556

57+
UseTextureCompression = EditorGUILayout.Toggle("Compress Font Atlas", UseTextureCompression);
58+
59+
if (UseTextureCompression)
60+
{
61+
EditorGUILayout.HelpBox("Enabling compression can cause visible artifacts on text depending on the font. On most fonts the artifacts may make the text look wobbly along edges. Check to make sure artifacts do not appear when you enable this.", MessageType.Warning);
62+
}
63+
5664
EditorGUI.BeginDisabledGroup(FontToConvert == null);
5765
if (GUILayout.Button("Generate Atlas"))
5866
{
@@ -148,9 +156,15 @@ private void GenerateAtlas()
148156

149157
}
150158

151-
EditorUtility.ClearProgressBar();
152-
153159
newAtlas.Apply(false);
160+
161+
if (UseTextureCompression)
162+
{
163+
EditorUtility.DisplayProgressBar("Generating MSDF Atlas...", "Compressing Atlas...", 1f);
164+
EditorUtility.CompressTexture(newAtlas, TextureFormat.BC7, TextureCompressionQuality.Best);
165+
}
166+
167+
EditorUtility.ClearProgressBar();
154168

155169
string fontPath = AssetDatabase.GetAssetPath(FontToConvert);
156170
string savePath = Path.Combine(Path.GetDirectoryName(fontPath), Path.GetFileNameWithoutExtension(fontPath) + "_msdfAtlas.asset");

Merlin/Shaders/MSDFTMFont.shader

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
Properties
44
{
55
[HideInInspector]_MainTex ("Texture", 2D) = "white" {}
6-
[HDR]_TextColor("Text Color", Color) = (1, 1, 1, 1)
6+
[HDR]_Color("Text Color", Color) = (1, 1, 1, 1)
77
[NoScaleOffset]_MSDFTex("MSDF Texture", 2D) = "black" {}
8-
_PixelRange("Pixel Range", Float) = 4.0
8+
[HideIninspector]_PixelRange("Pixel Range", Float) = 4.0
99
}
1010
SubShader
1111
{
12-
Tags { "RenderType"="Cutout" "Queue"="AlphaTest+1"
12+
Tags { "RenderType"="Cutout" "Queue"="AlphaTest"
1313
"IgnoreProjector" = "True" }
14-
LOD 100
1514

1615
Pass
1716
{
@@ -27,16 +26,18 @@
2726
struct appdata
2827
{
2928
float4 vertex : POSITION;
29+
float4 color : COLOR;
3030
float2 uv : TEXCOORD0;
3131
};
3232

3333
struct v2f
3434
{
3535
float4 vertex : SV_POSITION;
36+
float4 color : COLOR;
3637
float2 uv : TEXCOORD0;
3738
};
3839

39-
float4 _TextColor;
40+
float4 _Color;
4041
sampler2D _MSDFTex; float4 _MSDFTex_TexelSize;
4142
float _PixelRange;
4243

@@ -50,6 +51,7 @@
5051
v2f o;
5152
o.vertex = UnityObjectToClipPos(v.vertex);
5253
o.uv = v.uv;
54+
o.color = v.color * _Color;
5355

5456
return o;
5557
}
@@ -59,14 +61,13 @@
5961
float2 msdfUnit = _PixelRange / _MSDFTex_TexelSize.zw;
6062

6163
float4 sampleCol = tex2D(_MSDFTex, i.uv);
62-
float sigDist = median(sampleCol.r, sampleCol.g, sampleCol.b) - 0.46;
63-
sigDist *= max(dot(msdfUnit, 0.5/fwidth(i.uv)), 0.9); // Max to handle fading out to quads in the distance
64+
float sigDist = median(sampleCol.r, sampleCol.g, sampleCol.b) - 0.5;
65+
sigDist *= max(dot(msdfUnit, 0.5/fwidth(i.uv)), 1); // Max to handle fading out to quads in the distance
6466
float opacity = clamp(sigDist + 0.5, 0.0, 1.0);
65-
float4 color = float4(_TextColor.rgb, _TextColor.a * opacity);
67+
float4 color = float4(i.color.rgb, i.color.a * opacity);
6668

6769
clip(color.a - 0.005);
6870

69-
//return sampleCol;
7071
return color;
7172
}
7273
ENDCG

Merlin/Shaders/MSDFUIFont.shader

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ Shader "Merlin/UI/MSDF UI Font"
115115
float2 msdfUnit = _PixelRange / _MSDFTex_TexelSize.zw;
116116

117117
float4 sampleCol = tex2D(_MSDFTex, texcoord);
118-
float sigDist = median(sampleCol.r, sampleCol.g, sampleCol.b) - 0.46;
119-
sigDist *= max(dot(msdfUnit, 0.5 / fwidth(texcoord)), 0.9); // Max to handle fading out to quads in the distance
118+
float sigDist = median(sampleCol.r, sampleCol.g, sampleCol.b) - 0.5;
119+
sigDist *= max(dot(msdfUnit, 0.5 / fwidth(texcoord)), 1); // Max to handle fading out to quads in the distance
120120
float opacity = clamp(sigDist + 0.5, 0.0, 1.0);
121121
float4 color = float4(1, 1, 1, opacity);
122122

0 commit comments

Comments
 (0)