cleanup normal calculations
This commit is contained in:
parent
a3dcaebd57
commit
22c946ec1a
4 changed files with 20 additions and 21 deletions
|
@ -60,4 +60,14 @@ float getFresnel(float3 normal, float3 view, float specularValue)
|
||||||
return specularValue + (1 - specularValue) * pow(1 - saturate(dot(normal, view)), 5);
|
return specularValue + (1 - specularValue) * pow(1 - saturate(dot(normal, view)), 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gets normal: n*TBN, where n = sampled normal map, N = normal vector, T = processed tangent vector (t*N-dot(t,N), B = processed bitangent vector (cross(T,N))
|
||||||
|
float3 getNormal(SamplerState normalSampler, Texture2D map, float2 uv, float3 normal, float3 tangent, float intensity)
|
||||||
|
{
|
||||||
|
float3 n = map.Sample(normalSampler, uv).rgb * 2 - 1;
|
||||||
|
float3 T = normalize(tangent - normal * dot(tangent, normal)) * intensity;
|
||||||
|
float3 B = cross(T, normal);
|
||||||
|
float3x3 TBN = float3x3(T, B, normal);
|
||||||
|
return mul(n, TBN);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -35,11 +35,7 @@ float4 main(VertexToPixel input) : SV_TARGET
|
||||||
|
|
||||||
float4 albedo = pow(Albedo.Sample(Sampler, input.uv), 2.2f);
|
float4 albedo = pow(Albedo.Sample(Sampler, input.uv), 2.2f);
|
||||||
|
|
||||||
float3 N = Normal.Sample(Sampler, input.uv).rgb * 2 - 1;
|
float3 normal = getNormal(Sampler, Normal, input.uv, input.normal, input.tangent, normalIntensity);
|
||||||
float3 T = normalize(input.tangent - input.normal * dot(input.tangent, input.normal)) * normalIntensity;
|
|
||||||
float3 B = cross(T, input.normal);
|
|
||||||
float3x3 TBN = float3x3(T, B, input.normal);
|
|
||||||
input.normal = mul(N, TBN);
|
|
||||||
|
|
||||||
float roughness = Roughness.Sample(Sampler, input.uv).r;
|
float roughness = Roughness.Sample(Sampler, input.uv).r;
|
||||||
float metalness = Metalness.Sample(Sampler, input.uv).r;
|
float metalness = Metalness.Sample(Sampler, input.uv).r;
|
||||||
|
@ -51,10 +47,10 @@ float4 main(VertexToPixel input) : SV_TARGET
|
||||||
switch (lights[i].Type)
|
switch (lights[i].Type)
|
||||||
{
|
{
|
||||||
case LIGHT_TYPE_DIRECTIONAL:
|
case LIGHT_TYPE_DIRECTIONAL:
|
||||||
light += directionalLightPBR(lights[i], input.normal, view, roughness, metalness, albedo, specular);
|
light += directionalLightPBR(lights[i], normal, view, roughness, metalness, albedo, specular);
|
||||||
break;
|
break;
|
||||||
case LIGHT_TYPE_POINT:
|
case LIGHT_TYPE_POINT:
|
||||||
light += pointLightPBR(lights[i], input.normal, view, roughness, metalness, albedo, specular, input.worldPosition);
|
light += pointLightPBR(lights[i], normal, view, roughness, metalness, albedo, specular, input.worldPosition);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,7 @@ float4 main(VertexToPixel input) : SV_TARGET
|
||||||
// ensure input normals are normalized
|
// ensure input normals are normalized
|
||||||
input.normal = normalize(input.normal);
|
input.normal = normalize(input.normal);
|
||||||
input.tangent = normalize(input.tangent);
|
input.tangent = normalize(input.tangent);
|
||||||
|
float3 normal = input.normal;
|
||||||
|
|
||||||
float3 surface = tint;
|
float3 surface = tint;
|
||||||
float alphaValue = alpha;
|
float alphaValue = alpha;
|
||||||
|
@ -59,11 +60,7 @@ float4 main(VertexToPixel input) : SV_TARGET
|
||||||
|
|
||||||
if (hasNormalMap > 0)
|
if (hasNormalMap > 0)
|
||||||
{
|
{
|
||||||
float3 unpackedNormal = Normal.Sample(BasicSampler, input.uv).rgb * 2 - 1;
|
normal = getNormal(BasicSampler, Normal, input.uv, input.normal, input.tangent, normalIntensity);
|
||||||
float3 T = normalize(input.tangent - input.normal * dot(input.tangent, input.normal)) * normalIntensity;
|
|
||||||
float3 B = cross(T, input.normal);
|
|
||||||
float3x3 TBN = float3x3(T, B, input.normal);
|
|
||||||
input.normal = mul(unpackedNormal, TBN);
|
|
||||||
}
|
}
|
||||||
input.uv = input.uv * scale + offset;
|
input.uv = input.uv * scale + offset;
|
||||||
|
|
||||||
|
@ -82,10 +79,10 @@ float4 main(VertexToPixel input) : SV_TARGET
|
||||||
switch (lights[i].Type)
|
switch (lights[i].Type)
|
||||||
{
|
{
|
||||||
case LIGHT_TYPE_DIRECTIONAL:
|
case LIGHT_TYPE_DIRECTIONAL:
|
||||||
light += calculateDirectionalLight(lights[i], -input.normal, view, roughness, surface, specular);
|
light += calculateDirectionalLight(lights[i], -normal, view, roughness, surface, specular);
|
||||||
break;
|
break;
|
||||||
case LIGHT_TYPE_POINT:
|
case LIGHT_TYPE_POINT:
|
||||||
light += calculatePointLight(lights[i], input.normal, view, input.worldPosition, roughness, surface, specular);
|
light += calculatePointLight(lights[i], normal, view, input.worldPosition, roughness, surface, specular);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,9 +91,9 @@ float4 main(VertexToPixel input) : SV_TARGET
|
||||||
|
|
||||||
if (hasReflectionMap > 0)
|
if (hasReflectionMap > 0)
|
||||||
{
|
{
|
||||||
float3 reflVec = getReflection(view, input.normal);
|
float3 reflVec = getReflection(view, normal);
|
||||||
float3 reflCol = Reflection.Sample(BasicSampler, reflVec).rgba;
|
float3 reflCol = Reflection.Sample(BasicSampler, reflVec).rgba;
|
||||||
final = lerp(final, reflCol, getFresnel(input.normal, view, F0_NON_METAL));
|
final = lerp(final, reflCol, getFresnel(normal, view, F0_NON_METAL));
|
||||||
}
|
}
|
||||||
|
|
||||||
return float4(pow(final, 1.0f/2.2f), alphaValue);
|
return float4(pow(final, 1.0f/2.2f), alphaValue);
|
||||||
|
|
|
@ -85,11 +85,7 @@ float4 main(VertexToPixel input) : SV_TARGET
|
||||||
|
|
||||||
if (hasNormalMap > 0)
|
if (hasNormalMap > 0)
|
||||||
{
|
{
|
||||||
float3 unpackedNormal = Normal.Sample(BasicSampler, input.uv).rgb * 2 - 1;
|
normal = getNormal(BasicSampler, Normal, input.uv, input.normal, input.tangent, normalIntensity);
|
||||||
float3 T = normalize(input.tangent - input.normal * dot(input.tangent, input.normal)) * normalIntensity;
|
|
||||||
float3 B = cross(T, input.normal);
|
|
||||||
float3x3 TBN = float3x3(T, B, input.normal);
|
|
||||||
normal = mul(unpackedNormal, TBN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float specularValue = 1;
|
float specularValue = 1;
|
||||||
|
|
Reference in a new issue