2013-12-09 88 views
2

我想有一个​​质感覆盖在一个background质感。除了这两个纹理之外,我还有一个mask,指示前景的哪些部分应该是透明的。这是我试过的:修改纹理的透明度LibGDX

// Initially, the mask should have an alpha of 1 
Pixmap mask = new Pixmap(128, 128, Pixmap.Format.Alpha); 

// Cut a rectangle of alpha value 0 
mask.setColor(new Color(0f, 0f, 0f, 0f)); 
mask.fillRectangle(0, 0, 32, 32); 

// Load the foreground. The foreground should the same alpha values 
// as the mask. If the mask has an alpha value of 1, then the foreground is 
// visible. If the mask is 0, then the foreground is invisible. 
Pixmap fg = new Pixmap(Gdx.files.internal("foreground.png")); 
fg.drawPixmap(mask, fg.getWidth(), fg.getHeight()); 
Texture foreground = new Texture(fg); 

Texture background = new Texture("background.png"); 

不用说,结果不是我想要的。我应该怎么改变,这样的背景是可见的地方面膜具有的alpha值为0,前景是看得见的地方面具有1

回答

6

alpha值我觉得这里的问题是勾兑。 Pixmap.setBlending()默认设置为SourceOver。这意味着绘制一个alpha 0的矩形根本不会改变,因为您绘制了一个不可见的矩形。尝试将其设置为Pixmap.Blending.None以切出矩形。

// Initially, the mask should have an alpha of 1 
Pixmap mask = new Pixmap(128, 128, Pixmap.Format.Alpha); 

// Cut a rectangle of alpha value 0 
mask.setBlending(Pixmap.Blending.None); 
mask.setColor(new Color(0f, 0f, 0f, 0f)); 
mask.fillRectangle(0, 0, 32, 32); 

Pixmap fg = new Pixmap(Gdx.files.internal("foreground.png")); 
fg.drawPixmap(mask, fg.getWidth(), fg.getHeight()); 
mask.setBlending(Pixmap.Blending.SourceOver); 

Texture foreground = new Texture(fg); 
Texture background = new Texture("background.png"); 

其实你甚至都不需要创建一个面具,但对前景像素映射,你可以直接“切出”的矩形。