ADD: Normal mapping

This commit is contained in:
2025-11-01 17:32:14 +09:00
parent d5ff6263ee
commit fbc937974d
28 changed files with 2802 additions and 264 deletions

View File

@@ -6,6 +6,7 @@ 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;
@@ -20,7 +21,17 @@ void main() {
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(normalize(inNormal), roughness);
outNorm = vec4(Nw, roughness);
outAlbedo = vec4(albedo, metallic);
}

View File

@@ -28,8 +28,10 @@ layout(set = 1, binding = 0) uniform GLTFMaterialData{
vec4 colorFactors;
vec4 metal_rough_factors;
vec4 extra[14];
} materialData;
layout(set = 1, binding = 1) uniform sampler2D colorTex;
layout(set = 1, binding = 2) uniform sampler2D metalRoughTex;
layout(set = 1, binding = 3) uniform sampler2D normalMap; // tangent-space normal, UNORM

View File

@@ -7,6 +7,7 @@ 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 outFragColor;
@@ -57,7 +58,14 @@ void main()
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);
vec3 N = normalize(inNormal);
// Normal mapping path for forward/transparent pipeline
vec3 Nm = texture(normalMap, inUV).xyz * 2.0 - 1.0;
float normalScale = max(materialData.extra[0].x, 0.0);
Nm.xy *= normalScale;
vec3 Nn = normalize(inNormal);
vec3 T = normalize(inTangent.xyz);
vec3 B = normalize(cross(Nn, T)) * inTangent.w;
vec3 N = normalize(T * Nm.x + B * Nm.y + Nn * Nm.z);
vec3 camPos = vec3(inverse(sceneData.view)[3]);
vec3 V = normalize(camPos - inWorldPos);
vec3 L = normalize(-sceneData.sunlightDirection.xyz);

View File

@@ -9,6 +9,7 @@ layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 outColor;
layout (location = 2) out vec2 outUV;
layout (location = 3) out vec3 outWorldPos;
layout (location = 4) out vec4 outTangent; // xyz: world tangent, w: sign
struct Vertex {
@@ -17,6 +18,7 @@ struct Vertex {
vec3 normal;
float uv_y;
vec4 color;
vec4 tangent;
};
layout(buffer_reference, std430) readonly buffer VertexBuffer{
@@ -38,6 +40,8 @@ void main()
gl_Position = sceneData.viewproj * worldPos;
outNormal = (PushConstants.render_matrix * vec4(v.normal, 0.f)).xyz;
vec3 worldTangent = (PushConstants.render_matrix * vec4(v.tangent.xyz, 0.f)).xyz;
outTangent = vec4(normalize(worldTangent), v.tangent.w);
// Pass pure vertex color; apply baseColorFactor only in fragment
outColor = v.color.xyz;
outUV.x = v.uv_x;

View File

@@ -9,6 +9,7 @@ struct Vertex {
vec3 position; float uv_x;
vec3 normal; float uv_y;
vec4 color;
vec4 tangent;
};
layout(buffer_reference, std430) readonly buffer VertexBuffer{