Browse Source

Remove useSDF parameter in Text and Text3D classes, now signed distance field font is a bitmap font file with .sdf extension.

aster 11 năm trước cách đây
mục cha
commit
d72dcde96d

+ 1 - 6
Bin/CoreData/Shaders/GLSL/Text.glsl

@@ -26,12 +26,7 @@ void PS()
     else
         gl_FragColor.a = vColor.a;
 
-    // Stroke effect testing
-    if (distance < 0.53f)
-        gl_FragColor.r = 1.0f;
-
-    float width = 0.015f;
-    gl_FragColor.a *= smoothstep(0.5f - width, 0.5f + width, distance);
+    gl_FragColor.a *= smoothstep(0.5f, 0.505f, distance);
 #else
     gl_FragColor.a = vColor.a * texture2D(sDiffMap, vTexCoord).a;
 #endif

+ 1 - 6
Bin/CoreData/Shaders/HLSL/Text.hlsl

@@ -29,12 +29,7 @@ void PS(float4 iColor : COLOR0,
     else
         oColor.a = iColor.a;
 
-    // Stroke effect testing
-    if (distance < 0.53f)
-        oColor.r = 1.0f;
-
-    float width = 0.015f;
-    oColor.a *= smoothstep(0.5f - width, 0.5f + width, distance);
+    oColor.a *= smoothstep(0.5f, 0.505f, distance);
 #else
     oColor.a = iColor.a * tex2D(sDiffMap, iTexCoord).a;
 #endif

+ 2 - 2
Bin/Data/LuaScripts/35_SignedDistanceFieldText.lua

@@ -70,10 +70,10 @@ function CreateScene()
         local mushroomTitleText = mushroomTitleNode:CreateComponent("Text3D")
         mushroomTitleText.text = "Mushroom " .. i
         if (i % 2) == 0 then
-            mushroomTitleText:SetFont(cache:GetResource("Font", "Fonts/BlueHighway.fnt"), 24, true)
+            mushroomTitleText:SetFont(cache:GetResource("Font", "Fonts/BlueHighway.sdf"), 24)
             mushroomTitleText.color = Color(0.0, 1.0, 0.0)
         else
-            mushroomTitleText:SetFont(cache:GetResource("Font", "Fonts/BlueHighway.ttf"), 24, false)
+            mushroomTitleText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.sdf"), 24)
             mushroomTitleText.color = Color(1.0, 0.0, 0.0)
         end
         mushroomTitleText:SetAlignment(HA_CENTER, VA_CENTER)

+ 2 - 2
Bin/Data/Scripts/35_SignedDistanceFieldText.as

@@ -74,12 +74,12 @@ void CreateScene()
         mushroomTitleText.text = "Mushroom " + i;
         if ((i % 2) == 0)
         {
-            mushroomTitleText.SetFont(cache.GetResource("Font", "Fonts/BlueHighway.fnt"), 24, true);
+            mushroomTitleText.SetFont(cache.GetResource("Font", "Fonts/BlueHighway.sdf"), 24);
             mushroomTitleText.color = Color(0.0f, 1.0f, 0.0f);
         }
         else
         {
-            mushroomTitleText.SetFont(cache.GetResource("Font", "Fonts/BlueHighway.ttf"), 24, false);
+            mushroomTitleText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.sdf"), 24);
             mushroomTitleText.color = Color(1.0f, 0.0f, 0.0f);
         }
         mushroomTitleText.SetAlignment(HA_CENTER, VA_CENTER);

+ 2 - 3
Source/Engine/LuaScript/pkgs/UI/Text.pkg

@@ -12,8 +12,8 @@ class Text : public UIElement
     Text();
     virtual ~Text();
 
-    bool SetFont(const String fontName, int size = DEFAULT_FONT_SIZE, bool useSDF = false);
-    bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE, bool useSDF = false);
+    bool SetFont(const String fontName, int size = DEFAULT_FONT_SIZE);
+    bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE);
     
     void SetText(const String text);
     
@@ -51,7 +51,6 @@ class Text : public UIElement
     
     tolua_property__get_set Font* font;
     tolua_readonly tolua_property__get_set int fontSize;
-    tolua_readonly tolua_property__is_set bool useSDF;
     tolua_property__get_set String text;
     tolua_property__get_set HorizontalAlignment textAlignment;
     tolua_property__get_set float rowSpacing;

