ForwardLit.frag 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "Uniforms.frag"
  2. #include "Samplers.frag"
  3. #include "Lighting.frag"
  4. #include "Fog.frag"
  5. varying vec2 vTexCoord;
  6. #ifdef VERTEXCOLOR
  7. varying vec4 vColor;
  8. #endif
  9. varying vec4 vLightVec;
  10. #ifdef SPECULAR
  11. varying vec3 vEyeVec;
  12. #endif
  13. #ifndef NORMALMAP
  14. varying vec3 vNormal;
  15. #endif
  16. #ifdef SHADOW
  17. #if defined(DIRLIGHT)
  18. varying vec4 vShadowPos[4];
  19. #elif defined(SPOTLIGHT)
  20. varying vec4 vShadowPos;
  21. #else
  22. varying vec3 vShadowPos;
  23. #endif
  24. #endif
  25. #ifdef SPOTLIGHT
  26. varying vec4 vSpotPos;
  27. #endif
  28. #ifdef POINTLIGHT
  29. varying vec3 vCubeMaskVec;
  30. #endif
  31. void main()
  32. {
  33. #ifdef DIFFMAP
  34. vec4 diffColor = cMatDiffColor * texture2D(sDiffMap, vTexCoord);
  35. #else
  36. vec4 diffColor = cMatDiffColor;
  37. #endif
  38. #ifdef VERTEXCOLOR
  39. diffColor *= vColor;
  40. #endif
  41. vec3 lightColor;
  42. vec3 lightDir;
  43. vec3 finalColor;
  44. float diff;
  45. #ifdef NORMALMAP
  46. vec3 normal = DecodeNormal(texture2D(sNormalMap, vTexCoord));
  47. #else
  48. vec3 normal = normalize(vNormal);
  49. #endif
  50. #ifdef DIRLIGHT
  51. #ifdef NORMALMAP
  52. lightDir = normalize(vLightVec.xyz);
  53. #else
  54. lightDir = vLightVec.xyz;
  55. #endif
  56. diff = GetDiffuseDir(normal, lightDir);
  57. #else
  58. diff = GetDiffusePointOrSpot(normal, vLightVec.xyz, lightDir);
  59. #endif
  60. #ifdef SHADOW
  61. #if defined(DIRLIGHT)
  62. vec4 shadowPos = GetDirShadowPos(vShadowPos, vLightVec.w);
  63. diff *= min(GetShadow(shadowPos) + GetShadowFade(vLightVec.w), 1.0);
  64. #elif defined(SPOTLIGHT)
  65. diff *= GetShadow(vShadowPos);
  66. #else
  67. diff *= GetCubeShadow(vShadowPos);
  68. #endif
  69. #endif
  70. #if defined(SPOTLIGHT)
  71. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  72. #elif defined(CUBEMASK)
  73. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  74. #else
  75. lightColor = cLightColor.rgb;
  76. #endif
  77. #ifdef SPECULAR
  78. #ifdef SPECMAP
  79. vec3 specColor = cMatSpecColor.rgb * texture2D(sSpecMap, vTexCoord).g;
  80. #else
  81. vec3 specColor = cMatSpecColor.rgb;
  82. #endif
  83. float spec = GetSpecular(normal, vEyeVec, lightDir, cMatSpecColor.a);
  84. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  85. #else
  86. finalColor = diff * lightColor * diffColor.rgb;
  87. #endif
  88. #ifdef AMBIENT
  89. finalColor += cAmbientColor * diffColor.rgb;
  90. gl_FragColor = vec4(GetFog(finalColor, vLightVec.w), diffColor.a);
  91. #else
  92. gl_FragColor = vec4(GetLitFog(finalColor, vLightVec.w), diffColor.a);
  93. #endif
  94. }