libgdx:统一颜色不发送到片段着色器
问题描述:
我有一个非常简单的着色器,它使用自定义着色器程序。我正在发送摄像机转换,世界转换为制服,这一切都有效。我也以同样的方式发送统一的颜色,但它似乎不影响屏幕上的颜色。使用set()
它只会出现黑色,因为颜色设置不正确。从libgdx着色libgdx:统一颜色不发送到片段着色器
代码:
protected final int u_projTrans = register(new Uniform("u_projTrans"));
protected final int u_worldTrans = register(new Uniform("u_worldTrans"));
protected final int u_color = register(new Uniform("u_color"));
@Override
public void begin(Camera camera, RenderContext context) {
program.begin();
context.setDepthTest(GL20.GL_LEQUAL, 0f, 1f);
context.setDepthMask(true);
set(u_projTrans, camera.combined);
}
@Override
public void render(Renderable renderable) {
set(u_worldTrans, renderable.worldTransform);
set(u_color, 1, 1, 1); // binding, but no change in color
// program.setUniformf("u_color", 1, 1, 1); // works
renderable.meshPart.render(program);
}
综观上述代码中,我结合在开始()函数和u_projTrans我结合在渲染()函数u_worldTrans。我知道这些都是基于我看到的图像工作。
顶点着色器:
attribute vec3 a_position;
uniform mat4 u_projTrans;
uniform mat4 u_worldTrans;
void main() {
gl_Position = u_projTrans * u_worldTrans * vec4(a_position, 1.0);
}
片段着色器:
uniform vec3 u_color;
void main() {
gl_FragColor = vec4(u_color, 1); // hard-coding this to a color works, so the value in u_color seems to be (0,0,0)
}
当我尝试设置颜色均匀它说,它的正确结合,但我只是不明白这就像在颜色的任何变化该值被设置为(0,0,0)。但是,使用program.setUniformf()
的作品。使用set()
我完全按照我为其他人的方式进行绑定,所以除了将其作为vec3
而不是mat4
发送外,我不知道有什么不同。
有没有人知道为什么我在set(u_color, 1, 1, 1)
中传递的值不会影响着色器?
答
当您致电set(u_color, 1, 1, 1);
时,您正在使用set(Uniform, int, int int)
方法。你想要的是set(Uniform, float, float, float)
方法,所以用set(u_color, 1f, 1f, 1f)
代替。
修复它。我很困惑,因为我认为它可能会将它解释为一个整数,并尝试了一个更高的值,如1000,但它仍然是黑色的。我没有意识到它只会失败。谢谢! – voodoogiant