Pau*_*nta 2 java opengl textures pixmap libgdx
我想在foreground纹理上叠加background纹理.除了这两个纹理,我还有一个mask指示前景的哪些部分应该是透明的.这是我试过的:
// 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");
Run Code Online (Sandbox Code Playgroud)
不用说,结果不是我想要的结果.我应该更改什么,以便在蒙版的alpha值为0的任何地方都可以看到背景,并且在蒙版的alpha值为1的任何地方都可以看到前景.
我认为这里的问题是混合.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");
Run Code Online (Sandbox Code Playgroud)
实际上你甚至不需要创建一个蒙版,但你可以直接"剪切"前景像素图上的矩形.