+ 2 - 4
Source/Engine/LuaScript/pkgs/UI/Text3D.pkg

@@ -5,8 +5,8 @@ class Text3D : public Drawable
     Text3D();
     ~Text3D();
     
-    bool SetFont(const String fontName, int size = DEFAULT_FONT_SIZE, bool useSDF =  false);
-    bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE, bool useSDF =  false);
+    bool SetFont(const String fontName, int size = DEFAULT_FONT_SIZE);
+    bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE);
     
     void SetMaterial(Material* material);
     
@@ -30,7 +30,6 @@ class Text3D : public Drawable
     Font* GetFont() const;
     Material* GetMaterial() const;
     int GetFontSize() const;
-    bool IsUseSDF() const;
     const String GetText() const;
     HorizontalAlignment GetTextAlignment() const;
     HorizontalAlignment GetHorizontalAlignment() const;
@@ -54,7 +53,6 @@ class Text3D : public Drawable
     tolua_property__get_set Font* font;
     tolua_property__get_set Material* material;
     tolua_readonly tolua_property__get_set int fontSize;
-    tolua_readonly tolua_property__is_set bool useSDF;
     tolua_property__get_set String text;
     tolua_property__get_set HorizontalAlignment textAlignment;
     tolua_property__get_set HorizontalAlignment horizontalAlignment;

+ 4 - 4
Source/Engine/Script/UIAPI.cpp

@@ -335,8 +335,8 @@ static void RegisterText(asIScriptEngine* engine)
     engine->RegisterEnumValue("TextEffect", "TE_STROKE", TE_STROKE);
 
     RegisterUIElement<Text>(engine, "Text");
-    engine->RegisterObjectMethod("Text", "bool SetFont(const String&in, int, bool=false)", asMETHODPR(Text, SetFont, (const String&, int, bool), bool), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Text", "bool SetFont(Font@+, int, bool=false)", asMETHODPR(Text, SetFont, (Font*, int, bool), bool), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Text", "bool SetFont(const String&in, int)", asMETHODPR(Text, SetFont, (const String&, int), bool), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Text", "bool SetFont(Font@+, int)", asMETHODPR(Text, SetFont, (Font*, int), bool), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text", "void SetSelection(uint, uint arg1 = M_MAX_UNSIGNED)", asMETHOD(Text, SetSelection), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text", "void ClearSelection()", asMETHOD(Text, ClearSelection), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text", "Font@+ get_font() const", asMETHOD(Text, GetFont), asCALL_THISCALL);
@@ -370,8 +370,8 @@ static void RegisterText(asIScriptEngine* engine)
 static void RegisterText3D(asIScriptEngine* engine)
 {
     RegisterDrawable<Text3D>(engine, "Text3D");
-    engine->RegisterObjectMethod("Text3D", "bool SetFont(const String&in, int, bool=false)", asMETHODPR(Text3D, SetFont, (const String&, int, bool), bool), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Text3D", "bool SetFont(Font@+, int, bool=false)", asMETHODPR(Text3D, SetFont, (Font*, int, bool), bool), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Text3D", "bool SetFont(const String&in, int)", asMETHODPR(Text3D, SetFont, (const String&, int), bool), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Text3D", "bool SetFont(Font@+, int)", asMETHODPR(Text3D, SetFont, (Font*, int), bool), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text3D", "void SetAlignment(HorizontalAlignment, VerticalAlignment)", asMETHOD(Text3D, SetAlignment), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text3D", "Font@+ get_font() const", asMETHOD(Text3D, GetFont), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text3D", "int get_fontSize() const", asMETHOD(Text3D, GetFontSize), asCALL_THISCALL);

+ 5 - 2
Source/Engine/UI/Font.cpp

@@ -41,7 +41,8 @@ static const int MAX_POINT_SIZE = 96;
 Font::Font(Context* context) :
     Resource(context),
     fontDataSize_(0),
-    fontType_(FONT_NONE)
+    fontType_(FONT_NONE),
+    sdfFont_(false)
 {
 }
 
@@ -83,9 +84,11 @@ bool Font::BeginLoad(Deserializer& source)
     String ext = GetExtension(GetName());
     if (ext == ".ttf" || ext == ".otf" || ext == ".woff")
         fontType_ = FONT_FREETYPE;
-    else if (ext == ".xml" || ext == ".fnt")
+    else if (ext == ".xml" || ext == ".fnt" || ext == ".sdf")
         fontType_ = FONT_BITMAP;
 
+    sdfFont_ = ext == ".sdf";
+
     SetMemoryUse(fontDataSize_);
     return true;
 }

