libgdx中的距离字段字体

时间:2014-06-22 13:20:29

标签: libgdx

我正在尝试渲染平滑的可缩放位图字体。在使用距离字段字体检查question之一answers之后。

我完全按照LibGDX wiki article中提到的有关距离字体的方式进行操作。但是我无法让它发挥作用。字体变得模糊。

example preview

这是我用来生成此输出的代码

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();

}
}

我不确定我是否正确理解了距离场字体的功能。如果有人能解释如何渲染字体平滑。

2 个答案:

答案 0 :(得分:4)

我认为它需要着色器,如果我记得正确,着色器需要GL20。正如它在维基中所说,你需要.frag和.vert文件。我在此Libgdx测试的帮助下修改了您的代码:http://git.io/-yAmNg

It looks like this with different smoothing.

它看起来像是不同的平滑。

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();                                                                

   }

答案 1 :(得分:1)

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