MsCommonFrag.glsl 4.7 KB

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