Advertisement
ThinMatrix

Lighting Fragment Shader

Aug 25th, 2022
3,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 150
  2.  
  3. const vec3 GROUND_COL = vec3(0.8, 1.0, 0.5);
  4. const vec3 SKY_COL = vec3(0.5, 0.75, 1.0);
  5.  
  6. in vec3 pass_colour;
  7. in vec3 pass_worldPos;
  8. in vec3 pass_worldNormal;
  9. in vec4 pass_material;//spec, shinyness, matFactor, flex
  10.  
  11. out vec4 out_colour;
  12.  
  13. uniform vec3 cameraPosition;
  14. uniform vec3 lightDirection;
  15. uniform vec3 lightColour;
  16. uniform vec3 lightSettings;//ambient, diffuse, shade
  17.  
  18. void main(void){
  19.  
  20.     vec3 normal = normalize(pass_worldNormal);
  21.     vec3 toCamera = normalize(cameraPosition - pass_worldPos);
  22.     vec3 toLight = normalize(-lightDirection);//already normalized?
  23.     vec3 reflectedView = reflect(-toCamera, normal);
  24.  
  25.     float sunDiffuse = clamp(dot(normal, toLight), 0.0, 1.0);
  26.     float skyDiffuse = 0.5 + 0.5 * clamp(normal.y, 0.0, 1.0);
  27.     float bounceDiffuse = 0.1 + 0.9 *clamp(-normal.y, 0.0, 1.0) * clamp(1.0 - 0.1 * pass_worldPos.y, 0.0, 1.0);
  28.     float fresnel = pow(1.0 - clamp(dot(toCamera, normal), 0.0, 1.0), 2.5);
  29.     float sunSpecular = pow(clamp(dot(reflectedView, -lightDirection), 0.0, 1.0), 8.0) * (0.8 + 5.0 * fresnel);
  30.     float rimLight = fresnel;
  31.     float skyReflection = smoothstep(0.0, 0.4, reflectedView.y) * (0.4 + 0.6 * fresnel);//use shinyness
  32.  
  33.     vec3 lighting = vec3(0.0);
  34.     lighting += 1.2 * skyDiffuse * SKY_COL;
  35.     lighting += 1.1 * sunDiffuse * lightColour;
  36.     lighting += 0.2 * bounceDiffuse * GROUND_COL;
  37.  
  38.     vec3 finalColour = pass_colour * lighting;
  39.     finalColour += 0.07 * skyReflection * SKY_COL * skyDiffuse;
  40.     finalColour += 0.15 * rimLight * lightColour;
  41.     finalColour += 0.25 * sunSpecular * lightColour;
  42.  
  43.     out_colour = vec4(pow(finalColour, vec3(0.4545)), 1.0);
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement