我想在libGDX中使用Mesh类做一个简单的渐变。这应该是一个包含渐变的绘制矩形,但问题是矩形是完全黑色的!
我的代码:
public class HomeScreen extends
ScreenAdapter {
OrthographicCamera camera;
Mesh mesh;
ShaderProgram shader;
public HomeScreen() {
Logger.message("HomeScreen()", "^");
this.camera = new OrthographicCamera(2, 2);
this.mesh = new Mesh(false, 4, 4,
new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE)
);
this.shader = SpriteBatch.createDefaultShader();
this.camera.position.set(0, 0, 0);
this.camera.update();
this.mesh.setVertices(new float[] {
-0.5f, -0.5f, 0, Color.BLUE.toFloatBits(),
-0.5f, 0.5f, 0, Color.RED.toFloatBits(),
0.5f, 0.5f, 0, Color.RED.toFloatBits(),
0.5f, -0.5f, 0, Color.BLUE.toFloatBits(),
});
this.mesh.setIndices(new short[] {
0, 1, 3, 2
});
Logger.message("HomeScreen()", "$");
}
@Override
public void dispose() {
Logger.message("HomeScreen.dipose()", "^");
this.mesh.dispose();
this.shader.dispose();
Logger.message("HomeScreen.dipose()", "$");
}
@Override
public void render(final float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
this.shader.begin();
this.shader.setUniformMatrix("u_projTrans", this.camera.combined);
this.mesh.render(this.shader, GL20.GL_TRIANGLE_STRIP);
this.shader.end();
}
}
结果:
有什么想法吗?
答案 0 :(得分:5)
libgdx中SpriteBatches的默认着色器需要纹理才能工作。在查看shader code时,会看到最终结果是顶点颜色和纹理颜色的组合:
gl_FragColor = v_color * texture2D(u_texture, v_texCoords);
当不提供纹理时,顶点颜色将乘以[0,0,0,0],这会导致黑色输出。