LitSolid.glsl 8.2 KB

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