+ 4 - 0
Source/Engine/UI/Font.h

@@ -61,6 +61,8 @@ public:
     bool SaveXML(Serializer& dest, int pointSize, bool usedGlyphs = false);
     /// Return font face. Pack and render to a texture if not rendered yet. Return null on error.
     FontFace* GetFace(int pointSize);
+    /// Is signed distance field font.
+    bool IsSDFFont() const { return sdfFont_; }
     
     /// Release font faces and recreate them next time when requested. Called when font textures lost or global font properties change.
     void ReleaseFaces();
@@ -79,6 +81,8 @@ private:
     unsigned fontDataSize_;
     /// Font type.
     FONT_TYPE fontType_;
+    /// Signed distance field font.
+    bool sdfFont_;
 };
 
 }

+ 4 - 9
Source/Engine/UI/Text.cpp

@@ -52,7 +52,6 @@ Text::Text(Context* context) :
     UIElement(context),
     usedInText3D_(false),
     fontSize_(DEFAULT_FONT_SIZE),
-    useSDF_(false),
     textAlignment_(HA_LEFT),
     rowSpacing_(1.0f),
     wordWrap_(false),
@@ -228,13 +227,13 @@ void Text::OnIndentSet()
     charLocationsDirty_ = true;
 }
 
-bool Text::SetFont(const String& fontName, int size, bool useSDF)
+bool Text::SetFont(const String& fontName, int size)
 {
     ResourceCache* cache = GetSubsystem<ResourceCache>();
-    return SetFont(cache->GetResource<Font>(fontName), size, useSDF);
+    return SetFont(cache->GetResource<Font>(fontName), size);
 }
 
-bool Text::SetFont(Font* font, int size, bool useSDF)
+bool Text::SetFont(Font* font, int size)
 {
     if (!font)
     {
@@ -242,14 +241,10 @@ bool Text::SetFont(Font* font, int size, bool useSDF)
         return false;
     }
 
-    if (!usedInText3D_ && useSDF)
-        useSDF = false;
-
-    if (font != font_ || size != fontSize_ || useSDF != useSDF_)
+    if (font != font_ || size != fontSize_)
     {
         font_ = font;
         fontSize_ = Max(size, 1);
-        useSDF_ = useSDF;
         UpdateText();
     }
 

+ 2 - 6
Source/Engine/UI/Text.h

@@ -94,9 +94,9 @@ public:
     virtual void OnIndentSet();
 
     /// Set font and font size and use signed distance field.
-    bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE, bool useSDF = false);
+    bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE);
     /// Set font and font size and use signed distance field.
-    bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE, bool useSDF = false);
+    bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE);
     /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
     void SetText(const String& text);
     /// Set row alignment.
@@ -122,8 +122,6 @@ public:
     Font* GetFont() const { return font_; }
     /// Return font size.
     int GetFontSize() const { return fontSize_; }
-    /// Return use signed distance field.
-    bool IsUseSDF() const { return useSDF_; }
     /// Return text.
     const String& GetText() const { return text_; }
     /// Return row alignment.
@@ -190,8 +188,6 @@ protected:
     WeakPtr<FontFace> fontFace_;
     /// Font size.
     int fontSize_;
-    /// Use SDF.
-    bool useSDF_;
     /// UTF-8 encoded text.
     String text_;
     /// Row alignment.

+ 7 - 11
Source/Engine/UI/Text3D.cpp

