MsMpGeneric.glsl 9.0 KB

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