MsCommonFrag.glsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #define DEFAULT_FLOAT_PRECISION highp
  2. #pragma anki include "shaders/Common.glsl"
  3. #if !GL_ES && __VERSION__ > 400
  4. layout(early_fragment_tests) in;
  5. #endif
  6. #pragma anki include "shaders/Pack.glsl"
  7. #pragma anki include "shaders/MsBsCommon.glsl"
  8. //==============================================================================
  9. // Variables =
  10. //==============================================================================
  11. /// Input
  12. #if TESSELLATION
  13. in mediump vec2 teTexCoords;
  14. #if PASS_COLOR
  15. in mediump vec3 teNormal;
  16. in mediump vec4 teTangent;
  17. in mediump vec3 teVertPosViewSpace;
  18. #endif
  19. #else // no TESSELLATION
  20. #if INSTANCE_ID_FRAGMENT_SHADER
  21. flat in mediump uint vInstanceId;
  22. #endif
  23. in mediump vec2 vTexCoords;
  24. #if PASS_COLOR
  25. in mediump vec3 vNormal;
  26. in mediump vec4 vTangent;
  27. in mediump vec3 vVertPosViewSpace;
  28. #endif
  29. #endif // TESSELLATION
  30. // Output
  31. #if PASS_COLOR
  32. # if USE_MRT
  33. layout(location = 0) out vec4 fMsFai0;
  34. layout(location = 1) out vec4 fMsFai1;
  35. # else
  36. layout(location = 0) out uvec2 fMsFai0;
  37. # endif
  38. # define fMsFai0_DEFINED
  39. #endif
  40. //==============================================================================
  41. // Functions =
  42. //==============================================================================
  43. // Getter
  44. #if PASS_COLOR
  45. # define getNormal_DEFINED
  46. vec3 getNormal()
  47. {
  48. #if TESSELLATION
  49. return normalize(teNormal);
  50. #else
  51. return normalize(vNormal);
  52. #endif
  53. }
  54. #endif
  55. // Getter
  56. #if PASS_COLOR
  57. # define getTangent_DEFINED
  58. vec4 getTangent()
  59. {
  60. #if TESSELLATION
  61. return teTangent;
  62. #else
  63. return vTangent;
  64. #endif
  65. }
  66. #endif
  67. // Getter
  68. #define getTextureCoord_DEFINED
  69. vec2 getTextureCoord()
  70. {
  71. #if TESSELLATION
  72. return teTexCoords;
  73. #else
  74. return vTexCoords;
  75. #endif
  76. }
  77. // Getter
  78. #if PASS_COLOR
  79. # define getPositionViewSpace_DEFINED
  80. vec3 getPositionViewSpace()
  81. {
  82. #if TESSELLATION
  83. return vec3(0.0);
  84. #else
  85. return vVertPosViewSpace;
  86. #endif
  87. }
  88. #endif
  89. // Do normal mapping
  90. #if PASS_COLOR
  91. # define getNormalFromTexture_DEFINED
  92. vec3 getNormalFromTexture(in vec3 normal, in vec4 tangent,
  93. in sampler2D map, in highp vec2 texCoords)
  94. {
  95. # if LOD > 0
  96. return normalize(normal);
  97. # else
  98. // First read the texture
  99. vec3 nAtTangentspace = normalize((texture(map, texCoords).rgb - 0.5) * 2.0);
  100. vec3 n = normal; // Assume that getNormal() is called
  101. vec3 t = normalize(tangent.xyz);
  102. vec3 b = cross(n, t) * tangent.w;
  103. mat3 tbnMat = mat3(t, b, n);
  104. return tbnMat * nAtTangentspace;
  105. # endif
  106. }
  107. #endif
  108. // Do environment mapping
  109. #if PASS_COLOR
  110. # define getEnvironmentColor_DEFINED
  111. vec3 getEnvironmentColor(in vec3 vertPosViewSpace, in vec3 normal,
  112. in sampler2D map)
  113. {
  114. // In case of normal mapping I could play with vertex's normal but this
  115. // gives better results and its allready computed
  116. vec3 u = normalize(vertPosViewSpace);
  117. vec3 r = reflect(u, normal);
  118. r.z += 1.0;
  119. float m = 2.0 * length(r);
  120. vec2 semTexCoords = r.xy / m + 0.5;
  121. vec3 semCol = texture(map, semTexCoords).rgb;
  122. return semCol;
  123. }
  124. #endif
  125. // Using a 4-channel texture and a tolerance discard the fragment if the
  126. // texture's alpha is less than the tolerance
  127. #define readTextureRgbAlphaTesting_DEFINED
  128. vec3 readTextureRgbAlphaTesting(
  129. in sampler2D map,
  130. in highp vec2 texCoords,
  131. in float tolerance)
  132. {
  133. #if PASS_COLOR
  134. vec4 col = vec4(texture(map, texCoords));
  135. if(col.a < tolerance)
  136. {
  137. discard;
  138. }
  139. return col.rgb;
  140. #else // Depth
  141. # if LOD > 0
  142. return vec3(0.0);
  143. # else
  144. float a = float(texture(map, texCoords).a);
  145. if(a < tolerance)
  146. {
  147. discard;
  148. }
  149. return vec3(0.0);
  150. # endif
  151. #endif
  152. }
  153. // Just read the RGB color from texture
  154. #if PASS_COLOR
  155. # define readRgbFromTexture_DEFINED
  156. vec3 readRgbFromTexture(in sampler2D tex, in highp vec2 texCoords)
  157. {
  158. return vec3(texture(tex, texCoords)).rgb;
  159. }
  160. #endif
  161. // Write the data to FAIs
  162. #if PASS_COLOR
  163. # define writeFais_DEFINED
  164. void writeFais(
  165. in vec3 diffColor, // from 0 to 1
  166. in vec3 normal,
  167. in vec2 specularComponent, // Streangth and shininess
  168. in float blurring)
  169. {
  170. writeGBuffer(
  171. diffColor, normal, specularComponent.x, specularComponent.y,
  172. fMsFai0
  173. #if USE_MRT
  174. , fMsFai1
  175. #endif
  176. );
  177. }
  178. #endif