Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float3x3 ConstructCotangentFrame(float3 p, float3 n, float2 uv) {
- // find the edge vectors
- float3 dp1 = ddx(p);
- float3 dp2 = ddy(p);
- float2 duv1 = ddx(uv);
- float2 duv2 = ddy(uv);
- // solve the linear system
- float3 dp2perp = cross(dp2, n);
- float3 dp1perp = cross(n, dp1);
- float3 T = dp2perp * duv1.x + dp1perp * duv2.x;
- float3 B = dp2perp * duv1.y + dp1perp * duv2.y;
- // construct the scale invariant frame
- float invmax = rsqrt(max(dot(T, T), dot(B, B)));
- return float3x3(T * invmax, B * invmax, n);
- }
- float3 PerturbNormal(float3 positionVS, float3 normal, float2 texCoord) {
- float3 n = 2.0f * normalMap.Sample(linearSampler, texCoord).xyz - 1.0f;
- float3x3 TBN = ConstructCotangentFrame(positionVS, normal, texCoord);
- return normalize(mul(n, TBN));
- }
- // Description:
- // Shader entry-point.
- float4 PSMain(PSInput input) : SV_TARGET {
- float3 normal = PerturbNormal(input.positionVS, input.normalVS, input.texCoord);
- return float4(CalculateBlinnPhong(input.positionVS, normal, input.texCoord), 1.0f);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement