Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 手机无法编译着色器_Java_Android_Opengl Es_Libgdx_Shader - Fatal编程技术网

Java 手机无法编译着色器

Java 手机无法编译着色器,java,android,opengl-es,libgdx,shader,Java,Android,Opengl Es,Libgdx,Shader,我有一个在libGDX上工作的项目。我正在对距离场字体进行测试,在Android手机Galaxy Core 2 4.4.2上编译着色器时遇到问题。当部署到我的手机上时,我会遇到错误,而桌面应用程序基本上可以正常工作——我会说的 我会带你经历我一直在尝试的 我希望能够在运行时启用和禁用字体边框,我可以使用以下着色器和方法在桌面应用程序上完成这项工作 .frag: #ifdef GL_ES precision mediump float; #else #define LOWP #endif un

我有一个在libGDX上工作的项目。我正在对距离场字体进行测试,在Android手机Galaxy Core 2 4.4.2上编译着色器时遇到问题。当部署到我的手机上时,我会遇到错误,而桌面应用程序基本上可以正常工作——我会说的

我会带你经历我一直在尝试的

我希望能够在运行时启用和禁用字体边框,我可以使用以下着色器和方法在桌面应用程序上完成这项工作

.frag:

#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);
在此设置中,在我的手机上运行会出现以下错误:

"cannot compare float to int"
在.frag中引用此行

if (u_enableOutline > 0){
我说得很公平,所以我更改数据类型如下:

uniform int u_enableOutline;
以及通过int的方法:

public void enableOutline(int enable) {
        ENABLE_OUTLINE = enable;
}
但是没有办法将int传递给着色器,这就是我选择使用float的原因,请参见下图:正因为如此,我启用大纲的方法无法工作,因为混合了数据类型


因此,我的问题是:我能否绕过这个问题,以便在这些限制条件下启用和禁用手机上的边框?

听起来您正在使用的API没有为您提供使用bool和int作为制服的可能性。使用浮动来规避这一问题的解决方案似乎是一个好主意

在您的示例中,您遇到的问题是,与C和java不同,GLSL编译器不会隐式地将int转换为float

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

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

if (u_enableOutline > 0.0){

应该解决您的问题

如果您尝试启用Outline>0f{此外,还应该有一个setUniformi版本,它接受INT。如果在比较中使用0.0而不是0,则应该编译。我尝试使用0f,但在Android上也产生了语法错误。指定0.0解决了这个问题。谢谢,这很有效!我尝试使用0f,但也产生了一个错误。我没有想到格式化它正如0.0.5所述,GLSL的语法可能有点棘手,特别是如果您习惯于java和C。我使用这个很好的工具来验证片段着色器:。它对顶点着色器不起作用,但有助于验证语法。