libGdx专题

使用FrameBuffer黑遮罩,显示灯光效果

2022-03-16  本文已影响0人  大旺旺的弟弟小旺旺

在最上层使用frameBuffer绘制出显示区域 ,

public class Demo extends Group {

    private FrameBuffer lightsBuffer;
    private final TextureRegion lightsBufferRegion;

    Image image;
    public Demo(){
        image = new Image(new Texture("lights.png"));
        image.setSize(1000,1000);
        image.setY(100);
        lightsBufferRegion = new TextureRegion();
        lightsBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, (int)Constant.GAMEWIDTH, (int)Constant.GAMEHIGHT, true);
        lightsBuffer.getColorBufferTexture().setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
        lightsBufferRegion.setRegion(lightsBuffer.getColorBufferTexture());
        lightsBufferRegion.flip(false, true);
    }

    boolean fl= false;
    @Override
    public void act(float delta) {
        super.act(delta);
        if (fl) {
            image.setScale(image.getScaleX() + 0.001F);
            if (image.getScaleX()>10F) {
                fl = false;
            }
        }else {
            image.setScale(image.getScaleX() - 0.001F);
            if (image.getScaleX()<0.1F) {
                fl = true;
            }
        }

    }

    float ss =  0.1F;
    @Override
    public void draw(Batch batch, float parentAlpha) {
        lightsBuffer.begin();
        Gdx.gl.glClearColor(ss, ss, ss, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);



        batch.end();
        batch.begin();
        Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
        Gdx.gl20.glEnable(GL20.GL_BLEND);
        batch.setBlendFunction(GL20.GL_SRC_ALPHA, -1);
//        super.draw(batch, parentAlpha);
//        batch.draw(image,100,100);
        image.draw(batch,parentAlpha);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
        batch.end();
        batch.begin();
//        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
        lightsBuffer.end();
        batch.setBlendFunction(GL20.GL_DST_COLOR, GL20.GL_ZERO);
        batch.draw(lightsBufferRegion, 0, 0);
        batch.flush();
    }
}

效果


image.png
上一篇下一篇

猜你喜欢

热点阅读