Libgdx 渐变不适用于具有纹理的 PolygonRegion

问题描述 投票:0回答:1

我在这里初始化了一个多边形:

     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);
         }

     }

函数 CloudToVertices() 工作完美,因为当我没有渐变颜色时。多边形正确显示并填充:

Fill polygon without gradient Not fill polygon

当我想使用渐变时,我得到这个结果:

The gradient polygon

多边形未填充。如果我减少像素图的宽度,我就会得到这个:

A polygon with a pixmap width = 2.

如果放大像素图的尺寸越大,显示的区域就越小。

你有解决办法吗?我想显示一个具有漂亮渐变的多边形。

我想显示一个具有漂亮渐变的多边形。

libgdx textures polygon gradient pixmap
1个回答
0
投票

向上。救命,我需要有人帮助。

© www.soinside.com 2019 - 2024. All rights reserved.