如何在透明纹理上停止OpenGL背景出血 [英] How to stop OpenGL background bleed on transparent textures

查看:133
本文介绍了如何在透明纹理上停止OpenGL背景出血的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS OpenGL ES 2.0 3D游戏,并且正在努力使透明纹理能够很好地工作,在这个特殊的栅栏示例中.

I have an iOS OpenGL ES 2.0 3D game and am working to get transparent textures working nicely, in this particular example for a fence.

我将从最终结果开始.绿色背景/透明色的碎片穿过围栏的边缘-注意它不是全部边缘,有些还可以:

I'll start with the final result. The bits of green background/clear color are coming through around the edges of the fence - note how it isn't ALL edges and some of it is ok:

右上角没有出血的原因是操作顺序.从下面的照片中可以看到,绘制顺序包括一些在栅栏之前绘制的建筑物.但是大部分都在篱笆后面:

The reason for the lack of bleed in the top right is order of operations. As you can see from the following shots, the order of draw includes some buildings that get drawn BEFORE the fence. But most of it is after the fence:

因此,一种解决方案是始终最后绘制我的透明纹理对象.我想探索其他解决方案,因为我的管道可能并不总是允许这样做.我正在寻找其他建议来解决此问题而不对我的抽奖进行排序.

So one solution is to always draw my transparent textured objects last. I would like to explore other solutions, as my pipeline might not always allow this. I'm looking for other suggestions to solve this problem without sorting my draws.

这可能是深度或混合功能,但是我尝试了很多东西,但似乎无济于事(不同的混合功能,不同的废弃alpha级别,不同的背景颜色,不同的纹理设置).

This is likely a depth or blend function, but i've tried a ton of stuff and nothing seems to work (different blend functions, different discard alpha levels, different background colors, different texture settings).

以下是我实施的一些细节.

Here are some specifics of my implementation.

在我的碎片着色器中,我扔掉了具有透明度的片段-这样,它们就不会渲染到深处:

In my frag shader I'm throwing out fragments that have transparency - this way they won't render to depth:

lowp vec4 texVal = texture2D(sTexture, texCoord);
if(texVal.w < 0.5)
    discard;

我正在使用一个带有mipmapping的巨型PVR纹理图集-纹理本身应该只具有0或1的alpha值,但是混合时可能会导致这种情况:

I'm using one giant PVR texture atlas with mipmapping - the texture itself SHOULD just have 0 or 1 for alpha, but something with the blending could be causing this:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

渲染时我正在使用以下混合:

I'm using the following blending when rendering:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

任何解决这个问题的建议都很棒!

Any suggestions to fix this bleed would be great!

编辑-按照注释LINEAR/NEAREST的建议,尝试对纹理使用不同的最小值过滤器,但结果相同.注意我也尝试过NEAREST/NEAREST,但没有运气:

EDIT - tried a different min filter for the texture as suggested in the comments, LINEAR/NEAREST, but same result. Note I have also tried NEAREST/NEAREST and no luck:

推荐答案

尝试增加alpha过滤器限制,

try increasing the alpha filter limit,

lowp vec4 texVal = texture2D(sTexture, texCoord); if(texVal.w < 0.9) discard;

lowp vec4 texVal = texture2D(sTexture, texCoord); if(texVal.w < 0.9) discard;

这篇关于如何在透明纹理上停止OpenGL背景出血的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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