在法線貼圖實作中,您可能忽略了幾個關鍵方面:
顏色混合:
環境和鏡面反射:
函數:
#version 430 uniform sampler2D diffuseMap; uniform sampler2D normalMap; uniform mat4 ModelMatrix; uniform vec3 CameraPosition; uniform struct Light { float ambient; vec3 position; } light; uniform float shininess; in vec2 fsCoords; in vec3 fsVertex; in mat3 TBNMatrix; out vec4 color; void main() { // Base color vec3 brownColor = vec3(153.0 / 255.0, 102.0 / 255.0, 51.0 / 255.0); vec3 baseColor = brownColor + 0.25; // Add a fixed base color // Normal vec3 normal = texture(normalMap, fsCoords).rgb * 2.0 - 1.0; // Surface position and lighting vec3 surfacePos = vec3(ModelMatrix * vec4(fsVertex, 1.0)); vec3 surfaceToLight = normalize(TBNMatrix * (light.position - surfacePos)); // Unit vector vec3 eyePos = TBNMatrix * CameraPosition; // Diffuse float diffuse = max(0.0, dot(normal, surfaceToLight)); // Specular float specular; vec3 incidentVector = -surfaceToLight; // Unit vec3 reflectionVector = reflect(incidentVector, normal); // Unit vector vec3 surfaceToCamera = normalize(eyePos - surfacePos); // Unit vector float cosAngle = max(0.0, dot(surfaceToCamera, reflectionVector)); if (diffuse > 0.0) specular = pow(cosAngle, shininess); // Lighting vec3 finalColor = baseColor * light.ambient; finalColor += (diffuse + specular) * light.position; // Output color color = vec4(finalColor, 1.0); }
以上是為什麼我的法線貼圖渲染不正確?的詳細內容。更多資訊請關注PHP中文網其他相關文章!