@@ -71,7 +71,6 @@ void Text3D::RegisterObject(Context* context)
     ACCESSOR_ATTRIBUTE(Text3D, VAR_RESOURCEREF, "Font", GetFontAttr, SetFontAttr, ResourceRef, ResourceRef(Font::GetTypeStatic()), AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(Text3D, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
     ATTRIBUTE(Text3D, VAR_INT, "Font Size", text_.fontSize_, DEFAULT_FONT_SIZE, AM_DEFAULT);
-    ATTRIBUTE(Text3D, VAR_BOOL, "Use SDF", text_.useSDF_, false, AM_DEFAULT);
     ATTRIBUTE(Text3D, VAR_STRING, "Text", text_.text_, String::EMPTY, AM_DEFAULT);
     ENUM_ATTRIBUTE(Text3D, "Text Alignment", text_.textAlignment_, horizontalAlignments, HA_LEFT, AM_DEFAULT);
     ATTRIBUTE(Text3D, VAR_FLOAT, "Row Spacing", text_.rowSpacing_, 1.0f, AM_DEFAULT);
@@ -158,9 +157,9 @@ void Text3D::SetMaterial(Material* material)
     UpdateTextMaterials(true);
 }
 
-bool Text3D::SetFont(const String& fontName, int size, bool useSDF)
+bool Text3D::SetFont(const String& fontName, int size)
 {
-    bool success = text_.SetFont(fontName, size, useSDF);
+    bool success = text_.SetFont(fontName, size);
 
     // Changing font requires materials to be re-evaluated. Material evaluation can not be done in worker threads,
     // so UI batches must be brought up-to-date immediately
@@ -171,9 +170,9 @@ bool Text3D::SetFont(const String& fontName, int size, bool useSDF)
     return success;
 }
 
-bool Text3D::SetFont(Font* font, int size, bool useSDF)
+bool Text3D::SetFont(Font* font, int size)
 {
-    bool success = text_.SetFont(font, size, useSDF);
+    bool success = text_.SetFont(font, size);
 
     MarkTextDirty();
     UpdateTextBatches();
@@ -310,11 +309,6 @@ int Text3D::GetFontSize() const
     return text_.GetFontSize();
 }
 
-bool Text3D::IsUseSDF() const
-{
-    return text_.IsUseSDF();
-}
-
 const String& Text3D::GetText() const
 {
     return text_.GetText();
@@ -530,8 +524,10 @@ void Text3D::UpdateTextMaterials(bool forceUpdate)
                 Pass* pass = tech->CreatePass(PASS_ALPHA);
                 pass->SetVertexShader("Text");
                 pass->SetPixelShader("Text");
-                if (IsUseSDF())
+
+                if (GetFont()->IsSDFFont())
                     pass->SetPixelShaderDefines("SIGNED_DISTANCE_FIELD");
+
                 pass->SetBlendMode(BLEND_ALPHA);
                 pass->SetDepthWrite(false);
                 material->SetTechnique(0, tech);

+ 2 - 4
Source/Engine/UI/Text3D.h

@@ -55,9 +55,9 @@ public:
     virtual UpdateGeometryType GetUpdateGeometryType();
     
     /// Set font and font size and use signed distance field font. Return true if successful.
-    bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE, bool useSDF = false);
+    bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE);
     /// Set font and font size and use signed distance field font. Return true if successful.
-    bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE, bool useSDF = false);
+    bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE);
     /// Set material.
     void SetMaterial(Material* material);
     /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
@@ -97,8 +97,6 @@ public:
     Material* GetMaterial() const;
     /// Return font size.
     int GetFontSize() const;
-    /// Is use signed distance field.
-    bool IsUseSDF() const;
     /// Return text.
     const String& GetText() const;
     /// Return row alignment.

+ 2 - 2
Source/Samples/35_SignedDistanceFieldText/SignedDistanceFieldText.cpp

@@ -117,12 +117,12 @@ void SignedDistanceFieldText::CreateScene()
         mushroomTitleText->SetText("Mushroom " + String(i));
         if ((i % 2) == 0)
         {
-            mushroomTitleText->SetFont(cache->GetResource<Font>("Fonts/BlueHighway.fnt"), 24, true);
+            mushroomTitleText->SetFont(cache->GetResource<Font>("Fonts/BlueHighway.sdf"), 24);
             mushroomTitleText->SetColor(Color::GREEN);
         }
         else
         {
-            mushroomTitleText->SetFont(cache->GetResource<Font>("Fonts/BlueHighway.ttf"), 24, false);
+            mushroomTitleText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.sdf"), 24);
             mushroomTitleText->SetColor(Color::RED);
         }
         mushroomTitleText->SetAlignment(HA_CENTER, VA_CENTER);