ms_mp_generic.glsl 9.9 KB

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