MsCommonFrag.glsl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include "shaders/Common.glsl"
  6. #if !GL_ES && __VERSION__ > 400
  7. layout(early_fragment_tests) in;
  8. #endif
  9. #include "shaders/Pack.glsl"
  10. #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(
  75. in vec3 normal, in vec4 tangent, 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,
  94. in vec4 tangent,
  95. in sampler2D map,
  96. in sampler2D map2,
  97. in highp vec2 texCoords,
  98. in float texCoords2Scale)
  99. {
  100. #if LOD > 0
  101. return normalize(normal);
  102. #else
  103. // First read the textures
  104. vec3 nAtTangentspace0 =
  105. normalize((texture(map, texCoords).rgb - 0.5) * 2.0);
  106. vec3 nAtTangentspace1 =
  107. normalize((texture(map2, texCoords * texCoords2Scale).rgb - 0.5) * 2.0);
  108. vec3 nAtTangentspace = (nAtTangentspace0 + nAtTangentspace1) / 2.0;
  109. vec3 n = normal; // Assume that getNormal() is called
  110. vec3 t = normalize(tangent.xyz);
  111. vec3 b = cross(n, t) * tangent.w;
  112. mat3 tbnMat = mat3(t, b, n);
  113. return tbnMat * nAtTangentspace;
  114. #endif
  115. }
  116. #endif
  117. // Do environment mapping
  118. #if PASS == COLOR
  119. #define readEnvironmentColor_DEFINED
  120. vec3 readEnvironmentColor(
  121. in vec3 vertPosViewSpace, in vec3 normal, in sampler2D map)
  122. {
  123. // In case of normal mapping I could play with vertex's normal but this
  124. // gives better results and its allready computed
  125. vec3 u = normalize(vertPosViewSpace);
  126. vec3 r = reflect(u, normal);
  127. r.z += 1.0;
  128. float m = 2.0 * length(r);
  129. vec2 semTexCoords = r.xy / m + 0.5;
  130. vec3 semCol = texture(map, semTexCoords).rgb;
  131. return semCol;
  132. }
  133. #endif
  134. // Using a 4-channel texture and a tolerance discard the fragment if the
  135. // texture's alpha is less than the tolerance
  136. #define readTextureRgbAlphaTesting_DEFINED
  137. vec3 readTextureRgbAlphaTesting(
  138. in sampler2D map, in highp vec2 texCoords, in float tolerance)
  139. {
  140. #if PASS == COLOR
  141. vec4 col = vec4(texture(map, texCoords));
  142. if(col.a < tolerance)
  143. {
  144. discard;
  145. }
  146. return col.rgb;
  147. #else // Depth
  148. #if LOD > 0
  149. return vec3(0.0);
  150. #else
  151. float a = float(texture(map, texCoords).a);
  152. if(a < tolerance)
  153. {
  154. discard;
  155. }
  156. return vec3(0.0);
  157. #endif
  158. #endif
  159. }
  160. // Just read the RGB color from texture
  161. #if PASS == COLOR
  162. #define readRgbFromTexture_DEFINED
  163. vec3 readRgbFromTexture(in sampler2D tex, in highp vec2 texCoords)
  164. {
  165. return vec3(texture(tex, texCoords)).rgb;
  166. }
  167. #endif
  168. // Just read the RGB color from cube texture
  169. #if PASS == COLOR
  170. #define readRgbFromCubeTexture_DEFINED
  171. vec3 readRgbFromCubeTexture(in samplerCube tex, in mediump vec3 texCoord)
  172. {
  173. return texture(tex, texCoord).rgb;
  174. }
  175. #endif
  176. // Just read the R color from texture
  177. #if PASS == COLOR
  178. #define readRFromTexture_DEFINED
  179. float readRFromTexture(in sampler2D tex, in highp vec2 texCoords)
  180. {
  181. return vec3(texture(tex, texCoords)).r;
  182. }
  183. #endif
  184. // Write the data to FAIs
  185. #if PASS == COLOR
  186. #define writeRts_DEFINED
  187. void writeRts(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. in float metallic)
  194. {
  195. GbufferInfo g;
  196. g.diffuse = mix(diffColor, vec3(0.0), metallic);
  197. g.normal = normal;
  198. g.specular = mix(specularColor, diffColor, metallic);
  199. g.roughness = roughness;
  200. g.subsurface = subsurface;
  201. g.emission = emission;
  202. g.metallic = metallic;
  203. writeGBuffer(g, out_msRt0, out_msRt1, out_msRt2);
  204. }
  205. #endif