更新时间:2023-01-27 13:52:04
您正在尝试做的事情看起来像是非常巧妙地使用了混合.但我相信你应用它的确切方式是被设计破坏的".让我们来看看这些步骤:
问题在于,对于您在第 3 步中擦除的部分,之前的背景不会恢复.实在不行,因为你在第 2 步把它擦掉了.整个纹理区域的背景在第 2 步被替换了,一旦它消失了就无法恢复.
现在的问题当然是如何解决这个问题.我可以想到两种常规方法:
所以这些将是标准解决方案.但是受到您尝试中的想法的启发,我认为实际上可以通过混合来实现它.我想出的方法使用了稍微不同的序列和不同的混合函数.我还没有尝试过,但我认为它应该可以工作:
DST_ALPHA
) 中的 alpha 分量进行混合.您需要一个带有 alpha 组件的帧缓冲区才能使其工作.确保在设置上下文/表面时为帧缓冲区请求 alpha 位.
代码序列如下所示:
//绘制背景.glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_TRUE);glDisable(GL_BLEND);//绘制蒙版.glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);glEnable(GL_BLEND);glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);//绘制纹理.
In libGdx, i'm trying to create a shaped texture: Take a fully-visible rectangle texture and mask it to obtain a shaped textured, as shown here:
Here I test it on rectangle, but i will want to use it on any shape. I have looked into this tutorial and came with an idea to first draw the texture, and then the mask with blanding function:
batch.setBlendFunction(GL20.GL_ZERO, GL20.GL_SRC_ALPHA);
Crucial part of the test code:
batch0.enableBlending();
batch0.begin();
batch0.draw(original, 0, 0); //to see the original
batch0.draw(mask, width1, 0); //and the mask
batch0.draw(original, 0, height1); //base for the result
batch0.setBlendFunction(GL20.GL_ZERO, GL20.GL_SRC_ALPHA);
batch0.draw(mask, 0, height1); //draw mask on result
batch0.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
batch0.end();
The center ot the texture get's selected well, but instead of transparent color around, i see black:
Why is the result blank and not transparent?
(Full code - Warning: very messy)
What you're trying to do looks like a pretty clever use of blending. But I believe the exact way you apply it is "broken by design". Let's walk through the steps:
The problem is that for the parts you erase in step 3, the previous background is not coming back. It really can't, because you wiped it out in step 2. The background of the whole texture area was replaced in step 2, and once it's gone there's no way to bring it back.
Now the question is of course how you can fix this. There are two conventional approaches I can think of:
So those would be the standard solutions. But inspired by the idea in your attempt, I think it's actually possible to get this to work with just blending. The approach that I came up with uses a slightly different sequence and different blend functions. I haven't tried this out, but I think it should work:
DST_ALPHA
) for blending.You will need a framebuffer with an alpha component for this to work. Make sure that you request alpha bits for your framebuffer when setting up your context/surface.
The code sequence would look like this:
// Draw background.
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
glDisable(GL_BLEND);
// Draw mask.
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glEnable(GL_BLEND);
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
// Draw texture.