From c4783db717ad87d3642c1ac1192ba34331e37557 Mon Sep 17 00:00:00 2001 From: yum Date: Thu, 26 Jan 2023 12:47:36 -0800 Subject: Improve font rendering * Enable streaming mipmaps on glyph bitmaps * Sample glyph bitmaps using mipmaps * Add temporal noise to letter UVs (dithering) --- Fonts/Bitmaps/font-0.png.meta | 18 +++---- Fonts/Bitmaps/font-1.png.meta | 18 +++---- Fonts/Bitmaps/font-2.png.meta | 18 +++---- Fonts/Bitmaps/font-3.png.meta | 18 +++---- Fonts/Bitmaps/font-4.png.meta | 18 +++---- Fonts/Bitmaps/font-5.png.meta | 18 +++---- Fonts/Bitmaps/font-6.png.meta | 18 +++---- Shaders/TaSTT_lighting_template.cginc | 90 +++++++++++++++++------------------ 8 files changed, 107 insertions(+), 109 deletions(-) diff --git a/Fonts/Bitmaps/font-0.png.meta b/Fonts/Bitmaps/font-0.png.meta index 4316b24..e7d9c33 100644 --- a/Fonts/Bitmaps/font-0.png.meta +++ b/Fonts/Bitmaps/font-0.png.meta @@ -12,7 +12,7 @@ TextureImporter: fadeOut: 0 borderMipMap: 0 mipMapsPreserveCoverage: 0 - alphaTestReferenceValue: 0.5 + alphaTestReferenceValue: 0 mipMapFadeDistanceStart: 1 mipMapFadeDistanceEnd: 3 bumpmap: @@ -21,7 +21,7 @@ TextureImporter: heightScale: 0.25 normalMapFilter: 0 isReadable: 0 - streamingMipmaps: 0 + streamingMipmaps: 1 streamingMipmapsPriority: 0 grayScaleToAlpha: 0 generateCubemap: 6 @@ -39,7 +39,7 @@ TextureImporter: wrapW: 0 nPOTScale: 1 lightmap: 0 - compressionQuality: 50 + compressionQuality: 100 spriteMode: 0 spriteExtrude: 1 spriteMeshType: 1 @@ -62,10 +62,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: DefaultTexturePlatform maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -74,10 +74,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Standalone maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -86,10 +86,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Android maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 diff --git a/Fonts/Bitmaps/font-1.png.meta b/Fonts/Bitmaps/font-1.png.meta index 62c9a60..4f7a87a 100644 --- a/Fonts/Bitmaps/font-1.png.meta +++ b/Fonts/Bitmaps/font-1.png.meta @@ -12,7 +12,7 @@ TextureImporter: fadeOut: 0 borderMipMap: 0 mipMapsPreserveCoverage: 0 - alphaTestReferenceValue: 0.5 + alphaTestReferenceValue: 0 mipMapFadeDistanceStart: 1 mipMapFadeDistanceEnd: 3 bumpmap: @@ -21,7 +21,7 @@ TextureImporter: heightScale: 0.25 normalMapFilter: 0 isReadable: 0 - streamingMipmaps: 0 + streamingMipmaps: 1 streamingMipmapsPriority: 0 grayScaleToAlpha: 0 generateCubemap: 6 @@ -39,7 +39,7 @@ TextureImporter: wrapW: 0 nPOTScale: 1 lightmap: 0 - compressionQuality: 50 + compressionQuality: 100 spriteMode: 0 spriteExtrude: 1 spriteMeshType: 1 @@ -62,10 +62,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: DefaultTexturePlatform maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -74,10 +74,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Standalone maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -86,10 +86,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Android maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 diff --git a/Fonts/Bitmaps/font-2.png.meta b/Fonts/Bitmaps/font-2.png.meta index 475ca07..07d8ca0 100644 --- a/Fonts/Bitmaps/font-2.png.meta +++ b/Fonts/Bitmaps/font-2.png.meta @@ -12,7 +12,7 @@ TextureImporter: fadeOut: 0 borderMipMap: 0 mipMapsPreserveCoverage: 0 - alphaTestReferenceValue: 0.5 + alphaTestReferenceValue: 0 mipMapFadeDistanceStart: 1 mipMapFadeDistanceEnd: 3 bumpmap: @@ -21,7 +21,7 @@ TextureImporter: heightScale: 0.25 normalMapFilter: 0 isReadable: 0 - streamingMipmaps: 0 + streamingMipmaps: 1 streamingMipmapsPriority: 0 grayScaleToAlpha: 0 generateCubemap: 6 @@ -39,7 +39,7 @@ TextureImporter: wrapW: 0 nPOTScale: 1 lightmap: 0 - compressionQuality: 50 + compressionQuality: 100 spriteMode: 0 spriteExtrude: 1 spriteMeshType: 1 @@ -62,10 +62,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: DefaultTexturePlatform maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -74,10 +74,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Standalone maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -86,10 +86,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Android maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 diff --git a/Fonts/Bitmaps/font-3.png.meta b/Fonts/Bitmaps/font-3.png.meta index b1c1603..d2fc0e3 100644 --- a/Fonts/Bitmaps/font-3.png.meta +++ b/Fonts/Bitmaps/font-3.png.meta @@ -12,7 +12,7 @@ TextureImporter: fadeOut: 0 borderMipMap: 0 mipMapsPreserveCoverage: 0 - alphaTestReferenceValue: 0.5 + alphaTestReferenceValue: 0 mipMapFadeDistanceStart: 1 mipMapFadeDistanceEnd: 3 bumpmap: @@ -21,7 +21,7 @@ TextureImporter: heightScale: 0.25 normalMapFilter: 0 isReadable: 0 - streamingMipmaps: 0 + streamingMipmaps: 1 streamingMipmapsPriority: 0 grayScaleToAlpha: 0 generateCubemap: 6 @@ -39,7 +39,7 @@ TextureImporter: wrapW: 0 nPOTScale: 1 lightmap: 0 - compressionQuality: 50 + compressionQuality: 100 spriteMode: 0 spriteExtrude: 1 spriteMeshType: 1 @@ -62,10 +62,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: DefaultTexturePlatform maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -74,10 +74,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Standalone maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -86,10 +86,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Android maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 diff --git a/Fonts/Bitmaps/font-4.png.meta b/Fonts/Bitmaps/font-4.png.meta index 5b723d3..55f89ef 100644 --- a/Fonts/Bitmaps/font-4.png.meta +++ b/Fonts/Bitmaps/font-4.png.meta @@ -12,7 +12,7 @@ TextureImporter: fadeOut: 0 borderMipMap: 0 mipMapsPreserveCoverage: 0 - alphaTestReferenceValue: 0.5 + alphaTestReferenceValue: 0 mipMapFadeDistanceStart: 1 mipMapFadeDistanceEnd: 3 bumpmap: @@ -21,7 +21,7 @@ TextureImporter: heightScale: 0.25 normalMapFilter: 0 isReadable: 0 - streamingMipmaps: 0 + streamingMipmaps: 1 streamingMipmapsPriority: 0 grayScaleToAlpha: 0 generateCubemap: 6 @@ -39,7 +39,7 @@ TextureImporter: wrapW: 0 nPOTScale: 1 lightmap: 0 - compressionQuality: 50 + compressionQuality: 100 spriteMode: 0 spriteExtrude: 1 spriteMeshType: 1 @@ -62,10 +62,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: DefaultTexturePlatform maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -74,10 +74,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Standalone maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -86,10 +86,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Android maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 diff --git a/Fonts/Bitmaps/font-5.png.meta b/Fonts/Bitmaps/font-5.png.meta index 57852ac..d40c504 100644 --- a/Fonts/Bitmaps/font-5.png.meta +++ b/Fonts/Bitmaps/font-5.png.meta @@ -12,7 +12,7 @@ TextureImporter: fadeOut: 0 borderMipMap: 0 mipMapsPreserveCoverage: 0 - alphaTestReferenceValue: 0.5 + alphaTestReferenceValue: 0 mipMapFadeDistanceStart: 1 mipMapFadeDistanceEnd: 3 bumpmap: @@ -21,7 +21,7 @@ TextureImporter: heightScale: 0.25 normalMapFilter: 0 isReadable: 0 - streamingMipmaps: 0 + streamingMipmaps: 1 streamingMipmapsPriority: 0 grayScaleToAlpha: 0 generateCubemap: 6 @@ -39,7 +39,7 @@ TextureImporter: wrapW: 0 nPOTScale: 1 lightmap: 0 - compressionQuality: 50 + compressionQuality: 100 spriteMode: 0 spriteExtrude: 1 spriteMeshType: 1 @@ -62,10 +62,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: DefaultTexturePlatform maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -74,10 +74,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Standalone maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -86,10 +86,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Android maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 diff --git a/Fonts/Bitmaps/font-6.png.meta b/Fonts/Bitmaps/font-6.png.meta index 2a01dd3..e96c5da 100644 --- a/Fonts/Bitmaps/font-6.png.meta +++ b/Fonts/Bitmaps/font-6.png.meta @@ -12,7 +12,7 @@ TextureImporter: fadeOut: 0 borderMipMap: 0 mipMapsPreserveCoverage: 0 - alphaTestReferenceValue: 0.5 + alphaTestReferenceValue: 0 mipMapFadeDistanceStart: 1 mipMapFadeDistanceEnd: 3 bumpmap: @@ -21,7 +21,7 @@ TextureImporter: heightScale: 0.25 normalMapFilter: 0 isReadable: 0 - streamingMipmaps: 0 + streamingMipmaps: 1 streamingMipmapsPriority: 0 grayScaleToAlpha: 0 generateCubemap: 6 @@ -39,7 +39,7 @@ TextureImporter: wrapW: 0 nPOTScale: 1 lightmap: 0 - compressionQuality: 50 + compressionQuality: 100 spriteMode: 0 spriteExtrude: 1 spriteMeshType: 1 @@ -62,10 +62,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: DefaultTexturePlatform maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -74,10 +74,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Standalone maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 @@ -86,10 +86,10 @@ TextureImporter: - serializedVersion: 3 buildTarget: Android maxTextureSize: 8192 - resizeAlgorithm: 0 + resizeAlgorithm: 1 textureFormat: -1 textureCompression: 2 - compressionQuality: 50 + compressionQuality: 100 crunchedCompression: 0 allowsAlphaSplitting: 0 overridden: 0 diff --git a/Shaders/TaSTT_lighting_template.cginc b/Shaders/TaSTT_lighting_template.cginc index 13729bd..e86df16 100644 --- a/Shaders/TaSTT_lighting_template.cginc +++ b/Shaders/TaSTT_lighting_template.cginc @@ -23,16 +23,25 @@ struct v2f #endif }; -SamplerState sampler_linear_repeat; - -Texture2D _Font_0x0000_0x1FFF; -Texture2D _Font_0x2000_0x3FFF; -Texture2D _Font_0x4000_0x5FFF; -Texture2D _Font_0x6000_0x7FFF; -Texture2D _Font_0x8000_0x9FFF; -Texture2D _Font_0xA000_0xBFFF; -Texture2D _Font_0xC000_0xDFFF; -Texture2D _Img_0xE000_0xE03F; +float Use_Custom_Background; +sampler2D Custom_Background; + +sampler2D _Font_0x0000_0x1FFF; +float4 _Font_0x0000_0x1FFF_TexelSize; +sampler2D _Font_0x2000_0x3FFF; +float4 _Font_0x2000_0x3FFF_TexelSize; +sampler2D _Font_0x4000_0x5FFF; +float4 _Font_0x4000_0x5FFF_TexelSize; +sampler2D _Font_0x6000_0x7FFF; +float4 _Font_0x6000_0x7FFF_TexelSize; +sampler2D _Font_0x8000_0x9FFF; +float4 _Font_0x8000_0x9FFF_TexelSize; +sampler2D _Font_0xA000_0xBFFF; +float4 _Font_0xA000_0xBFFF_TexelSize; +sampler2D _Font_0xC000_0xDFFF; +float4 _Font_0xC000_0xDFFF_TexelSize; +sampler2D _Img_0xE000_0xE03F; +float4 _Img_0xE000_0xE03F_TexelSize; fixed4 Text_Color; fixed4 Background_Color; @@ -80,9 +89,6 @@ fixed4 float3tofixed4(in float3 f3, in float alpha) alpha); } -float Use_Custom_Background; -Texture2D Custom_Background; - // %TEMPLATE__CG_ROW_COL_PARAMS% void getVertexLightColor(inout v2f i) @@ -244,30 +250,6 @@ float2 GetLetter(float2 uv, int nth_letter, return result; } -Texture2D GetTexture(int which_letter) { - int which_texture = (int) floor(which_letter / (64 * 64)); - - [forcecase] switch (which_letter) - { - case 0: - return _Font_0x0000_0x1FFF; - case 1: - return _Font_0x2000_0x3FFF; - case 2: - return _Font_0x4000_0x5FFF; - case 3: - return _Font_0x6000_0x7FFF; - case 4: - return _Font_0x8000_0x9FFF; - case 5: - return _Font_0xA000_0xBFFF; - case 6: - return _Font_0xC000_0xDFFF; - default: - return _Font_0x0000_0x1FFF; - } -} - // Get the value of the parameter for the cell we're in. int GetLetterParameter(float2 uv) { @@ -469,6 +451,15 @@ fixed4 frag (v2f i) : SV_Target uv_margin *= 4; float2 uv_with_margin = AddMarginToUV(uv, uv_margin); + // We use ddx/ddy to get the correct mipmaps of the font textures. This + // confers 2 main benefits: + // 1. We don't use as much VRAM for distant players. + // 2. Glyphs anti-alias much more nicely. + // Dividing the derivative subjectively makes the resulting mip-maps look a + // little more legible. + float2 iddx = ddx(i.uv.x) / 2; + float2 iddy = ddy(i.uv.y) / 2; + fixed4 text = fixed4(0, 0, 0, 0); { int letter = GetLetterParameter(uv_with_margin); @@ -486,32 +477,39 @@ fixed4 frag (v2f i) : SV_Target letter_uv = GetLetter(uv_with_margin, letter, texture_cols, texture_rows, 8, 4); } + // Add a small amount of temporal noise to the letter UV. This makes text + // more readable at intermediate distances. + // Assume that all glyph bitmaps have the same texelsize. + float noise = frac(i.position.x * i.position.y) + _SinTime[3]/2; + letter_uv.x += noise * _Font_0x0000_0x1FFF_TexelSize[0]; + letter_uv.y += noise * _Font_0x0000_0x1FFF_TexelSize[1]; + int which_texture = (int) floor(letter / (64 * 128)); [forcecase] switch (which_texture) { case 0: - text = _Font_0x0000_0x1FFF.Sample(sampler_linear_repeat, letter_uv); + text = tex2Dgrad(_Font_0x0000_0x1FFF, letter_uv, iddx, iddy); break; case 1: - text = _Font_0x2000_0x3FFF.Sample(sampler_linear_repeat, letter_uv); + text = tex2Dgrad(_Font_0x2000_0x3FFF, letter_uv, iddx, iddy); break; case 2: - text = _Font_0x4000_0x5FFF.Sample(sampler_linear_repeat, letter_uv); + text = tex2Dgrad(_Font_0x4000_0x5FFF, letter_uv, iddx, iddy); break; case 3: - text = _Font_0x6000_0x7FFF.Sample(sampler_linear_repeat, letter_uv); + text = tex2Dgrad(_Font_0x6000_0x7FFF, letter_uv, iddx, iddy); break; case 4: - text = _Font_0x8000_0x9FFF.Sample(sampler_linear_repeat, letter_uv); + text = tex2Dgrad(_Font_0x8000_0x9FFF, letter_uv, iddx, iddy); break; case 5: - text = _Font_0xA000_0xBFFF.Sample(sampler_linear_repeat, letter_uv); + text = tex2Dgrad(_Font_0xA000_0xBFFF, letter_uv, iddx, iddy); break; case 6: - text = _Font_0xC000_0xDFFF.Sample(sampler_linear_repeat, letter_uv); + text = tex2Dgrad(_Font_0xC000_0xDFFF, letter_uv, iddx, iddy); break; default: - text = _Img_0xE000_0xE03F.Sample(sampler_linear_repeat, letter_uv); + text = tex2Dgrad(_Img_0xE000_0xE03F, letter_uv, iddx, iddy); break; } } @@ -520,7 +518,7 @@ fixed4 frag (v2f i) : SV_Target if (Use_Custom_Background) { return light( i, - Custom_Background.Sample(sampler_linear_repeat, uv)); + tex2Dgrad(Custom_Background, uv, iddx, iddy)); } else { return light(i, Background_Color); } -- cgit v1.2.3