fragment.fs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #version 430
  2. layout (location = 0) in vec3 pos;
  3. layout (location = 1) in vec2 tex;
  4. layout (location = 2) in vec4 vertexColor;
  5. uniform sampler2D samp;
  6. uniform mat4 viewMatrix;
  7. uniform mat4 modelMatrix;
  8. uniform vec3 ambientLight;
  9. struct Light
  10. {
  11. vec2 pos;
  12. float strength;
  13. vec3 color;
  14. };
  15. uniform Light lights[32];
  16. uniform bool useTexture;
  17. uniform bool useColor;
  18. uniform bool useLight;
  19. in vec2 tc;
  20. in vec2 loc;
  21. in vec4 vColor;
  22. out vec4 color;
  23. vec4 getLight()
  24. {
  25. vec3 light = vec3(0, 0, 0);
  26. for(int i = 0; i < 32; i++)
  27. {
  28. light += lights[i].color * max(1 - lights[i].strength * length(loc - lights[i].pos), 0);
  29. }
  30. return vec4(min(light, vec3(1, 1, 1)), 1);
  31. }
  32. void main(void)
  33. {
  34. if(useTexture)
  35. {
  36. color = texture(samp, tc);
  37. if(color.a == 0.0)
  38. {
  39. discard;
  40. }
  41. if(useColor)
  42. {
  43. color = vColor;
  44. }
  45. }
  46. else
  47. {
  48. color = vColor;
  49. }
  50. if(useLight)
  51. {
  52. color = pow(pow(color, vec4(1 / 2.2)) * vec4(ambientLight, 1.0) + getLight(), vec4(2.2));
  53. }
  54. }