2017-08-30 48 views
0

我使用LibGDX。 SpriteBatchSprites是否可以实现此行为?蓝色精灵周围有alpha,并以那个薄的蓝色边框结束。变更部分呈现下面的另一个阿尔法

红色精灵第一次呈现,那么蓝色的,并且在红色的根据从蓝色阿尔法孔,图案背景是那里只是为了演示

正是我想要的这个家伙不(使GL_DEPTH_TEST不为我工作) Sprite quads & depth testing correctly in OpenGL ES 2

+0

你可以使用spritebatch和shape renderer来做到这一点。 – arv

+0

这个例子只是一个简化版本,我的问题,我必须使用spritebatch因为我渲染精灵,不geomtery –

+0

你在运行时创建精灵的纹理(图片)? – arv

回答

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