LibGDX Stencil在使用SpriteBatch时缓冲 [英] LibGDX Stencil Buffers while using SpriteBatch

查看:244
本文介绍了LibGDX Stencil在使用SpriteBatch时缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前的问题和帖子的延续,见此处。感谢我在那里收到的答案,我觉得我能够更接近我的目标,以及进一步学习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在复杂形状上的精灵和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.

这篇关于LibGDX Stencil在使用SpriteBatch时缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