Hey guys,
I’m trying to draw a filled rectangle with a horizontal gradient that goes from a color to transparent. I’m using the ShapeRenderer.filledRect() method that takes x, y, width, height, as well as 4 corner colors to create a gradient:
shapeRenderer.filledRect(-25f, -25f, 50, 50, Color.RED, Color.CLEAR, Color.CLEAR, Color.RED);
What I would expect is a rectangle that’s red on the left, completely clear on the right, and a gradient between the two. Instead what I get is a rectangle that’s red on the right and completely black (and completely opaque, not transparent at all) on the right.
I’ve put together a little example using the default class created by LibGDX. Here I’m using a green transparency just to demonstrate:
package com.me.mygdxgame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
public class MyGdxGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
private ShapeRenderer shapeRenderer;
Color greenClear = new Color(0, 1, 0, 0);
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(100, 100);
batch = new SpriteBatch();
shapeRenderer = new ShapeRenderer();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(100, 100);
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.FilledRectangle);
shapeRenderer.filledRect(-25f, -25f, 50, 50, Color.RED, greenClear, greenClear, Color.RED);
shapeRenderer.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
Actually, I just realized that no transparency is drawn at all by ShapeRenderer. Changing the above code to the following results in a completely blue rectangle being drawn, despite having an alpha value of .5.
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.setColor(0, 0, 1, .5f);
shapeRenderer.begin(ShapeType.FilledRectangle);
shapeRenderer.filledRect(-25f, -25f, 50, 50);
//shapeRenderer.filledRect(-25f, -25f, 50, 50, Color.RED, greenClear, greenClear, Color.RED);
shapeRenderer.end();
In my actual program I’m displaying images with transparent gradients, so I know it’s at least possible- and if I can’t figure out how to do it using this method, I’ll just use another image. But I’ve looked through the source code of that filledRect() method, and it seems to be applying the opacity correctly… so why doesn’t it show up?
Is there something I’m missing? I’ve tried googling but I can’t find anything that seems to resemble my problem. Any help is very appreciated!