ADD: Normal mapping
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user