normalmap.frag 929 B

123456789101112131415161718192021222324252627282930
  1. #ifdef Use_Bindless_Texture
  2. layout(bindless_sampler) uniform sampler2D normalMap;
  3. layout(bindless_sampler) uniform sampler2D DiffuseForAlpha;
  4. #else
  5. uniform sampler2D normalMap;
  6. uniform sampler2D DiffuseForAlpha;
  7. #endif
  8. in vec3 tangent;
  9. in vec3 bitangent;
  10. in vec2 uv;
  11. out vec3 EncodedNormal;
  12. vec2 EncodeNormal(vec3 n);
  13. void main()
  14. {
  15. // normal in Tangent Space
  16. vec3 TS_normal = 2.0 * texture(normalMap, uv).rgb - 1.0;
  17. float alpha = texture(DiffuseForAlpha, uv).a;
  18. // Because of interpolation, we need to renormalize
  19. vec3 Frag_tangent = normalize(tangent);
  20. vec3 Frag_normal = normalize(cross(Frag_tangent, bitangent));
  21. vec3 Frag_bitangent = cross(Frag_normal, Frag_tangent);
  22. vec3 FragmentNormal = TS_normal.x * Frag_tangent + TS_normal.y * Frag_bitangent - TS_normal.z * Frag_normal;
  23. EncodedNormal.xy = 0.5 * EncodeNormal(normalize(FragmentNormal)) + 0.5;
  24. EncodedNormal.z = 1. - alpha;
  25. }