I’m drawing a gradient using the SpriteBatch method draw(Texture, float[], int, int) that takes an array of vertices and other data alongside a texture and renders it to screen. I set each corner to a specific colour, and it renders it pretty well. However, I’ve noticed an artefact of sorts. There’s a line going bottom-left to top-right that doesn’t “seem” correct.
On the coloured (red green blue white) gradient, you don’t really notice it. If you turn your head and look at it on your monitor from an angle, it becomes extremely visible. You can also see the lighting on the blocks how there’s lines going top-left-to-bottom-right making it look jagged.
The gradient code is a modified version of davedes horizontal/vertical code here made to fit with four colours instead of only two. I am not using ShapeRenderer because its alpha blending (even with blending turned on) doesn’t exactly work very well.
Gradient code:
private static float[] gradientverts = new float[20];
public static void drawGradient(SpriteBatch batch, float x, float y, float width, float height,
Color bl, Color br, Color tr, Color tl) {
int idx = 0;
gradientverts[idx++] = x;
gradientverts[idx++] = y;
gradientverts[idx++] = bl.toFloatBits(); // bottom left
gradientverts[idx++] = filltexRegion.getU(); //NOTE: texture coords origin is top left
gradientverts[idx++] = filltexRegion.getV2();
gradientverts[idx++] = x;
gradientverts[idx++] = y + height;
gradientverts[idx++] = tl.toFloatBits(); // top left
gradientverts[idx++] = filltexRegion.getU();
gradientverts[idx++] = filltexRegion.getV();
gradientverts[idx++] = x + width;
gradientverts[idx++] = y + height;
gradientverts[idx++] = tr.toFloatBits(); // top right
gradientverts[idx++] = filltexRegion.getU2();
gradientverts[idx++] = filltexRegion.getV();
gradientverts[idx++] = x + width;
gradientverts[idx++] = y;
gradientverts[idx++] = br.toFloatBits(); // bottom right
gradientverts[idx++] = filltexRegion.getU2();
gradientverts[idx++] = filltexRegion.getV2();
batch.draw(filltexRegion.getTexture(), gradientverts, 0, gradientverts.length);
}
filltexRegion
is a TextureRegion that contains a Texture of a 1x1 white square (generated by a pixmap at runtime).
Thanks!