好吧,所以我在LWJGL中遇到透明度问题。似乎着色器甚至没有使用alpha通道。例如,如果我将alpha通道设置为0.1,则所有内容仍然显示为实心。但是,我创建的程序只需要对象是可靠的或完全清晰的。通常,我会在片段着色器中使用discard命令,但是,我将渲染到多个目标,因此会丢弃我渲染的两个目标的片段。我也在关注ThnMatrix的教程。
所以我的问题是,为什么在我的着色器中渲染时alpha不起作用?我已经尝试了所有GL_BLEND的东西,但仍然没有运气。谢谢!
P.S。我有另一个着色器,使GUI和透明度/半透明效果正常。我在着色器之间看到的唯一主要区别是顶点着色器。
Fragment Shader:
#version 330 core
in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector[4];
in vec3 toCameraVector;
in float visibility;
layout (location = 0) out vec4 out_Color;
layout (location = 1)out vec4 out_BrightColour;
uniform sampler2D textureSampler;
uniform vec3 lightColor[4];
uniform vec3 attenuation[4];
uniform float shineDamper;
uniform float reflectivity;
uniform float selected;
//effects
uniform float bloomFactor = 0;
uniform vec3 skyColor;
void main(void){
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitVectorToCamera = normalize(toCameraVector);
vec3 totalDiffuse = vec3(0.0);
vec3 totalSpecular = vec3(0.0);
for(int i = 0; i < 4; i++){
float distance = length(toLightVector[i]);
float attFactor = attenuation[i].x + (attenuation[i].y * distance) + (attenuation[i].z * distance * distance);
vec3 unitLightVector = normalize(toLightVector[i]);
float nDot1 = dot(unitNormal, unitLightVector);
float brightness = max(nDot1, 0.0);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
specularFactor = max(specularFactor, 0.0);
float dampedFactor = pow(specularFactor, shineDamper);
totalDiffuse = totalDiffuse + (brightness * lightColor[i])/attFactor;
totalSpecular = totalSpecular + (dampedFactor * lightColor[i] * reflectivity)/attFactor;
}
totalDiffuse = max(totalDiffuse, 0.2);
vec4 textureColor = texture(textureSampler, pass_textureCoords);
out_Color = vec4(totalDiffuse, 1.0) * textureColor + vec4(totalSpecular, 1.0);
out_Color = mix(vec4(skyColor, 1.0), out_Color, visibility);
if(selected > 0.5){
out_Color.r += 0.75;
}
if(out_Color.a<0.5){
discard;
}
//float brightness = (out_Color.r * 0.2126) + (out_Color.g * 0.7152) + (out_Color.b * 0.0722);
out_BrightColour = vec4(0.0);
}
答案 0 :(得分:1)
out_Color = vec4(totalDiffuse, 1.0) * textureColor + vec4(totalSpecular, 1.0);
此等式保证 out_Color.a
始终为&gt; = 1.0。
如果textureColor.a
为0.0,完全透明,则将0乘以1将保留为零。现在,当我们添加1时会发生什么?这就是textureColor.a
是其最低可能值的情况。如果它是1.0,我们得到out_Color.a
的2.0(将被限制回1.0)。