将另一个下方渲染的精灵部分更改为alpha

时间:2017-08-30 09:19:38

标签: libgdx

我正在使用LibGDX。是否可以使用SpriteBatchSprites来实现此行为?蓝色精灵周围有alpha,并以那个细蓝色边框结束。

首先渲染红色精灵,然后渲染蓝色精灵,红色精灵中有一个洞取决于蓝色的alpha,图案背景仅用于演示

我想要这个家伙没有的东西(启用GL_DEPTH_TEST对我不起作用) Sprite quads & depth testing correctly in OpenGL ES 2

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture bg, red, blue;

    @Override
    public void create () {
        batch = new SpriteBatch();
        bg = new Texture("bg.png");
        red = new Texture("red.png");
        blue = new Texture("blue.png");
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0.7f, 0.7f, 0.7f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClear(GL20.GL_DEPTH_BUFFER_BIT); //we have to clear depth buffer

        //BG
        batch.begin();
        batch.draw(bg, 0, 0);
        batch.end();

        Gdx.gl.glEnable(GL20.GL_DEPTH_TEST); //1. TRICK
        batch.begin();
        Gdx.gl.glDepthMask(true); //2. TRICK (after batch.begin())
        batch.draw(blue, 220, 220); //reversed rendering order (blue first! can you tell me why?)
        batch.draw(red, 200, 200);
        batch.end();
        Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
    }

    @Override
    public void dispose () {
        batch.dispose();
        bg.dispose();
        red.dispose();
        blue.dispose();
    }
}

enter image description here