MsCommonFrag.glsl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #define DEFAULT_FLOAT_PRECISION highp
  6. #pragma anki include "shaders/Common.glsl"
  7. #if !GL_ES && __VERSION__ > 400
  8. layout(early_fragment_tests) in;
  9. #endif
  10. #pragma anki include "shaders/Pack.glsl"
  11. #pragma anki include "shaders/MsBsCommon.glsl"
  12. //==============================================================================
  13. // Variables =
  14. //==============================================================================
  15. //
  16. // Input
  17. //
  18. layout(location = 0) in mediump vec2 inTexCoord;
  19. #if PASS == COLOR
  20. layout(location = 1) in mediump vec3 inNormal;
  21. layout(location = 2) in mediump vec4 inTangent;
  22. layout(location = 3) in mediump vec3 inVertPosViewSpace;
  23. #endif
  24. //
  25. // Output
  26. //
  27. #if PASS == COLOR
  28. layout(location = 0) out vec4 outMsRt0;
  29. layout(location = 1) out vec4 outMsRt1;
  30. # define outMsRt0_DEFINED
  31. # define outMsRt1_DEFINED
  32. #endif
  33. //==============================================================================
  34. // Functions =
  35. //==============================================================================
  36. // Getter
  37. #if PASS == COLOR
  38. # define getNormal_DEFINED
  39. vec3 getNormal()
  40. {
  41. return normalize(inNormal);
  42. }
  43. #endif
  44. // Getter
  45. #if PASS == COLOR
  46. # define getTangent_DEFINED
  47. vec4 getTangent()
  48. {
  49. return inTangent;
  50. }
  51. #endif
  52. // Getter
  53. #define getTextureCoord_DEFINED
  54. vec2 getTextureCoord()
  55. {
  56. return inTexCoord;
  57. }
  58. // Getter
  59. #if PASS == COLOR
  60. # define getPositionViewSpace_DEFINED
  61. vec3 getPositionViewSpace()
  62. {
  63. #if TESSELLATION
  64. return vec3(0.0);
  65. #else
  66. return inVertPosViewSpace;
  67. #endif
  68. }
  69. #endif
  70. // Do normal mapping
  71. #if PASS == COLOR
  72. # define readNormalFromTexture_DEFINED
  73. vec3 readNormalFromTexture(in vec3 normal, in vec4 tangent,
  74. in sampler2D map, in highp vec2 texCoords)
  75. {
  76. # if LOD > 0
  77. return normalize(normal);
  78. # else
  79. // First read the texture
  80. vec3 nAtTangentspace = normalize((texture(map, texCoords).rgb - 0.5) * 2.0);
  81. vec3 n = normal; // Assume that getNormal() is called
  82. vec3 t = normalize(tangent.xyz);
  83. vec3 b = cross(n, t) * tangent.w;
  84. mat3 tbnMat = mat3(t, b, n);
  85. return tbnMat * nAtTangentspace;
  86. # endif
  87. }
  88. #endif
  89. // Do normal mapping by combining normal maps
  90. #if PASS == COLOR
  91. # define combineNormalFromTextures_DEFINED
  92. vec3 combineNormalFromTextures(in vec3 normal, in vec4 tangent,
  93. in sampler2D map, in sampler2D map2, in highp vec2 texCoords,
  94. in float texCoords2Scale)
  95. {
  96. # if LOD > 0
  97. return normalize(normal);
  98. # else
  99. // First read the textures
  100. vec3 nAtTangentspace0 =
  101. normalize((texture(map, texCoords).rgb - 0.5) * 2.0);
  102. vec3 nAtTangentspace1 =
  103. normalize((texture(map2, texCoords * texCoords2Scale).rgb - 0.5) * 2.0);
  104. vec3 nAtTangentspace = (nAtTangentspace0 + nAtTangentspace1) / 2.0;
  105. vec3 n = normal; // Assume that getNormal() is called
  106. vec3 t = normalize(tangent.xyz);
  107. vec3 b = cross(n, t) * tangent.w;
  108. mat3 tbnMat = mat3(t, b, n);
  109. return tbnMat * nAtTangentspace;
  110. # endif
  111. }
  112. #endif
  113. // Do environment mapping
  114. #if PASS == COLOR
  115. # define readEnvironmentColor_DEFINED
  116. vec3 readEnvironmentColor(in vec3 vertPosViewSpace, in vec3 normal,
  117. in sampler2D map)
  118. {
  119. // In case of normal mapping I could play with vertex's normal but this
  120. // gives better results and its allready computed
  121. vec3 u = normalize(vertPosViewSpace);
  122. vec3 r = reflect(u, normal);
  123. r.z += 1.0;
  124. float m = 2.0 * length(r);
  125. vec2 semTexCoords = r.xy / m + 0.5;
  126. vec3 semCol = texture(map, semTexCoords).rgb;
  127. return semCol;
  128. }
  129. #endif
  130. // Using a 4-channel texture and a tolerance discard the fragment if the
  131. // texture's alpha is less than the tolerance
  132. #define readTextureRgbAlphaTesting_DEFINED
  133. vec3 readTextureRgbAlphaTesting(
  134. in sampler2D map,
  135. in highp vec2 texCoords,
  136. in float tolerance)
  137. {
  138. #if PASS == COLOR
  139. vec4 col = vec4(texture(map, texCoords));
  140. if(col.a < tolerance)
  141. {
  142. discard;
  143. }
  144. return col.rgb;
  145. #else // Depth
  146. # if LOD > 0
  147. return vec3(0.0);
  148. # else
  149. float a = float(texture(map, texCoords).a);
  150. if(a < tolerance)
  151. {
  152. discard;
  153. }
  154. return vec3(0.0);
  155. # endif
  156. #endif
  157. }
  158. // Just read the RGB color from texture
  159. #if PASS == COLOR
  160. # define readRgbFromTexture_DEFINED
  161. vec3 readRgbFromTexture(in sampler2D tex, in highp vec2 texCoords)
  162. {
  163. return vec3(texture(tex, texCoords)).rgb;
  164. }
  165. #endif
  166. // Just read the RGB color from cube texture
  167. #if PASS == COLOR
  168. # define readRgbFromCubeTexture_DEFINED
  169. vec3 readRgbFromCubeTexture(in samplerCube tex, in mediump vec3 texCoord)
  170. {
  171. return texture(tex, texCoord).rgb;
  172. }
  173. #endif
  174. // Just read the R color from texture
  175. #if PASS == COLOR
  176. # define readRFromTexture_DEFINED
  177. float readRFromTexture(in sampler2D tex, in highp vec2 texCoords)
  178. {
  179. return vec3(texture(tex, texCoords)).r;
  180. }
  181. #endif
  182. // Write the data to FAIs
  183. #if PASS == COLOR
  184. # define writeRts_DEFINED
  185. void writeRts(
  186. in vec3 diffColor, // from 0 to 1
  187. in vec3 normal,
  188. in float specularColor,
  189. in float specularPower,
  190. in float blurring)
  191. {
  192. writeGBuffer(diffColor, normal, specularColor, specularPower,
  193. outMsRt0, outMsRt1);
  194. }
  195. #endif