周围透明的黑色圆圈 [英] transparent circle black around

查看:66
本文介绍了周围透明的黑色圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作透明的圆圈以及带有alpha的黑色周围的所有内容.我正在使用此代码,但某种程度上它不起作用.

I'm trying to make transparent circle and everything around black with alpha. I'm using this code and it somehow doesn't work.

Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_DST_COLOR, GL20.GL_ZERO);
Gdx.gl.glBlendEquation(GL20.GL_FUNC_ADD);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(new Color(1, 1, 1, 1f));
shapeRenderer.circle(150,500,300);
shapeRenderer.end();
Gdx.gl.glDisable(GL20.GL_BLEND);

我想要这个:

推荐答案

您可以根据需要使用FrameBuffer.

public class GdxTest extends ApplicationAdapter implements InputProcessor {

    FrameBuffer frameBuffer;
    SpriteBatch spriteBatch;
    OrthographicCamera camera;

    Vector3 vector3;

    Texture texture,texture1;
    Sprite sprite;

    @Override
    public void create() {

        vector3=new Vector3();
        camera=new OrthographicCamera();
        camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
        spriteBatch=new SpriteBatch();

        texture=new Texture("light.png");

        sprite=new Sprite(texture);
        sprite.setSize(150,150);
        texture1=new Texture("badlogic.jpg");

        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void render() {

        frameBuffer.begin();

        Gdx.gl.glClearColor(.2f,.2f,.2f,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        spriteBatch.setProjectionMatrix(camera.combined);
        spriteBatch.setBlendFunction(GL20.GL_ONE,GL20.GL_ONE);
        spriteBatch.begin();
        sprite.draw(spriteBatch);
        spriteBatch.end();

        frameBuffer.end();

        spriteBatch.setBlendFunction(GL20.GL_SRC_ALPHA,GL20.GL_ONE_MINUS_SRC_ALPHA);
        spriteBatch.begin();

        spriteBatch.draw(texture1,100,100);
        spriteBatch.end();

        spriteBatch.setProjectionMatrix(spriteBatch.getProjectionMatrix().idt());

        spriteBatch.setBlendFunction( GL20.GL_ZERO,GL20.GL_SRC_COLOR);
        spriteBatch.begin();

        spriteBatch.draw(frameBuffer.getColorBufferTexture(),-1,1,2,-2);
        spriteBatch.end();
    }

    @Override
    public void resize(int width, int height) {

       if(frameBuffer !=null && (frameBuffer.getWidth()!=width || frameBuffer.getHeight()!=height )) {
           frameBuffer.dispose();
           frameBuffer=null;
       }

       if(frameBuffer==null){
           try {
               frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
           }catch (GdxRuntimeException e){
               frameBuffer=new FrameBuffer(Pixmap.Format.RGB565,width,height,false);
           }
       }
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {

        vector3.set(screenX,screenY,0);
        camera.unproject(vector3);
        sprite.setPosition(vector3.x-sprite.getWidth()/2,vector3.y-sprite.getHeight()/2);

        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}

这里是light.png

我的预期输出:

这篇关于周围透明的黑色圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