iOS上的Libgdx距离字段字体支持

时间:2015-01-11 21:24:39

标签: java ios libgdx shader

使用Libgdx wiki中解释的距离字段字体:https://github.com/libgdx/libgdx/wiki/Distance-field-fonts 在android上给出了很好的结果。我使用了提供的示例着色器。

但是当我在iOS上运行相同的程序时,文本到处都是白色(见下图)。 iOS不支持距离字段,还是需要添加/更改其他内容以使其起作用?

distance field problem

着色器代码font.vert:

uniform mat4 u_projTrans;

attribute vec4 a_position;
attribute vec2 a_texCoord0;
attribute vec4 a_color;

varying vec4 v_color;
varying vec2 v_texCoord;

void main() {
    gl_Position = u_projTrans * a_position;
    v_texCoord = a_texCoord0;
    v_color = a_color;
}

font.frag:

#ifdef GL_ES
    precision mediump float;
#endif

uniform sampler2D u_texture;

varying vec4 v_color;
varying vec2 v_texCoord;

const float smoothing = 1.0/16.0;

void main() {
    float distance = texture2D(u_texture, v_texCoord).a;
    float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);
    gl_FragColor = vec4(v_color.rgb, alpha);
}

加载如下字体:

BitmapFont bf = new BitmapFont(Gdx.files.internal("fonts/mont-b.fnt"));
bf.getRegion().getTexture().setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Linear);

和着色器:

fontShader = new ShaderProgram(Gdx.files.internal("shader/font.vert"),     Gdx.files.internal("shader/font.frag"));

2 个答案:

答案 0 :(得分:1)

我认为问题可能是您没有在字体纹理上启用mip映射,因为您只使用了没有TextureRegion参数的简单BitmapFont构造函数,因此它假设没有mip贴图加载纹理

你必须创建一个支持这样的mip贴图的Texture,如libgdx wiki所示:

Texture texture = new Texture(Gdx.files.internal("yourFont.png"), true); //true to enable mip maps
texture.setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Linear);

然后你可以使用mip贴图加载你的BitmapFont,所以BitmapFont不会创建自己的纹理而没有mip贴图:

BitmapFont bf = new BitmapFont(Gdx.files.internal("fonts/mont-b.fnt"), new TextureRegion(texture));

答案 1 :(得分:0)

你的代码很好。但是,默认情况下,iOS会对所有图像使用预乘alpha。因此,您使用的图像将转换为预乘的alpha格式,而不是您提供的格式。

不确定确切位置,但您需要为图像禁用预乘的alpha以及"框"将会消失。

不,你的问题与mipmapping无关。