MaterialFragmentFunctions.glsl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /// @file
  2. /// The file contains common functions for fragment operations
  3. #pragma anki include "shaders/Pack.glsl"
  4. #pragma anki include "shaders/MaterialCommonFunctions.glsl"
  5. /// @param[in] normal The fragment's normal in view space
  6. /// @param[in] tangent The tangent
  7. /// @param[in] tangent Extra stuff for the tangent
  8. /// @param[in] map The map
  9. /// @param[in] texCoords Texture coordinates
  10. #if defined(PASS_COLOR)
  11. # define getNormalFromTexture_DEFINED
  12. vec3 getNormalFromTexture(in vec3 normal, in vec3 tangent, in float tangentW,
  13. in sampler2D map, in vec2 texCoords)
  14. {
  15. # if LOD > 0
  16. return normalize(normal);
  17. # else
  18. vec3 n = normalize(normal);
  19. vec3 t = normalize(tangent);
  20. vec3 b = cross(n, t) * tangentW;
  21. mat3 tbnMat = mat3(t, b, n);
  22. vec3 nAtTangentspace = (texture(map, texCoords).rgb - 0.5) * 2.0;
  23. return normalize(tbnMat * nAtTangentspace);
  24. # endif
  25. }
  26. #endif
  27. /// Just normalize
  28. #if defined(PASS_COLOR)
  29. # define getNormalSimple_DEFINED
  30. vec3 getNormalSimple(in vec3 normal)
  31. {
  32. return normalize(normal);
  33. }
  34. #endif
  35. /// Environment mapping calculations
  36. /// @param[in] vertPosViewSpace Fragment position in view space
  37. /// @param[in] normal Fragment's normal in view space as well
  38. /// @param[in] map The env map
  39. /// @return The color
  40. #if defined(PASS_COLOR)
  41. # define getEnvironmentColor_DEFINED
  42. vec3 getEnvironmentColor(in vec3 vertPosViewSpace, in vec3 normal,
  43. in sampler2D map)
  44. {
  45. // In case of normal mapping I could play with vertex's normal but this
  46. // gives better results and its allready computed
  47. vec3 u = normalize(vertPosViewSpace);
  48. vec3 r = reflect(u, normal);
  49. r.z += 1.0;
  50. float m = 2.0 * length(r);
  51. vec2 semTexCoords = r.xy / m + 0.5;
  52. vec3 semCol = texture(map, semTexCoords).rgb;
  53. return semCol;
  54. }
  55. #endif
  56. /// Using a 4-channel texture and a tolerance discard the fragment if the
  57. /// texture's alpha is less than the tolerance
  58. /// @param[in] map The diffuse map
  59. /// @param[in] tolerance Tolerance value
  60. /// @param[in] texCoords Texture coordinates
  61. /// @return The RGB channels of the map
  62. #define getDiffuseColorAndDoAlphaTesting_DEFINED
  63. vec3 getDiffuseColorAndDoAlphaTesting(
  64. in sampler2D map,
  65. in vec2 texCoords,
  66. in float tolerance)
  67. {
  68. #if defined(PASS_COLOR)
  69. vec4 col = texture(map, texCoords);
  70. if(col.a < tolerance)
  71. {
  72. discard;
  73. }
  74. return col.rgb;
  75. #else // Depth
  76. # if LOD > 0
  77. return vec3(0.0);
  78. # else
  79. float a = texture(map, texCoords).a;
  80. if(a < tolerance)
  81. {
  82. discard;
  83. }
  84. return vec3(0.0);
  85. # endif
  86. #endif
  87. }
  88. /// Just read the RGB color from texture
  89. #if defined(PASS_COLOR)
  90. # define readRgbFromTexture_DEFINED
  91. vec3 readRgbFromTexture(in sampler2D tex, in vec2 texCoords)
  92. {
  93. return texture(tex, texCoords).rgb;
  94. }
  95. #endif
  96. /// Write the data to FAIs
  97. #if defined(PASS_COLOR)
  98. # define writeFais_DEFINED
  99. void writeFais(
  100. in vec3 diffCol, // Normalized
  101. in vec3 normal,
  102. in float specularComponent, // Streangth and shininess
  103. in float blurring)
  104. {
  105. // Diffuse color and specular
  106. fMsFai0[0] = packUnorm4x8(vec4(diffCol, specularComponent));
  107. // Normal
  108. fMsFai0[1] = packHalf2x16(packNormal(normal));
  109. }
  110. #endif
  111. /// Write the data to FAIs
  112. #if defined(PASS_COLOR)
  113. # define writeFaisPackSpecular_DEFINED
  114. void writeFaisPackSpecular(
  115. in vec3 diffCol, // Normalized
  116. in vec3 normal,
  117. in vec2 specular, // Streangth and shininess
  118. in float blurring)
  119. {
  120. writeFais(diffCol, normal, packSpecular(specular), blurring);
  121. }
  122. #endif