LitSolid.glsl 8.1 KB

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