纯材质
#version 430 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTexCoord;
layout(std140 , binding = 0) uniform PV
{
mat4 projection;
mat4 view;
};
out vec2 TexCoord;
uniform mat4 model;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}
-----------------------
#version 430 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D albedoMap;
void main()
{
FragColor = vec4(texture(albedoMap, TexCoord).rgb * 0.5,1.0);
}
高光
#version 420 core
layout(location = 0) in vec3 aPos;
layout(location = 2) in vec3 aNormal;
layout(location = 1) in vec2 aTexCoord;
layout(std140 , binding = 0) uniform PV
{
mat4 projection;
mat4 view;
};
out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoord;
uniform mat4 model;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal;
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}
-------------
#version 420 core
out vec4 FragColor;
layout(std140 , binding = 1) uniform BaseLight
{
vec3 lightDir;
vec3 color;
vec3 ambient;
float gloss;
};
layout(std140 , binding = 2) uniform BaseView
{
vec3 viewPos;
};
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;
uniform sampler2D albedoMap;
void main()
{
vec3 ambient = BaseLight.ambient * texture(albedoMap, TexCoords).rgb;
vec3 norm = normalize(Normal);
float diff = max(dot(norm, BaseLight.lightDir), 0.0);
vec3 diffuse = BaseLight.color * diff * texture(albedoMap, TexCoords).rgb;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 halfDir = normalize(viewDir + BaseLight.lightDir);
float spec = pow(max(dot(norm, halfDir), 0.0), BaseLight.gloss);
vec3 specular = BaseLight.color * spec * texture(albedoMap, TexCoords).rgb;
vec3 result = ambient + diffuse + specular;
result = result / (result + vec3(1.0));
result = pow(result, vec3(1.0/2.2));
FragColor = vec4(result, 1.0);
}
高质量
#version 430 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec3 Tangent;
layout (location = 4) in vec3 Bitangent;
layout (location = 5) in ivec4 BoneIDs;
layout (location = 6) in vec4 Weights;
out vec2 TexCoords;
out vec3 WorldPos;
out vec3 Normal;
layout(std140 , binding = 0) uniform PV
{
mat4 projection;
mat4 view;
};
uniform mat4 model;
const int MAX_BONES = 100;
uniform mat4 gBones[MAX_BONES];
void main()
{
mat4 BoneTransform = gBones[ BoneIDs[0] ] * Weights[0];
BoneTransform += gBones[ BoneIDs[1] ] * Weights[1];
BoneTransform += gBones[ BoneIDs[2] ] * Weights[2];
BoneTransform += gBones[ BoneIDs[3] ] * Weights[3];
vec4 tPos = BoneTransform * vec4(aPos, 1.0);
TexCoords = aTexCoords;
WorldPos = vec3(model * tPos);
Normal = mat3(model) * aNormal;
gl_Position = projection * view * model * tPos;
}
------------------------
#version 430 core
out vec4 FragColor;
in vec2 TexCoords;
in vec3 WorldPos;
in vec3 Normal;
uniform sampler2D albedoMap;
uniform sampler2D specularMap;
uniform sampler2D normalMap;
uniform sampler2D heightMap;
uniform sampler2D aoMap;
uniform sampler2D metallicMap;
uniform sampler2D roughnessMap;
layout(std140 , binding = 1) uniform BaseLight
{
vec3 lightDir;
vec3 color;
vec3 ambient;
float gloss;
};
layout(std140 , binding = 2) uniform BaseView
{
vec3 viewPos;
};
uniform vec3 lightPositions[1];
uniform vec3 lightColors[1];
uniform vec3 camPos;
const float PI = 3.14159265359;
vec3 getNormalFromMap()
{
vec3 tangentNormal = texture(normalMap, TexCoords).xyz * 2.0 - 1.0;
vec3 Q1 = dFdx(WorldPos);
vec3 Q2 = dFdy(WorldPos);
vec2 st1 = dFdx(TexCoords);
vec2 st2 = dFdy(TexCoords);
vec3 N = normalize(Normal);
vec3 T = normalize(Q1*st2.t - Q2*st1.t);
vec3 B = -normalize(cross(N, T));
mat3 TBN = mat3(T, B, N);
return normalize(TBN * tangentNormal);
}
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float nom = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return nom / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float nom = NdotV;
float denom = NdotV * (1.0 - k) + k;
return nom / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
void main()
{
vec3 albedo = pow(texture(albedoMap, TexCoords).rgb, vec3(2.2));
float metallic = texture(metallicMap, TexCoords).r;
float roughness = texture(roughnessMap, TexCoords).r;
float ao = texture(aoMap, TexCoords).r;
vec3 N = getNormalFromMap();
vec3 V = normalize(camPos - WorldPos);
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
vec3 Lo = vec3(0.0);
for(int i = 0; i < 1; ++i)
{
vec3 L = normalize(lightPositions[i] - WorldPos);
vec3 H = normalize(V + L);
float distance = length(lightPositions[i] - WorldPos);
float attenuation = 1.0 / (distance * distance);
vec3 radiance = lightColors[i] * 1;
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 numerator = NDF * G * F;
float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
vec3 specular = numerator / denominator;
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
}
vec3 ambient = vec3(0.03) * albedo * ao;
vec3 color = ambient + Lo;
color = color / (color + vec3(1.0));
color = pow(color, vec3(1.0/2.2));
FragColor = vec4(color, 1.0);
}
|