Lighting.frag 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #import "Common/ShaderLib/Parallax.glsllib"
  2. #import "Common/ShaderLib/Optics.glsllib"
  3. #define ATTENUATION
  4. //#define HQ_ATTENUATION
  5. varying vec2 texCoord;
  6. #ifdef SEPARATE_TEXCOORD
  7. varying vec2 texCoord2;
  8. #endif
  9. varying vec3 AmbientSum;
  10. varying vec4 DiffuseSum;
  11. varying vec3 SpecularSum;
  12. #ifndef VERTEX_LIGHTING
  13. uniform vec4 g_LightDirection;
  14. //varying vec3 vPosition;
  15. varying vec3 vViewDir;
  16. varying vec4 vLightDir;
  17. varying vec3 lightVec;
  18. #else
  19. varying vec2 vertexLightValues;
  20. #endif
  21. #ifdef DIFFUSEMAP
  22. uniform sampler2D m_DiffuseMap;
  23. #endif
  24. #ifdef SPECULARMAP
  25. uniform sampler2D m_SpecularMap;
  26. #endif
  27. #ifdef PARALLAXMAP
  28. uniform sampler2D m_ParallaxMap;
  29. #endif
  30. #if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING)
  31. uniform float m_ParallaxHeight;
  32. #endif
  33. #ifdef LIGHTMAP
  34. uniform sampler2D m_LightMap;
  35. #endif
  36. #ifdef NORMALMAP
  37. uniform sampler2D m_NormalMap;
  38. #else
  39. varying vec3 vNormal;
  40. #endif
  41. #ifdef ALPHAMAP
  42. uniform sampler2D m_AlphaMap;
  43. #endif
  44. #ifdef COLORRAMP
  45. uniform sampler2D m_ColorRamp;
  46. #endif
  47. uniform float m_AlphaDiscardThreshold;
  48. #ifndef VERTEX_LIGHTING
  49. uniform float m_Shininess;
  50. #ifdef HQ_ATTENUATION
  51. uniform vec4 g_LightPosition;
  52. #endif
  53. #ifdef USE_REFLECTION
  54. uniform float m_ReflectionPower;
  55. uniform float m_ReflectionIntensity;
  56. varying vec4 refVec;
  57. uniform ENVMAP m_EnvMap;
  58. #endif
  59. float tangDot(in vec3 v1, in vec3 v2){
  60. float d = dot(v1,v2);
  61. #ifdef V_TANGENT
  62. d = 1.0 - d*d;
  63. return step(0.0, d) * sqrt(d);
  64. #else
  65. return d;
  66. #endif
  67. }
  68. float lightComputeDiffuse(in vec3 norm, in vec3 lightdir, in vec3 viewdir){
  69. #ifdef MINNAERT
  70. float NdotL = max(0.0, dot(norm, lightdir));
  71. float NdotV = max(0.0, dot(norm, viewdir));
  72. return NdotL * pow(max(NdotL * NdotV, 0.1), -1.0) * 0.5;
  73. #else
  74. return max(0.0, dot(norm, lightdir));
  75. #endif
  76. }
  77. float lightComputeSpecular(in vec3 norm, in vec3 viewdir, in vec3 lightdir, in float shiny){
  78. // NOTE: check for shiny <= 1 removed since shininess is now
  79. // 1.0 by default (uses matdefs default vals)
  80. #ifdef LOW_QUALITY
  81. // Blinn-Phong
  82. // Note: preferably, H should be computed in the vertex shader
  83. vec3 H = (viewdir + lightdir) * vec3(0.5);
  84. return pow(max(tangDot(H, norm), 0.0), shiny);
  85. #elif defined(WARDISO)
  86. // Isotropic Ward
  87. vec3 halfVec = normalize(viewdir + lightdir);
  88. float NdotH = max(0.001, tangDot(norm, halfVec));
  89. float NdotV = max(0.001, tangDot(norm, viewdir));
  90. float NdotL = max(0.001, tangDot(norm, lightdir));
  91. float a = tan(acos(NdotH));
  92. float p = max(shiny/128.0, 0.001);
  93. return NdotL * (1.0 / (4.0*3.14159265*p*p)) * (exp(-(a*a)/(p*p)) / (sqrt(NdotV * NdotL)));
  94. #else
  95. // Standard Phong
  96. vec3 R = reflect(-lightdir, norm);
  97. return pow(max(tangDot(R, viewdir), 0.0), shiny);
  98. #endif
  99. }
  100. vec2 computeLighting(in vec3 wvNorm, in vec3 wvViewDir, in vec3 wvLightDir){
  101. float diffuseFactor = lightComputeDiffuse(wvNorm, wvLightDir, wvViewDir);
  102. float specularFactor = lightComputeSpecular(wvNorm, wvViewDir, wvLightDir, m_Shininess);
  103. #ifdef HQ_ATTENUATION
  104. float att = clamp(1.0 - g_LightPosition.w * length(lightVec), 0.0, 1.0);
  105. #else
  106. float att = vLightDir.w;
  107. #endif
  108. specularFactor *= diffuseFactor;
  109. return vec2(diffuseFactor, specularFactor) * vec2(att);
  110. }
  111. #endif
  112. void main(){
  113. vec2 newTexCoord;
  114. #if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING)
  115. #ifdef STEEP_PARALLAX
  116. #ifdef NORMALMAP_PARALLAX
  117. //parallax map is stored in the alpha channel of the normal map
  118. newTexCoord = steepParallaxOffset(m_NormalMap, vViewDir, texCoord, m_ParallaxHeight);
  119. #else
  120. //parallax map is a texture
  121. newTexCoord = steepParallaxOffset(m_ParallaxMap, vViewDir, texCoord, m_ParallaxHeight);
  122. #endif
  123. #else
  124. #ifdef NORMALMAP_PARALLAX
  125. //parallax map is stored in the alpha channel of the normal map
  126. newTexCoord = classicParallaxOffset(m_NormalMap, vViewDir, texCoord, m_ParallaxHeight);
  127. #else
  128. //parallax map is a texture
  129. newTexCoord = classicParallaxOffset(m_ParallaxMap, vViewDir, texCoord, m_ParallaxHeight);
  130. #endif
  131. #endif
  132. #else
  133. newTexCoord = texCoord;
  134. #endif
  135. #ifdef DIFFUSEMAP
  136. vec4 diffuseColor = texture2D(m_DiffuseMap, newTexCoord);
  137. #else
  138. vec4 diffuseColor = vec4(1.0);
  139. #endif
  140. float alpha = DiffuseSum.a * diffuseColor.a;
  141. #ifdef ALPHAMAP
  142. alpha = alpha * texture2D(m_AlphaMap, newTexCoord).r;
  143. #endif
  144. if(alpha < m_AlphaDiscardThreshold){
  145. discard;
  146. }
  147. #ifndef VERTEX_LIGHTING
  148. float spotFallOff = 1.0;
  149. #if __VERSION__ >= 110
  150. // allow use of control flow
  151. if(g_LightDirection.w != 0.0){
  152. #endif
  153. vec3 L = normalize(lightVec.xyz);
  154. vec3 spotdir = normalize(g_LightDirection.xyz);
  155. float curAngleCos = dot(-L, spotdir);
  156. float innerAngleCos = floor(g_LightDirection.w) * 0.001;
  157. float outerAngleCos = fract(g_LightDirection.w);
  158. float innerMinusOuter = innerAngleCos - outerAngleCos;
  159. spotFallOff = (curAngleCos - outerAngleCos) / innerMinusOuter;
  160. #if __VERSION__ >= 110
  161. if(spotFallOff <= 0.0){
  162. gl_FragColor.rgb = AmbientSum * diffuseColor.rgb;
  163. gl_FragColor.a = alpha;
  164. return;
  165. }else{
  166. spotFallOff = clamp(spotFallOff, 0.0, 1.0);
  167. }
  168. }
  169. #else
  170. spotFallOff = clamp(spotFallOff, step(g_LightDirection.w, 0.001), 1.0);
  171. #endif
  172. #endif
  173. // ***********************
  174. // Read from textures
  175. // ***********************
  176. #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
  177. vec4 normalHeight = texture2D(m_NormalMap, newTexCoord);
  178. vec3 normal = (normalHeight.xyz * vec3(2.0) - vec3(1.0));
  179. #ifdef LATC
  180. normal.z = sqrt(1.0 - (normal.x * normal.x) - (normal.y * normal.y));
  181. #endif
  182. //normal.y = -normal.y;
  183. #elif !defined(VERTEX_LIGHTING)
  184. vec3 normal = vNormal;
  185. #if !defined(LOW_QUALITY) && !defined(V_TANGENT)
  186. normal = normalize(normal);
  187. #endif
  188. #endif
  189. #ifdef SPECULARMAP
  190. vec4 specularColor = texture2D(m_SpecularMap, newTexCoord);
  191. #else
  192. vec4 specularColor = vec4(1.0);
  193. #endif
  194. #ifdef LIGHTMAP
  195. vec3 lightMapColor;
  196. #ifdef SEPARATE_TEXCOORD
  197. lightMapColor = texture2D(m_LightMap, texCoord2).rgb;
  198. #else
  199. lightMapColor = texture2D(m_LightMap, texCoord).rgb;
  200. #endif
  201. specularColor.rgb *= lightMapColor;
  202. diffuseColor.rgb *= lightMapColor;
  203. #endif
  204. #ifdef VERTEX_LIGHTING
  205. vec2 light = vertexLightValues.xy;
  206. #ifdef COLORRAMP
  207. light.x = texture2D(m_ColorRamp, vec2(light.x, 0.0)).r;
  208. light.y = texture2D(m_ColorRamp, vec2(light.y, 0.0)).r;
  209. #endif
  210. gl_FragColor.rgb = AmbientSum * diffuseColor.rgb +
  211. DiffuseSum.rgb * diffuseColor.rgb * vec3(light.x) +
  212. SpecularSum * specularColor.rgb * vec3(light.y);
  213. #else
  214. vec4 lightDir = vLightDir;
  215. lightDir.xyz = normalize(lightDir.xyz);
  216. vec2 light = computeLighting(normal, vViewDir.xyz, lightDir.xyz) * spotFallOff;
  217. #ifdef COLORRAMP
  218. diffuseColor.rgb *= texture2D(m_ColorRamp, vec2(light.x, 0.0)).rgb;
  219. specularColor.rgb *= texture2D(m_ColorRamp, vec2(light.y, 0.0)).rgb;
  220. #endif
  221. // Workaround, since it is not possible to modify varying variables
  222. vec4 SpecularSum2 = vec4(SpecularSum, 1.0);
  223. #ifdef USE_REFLECTION
  224. vec4 refColor = Optics_GetEnvColor(m_EnvMap, refVec.xyz);
  225. // Interpolate light specularity toward reflection color
  226. // Multiply result by specular map
  227. specularColor = mix(SpecularSum2 * light.y, refColor, refVec.w) * specularColor;
  228. SpecularSum2 = vec4(1.0);
  229. light.y = 1.0;
  230. #endif
  231. gl_FragColor.rgb = AmbientSum * diffuseColor.rgb +
  232. DiffuseSum.rgb * diffuseColor.rgb * vec3(light.x) +
  233. SpecularSum2.rgb * specularColor.rgb * vec3(light.y);
  234. #endif
  235. gl_FragColor.a = alpha;
  236. }