LitSolid.glsl 7.9 KB

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