gpt4 book ai didi

java - 手机无法编译shader

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:40 24 4
gpt4 key购买 nike

我正在 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 的原因,请参见此图片: /image/Dsg98.jpg ),因此我启用轮廓的方法由于混合而不起作用增加数据类型。

所以我的问题是:我能否以某种方式解决这个问题,以便在考虑到这些限制的情况下启用和禁用手机上的边框?

最佳答案

听起来您正在使用的 API 无法为您提供使用 bool 和 int 作为 Uniform 的可能性。使用 float 来规避这个问题的解决方案似乎是一个好主意。

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

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

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

if (u_enableOutline > 0.0){

应该可以解决你的问题

关于java - 手机无法编译shader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28480089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com