2013-12-09 2 views
2

foreground 텍스처를 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"); 

말할 필요도없이 결과는 내가 원하는 것입니다. 마스크의 알파 값이 0이고 마스크의 알파 값이 1 인 모든 위치에서 배경이 보이도록 전경을 변경해야합니까?

답변

6

여기서 문제는 블렌딩이라고 생각합니다. Pixmap.setBlending()은 기본적으로 SourceOver으로 설정됩니다. 즉, 보이지 않는 사각형을 그리면 아무런 변화가없는 알파 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"); 

사실 당신은 심지어 마스크를 만들 필요가 없습니다,하지만 당신은 직접 전경 픽스맵에 사각형을 "잘라"수 있습니다.