linGdx绘制圆

作者: 大旺旺的弟弟小旺旺 | 来源:发表于2022-03-28 15:21 被阅读0次

绘制方式一:

public class PolyShowShape extends Group {
    float[] vertices;
    TextureRegion region = new TextureRegion(new Texture("BG.png"));
    PolygonSpriteBatch polygonSpriteBatch;
    PolygonSprite sprite;
    PolygonRegion PolygonRegion = null;
    private int idx = 0;
    short tri [];
    public PolyShowShape(){
        x = 0;
        y = 0;
        vertices = new float[2000];
        int idx = this.idx;

        for (float i = 0; i <= 360; i=i+0.5F) {
            vertices[idx ++] = (float) (100+ 100*Math.sin(i));
            vertices[idx ++] = (float) (100+ 100*Math.cos(i));
        }
        vertices[idx++] = 100;
        vertices[idx++] = 100;
        tri = new short[1200];
        for (int i = 0; i < 360; i++) {
            tri[3*i] = 360;
            tri[3*i+1] = (short) i;
            tri[3*i+2] = (short) (i+1);
        }

        PolygonRegion = new PolygonRegion(region,vertices,tri);
        sprite = new PolygonSprite(PolygonRegion);
//        Image image = new Image(new TextureRegion(sprite));
//        addActor();
        polygonSpriteBatch = new PolygonSpriteBatch();
        sprite.setPosition(100,100);
    }

    public void draw(Batch batch, float parentAlpha) {
        polygonSpriteBatch.begin();
        sprite.draw(polygonSpriteBatch);
        polygonSpriteBatch.end();
    }
}

效果


image.png

绘制方式二

public class SeneTest extends Group {
    private Image image1;
    private SpineActor spineActor;
    private Cir cir;
    public SeneTest(){
        image1 = new Image(new Texture("11.png"));
        addActor(image1);
//
//        addActor(image2);

        cir = new Cir(300,300,200);
        addActor(cir);
        spineActor = new SpineActor("spine/countrypic/Australia/Australia");
        addActor(spineActor);
        spineActor.setPosition(20,100, Align.center);
        spineActor.setAnimation("1",true);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.flush();
        Gdx.gl.glEnable(GL20.GL_STENCIL_TEST);
        Gdx.gl.glStencilOp(GL20.GL_KEEP, GL20.GL_KEEP, GL20.GL_REPLACE);//第一次绘制的像素的模版值 0+1 = 1
        Gdx.gl.glStencilFunc(GL20.GL_ALWAYS, 1, 0xFF);
        cir.draw(batch,parentAlpha);
        Gdx.gl.glStencilFunc(GL20.GL_EQUAL, 0x1, 0xFF);//等于1 通过测试 ,就是上次绘制的图 的范围 才通过测试。
        Gdx.gl.glStencilOp(GL20.GL_KEEP, GL20.GL_KEEP, GL20.GL_KEEP);//没有通过测试的,保留原来的,也就是保留上一次的值。
        spineActor.draw(batch,parentAlpha);
        batch.flush();
        Gdx.gl.glDisable(Gdx.gl.GL_STENCIL_TEST);
    }
}


public class Cir extends Actor {
    private float centerX;
    private float centerY;
    private float radius;
    private ShapeRenderer sr;

    public Cir(float x, float y, float radius) {
        this.centerX = x;
        this.centerY = y;
        this.radius = radius;
        setPosition(centerX - radius, centerY - radius);
        setSize(radius * 2, radius * 2);
        sr = new ShapeRenderer();
    }

    @Override
    public void act(float delta) {
        super.act(delta);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        if (sr != null) {
            batch.end();
            sr.setProjectionMatrix(batch.getProjectionMatrix());
            sr.setTransformMatrix(batch.getTransformMatrix());
            sr.setColor(Color.valueOf("FFFFFF00"));
            sr.begin(ShapeRenderer.ShapeType.Filled);
            sr.circle(centerX, centerY, radius);
            sr.end();
            batch.begin();
        }
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public float getRadius() {
        return radius;
    }
}

image.png

绘制方式三

public class MaskTest extends Group {
    private Image img;
    private Image mask;
    public MaskTest(){
        mask = new Image(new Texture("demo.png"));
        img = new Image(new Texture(Gdx.files.internal("sprite.png")));
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        drawAlphaMask(batch);
        //画前景色
        drawForeground(batch, 0, 0, (int) mask.getWidth()/2, (int)mask.getHeight()/2);
    }

    private void drawForeground(Batch batchPara, int clipX, int clipY ,int clipWidth ,int clipHeight) {
        Gdx.gl.glColorMask(true, true, true, true);
        batchPara.setBlendFunction(GL20.GL_DST_ALPHA, GL20.GL_ONE_MINUS_DST_ALPHA);
        Gdx.gl.glEnable(GL20.GL_SCISSOR_TEST);
        Gdx.gl.glScissor(clipX, clipY, clipWidth, clipHeight);
        img.draw(batchPara,1);
        batchPara.flush();
        Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
    }
    private void drawAlphaMask(Batch batch) {
        Gdx.gl.glColorMask(false, false, false, true);
        mask.draw(batch,1);
        batch.flush();
    }
}
image.png

相关文章

网友评论

    本文标题:linGdx绘制圆

    本文链接:https://www.haomeiwen.com/subject/qdupjrtx.html