MsMpGeneric.glsl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /// @file
  2. ///
  3. /// This a generic shader to fill the deferred shading buffers. You can always build your own if you dont need to write
  4. /// in all the buffers
  5. ///
  6. /// Control defines:
  7. /// DIFFUSE_MAPPING, NORMAL_MAPPING, SPECULAR_MAPPING, PARALLAX_MAPPING, ENVIRONMENT_MAPPING, ALPHA_TESTING
  8. #if defined(ALPHA_TESTING) && !defined(DIFFUSE_MAPPING)
  9. #error "Cannot have ALPHA_TESTING without DIFFUSE_MAPPING"
  10. #endif
  11. #if defined(DIFFUSE_MAPPING) || defined(NORMAL_MAPPING) || defined(SPECULAR_MAPPING)
  12. #define NEEDS_TEX_MAPPING 1
  13. #else
  14. #define NEEDS_TEX_MAPPING 0
  15. #endif
  16. #if defined(NORMAL_MAPPING) || defined(PARALLAX_MAPPING)
  17. #define NEEDS_TANGENT 1
  18. #else
  19. #define NEEDS_TANGENT 0
  20. #endif
  21. #pragma anki vertShaderBegins
  22. /// @name Attributes
  23. /// @{
  24. in vec3 position;
  25. in vec3 normal;
  26. #if NEEDS_TEX_MAPPING
  27. in vec2 texCoords;
  28. #endif
  29. #if NEEDS_TANGENT
  30. in vec4 tangent;
  31. #endif
  32. /// @}
  33. /// @name Uniforms
  34. /// @{
  35. uniform mat4 modelMat;
  36. uniform mat4 viewMat;
  37. uniform mat4 projectionMat;
  38. uniform mat4 modelViewMat;
  39. uniform mat3 normalMat;
  40. uniform mat4 modelViewProjectionMat;
  41. /// @}
  42. /// @name Varyings
  43. /// @{
  44. out vec3 vNormal;
  45. out vec2 vTexCoords;
  46. out vec3 vTangent;
  47. out float vTangentW;
  48. out vec3 vVertPosViewSpace; ///< For env mapping. AKA view vector
  49. /// @}
  50. //======================================================================================================================
  51. // main =
  52. //======================================================================================================================
  53. void main()
  54. {
  55. // calculate the vert pos, normal and tangent
  56. vNormal = normalMat * normal;
  57. #if NEEDS_TANGENT
  58. vTangent = normalMat * vec3(tangent);
  59. #endif
  60. gl_Position = modelViewProjectionMat * vec4(position, 1.0);
  61. // calculate the rest
  62. #if NEEDS_TEX_MAPPING
  63. vTexCoords = texCoords;
  64. #endif
  65. #if NEEDS_TANGENT
  66. vTangentW = tangent.w;
  67. #endif
  68. #if defined(ENVIRONMENT_MAPPING) || defined(PARALLAX_MAPPING)
  69. vVertPosViewSpace = vec3(modelViewMat * vec4(position, 1.0));
  70. #endif
  71. }
  72. #pragma anki fragShaderBegins
  73. /// @note The process of calculating the diffuse color for the diffuse MSFAI is divided into two parts. The first
  74. /// happens before the normal calculation and the other just after it. In the first part we read the texture (or the
  75. /// gl_Color) and we set the _diffColl_. In case of grass we discard. In the second part we calculate a SEM color and
  76. /// we combine it with the _diffColl_. We cannot put the second part before normal calculation because SEM needs
  77. /// the _normal_. Also we cannot put the first part after normal calculation because in case of grass we will waste
  78. /// calculations for the normal. For that two reasons we split the diffuse calculations in two parts
  79. #pragma anki include "shaders/Pack.glsl"
  80. #if defined(DIFFUSE_MAPPING)
  81. uniform sampler2D diffuseMap;
  82. #endif
  83. #if defined(NORMAL_MAPPING)
  84. uniform sampler2D normalMap;
  85. #endif
  86. #if defined(SPECULAR_MAPPING)
  87. uniform sampler2D specularMap;
  88. #endif
  89. #if defined(PARALLAX_MAPPING)
  90. uniform sampler2D heightMap;
  91. #endif
  92. #if defined(ENVIRONMENT_MAPPING)
  93. uniform sampler2D environmentMap;
  94. #endif
  95. uniform vec3 diffuseCol = vec3(1.0, 0.0, 1.0);
  96. uniform vec3 specularCol = vec3(1.0, 0.0, 1.0);
  97. uniform float shininess = 50.0;
  98. in vec3 vNormal;
  99. in vec3 vTangent;
  100. in float vTangentW;
  101. in vec2 vTexCoords;
  102. in vec3 vVertPosViewSpace;
  103. // @todo
  104. in vec3 eye;
  105. layout(location = 0) out vec2 fMsNormalFai;
  106. layout(location = 1) out vec3 fMsDiffuseFai;
  107. layout(location = 2) out vec4 fMsSpecularFai;
  108. //======================================================================================================================
  109. // main =
  110. //======================================================================================================================
  111. void main()
  112. {
  113. //
  114. // Paralax Mapping Calculations
  115. // The code below reads the height map, makes some calculations and returns a new texCoords
  116. //
  117. #if defined(PARALLAX_MAPPING)
  118. /*const float _scale = 0.04;
  119. const float _bias = scale * 0.4;
  120. vec3 _norm_eye = normalize(eye);
  121. float _h = texture2D(heightMap, vTexCoords).r;
  122. float _height = _scale * _h - _bias;
  123. vec2 _superTexCoords__v2f = _height * _norm_eye.xy + vTexCoords;*/
  124. vec2 _superTexCoords_ = vTexCoords;
  125. const float maxStepCount = 100.0;
  126. float nSteps = maxStepCount * length(_superTexCoords_);
  127. vec3 dir = vVertPosViewSpace;
  128. dir.xy /= 8.0;
  129. dir /= -nSteps * dir.z;
  130. float diff0, diff1 = 1.0 - texture2D(heightMap, _superTexCoords_).a;
  131. if(diff1 > 0.0)
  132. {
  133. do
  134. {
  135. _superTexCoords_ += dir.xy;
  136. diff0 = diff1;
  137. diff1 = texture2D(heightMap, _superTexCoords_).w;
  138. } while(diff1 > 0.0);
  139. _superTexCoords_.xy += (diff1 / (diff0 - diff1)) * dir.xy;
  140. }
  141. #else
  142. #define _superTexCoords_ vTexCoords
  143. #endif
  144. //
  145. // Diffuse Calculations (Part I)
  146. // Get the color from the diffuse map and discard if alpha testing is on and alpha is zero
  147. //
  148. vec3 _diffColl_;
  149. #if defined(DIFFUSE_MAPPING)
  150. #if defined(ALPHA_TESTING)
  151. vec4 _diffCol4_ = texture2D(diffuseMap, _superTexCoords_);
  152. if(_diffCol4_.a == 0.0)
  153. discard;
  154. _diffColl_ = _diffCol4_.rgb;
  155. #else // no alpha
  156. _diffColl_ = texture2D(diffuseMap, _superTexCoords_).rgb;
  157. #endif
  158. _diffColl_ *= diffuseCol.rgb;
  159. #else // no diff mapping
  160. _diffColl_ = diffuseCol.rgb;
  161. #endif
  162. //
  163. // Normal Calculations
  164. // Either use a normap map and make some calculations or use the vertex normal
  165. //
  166. #if defined(NORMAL_MAPPING)
  167. vec3 _n_ = normalize(vNormal);
  168. vec3 _t_ = normalize(vTangent);
  169. vec3 _b_ = cross(_n_, _t_) * vTangentW;
  170. mat3 _tbnMat_ = mat3(_t_, _b_, _n_);
  171. vec3 _nAtTangentspace_ = (texture2D(normalMap, _superTexCoords_).rgb - 0.5) * 2.0;
  172. vec3 _normal_ = normalize(_tbnMat_ * _nAtTangentspace_);
  173. #else
  174. vec3 _normal_ = normalize(vNormal);
  175. #endif
  176. //
  177. // Diffuse Calculations (Part II)
  178. // If SEM is enabled make some calculations (using the vVertPosViewSpace, environmentMap and the _normal_) and
  179. /// combine colors of SEM and the _diffColl_
  180. //
  181. #if defined(ENVIRONMENT_MAPPING)
  182. //
  183. // In case of normal mapping I could play with vertex's normal but this gives better results and its allready
  184. // computed
  185. //
  186. vec3 _u_ = normalize(vVertPosViewSpace);
  187. vec3 _r_ = reflect(_u_, _normal_);
  188. _r_.z += 1.0;
  189. float _m_ = 2.0 * length(_r_);
  190. vec2 _semTexCoords_ = _r_.xy / _m_ + 0.5;
  191. vec3 _semCol_ = texture2D(environmentMap, _semTexCoords_).rgb;
  192. _diffColl_ += _semCol_; // blend existing color with the SEM texture map
  193. #endif
  194. //
  195. // Specular Calculations
  196. //
  197. #if defined(SPECULAR_MAPPING)
  198. vec4 _specularCol_ = vec4(texture2D(specularMap, _superTexCoords_).rgb * specularCol, shininess);
  199. #else // no specular map
  200. vec4 _specularCol_ = vec4(specularCol, shininess);
  201. #endif
  202. //
  203. // Final Stage. Write all data
  204. //
  205. fMsNormalFai = packNormal(_normal_);
  206. fMsDiffuseFai = _diffColl_;
  207. fMsSpecularFai = _specularCol_;
  208. /*#if defined(HARDWARE_SKINNING)
  209. gl_FragData[1] = gl_Color;
  210. #endif*/
  211. }