Advertisement
Real_Jace_Dev

Fragment Shader

Feb 26th, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Light {
  2.     vec3 position;
  3.     vec3 Iambient;
  4.     vec3 Idiffuse;
  5.     vec3 Ispecular;
  6. };
  7.  
  8. uniform Light lights[LIGHTS_AMOUNT];
  9. uniform vec3 cameraPosition;
  10.  
  11. float constant = 1.0;
  12. float linear = 0.007;
  13. float quadratic = 0.002;
  14.  
  15. vec3 lightning(vec3 color) {
  16.  
  17.     vec3 finalDiffuse = vec3(1.0);
  18.  
  19.     for (int i=0; i <= LIGHTS_AMOUNT; i++) {
  20.  
  21.         Light light = lights[i];
  22.  
  23.         vec3 Normal = normalize(normal);
  24.  
  25.         // Ambient
  26.         vec3 ambient = light.Iambient;
  27.         // Diffuse
  28.         vec3 lightDirection = normalize(light.position - fragmentPosition);
  29.         float diff = max(0,dot(lightDirection,Normal));
  30.         vec3 diffuse = diff * light.Idiffuse;
  31.         // Specular
  32.         vec3 viewDirection = normalize(cameraPosition - fragmentPosition);
  33.         vec3 reflectDirection = reflect(-lightDirection,Normal);
  34.         float spec = pow(max(dot(viewDirection,reflectDirection),0),32);
  35.         vec3 specular = spec * light.Ispecular;
  36.  
  37.         float distance = length(light.position - fragmentPosition);
  38.         float attenuation = 1.0 / (constant + linear * distance + quadratic * (distance * distance));
  39.  
  40.         ambient *= attenuation;
  41.         diffuse *= attenuation;
  42.         specular *= attenuation;
  43.  
  44.         finalDiffuse += (ambient + diffuse + specular);
  45.  
  46.     }
  47.  
  48.     return color * finalDiffuse;
  49. }
  50.  
  51. void main() {
  52.     vec3 color = texture(textureData,uv_0).rgb;
  53.     vec3 lightningColor = lightning(color);
  54.     fragColor = vec4(lightningColor,1.0);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement