IsLpGeneric.glsl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /// @file
  2. ///
  3. /// Illumination stage lighting pass general shader program
  4. #pragma anki vertShaderBegins
  5. layout(location = 0) in vec2 position; ///< the vert coords are {1.0,1.0}, {0.0,1.0}, {0.0,0.0}, {1.0,0.0}
  6. layout(location = 1) in vec3 viewVector;
  7. out vec2 vTexCoords;
  8. out vec3 vPosition;
  9. //======================================================================================================================
  10. // main =
  11. //======================================================================================================================
  12. void main()
  13. {
  14. vPosition = viewVector;
  15. vTexCoords = position;
  16. vec2 _vertPosNdc_ = position * 2.0 - 1.0;
  17. gl_Position = vec4(_vertPosNdc_, 0.0, 1.0);
  18. }
  19. #pragma anki fragShaderBegins
  20. #pragma anki include "shaders/Pack.glsl"
  21. /// @name Uniforms
  22. /// @{
  23. uniform sampler2D msNormalFai, msDiffuseFai, msSpecularFai, msDepthFai;
  24. uniform vec2 planes; ///< for the calculation of frag pos in view space
  25. uniform vec3 lightPos; ///< Light pos in eye space
  26. uniform float lightRadius;
  27. uniform vec3 lightDiffuseCol;
  28. uniform vec3 lightSpecularCol;
  29. #if defined(SPOT_LIGHT_ENABLED)
  30. uniform sampler2D lightTex;
  31. uniform mat4 texProjectionMat;
  32. #if defined(SHADOW_ENABLED)
  33. uniform sampler2DShadow shadowMap;
  34. #endif
  35. #endif
  36. /// @}
  37. /// @name Varyings
  38. /// @{
  39. in vec2 vTexCoords;
  40. in vec3 vPosition; ///< for the calculation of frag pos in view space
  41. /// @}
  42. /// @name Output
  43. /// @{
  44. out vec3 fColor;
  45. /// @}
  46. //======================================================================================================================
  47. // getFragPosVSpace =
  48. //======================================================================================================================
  49. /// @return frag pos in view space
  50. vec3 getFragPosVSpace()
  51. {
  52. float _depth_ = texture2D(msDepthFai, vTexCoords).r;
  53. if(_depth_ == 1.0)
  54. {
  55. discard;
  56. }
  57. vec3 _fragPosVspace_;
  58. vec3 _vposn_ = normalize(vPosition);
  59. _fragPosVspace_.z = -planes.y / (planes.x + _depth_);
  60. _fragPosVspace_.xy = _vposn_.xy * (_fragPosVspace_.z / _vposn_.z);
  61. return _fragPosVspace_;
  62. }
  63. //======================================================================================================================
  64. // getAttenuation =
  65. //======================================================================================================================
  66. /// @return The attenuation factor fiven the distance from the frag to the light source
  67. float getAttenuation(in float _fragLightDist_)
  68. {
  69. return clamp(1.0 - sqrt(_fragLightDist_) / lightRadius, 0.0, 1.0);
  70. //return 1.0 - _fragLightDist_ * _inv_light_radius;
  71. }
  72. //======================================================================================================================
  73. // pcfLow =
  74. //======================================================================================================================
  75. #if defined(SPOT_LIGHT_ENABLED) && defined(SHADOW_ENABLED)
  76. /// @return The blurred shadow
  77. float pcfLow(in vec3 _shadowUv_)
  78. {
  79. const float _mapScale_ = 1.0 / SHADOWMAP_SIZE;
  80. const int _kernelSize_ = 8;
  81. const vec2 _kernel_[_kernelSize_] = vec2[]
  82. (
  83. vec2(_mapScale_, _mapScale_),
  84. vec2(_mapScale_, -_mapScale_),
  85. vec2(-_mapScale_, _mapScale_),
  86. vec2(-_mapScale_, -_mapScale_),
  87. vec2(0.0, _mapScale_),
  88. vec2(0.0, -_mapScale_),
  89. vec2(_mapScale_, 0.0),
  90. vec2(-_mapScale_, 0.0)
  91. );
  92. float _shadowCol_ = shadow2D(shadowMap, _shadowUv_).r;
  93. for(int i=0; i<_kernelSize_; i++)
  94. {
  95. vec3 _uvCoord_ = vec3(_shadowUv_.xy + _kernel_[i], _shadowUv_.z);
  96. _shadowCol_ += shadow2D(shadowMap, _uvCoord_).r;
  97. }
  98. _shadowCol_ *= (1.0 / 9.0);
  99. return _shadowCol_;
  100. }
  101. #endif
  102. //======================================================================================================================
  103. // doPhong =
  104. //======================================================================================================================
  105. /// Performs phong lighting using the MS FAIs and a few other things
  106. /// @param _fragPosVspace_ The fragment position in view space
  107. /// @param _fragLightDist_ Output needed for the attenuation calculation
  108. /// @return The final color
  109. vec3 doPhong(in vec3 _fragPosVspace_, out float _fragLightDist_)
  110. {
  111. // get the vector from the frag to the light
  112. vec3 _frag2LightVec_ = lightPos - _fragPosVspace_;
  113. // Instead of using normalize(_frag2LightVec_) we brake the operation because we want _fragLightDist_ for the calc of
  114. // the attenuation
  115. _fragLightDist_ = dot(_frag2LightVec_, _frag2LightVec_);
  116. vec3 _lightDir_ = _frag2LightVec_ * inversesqrt(_fragLightDist_);
  117. // Read the normal
  118. vec3 _normal_ = unpackNormal(texture2D(msNormalFai, vTexCoords).rg);
  119. // Lambert term
  120. float _lambertTerm_ = dot(_normal_, _lightDir_);
  121. if(_lambertTerm_ < 0.0)
  122. {
  123. discard;
  124. }
  125. //_lambertTerm_ = max(0.0, _lambertTerm_);
  126. // Diffuce
  127. vec3 _diffuse_ = texture2D(msDiffuseFai, vTexCoords).rgb;
  128. _diffuse_ *= lightDiffuseCol;
  129. vec3 _color_ = _diffuse_ * _lambertTerm_;
  130. // Specular
  131. vec4 _specularMix_ = texture2D(msSpecularFai, vTexCoords); // the MS specular FAI has the color and the shininess
  132. vec3 _specular_ = _specularMix_.xyz;
  133. float _shininess_ = _specularMix_.w;
  134. vec3 _eyeVec_ = normalize(-_fragPosVspace_);
  135. vec3 _h_ = normalize(_lightDir_ + _eyeVec_);
  136. float _specIntensity_ = pow(max(0.0, dot(_normal_, _h_)), _shininess_);
  137. _color_ += _specular_ * lightSpecularCol * (_specIntensity_ * _lambertTerm_);
  138. // end
  139. return _color_;
  140. }
  141. //======================================================================================================================
  142. // main =
  143. //======================================================================================================================
  144. void main()
  145. {
  146. // get frag pos in view space
  147. vec3 fragPosVspace = getFragPosVSpace();
  148. //
  149. // Point light
  150. //
  151. #if defined(POINT_LIGHT_ENABLED)
  152. // The func doPhong calculates the frag to light distance (_fragLightDist_) and be cause we need that distance
  153. // latter for other calculations we export it
  154. float _fragLightDist_;
  155. vec3 _color_ = doPhong(fragPosVspace, _fragLightDist_);
  156. fColor = _color_ * getAttenuation(_fragLightDist_);
  157. //
  158. // Spot light
  159. //
  160. #elif defined(SPOT_LIGHT_ENABLED)
  161. vec4 _vTexCoords2_ = texProjectionMat * vec4(fragPosVspace, 1.0);
  162. vec3 _vTexCoords3_ = _vTexCoords2_.xyz / _vTexCoords2_.w;
  163. if(_vTexCoords2_.w > 0.0 &&
  164. _vTexCoords3_.x > 0.0 &&
  165. _vTexCoords3_.x < 1.0 &&
  166. _vTexCoords3_.y > 0.0 &&
  167. _vTexCoords3_.y < 1.0 &&
  168. _vTexCoords2_.w < lightRadius)
  169. {
  170. // Get shadow
  171. #if defined(SHADOW_ENABLED)
  172. #if defined(PCF_ENABLED)
  173. float _shadowCol_ = pcfLow(_vTexCoords3_);
  174. #else
  175. float _shadowCol_ = shadow2D(shadowMap, _vTexCoords3_).r;
  176. #endif
  177. if(_shadowCol_ == 0.0)
  178. {
  179. discard;
  180. }
  181. #endif
  182. float _fragLightDist_;
  183. vec3 _color_ = doPhong(fragPosVspace, _fragLightDist_);
  184. vec3 _lightTexCol_ = texture2DProj(lightTex, _vTexCoords2_.xyz).rgb;
  185. float _att_ = getAttenuation(_fragLightDist_);
  186. #if defined(SHADOW_ENABLED)
  187. fColor = _lightTexCol_ * _color_ * (_shadowCol_ * _att_);
  188. #else
  189. fColor = _lightTexCol_ * _color_ * _att_;
  190. #endif
  191. }
  192. else
  193. {
  194. discard;
  195. }
  196. #endif // spot light
  197. /*#if defined(POINT_LIGHT_ENABLED)
  198. gl_FragData[0] = gl_FragData[0] - gl_FragData[0] + vec4(1, 0, 1, 1);
  199. #endif*/
  200. //gl_FragData[0] = gl_FragData[0] - gl_FragData[0] + vec4(1, 0, 1, 1);
  201. /*#if defined(SPOT_LIGHT_ENABLED)
  202. gl_FragData[0] = gl_FragData[0] - gl_FragData[0] + vec4(texture2D(msDepthFai, vTexCoords).r);
  203. //gl_FragData[0] = vec4(texture2D(msDepthFai, vTexCoords).rg), 1.0);
  204. #endif*/
  205. }