Advertisement
Guest User

Untitled

a guest
Dec 11th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. float3x3 ConstructCotangentFrame(float3 p, float3 n, float2 uv) {
  2. // find the edge vectors
  3. float3 dp1 = ddx(p);
  4. float3 dp2 = ddy(p);
  5. float2 duv1 = ddx(uv);
  6. float2 duv2 = ddy(uv);
  7.  
  8. // solve the linear system
  9. float3 dp2perp = cross(dp2, n);
  10. float3 dp1perp = cross(n, dp1);
  11. float3 T = dp2perp * duv1.x + dp1perp * duv2.x;
  12. float3 B = dp2perp * duv1.y + dp1perp * duv2.y;
  13.  
  14. // construct the scale invariant frame
  15. float invmax = rsqrt(max(dot(T, T), dot(B, B)));
  16. return float3x3(T * invmax, B * invmax, n);
  17. }
  18.  
  19. float3 PerturbNormal(float3 positionVS, float3 normal, float2 texCoord) {
  20. float3 n = 2.0f * normalMap.Sample(linearSampler, texCoord).xyz - 1.0f;
  21.  
  22. float3x3 TBN = ConstructCotangentFrame(positionVS, normal, texCoord);
  23.  
  24. return normalize(mul(n, TBN));
  25. }
  26.  
  27. // Description:
  28. // Shader entry-point.
  29. float4 PSMain(PSInput input) : SV_TARGET {
  30. float3 normal = PerturbNormal(input.positionVS, input.normalVS, input.texCoord);
  31.  
  32. return float4(CalculateBlinnPhong(input.positionVS, normal, input.texCoord), 1.0f);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement