LitSolid.glsl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Lighting.glsl"
  6. #include "Fog.glsl"
  7. varying vec2 vTexCoord;
  8. #ifdef HEIGHTFOG
  9. varying vec3 vWorldPos;
  10. #endif
  11. #ifdef PERPIXEL
  12. varying vec4 vLightVec;
  13. #ifdef SPECULAR
  14. varying vec3 vEyeVec;
  15. #endif
  16. #ifndef NORMALMAP
  17. varying vec3 vNormal;
  18. #endif
  19. #ifdef SHADOW
  20. varying vec4 vShadowPos[NUMCASCADES];
  21. #endif
  22. #ifdef SPOTLIGHT
  23. varying vec4 vSpotPos;
  24. #endif
  25. #ifdef POINTLIGHT
  26. varying vec3 vCubeMaskVec;
  27. #endif
  28. #else
  29. varying vec4 vVertexLight;
  30. varying vec3 vNormal;
  31. #ifdef NORMALMAP
  32. varying vec3 vTangent;
  33. varying vec3 vBitangent;
  34. #endif
  35. varying vec4 vScreenPos;
  36. #ifdef ENVCUBEMAP
  37. varying vec3 vReflectionVec;
  38. #endif
  39. #if defined(LIGHTMAP) || defined(AO)
  40. varying vec2 vTexCoord2;
  41. #endif
  42. #endif
  43. void VS()
  44. {
  45. mat4 modelMatrix = iModelMatrix;
  46. vec3 worldPos = GetWorldPos(modelMatrix);
  47. gl_Position = GetClipPos(worldPos);
  48. vTexCoord = GetTexCoord(iTexCoord);
  49. #ifdef HEIGHTFOG
  50. vWorldPos = worldPos;
  51. #endif
  52. #if defined(PERPIXEL) && defined(NORMALMAP)
  53. vec3 vNormal;
  54. vec3 vTangent;
  55. vec3 vBitangent;
  56. #endif
  57. vNormal = GetWorldNormal(modelMatrix);
  58. #ifdef NORMALMAP
  59. vTangent = GetWorldTangent(modelMatrix);
  60. vBitangent = cross(vTangent, vNormal) * iTangent.w;
  61. #endif
  62. #ifdef PERPIXEL
  63. // Per-pixel forward lighting
  64. vec4 projWorldPos = vec4(worldPos, 1.0);
  65. #ifdef SHADOW
  66. // Shadow projection: transform from world space to shadow space
  67. for (int i = 0; i < NUMCASCADES; i++)
  68. vShadowPos[i] = GetShadowPos(i, projWorldPos);
  69. #endif
  70. #ifdef SPOTLIGHT
  71. // Spotlight projection: transform from world space to projector texture coordinates
  72. vSpotPos = cLightMatrices[0] * projWorldPos;
  73. #endif
  74. #ifdef POINTLIGHT
  75. vCubeMaskVec = mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz) * (cLightPos.xyz - worldPos);
  76. #endif
  77. #ifdef NORMALMAP
  78. mat3 tbn = mat3(vTangent, vBitangent, vNormal);
  79. #ifdef DIRLIGHT
  80. vLightVec = vec4(cLightDir * tbn, GetDepth(gl_Position));
  81. #else
  82. vLightVec = vec4((cLightPos.xyz - worldPos) * tbn * cLightPos.w, GetDepth(gl_Position));
  83. #endif
  84. #ifdef SPECULAR
  85. vEyeVec = (cCameraPos - worldPos) * tbn;
  86. #endif
  87. #else
  88. #ifdef DIRLIGHT
  89. vLightVec = vec4(cLightDir, GetDepth(gl_Position));
  90. #else
  91. vLightVec = vec4((cLightPos.xyz - worldPos) * cLightPos.w, GetDepth(gl_Position));
  92. #endif
  93. #ifdef SPECULAR
  94. vEyeVec = cCameraPos - worldPos;
  95. #endif
  96. #endif
  97. #else
  98. // Ambient & per-vertex lighting
  99. #if defined(LIGHTMAP) || defined(AO)
  100. // If using lightmap, disregard zone ambient light
  101. // If using AO, calculate ambient in the PS
  102. vVertexLight = vec4(0.0, 0.0, 0.0, GetDepth(gl_Position));
  103. vTexCoord2 = iTexCoord2;
  104. #else
  105. vVertexLight = vec4(GetAmbient(GetZonePos(worldPos)), GetDepth(gl_Position));
  106. #endif
  107. #ifdef NUMVERTEXLIGHTS
  108. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  109. vVertexLight.rgb += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
  110. #endif
  111. vScreenPos = GetScreenPos(gl_Position);
  112. #ifdef ENVCUBEMAP
  113. vReflectionVec = worldPos - cCameraPos;
  114. #endif
  115. #endif
  116. }
  117. void PS()
  118. {
  119. // Get material diffuse albedo
  120. #ifdef DIFFMAP
  121. vec4 diffInput = texture2D(sDiffMap, vTexCoord);
  122. #ifdef ALPHAMASK
  123. if (diffInput.a < 0.5)
  124. discard;
  125. #endif
  126. vec4 diffColor = cMatDiffColor * diffInput;
  127. #else
  128. vec4 diffColor = cMatDiffColor;
  129. #endif
  130. // Get material specular albedo
  131. #ifdef SPECMAP
  132. vec3 specColor = cMatSpecColor.rgb * texture2D(sSpecMap, vTexCoord).rgb;
  133. #else
  134. vec3 specColor = cMatSpecColor.rgb;
  135. #endif
  136. #if defined(PERPIXEL)
  137. // Per-pixel forward lighting
  138. vec3 lightColor;
  139. vec3 lightDir;
  140. vec3 finalColor;
  141. float diff;
  142. #ifdef NORMALMAP
  143. vec3 normal = DecodeNormal(texture2D(sNormalMap, vTexCoord));
  144. #else
  145. vec3 normal = normalize(vNormal);
  146. #endif
  147. diff = GetDiffuse(normal, vLightVec.xyz, lightDir);
  148. #ifdef SHADOW
  149. diff *= GetShadow(vShadowPos, vLightVec.w);
  150. #endif
  151. #if defined(SPOTLIGHT)
  152. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  153. #elif defined(CUBEMASK)
  154. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  155. #else
  156. lightColor = cLightColor.rgb;
  157. #endif
  158. #ifdef SPECULAR
  159. float spec = GetSpecular(normal, vEyeVec, lightDir, cMatSpecColor.a);
  160. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  161. #else
  162. finalColor = diff * lightColor * diffColor.rgb;
  163. #endif
  164. #ifdef HEIGHTFOG
  165. float fogFactor = GetHeightFogFactor(vLightVec.w, vWorldPos.y);
  166. #else
  167. float fogFactor = GetFogFactor(vLightVec.w);
  168. #endif
  169. #ifdef AMBIENT
  170. finalColor += cAmbientColor * diffColor.rgb;
  171. finalColor += cMatEmissiveColor;
  172. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  173. #else
  174. gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  175. #endif
  176. #elif defined(PREPASS)
  177. // Fill light pre-pass G-Buffer
  178. #ifdef NORMALMAP
  179. mat3 tbn = mat3(vTangent, vBitangent, vNormal);
  180. vec3 normal = tbn * DecodeNormal(texture2D(sNormalMap, vTexCoord.xy));
  181. #else
  182. vec3 normal = vNormal;
  183. #endif
  184. float specPower = cMatSpecColor.a / 255.0;
  185. gl_FragData[0] = vec4(normal * 0.5 + 0.5, specPower);
  186. gl_FragData[1] = vec4(EncodeDepth(vVertexLight.a), 0.0);
  187. #elif defined(DEFERRED)
  188. // Fill deferred G-buffer
  189. #ifdef NORMALMAP
  190. mat3 tbn = mat3(vTangent, vBitangent, vNormal);
  191. vec3 normal = tbn * DecodeNormal(texture2D(sNormalMap, vTexCoord));
  192. #else
  193. vec3 normal = vNormal;
  194. #endif
  195. float specIntensity = specColor.g;
  196. float specPower = cMatSpecColor.a / 255.0;
  197. vec3 finalColor = vVertexLight.rgb * diffColor.rgb;
  198. #ifdef AO
  199. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  200. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  201. #endif
  202. #ifdef ENVCUBEMAP
  203. normal = normalize(normal);
  204. finalColor = cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  205. #endif
  206. #ifdef LIGHTMAP
  207. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  208. #endif
  209. #ifdef EMISSIVEMAP
  210. finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord).rgb;
  211. #else
  212. finalColor += cMatEmissiveColor;
  213. #endif
  214. #ifdef HEIGHTFOG
  215. float fogFactor = GetHeightFogFactor(vVertexLight.a, vWorldPos.y);
  216. #else
  217. float fogFactor = GetFogFactor(vVertexLight.a);
  218. #endif
  219. gl_FragData[0] = vec4(GetFog(finalColor, fogFactor), 1.0);
  220. gl_FragData[1] = fogFactor * vec4(diffColor.rgb, specIntensity);
  221. gl_FragData[2] = vec4(normal * 0.5 + 0.5, specPower);
  222. gl_FragData[3] = vec4(EncodeDepth(vVertexLight.a), 0.0);
  223. #else
  224. // Ambient & per-vertex lighting
  225. vec3 finalColor = vVertexLight.rgb * diffColor.rgb;
  226. #ifdef AO
  227. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  228. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  229. #endif
  230. #ifdef MATERIAL
  231. // Add light pre-pass accumulation result
  232. // Lights are accumulated at half intensity. Bring back to full intensity now
  233. vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
  234. vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  235. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  236. #endif
  237. #ifdef ENVCUBEMAP
  238. #ifdef NORMALMAP
  239. mat3 tbn = mat3(vTangent, vBitangent, vNormal);
  240. vec3 normal = tbn * DecodeNormal(texture2D(sNormalMap, vTexCoord));
  241. #else
  242. vec3 normal = vNormal;
  243. #endif
  244. normal = normalize(normal);
  245. finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  246. #endif
  247. #ifdef LIGHTMAP
  248. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  249. #endif
  250. #ifdef EMISSIVEMAP
  251. finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord).rgb;
  252. #else
  253. finalColor += cMatEmissiveColor;
  254. #endif
  255. #ifdef HEIGHTFOG
  256. float fogFactor = GetHeightFogFactor(vVertexLight.a, vWorldPos.y);
  257. #else
  258. float fogFactor = GetFogFactor(vVertexLight.a);
  259. #endif
  260. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  261. #endif
  262. }