libgdx专题libgdx游戏开发

gdx实现遮罩特效

2016-09-27  本文已影响321人  纯洁的坏蛋

效果图

原图 遮罩图,蓝色部分其实是没有像素的,为了在文章里面显示方便,手动填充了蓝色颜色 最后得到的效果图

实现

@Overridepublic void update(float dt) {    
  batch.begin();    
  drawBackground(batch);    
  //画遮罩    
  drawAlphaMask(batch);    
  //画前景色    
  drawForeground(batch, 0, 0, mask.getWidth(), mask.getHeight());    
  batch.end();
  }

画遮罩

  private void drawAlphaMask(SpriteBatch batch) {    
  Gdx.gl.glColorMask(false, false, false, true);    
  //change the blending function for our alpha map    
  batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ZERO);   
   //draw alpha mask sprite(s)    
  batch.draw(mask, 0, 0, mask.getWidth(), mask.getHeight());   
   //flush the batch to the GPU    batch.flush();
  }

画需要裁剪的sprite

private void drawForeground(SpriteBatch batch, int clipX, int clipY, int clipWidth, int clipHeight) {  
  Gdx.gl.glColorMask(true, true, true, true);    
  batch.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);    
  batch.draw(img, 240 - img.getWidth() / 2f, 160 - img.getHeight() / 2f);    
  batch.flush();    
  Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
}

代码

https://github.com/tianqiujie/gdxplayground/tree/master/core/src/org/flixel/screens/MaskTest.java

上一篇 下一篇

猜你喜欢

热点阅读