我希望你能帮助解决这个问题。我目前正在使用延迟渲染器并正在尝试实现 SSAO,但这需要查看空间法线,并且我目前有世界空间法线,我正在尝试解决如何转换它们但一直卡住。
这是我的顶点着色器的主要部分,instanceTransform
是世界矩阵或实例矩阵的转置,因为相同的代码也用于实例模型。
VertexShaderOutput VertexShaderFunctionCommon(VertexShaderInput input, float4x4 instanceTransform)
{
VertexShaderOutput output = (VertexShaderOutput)0;
float4x4 worldViewProjection = mul(instanceTransform, ViewProjection);
output.Position = mul(float4(input.Position.xyz,1), worldViewProjection);
output.Depth = output.Position.zw;
// calculate tangent space to world space matrix using the world space tangent, binormal, and normal as basis vectors
output.TangentToWorld[0] = normalize(mul(input.Tangent, instanceTransform));
output.TangentToWorld[1] = normalize(mul(input.Binormal, instanceTransform));
output.TangentToWorld[2] = normalize(mul(input.Normal, instanceTransform));
return output;
}
像素着色器中的正常计算是:
// read the normal from the normal map
float3 normalFromMap = tex2D(normalSampler, input.TexCoord);
//tranform to [-1,1]
normalFromMap = 2.0f * normalFromMap - 1.0f;
//transform into world space
normalFromMap = mul(normalFromMap, input.TangentToWorld);
//normalize the result
normalFromMap = normalize(normalFromMap);
//output the normal, in [0,1] space
output.Normal.rgb = NormalEncode(normalFromMap);
你能帮忙吗?