No Description

uber.frag 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #version 450
  2. #extension GL_ARB_separate_shader_objects : enable
  3. #extension GL_ARB_shading_language_420pack : enable
  4. layout (binding = 1) uniform sampler2D samplerColormap;
  5. layout (binding = 2) uniform sampler2D samplerDiscard;
  6. layout (location = 0) in vec3 inNormal;
  7. layout (location = 1) in vec3 inColor;
  8. layout (location = 2) in vec2 inUV;
  9. layout (location = 3) in vec3 inViewVec;
  10. layout (location = 4) in vec3 inLightVec;
  11. layout (location = 0) out vec4 outFragColor;
  12. // We use this constant to control the flow of the shader depending on the
  13. // lighting model selected at pipeline creation time
  14. layout (constant_id = 0) const int LIGHTING_MODEL = 0;
  15. // Parameter for the toon shading part of the shader
  16. layout (constant_id = 1) const float PARAM_TOON_DESATURATION = 0.0f;
  17. void main()
  18. {
  19. switch (LIGHTING_MODEL) {
  20. case 0: // Phong
  21. {
  22. vec3 ambient = inColor * vec3(0.25);
  23. vec3 N = normalize(inNormal);
  24. vec3 L = normalize(inLightVec);
  25. vec3 V = normalize(inViewVec);
  26. vec3 R = reflect(-L, N);
  27. vec3 diffuse = max(dot(N, L), 0.0) * inColor;
  28. vec3 specular = pow(max(dot(R, V), 0.0), 32.0) * vec3(0.75);
  29. outFragColor = vec4(ambient + diffuse * 1.75 + specular, 1.0);
  30. break;
  31. }
  32. case 1: // Toon
  33. {
  34. vec3 N = normalize(inNormal);
  35. vec3 L = normalize(inLightVec);
  36. float intensity = dot(N,L);
  37. vec3 color;
  38. if (intensity > 0.98)
  39. color = inColor * 1.5;
  40. else if (intensity > 0.9)
  41. color = inColor * 1.0;
  42. else if (intensity > 0.5)
  43. color = inColor * 0.6;
  44. else if (intensity > 0.25)
  45. color = inColor * 0.4;
  46. else
  47. color = inColor * 0.2;
  48. // Desaturate a bit
  49. color = vec3(mix(color, vec3(dot(vec3(0.2126,0.7152,0.0722), color)), PARAM_TOON_DESATURATION));
  50. outFragColor.rgb = color;
  51. break;
  52. }
  53. case 2: // Textured
  54. {
  55. vec4 color = texture(samplerColormap, inUV).rrra;
  56. vec3 ambient = color.rgb * vec3(0.25) * inColor;
  57. vec3 N = normalize(inNormal);
  58. vec3 L = normalize(inLightVec);
  59. vec3 V = normalize(inViewVec);
  60. vec3 R = reflect(-L, N);
  61. vec3 diffuse = max(dot(N, L), 0.0) * color.rgb;
  62. float specular = pow(max(dot(R, V), 0.0), 32.0) * color.a;
  63. outFragColor = vec4(ambient + diffuse + vec3(specular), 1.0);
  64. break;
  65. }
  66. }
  67. }