Reputation: 1
I have a polygon initialized here:
public static Color lerp(Color start, Color end, float t) {
float r = start.r + (end.r - start.r) * t;
float g = start.g + (end.g - start.g) * t;
float b = start.b + (end.b - start.b) * t;
float a = start.a + (end.a - start.a) * t;
return new Color(r, g, b, a);
}
Color startColor = Color.RED;
Color endColor = Color.BLUE;
public void initPolygonSprite(){
Texture tx = null;
if(getGradients().size() == 0){
// Utiliser une texture monochrome si aucun dégradé n'est défini
pix = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pix.setColor(colorLibGDX);
pix.fill();
tx = new Texture(pix);
}
else{
// Créer une texture avec un dégradé linéaire
int n = 256;
pix = new Pixmap(n, 1, Pixmap.Format.RGBA8888);
for (int x = 0; x < n; x++) {
float percent = x / (float)(n-1);
Color color = lerp(startColor, endColor, percent);
pix.drawPixel(x, 0,Color.rgba8888(color));
}
tx = new Texture(pix);
tx.setFilter(Texture.TextureFilter.Linear,Texture.TextureFilter.Linear);
pix.dispose();
}
float[] vertices = CloudToVertices();
EarClippingTriangulator triangulator = new EarClippingTriangulator();
TextureRegion textureRegion = new TextureRegion(tx);
textureRegion.flip(false, true); //Flipping the texture region around the y axis
PolygonRegion polyReg = new PolygonRegion(
textureRegion,
vertices,
triangulator.computeTriangles(vertices).toArray());
if(poly == null){
poly = new PolygonSprite(polyReg);
polyBatch = new PolygonSpriteBatch();
}
else{
poly.setRegion(polyReg);
}
}
@Override
public void onRender(){
float x = getX();
float y = getY();
float w = getWidth();
float h = getHeight();
float ox =getOrigin_x();
float oy = getOrigin_y();
float r = getRotation();
float sx = getScale_x();
float sy = getScale_y();
float borderWidth = getBorderWidth();
y = Game.graphics.getScreenHeight() - y - h;
oy = h - oy;
color.set(getColor());
color.set( getColor());
colorLibGDX.a = color.a;
colorLibGDX.r = color.r;
colorLibGDX.g = color.g;
colorLibGDX.b = color.b;
// Gdx.graphics.getGL20().glEnable(GL20.GL_BLEND);
Gdx.gl.glEnable(GL20.GL_BLEND);
if(isFillMode()){
initPolygonSprite();
pix.setColor(Color.BLUE);
polyBatch.begin();
poly.setX(x);
poly.setY(y);
poly.setRotation(r);
poly.setOrigin(ox,oy);
poly.draw(polyBatch);
polyBatch.end();
}
else {
GraphicsLibgdx.renderer.identity();
GraphicsLibgdx.renderer.setColor(color.r, color.g, color.b, color.a*GraphicsLibgdx.batch.getColor().a);
GraphicsLibgdx.renderer.translate(x, y, 0.f);
GraphicsLibgdx.renderer.translate(ox, oy, 0.f);
GraphicsLibgdx.renderer.rotate(0.f, 0.f, 1.f, r);
GraphicsLibgdx.renderer.scale(sx, sy, 0.f);
GraphicsLibgdx.renderer.translate(-ox, -oy, 0.f);
GraphicsLibgdx.renderer.begin(ShapeRenderer.ShapeType.Line);
Gdx.gl20.glLineWidth(getBorderWidth());
GraphicsLibgdx.renderer.polygon(CloudToVertices());
GraphicsLibgdx.renderer.end();
GraphicsLibgdx.renderer.identity();
Gdx.gl20.glLineWidth(1);
}
}
The function CloudToVertices() is work perfecty because when i havn't gradient color. The polygon is correcty showed and fill:
Fill polygon without gradient Not fill polygon
When i want use the gradients, i have this result :
The polygon is not fill. And if i reduce the width of pixmap, i have this :
A polygon with a pixmap width = 2.
If I enlarge the size of the pixmap is larger, the smaller the area displayed.
Have you got a solution? I want to display a polygon with a beautiful gradient.
I want to display a polygon with a beautiful gradient.
Upvotes: 0
Views: 54