c# - 半透明地形问题(可能是深度缓冲区问题)
问题描述
我有渲染问题。即时渲染的块有些半透明。只要我使用 Nvidia 显卡,一切都很好。在 Intel(集成)或 AMD 显卡上,它看起来像这样:
如您所见,有正在渲染的块,应该在其他块之后。
这是我的准备方法。
public void prepare()
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.ClearColor(skyColour.X, skyColour.Y, skyColour.Z, 0f);
}
public void enableGLCaps()
{
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.DepthClamp);
GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
}
这是我的顶点着色器:
#version 440
in vec3 position;
in vec2 textureCoords;
out vec2 pass_textureCoords;
out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform float density;
uniform float gradient;
void main() {
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
vec4 positionRelativeToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelativeToCam;
pass_textureCoords = textureCoords;
float distance = length(positionRelativeToCam.xyz);
visibility = exp(-pow((distance*density), gradient));
visibility = clamp(visibility, 0.0, 1.0);
}
这里是我的片段着色器:
#version 440
uniform sampler2D textureSampler;
uniform vec3 skyColour;
in vec2 pass_textureCoords;
in float visibility;
out vec4 color;
void main() {
vec4 textureColour = texture(textureSampler, pass_textureCoords);
if (textureColour.a < 0.5)
discard;
color = mix(vec4(skyColour, 1.0), textureColour, visibility);
color.a = 1;
}
改变近平面没有帮助。
编辑1:
我已经渲染了 z 值并注意到,有些面孔肯定没有正确的值。我将一个区域(16*16*16 块)中的每个纹理渲染为单个 VAO(稍后将使用纹理图集,但我遇到了使用它的 mipmapping 闪烁/重叠)。被渲染为一个 VAO 的面没有相互重叠,因此 z 缓冲区在那里似乎没问题。看起来它在每次渲染调用后都会重新测试。这是我的渲染调用:
GL.DrawElements(BeginMode.Quads, vertices, DrawElementsType.UnsignedInt, 0);
解决方案:
我使用的深度函数是错误的。您应该使用 Lequal。我还必须将深度范围设置为 (0, 1)。
解决方案
推荐阅读
- odoo - 如何保护 Odoo 默认管理员帐户?
- twitter-bootstrap - 在 Bootstrap 4 中重新定位导航栏的元素
- mysql - 更新具有随机唯一数字的 SQL 表
- rust - 如何在结构中的数组块上实现迭代器?
- ios - 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“尝试插入非属性列表对象
- string - python pandas 数据框列的字符串值看起来像字典列表?
- amazon-web-services - AWS 公共子网 - 拒绝服务部署
- ruby - Ruby - 使用地图和大写方法将标题大写
- javascript - 如何用json打印出一个列表?
- ios - Flutter:激活键盘后如何去除键盘后面的白色背景