微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

法线贴图覆盖 DirectX 11 中的平滑边缘

如何解决法线贴图覆盖 DirectX 11 中的平滑边缘

我的对象是一个光滑的桶,只有颜色和原始法线看起来像这样:

barrel with the original normals

当我尝试通过纹理添加法线细节时,平滑法线会像这样被覆盖:

barrel with the overwritten normals

有没有更好的方法来合并新的法线纹理而不覆盖平滑的边缘?

代码主要来自此处的 rastertek 教程https://www.rastertek.com/tutdx11.html

顶点着色器

cbuffer MatrixBuffer
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
};

struct VertexInputType
{
    float4 position : POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : norMAL;
    float3 tangent : TANGENT;
    float3 binormal : BInorMAL;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : norMAL;
    float3 tangent : TANGENT;
    float3 binormal : BInorMAL;
};

PixelInputType normalMapVertexShader(VertexInputType input)
{
    PixelInputType output;
    

    // Change the position vector to be 4 units for proper matrix calculations.
    input.position.w = 1.0f;

    // Calculate the position of the vertex against the world,view,and projection matrices.
    output.position = mul(input.position,worldMatrix);
    output.position = mul(output.position,viewMatrix);
    output.position = mul(output.position,projectionMatrix);
    
    // Store the texture coordinates for the pixel shader.
    output.tex = input.tex;
    
    // Calculate the normal vector against the world matrix only and then normalize the final value.
    output.normal = mul(input.normal,(float3x3)worldMatrix);
    output.normal = normalize(output.normal);

    // Calculate the tangent vector against the world matrix only and then normalize the final value.
    output.tangent = mul(input.tangent,(float3x3)worldMatrix);
    output.tangent = normalize(output.tangent);

    // Calculate the binormal vector against the world matrix only and then normalize the final value.
    output.binormal = mul(input.binormal,(float3x3)worldMatrix);
    output.binormal = cross(output.tangent,output.normal);



    return output;
}

像素着色器

Texture2D shaderTextures[2];
SamplerState SampleType;

cbuffer LightBuffer
{
    float4 diffuseColor;
    float3 lightDirection;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : norMAL;
    float3 tangent : TANGENT;
    float3 binormal : BInorMAL;
};


float4 normalMapPixelShader(PixelInputType input) : SV_TARGET
{
    float4 textureColor;
    float4 bumpMap;
    float3 bumpnormal;
    float3 lightDir;
    float lightIntensity;
    float4 color;


    // Sample the texture pixel at this location.
    textureColor = shaderTextures[0].Sample(SampleType,input.tex);

    // Sample the pixel in the bump map.
    bumpMap = shaderTextures[1].Sample(SampleType,input.tex);

    // Expand the range of the normal value from (0,+1) to (-1,+1).
    bumpMap = (bumpMap * 2.0f) - 1.0f;


    // Calculate the normal from the data in the bump map.
    bumpnormal = (bumpMap.x * input.tangent) + (bumpMap.y * input.binormal) + (bumpMap.z * input.normal);

    // normalize the resulting bump normal.
    bumpnormal = normalize(bumpnormal);

    // Invert the light direction for calculations.
    lightDir = -lightDirection;




    // Calculate the amount of light on this pixel based on the bump map normal value.
    lightIntensity = saturate(dot(bumpnormal,lightDir));


    // Determine the final diffuse color based on the diffuse color and the amount of light intensity.
    color = saturate(diffuseColor * lightIntensity);

    // Combine the final bump light color with the texture color.
    color = color * textureColor;



    return color;
}

解决方法

问题在于这个公式:

// Calculate the normal from the data in the bump map.
bumpNormal = (bumpMap.x * input.tangent) + (bumpMap.y * input.binormal) + (bumpMap.z * input.normal);

更好的方法是为局部切线空间创建一个矩阵,然后从纹理转换局部法线。

// Given a local normal,transform it into a tangent space given by surface normal and tangent
float3 PeturbNormal(float3 localNormal,float3 surfaceNormalWS,float3 surfaceTangentWS)
{
    float3 normal = normalize(surfaceNormalWS);
    float3 tangent = normalize(surfaceTangentWS);
    float3 binormal = cross(normal,tangent);     // reconstructed from normal & tangent
    float3x3 TBN = { tangent,binormal,normal }; // world "frame" for local normal 

    return mul(localNormal,TBN);                // transform to local to world (tangent space)
}


// This function converts the R and G channels from a normal texture map
// Assumes the input data is "UNORM" (i.e. needs a x2 bias to get back -1..1)
// Also reconstructs the B channel in case the normal map was compressed as BC5_UNORM
float3 TwoChannelNormalX2(float2 normal)
{
    float2 xy = 2.0f * normal - 1.0f;
    float z = sqrt(1 - dot(xy,xy));
    return float3(xy.x,xy.y,z);
}
...

    // Before lighting,peturb the surface's normal by the one given in normal map.
    float3 localNormal = TwoChannelNormalX2(NormalTexture.Sample(Sampler,pin.TexCoord).xy);
    
    float3 N = PeturbNormal( localNormal,pin.NormalWS,pin.TangentWS);

取自 DirectX Tool KitNormalMap

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。