MsCommonFrag.glsl 5.1 KB

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