MsMpGeneric.glsl 7.5 KB

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