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);
}
添加回答
舉報