MsCommonFrag.glsl 4.2 KB

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