LitSolid.frag 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "Uniforms.frag"
  2. #include "Samplers.frag"
  3. #include "Lighting.frag"
  4. #include "Fog.frag"
  5. varying vec2 vTexCoord;
  6. #ifdef HEIGHTFOG
  7. varying vec3 vWorldPos;
  8. #endif
  9. #ifdef PERPIXEL
  10. varying vec4 vLightVec;
  11. #ifdef SPECULAR
  12. varying vec3 vEyeVec;
  13. #endif
  14. #ifndef NORMALMAP
  15. varying vec3 vNormal;
  16. #endif
  17. #ifdef SHADOW
  18. varying vec4 vShadowPos[NUMCASCADES];
  19. #endif
  20. #ifdef SPOTLIGHT
  21. varying vec4 vSpotPos;
  22. #endif
  23. #ifdef POINTLIGHT
  24. varying vec3 vCubeMaskVec;
  25. #endif
  26. #else
  27. varying vec4 vVertexLight;
  28. varying vec3 vNormal;
  29. #ifdef NORMALMAP
  30. varying vec3 vTangent;
  31. varying vec3 vBitangent;
  32. #endif
  33. varying vec4 vScreenPos;
  34. #ifdef ENVCUBEMAP
  35. varying vec3 vReflectionVec;
  36. #endif
  37. #if defined(LIGHTMAP) || defined(AO)
  38. varying vec2 vTexCoord2;
  39. #endif
  40. #endif
  41. void main()
  42. {
  43. // Get material diffuse albedo
  44. #ifdef DIFFMAP
  45. vec4 diffInput = texture2D(sDiffMap, vTexCoord);
  46. #ifdef ALPHAMASK
  47. if (diffInput.a < 0.5)
  48. discard;
  49. #endif
  50. vec4 diffColor = cMatDiffColor * diffInput;
  51. #else
  52. vec4 diffColor = cMatDiffColor;
  53. #endif
  54. // Get material specular albedo
  55. #ifdef SPECMAP
  56. vec3 specColor = cMatSpecColor.rgb * texture2D(sSpecMap, vTexCoord).rgb;
  57. #else
  58. vec3 specColor = cMatSpecColor.rgb;
  59. #endif
  60. #if defined(PERPIXEL)
  61. // Per-pixel forward lighting
  62. vec3 lightColor;
  63. vec3 lightDir;
  64. vec3 finalColor;
  65. float diff;
  66. #ifdef NORMALMAP
  67. vec3 normal = DecodeNormal(texture2D(sNormalMap, vTexCoord));
  68. #else
  69. vec3 normal = normalize(vNormal);
  70. #endif
  71. diff = GetDiffuse(normal, vLightVec.xyz, lightDir);
  72. #ifdef SHADOW
  73. diff *= GetShadow(vShadowPos, vLightVec.w);
  74. #endif
  75. #if defined(SPOTLIGHT)
  76. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  77. #elif defined(CUBEMASK)
  78. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  79. #else
  80. lightColor = cLightColor.rgb;
  81. #endif
  82. #ifdef SPECULAR
  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 HEIGHTFOG
  89. float fogFactor = GetHeightFogFactor(vLightVec.w, vWorldPos.y);
  90. #else
  91. float fogFactor = GetFogFactor(vLightVec.w);
  92. #endif
  93. #ifdef AMBIENT
  94. finalColor += cAmbientColor * diffColor.rgb;
  95. finalColor += cMatEmissiveColor;
  96. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  97. #else
  98. gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  99. #endif
  100. #elif defined(PREPASS)
  101. // Fill light pre-pass G-Buffer
  102. #ifdef NORMALMAP
  103. mat3 tbn = mat3(vTangent, vBitangent, vNormal);
  104. vec3 normal = tbn * DecodeNormal(texture2D(sNormalMap, vTexCoord.xy));
  105. #else
  106. vec3 normal = vNormal;
  107. #endif
  108. float specPower = cMatSpecColor.a / 255.0;
  109. gl_FragData[0] = vec4(normal * 0.5 + 0.5, specPower);
  110. gl_FragData[1] = vec4(EncodeDepth(vVertexLight.a), 0.0);
  111. #elif defined(DEFERRED)
  112. // Fill deferred G-buffer
  113. #ifdef NORMALMAP
  114. mat3 tbn = mat3(vTangent, vBitangent, vNormal);
  115. vec3 normal = tbn * DecodeNormal(texture2D(sNormalMap, vTexCoord));
  116. #else
  117. vec3 normal = vNormal;
  118. #endif
  119. float specIntensity = specColor.g;
  120. float specPower = cMatSpecColor.a / 255.0;
  121. vec3 finalColor = vVertexLight.rgb * diffColor.rgb;
  122. #ifdef AO
  123. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  124. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  125. #endif
  126. #ifdef ENVCUBEMAP
  127. normal = normalize(normal);
  128. finalColor = cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  129. #endif
  130. #ifdef LIGHTMAP
  131. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  132. #endif
  133. #ifdef EMISSIVEMAP
  134. finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord).rgb;
  135. #else
  136. finalColor += cMatEmissiveColor;
  137. #endif
  138. #ifdef HEIGHTFOG
  139. float fogFactor = GetHeightFogFactor(vVertexLight.a, vWorldPos.y);
  140. #else
  141. float fogFactor = GetFogFactor(vVertexLight.a);
  142. #endif
  143. gl_FragData[0] = vec4(GetFog(finalColor, fogFactor), 1.0);
  144. gl_FragData[1] = fogFactor * vec4(diffColor.rgb, specIntensity);
  145. gl_FragData[2] = vec4(normal * 0.5 + 0.5, specPower);
  146. gl_FragData[3] = vec4(EncodeDepth(vVertexLight.a), 0.0);
  147. #else
  148. // Ambient & per-vertex lighting
  149. vec3 finalColor = vVertexLight.rgb * diffColor.rgb;
  150. #ifdef AO
  151. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  152. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  153. #endif
  154. #ifdef MATERIAL
  155. // Add light pre-pass accumulation result
  156. // Lights are accumulated at half intensity. Bring back to full intensity now
  157. vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
  158. vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  159. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  160. #endif
  161. #ifdef ENVCUBEMAP
  162. #ifdef NORMALMAP
  163. mat3 tbn = mat3(vTangent, vBitangent, vNormal);
  164. vec3 normal = tbn * DecodeNormal(texture2D(sNormalMap, vTexCoord));
  165. #else
  166. vec3 normal = vNormal;
  167. #endif
  168. normal = normalize(normal);
  169. finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  170. #endif
  171. #ifdef LIGHTMAP
  172. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  173. #endif
  174. #ifdef EMISSIVEMAP
  175. finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord).rgb;
  176. #else
  177. finalColor += cMatEmissiveColor;
  178. #endif
  179. #ifdef HEIGHTFOG
  180. float fogFactor = GetHeightFogFactor(vVertexLight.a, vWorldPos.y);
  181. #else
  182. float fogFactor = GetFogFactor(vVertexLight.a);
  183. #endif
  184. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  185. #endif
  186. }