ToyEngine/Demo/Engine/Shaders/model.frag

61 lines
1.6 KiB
GLSL
Raw Normal View History

2023-04-16 17:12:29 +08:00
#version 450 core
uniform sampler2D texture_basecolor;
uniform sampler2D texture_metallic_roughness;
uniform sampler2D texture_normal;
uniform bool texture_basecolor_available = false;
uniform bool texture_metallic_roughness_available = false;
uniform bool texture_normal_available = false;
uniform vec3 baseColor;
uniform vec2 metallicRoughness;
layout (location = 0) out vec4 gBaseColor;
layout (location = 1) out vec3 gNormal;
layout (location = 2) out vec3 gPosition;
layout (location = 3) out vec2 gMetallicRoughness;
2023-04-18 23:58:56 +08:00
layout (location = 4) out vec4 gDebug;
2023-04-16 17:12:29 +08:00
in vec2 TexCoords;
in vec3 WorldPos;
in vec3 Normal;
vec3 getNormalFromMap()
{
vec3 tangentNormal = texture(texture_normal, 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);
}
void main()
{
if(texture_basecolor_available)
gBaseColor = texture(texture_basecolor, TexCoords);
else
gBaseColor = vec4(baseColor, 1);
if(gBaseColor.a<0.4)
discard;
gPosition = WorldPos;
if(texture_normal_available)
gNormal = getNormalFromMap();
else
gNormal = Normal;
if(!gl_FrontFacing)
gNormal = -gNormal;
if(texture_metallic_roughness_available)
gMetallicRoughness = texture(texture_metallic_roughness, TexCoords).bg;
else
gMetallicRoughness = metallicRoughness;
2023-04-18 23:58:56 +08:00
gDebug = vec4(0);
2023-04-16 17:12:29 +08:00
}