libgdx中的距离场字体

4
我正在尝试呈现平滑可缩放的位图字体。在查看了这个问题后,其中一个答案提到使用距离场字体。
我正按照LibGDX wiki文章关于距离场字体的说明进行操作。但是我无法使其正常工作。字体呈现出模糊效果。
这是我用来生成此输出的代码。
public class FontRenderTest implements ApplicationListener {
private Texture texture;
private SpriteBatch spriteBatch;
private BitmapFont font;

@Override
public void create() {
    spriteBatch = new SpriteBatch();

    Texture texture = new Texture(Gdx.files.internal("Raleway.png"), true); // true enables mipmaps
    texture.setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Linear); // linear filtering in nearest mipmap image

    font = new BitmapFont(Gdx.files.internal("Raleway.fnt"), new TextureRegion(texture), false);
}


@Override
public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    spriteBatch.begin();
    font.draw(spriteBatch, "This is hazy !!", 100, 150);
    spriteBatch.end();

}
}

我不确定我是否正确理解了距离场字体的功能。如果有人能够解释如何使字体渲染更加平滑,那么请帮忙解答。


你正在直接渲染距离场,而不是计算到边缘上最近点的距离。你需要一个片段着色器来完成这个任务。 - kusma
2个回答

5
我认为它需要一个着色器,如果我没记错,着色器需要GL20。正如维基上所说,您需要.frag和.vert文件。我使用这个Libgdx测试的帮助修改了您的代码:http://git.io/-yAmNg使用不同的平滑方式看起来像这样。 使用不同的平滑方式看起来像这样。
public class FontRenderTest implements ApplicationListener {                                                                
private Texture texture;                                                                                                    
private SpriteBatch spriteBatch;                                                                                            
private BitmapFont font;                                                                                                    
private DistanceFieldShader distanceFieldShader;                                                                            
private static class DistanceFieldShader extends ShaderProgram {                                                            
    public DistanceFieldShader () {                                                                                         
        // The vert and frag files are copied from http://git.io/yK63lQ (vert) and http://git.io/hAcw9Q (the frag)          
        super(Gdx.files.internal("data/shaders/distancefield.vert"), Gdx.files.internal("data/shaders/distancefield.frag"));
        if (!isCompiled()) {                                                                                                
            throw new RuntimeException("Shader compilation failed:\n" + getLog());                                          
        }                                                                                                                   
    }                                                                                                                       

    /** @param smoothing a value between 0 and 1 */                                                                         
    public void setSmoothing (float smoothing) {                                                                            
        float delta = 0.5f * MathUtils.clamp(smoothing, 0, 1);                                                              
        setUniformf("u_lower", 0.5f - delta);                                                                               
        setUniformf("u_upper", 0.5f + delta);                                                                               
    }                                                                                                                       
}                                                                                                                           
    @Override                                                                                                                   
    public void create() {                                                                                                      
        spriteBatch = new SpriteBatch();                                                                                        

        Texture texture = new Texture(Gdx.files.internal("hiero.png"), true); // true enables mipmaps                           
        texture.setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Linear); // linear filtering in nearest mipmap image 

        font = new BitmapFont(Gdx.files.internal("hiero.fnt"), new TextureRegion(texture), false);                              
        distanceFieldShader = new DistanceFieldShader();                                                                        
    }
       @Override                                                                             
   public void render() {                                                                
       Gdx.gl.glClearColor(0, 0, 0, 1);                                                  
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);                                         

       spriteBatch.begin();                                                              

       spriteBatch.setShader(distanceFieldShader);                                       
       font.draw(spriteBatch, "This is pretty sharp !!", 100, 120);                      
       distanceFieldShader.setSmoothing(0f);                                             

       spriteBatch.setShader(distanceFieldShader);                                       
       font.draw(spriteBatch, "This is hazy !!", 100, 150);                              
       distanceFieldShader.setSmoothing(1f);                                             

       spriteBatch.setShader(distanceFieldShader);                                       
       font.draw(spriteBatch, "This is pretty smooth !!", 100, 180);                     
       distanceFieldShader.setSmoothing(1/2f);                                           

       spriteBatch.end();                                                                

   }

我以为我可以在GL10中不使用着色器来使用这种技术。 - Ashraf Saleh
2
不幸的是,维基中的所有更新的代码示例都是针对GL20的,因为Libgdx放弃了GL10支持,所以我不知道是否有任何GL10等效代码。至少我找不到任何GL10的等效代码。 - Sampo Pietikäinen

1
使用由DistanceFieldFont.createDistanceFieldShader创建的着色器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接