使用 SpriteBatch 时的 LibGDX 模板缓冲区
这是我之前的问题和帖子的延续,参见这里一个>.感谢我在那里收到的答案,我觉得我能够更接近我的目标,以及进一步学习 OpenGL,但在弄清楚使用模板缓冲区的基础知识后不久,我遇到了一个问题.
This is a continuation of my previous problem and post, seen here. Thanks to the answer I received there I feel I was able to get a little closer to my goal, as well as further my learning of OpenGL, but shortly after figuring out the basics of working with stencil buffers, I've run into a problem.
似乎当我将精灵绘制到模板缓冲区时,它会绘制整个正方形区域,而不是像我无知地希望的那样仅绘制不完全透明的像素.我隐约明白为什么会这样,但我不确定解决方案在哪里.我已经对模板本身进行了很多试验,并且我修改了 spritebatch 用来丢弃低 alpha 片段的着色器,但我似乎看不到更大的图景.
It seems that when I draw a sprite to the stencil buffer, it draws the entire square area, rather than just the pixels that aren't fully transparent as I had ignorantly hoped. I vaguely understand why it happens that way, but I am not sure where the solution lies. I have experimented with the stencil itself quite a bit, and I have modified the shaders that the spritebatch uses to discard low-alpha fragments, but I seem to be failing to see the bigger picture.
作为问题的直观示例,我将继续使用我在上一个问题中使用的示例.现在,试图在彼此之上绘制两个圆圈(所以它们完美融合,没有重叠),我得到了这个:
As a visual example of the problem, I will continue with the examples I used in the previous question. Right now, trying to draw two circles over each other (So they blend perfectly, no overlapping), I am getting this :
所以,基本上,我有没有办法使用 LibGDX 的 Sprite 和 SpriteBatch 功能在复杂形状上使用模板缓冲区(圆圈仅用作示例),还是我需要寻找替代路线?
So, basically, is there a way for me to utilize stencil buffers using the Sprite and SpriteBatch functionality of LibGDX on complex shapes (Circles are only being used as an example), or do I need to look for an alternative route?
编辑 ::
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
Gdx.gl.glDepthMask(true);
batch.begin();
sprite.draw(batch);
sprite2.draw(batch);
batch.end();
Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
推荐答案
模板编写/测试发生在片段级别.当你画一个圆时,你实际上是在画一个四边形的片段,每个片段可能有纹理,也可能没有纹理.问题在于 GPU 在对片段进行模板测试时并不关心您在片段中写入的颜色.因此,您需要丢弃片段以停止对四边形中您不想写入模板值的部分进行模板写入.
Stencil writing/testing happens at the fragment level. When you draw a circle, you are actually drawing a quad of fragments where each fragment may or may not be textured. The issue of the matter is that the GPU doesn't care what color you write into the fragment when it does the stencil test for the fragment. Therefore, you need to discard fragments to stop the stencil writes for the parts of the quad where you don't want to write stencil values.
TL;DR 使用if(gl_FragColor.a <0.01) 丢弃;"在片段着色器中确保生成一圈片段(和模板值).
TL;DR Use "if(gl_FragColor.a < 0.01) discard;" in the fragment shader to make sure a circle of fragments (and stencil values) are generated.
相关文章