1 回答
TA贡献1895条经验 获得超7个赞
发生这种情况是因为您在顶点着色器中对纹理进行采样,这意味着您只能在每个三角形的角上获得三种颜色。其他像素被插值。
为了获得更好的质量,应该将纹理采样移动到片段着色器,并且应该插值 uv 坐标而不是颜色:
顶点着色器:
attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec3 a_texCoord0;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
varying vec3 fragPos;
varying vec3 normal;
varying vec2 texcoord0;
void main()
{
gl_Position = projection * view * model * vec4(a_position, 1.0);
fragPos = vec3(model * vec4(a_position, 1.0));
normal = a_normal;
texcoord0 = a_texCoord0;
}
片段着色器:
varying vec3 normal;
varying vec2 texcoord0;
varying vec3 fragPos;
uniform sampler2D u_texture;
uniform vec3 lightPos;
uniform vec3 lightColor;
void main()
{
vec3 color = texture(u_texture, texcoord0).rgb;
// Ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
// Diffuse
vec3 norm = normalize(normal);
vec3 lightDir = normalize(lightPos - fragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
//vec3 result = (ambient + diffuse) * color;
vec3 result = color;
gl_FragColor = vec4(result, 1.0);
}
添加回答
举报