test.frag.glsl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #version 330
  2. in FS_IN {
  3. vec3 normal;
  4. vec2 uv;
  5. vec3 pos;
  6. vec4 tangent;
  7. } fs_in;
  8. out vec4 color;
  9. section Material {
  10. uniform bool use_diffuse_texture;
  11. uniform vec3 diffuse_color;
  12. uniform sampler2D diffuse_texture;
  13. uniform bool use_normal_texture;
  14. uniform sampler2D normal_texture;
  15. uniform float normal_texture_scale;
  16. };
  17. section Lighting {
  18. uniform float ambient_light;
  19. uniform float light_angle_a;
  20. uniform float light_angle_b;
  21. uniform float light_intensity;
  22. uniform vec3 light_color;
  23. };
  24. vec4 shade(vec3 normal, vec3 tangent, vec3 bitangent, vec3 light_color, vec3 light_dir, float light_intensity)
  25. {
  26. float diffuse_intensity = max(dot(normal, light_dir),0);
  27. vec3 dcolor = diffuse_color;
  28. if (use_diffuse_texture)
  29. dcolor *= texture(diffuse_texture, vec2(fs_in.uv.x, -fs_in.uv.y)).xyz;
  30. return vec4(dcolor *
  31. light_color *
  32. light_intensity * 2 *
  33. mix(diffuse_intensity, 1, ambient_light), 1);
  34. }
  35. void main()
  36. {
  37. vec3 normal = normalize(fs_in.normal);
  38. vec3 tangent = normalize(fs_in.tangent.xyz);
  39. vec3 bitangent = cross(tangent, normal) * fs_in.tangent.w;
  40. float angle_a = (light_angle_a) * 2 * 3.1415;
  41. float angle_b = (light_angle_b) * 2 * 3.1415;
  42. vec3 light_dir = vec3(cos(angle_b) * sin(angle_a), cos(angle_b) * cos(angle_a), sin(angle_b));
  43. if (use_normal_texture) {
  44. vec3 snorm = texture(normal_texture, vec2(fs_in.uv.x, -fs_in.uv.y)).xyz;
  45. snorm -= vec3(0.5);
  46. snorm *= vec3(8 * normal_texture_scale, 8 * normal_texture_scale, 2);
  47. normal += (snorm.z * normal) + (snorm.y * tangent) + (snorm.x * bitangent);
  48. normal = normalize(normal);
  49. }
  50. color = shade(normal, tangent, bitangent, light_color, light_dir, light_intensity);
  51. }