2013-12-09 101 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"); 

其實你甚至都不需要創建一個面具,但對前景像素映射,你可以直接「切出」的矩形。