Guest User

frag_shader

a guest
Dec 28th, 2023
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #version 450
  2.  
  3. #define PI 3.14159265359
  4.  
  5. layout(binding = 0) uniform UBO{
  6. mat4 vM;
  7. mat4 cM;
  8. mat4 pM;
  9. vec4 lP;
  10. float time;
  11. uvec2 res;
  12. float varA;
  13. } ubo;
  14.  
  15. layout(binding = 1) uniform sampler2DArray tex_samplr;
  16.  
  17. layout(location = 0) in vec2 frag_uvco;
  18. layout(location = 1) in vec3 frag_norm;
  19. layout(location = 2) in vec3 frag_wpos;
  20. layout(location = 3) in vec3 frag_light_dir;
  21. layout(location = 4) in mat3 frag_TBN;
  22.  
  23. layout(location = 0) out vec4 out_frag;
  24.  
  25. uvec2 res = ubo.res;
  26. float time = ubo.time;
  27.  
  28. vec3 specular(){
  29. vec3 diffuse_light = vec3(1.);
  30. vec3 specular_light = vec3(.0);
  31. vec3 tex_color = texture(tex_samplr,vec3(frag_uvco * 10,0)).rgb;
  32. vec3 tex_norm = texture(tex_samplr,vec3(frag_uvco * 10,1)).rgb;
  33. tex_norm = normalize(tex_norm * 2.0 - 1.0);
  34. tex_norm = normalize(frag_TBN * tex_norm);
  35.  
  36. vec3 surf_norm = /*normalize(frag_norm) + */normalize(tex_norm);
  37.  
  38. // return surf_norm;
  39.  
  40. vec3 cam_wpos = ubo.cM[3].xyz;
  41. vec3 view_dir = normalize(cam_wpos - frag_wpos);
  42.  
  43. vec3 light_dir = frag_TBN * frag_light_dir; // Direction to light
  44. float fading = 1 / dot(light_dir, light_dir); // Attenuation
  45. light_dir = normalize(light_dir);
  46.  
  47. float cos_ang_incid = max(dot(surf_norm, light_dir), 0);
  48. vec3 intensity = vec3(1.) * fading;
  49. diffuse_light += intensity * cos_ang_incid * (ubo.varA / 10);
  50.  
  51. vec3 half_ang = normalize(light_dir + view_dir);
  52. float blinn_term = dot (surf_norm, half_ang);
  53. blinn_term = clamp (blinn_term, 0, 1);
  54. blinn_term = pow (blinn_term,192);
  55.  
  56. specular_light += intensity * blinn_term;
  57. specular_light *= ubo.varA / 50;
  58.  
  59. vec3 ambient = .25 * tex_color;
  60.  
  61. return ambient * diffuse_light + specular_light;
  62. }
  63.  
  64. void main(){
  65. out_frag.rgb = specular();
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment