Phone 无法编译着色器

Phone unable to compile shader

我在 libGDX 有一个项目正在进行。我正在 运行 对距离场字体进行测试,在我的 Android phone (Galaxy Core 2 4.4.2) 上编译着色器时我 运行 遇到了问题.在我的 phone 上部署时,我在桌面应用程序正常运行时遇到错误(主要是 - 我会解决的)。

我将向您介绍我一直在尝试的内容。

我希望能够在 运行 时间内启用和禁用字体边框,我可以使用以下着色器和方法在桌面应用程序上很好地完成此操作。

.片段:

#ifdef GL_ES

precision mediump float;
#else
#define LOWP
#endif

uniform sampler2D u_texture;
uniform float u_lower;
uniform float u_upper;

varying vec4 v_color;
uniform vec4 u_outlineColor;
uniform float u_enableOutline;
varying vec2 v_texCoord;

const float smoothing = 1.0/12.0;

const float outlineWidth = 3.0/12.0; //will need to be tweaked
const float outerEdgeCenter = 0.5 - outlineWidth; //for optimizing below calculation

void main() {

    float distance = texture2D(u_texture, v_texCoord).a;    

    if (u_enableOutline > 0){
        float alpha = smoothstep(outerEdgeCenter - smoothing, outerEdgeCenter + smoothing, distance);//Bigger to accomodate outline
        float border = smoothstep(0.45 - smoothing, 0.55 + smoothing, distance);
        gl_FragColor = vec4( mix(u_outlineColor.rgb, v_color.rgb, border), alpha );
    }
    else{
        float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);
        gl_FragColor = vec4(v_color.rgb, alpha);
    }   
}

.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;

}

用距离字体方法启用/禁用轮廓为:

public void enableOutline(float enable) {
        ENABLE_OUTLINE = enable;
}

其中ENABLE_OUTLINE通过

传递给着色器
distanceFieldShader.setUniformf("u_enableOutline", ENABLE_OUTLINE);

在此设置中,运行ning 在我的 phone 上出现以下错误:

"cannot compare float to int"

在 .frag 中引用这一行

if (u_enableOutline > 0){

我说的很对,所以我像这样更改数据类型:

uniform int u_enableOutline;

以及通过int传递的方法:

public void enableOutline(int enable) {
        ENABLE_OUTLINE = enable;
}

但是无法将 int 传递给着色器(这就是我选择使用浮点数的原因,请参见此图:http://imgur.com/nVTN12i)因此我启用轮廓的方法没有由于混合数据类型而工作。

所以我的问题是:我能以某种方式解决这个问题,以便我可以在 phone 上启用和禁用边框吗?

听起来您正在使用的 API 不提供将 bool 和 int 用作 Uniform 的可能性。使用浮动来规避这种情况的解决方案似乎是个好主意。

在您的示例中,您遇到的问题是因为,与 C 和 java 不同,GLSL 编译器不会隐式地将整数转换为浮点数。

您需要做的是告诉编译器您的“0”的类型。

通过使用语法 0.0,编译器知道您的常量是浮点数而不是整数。

if (u_enableOutline > 0.0){

应该可以解决您的问题