Files
QuaternionEngine/shaders/gbuffer.frag
2025-11-01 17:32:14 +09:00

38 lines
1.4 KiB
GLSL

#version 450
#extension GL_GOOGLE_include_directive : require
#include "input_structures.glsl"
layout(location = 0) in vec3 inNormal;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inUV;
layout(location = 3) in vec3 inWorldPos;
layout(location = 4) in vec4 inTangent;
layout(location = 0) out vec4 outPos;
layout(location = 1) out vec4 outNorm;
layout(location = 2) out vec4 outAlbedo;
void main() {
// Apply baseColor texture and baseColorFactor once
vec3 albedo = inColor * texture(colorTex, inUV).rgb * materialData.colorFactors.rgb;
// glTF metallic-roughness in G (roughness) and B (metallic)
vec2 mrTex = texture(metalRoughTex, inUV).gb;
float roughness = clamp(mrTex.x * materialData.metal_rough_factors.y, 0.04, 1.0);
float metallic = clamp(mrTex.y * materialData.metal_rough_factors.x, 0.0, 1.0);
// Normal mapping: decode tangent-space normal and transform to world space
// Expect UNORM normal map (not sRGB). Flat fallback is (0.5, 0.5, 1.0).
vec3 Nm = texture(normalMap, inUV).xyz * 2.0 - 1.0;
float normalScale = max(materialData.extra[0].x, 0.0);
Nm.xy *= normalScale;
vec3 N = normalize(inNormal);
vec3 T = normalize(inTangent.xyz);
vec3 B = normalize(cross(N, T)) * inTangent.w;
vec3 Nw = normalize(T * Nm.x + B * Nm.y + N * Nm.z);
outPos = vec4(inWorldPos, 1.0);
outNorm = vec4(Nw, roughness);
outAlbedo = vec4(albedo, metallic);
}