phong_fragment.glsl 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // Fragment Shader template
  3. //
  4. precision highp float;
  5. // Inputs from vertex shader
  6. in vec4 Position; // Vertex position in camera coordinates.
  7. in vec3 Normal; // Vertex normal in camera coordinates.
  8. in vec3 CamDir; // Direction from vertex to camera
  9. in vec2 FragTexcoord;
  10. #include <lights>
  11. #include <material>
  12. #include <phong_model>
  13. // Final fragment color
  14. out vec4 FragColor;
  15. void main() {
  16. // Mix material color with textures colors
  17. vec4 texMixed = vec4(1);
  18. vec4 texColor;
  19. #if MAT_TEXTURES==1
  20. MIX_TEXTURE(0)
  21. #elif MAT_TEXTURES==2
  22. MIX_TEXTURE(0)
  23. MIX_TEXTURE(1)
  24. #elif MAT_TEXTURES==3
  25. MIX_TEXTURE(0)
  26. MIX_TEXTURE(1)
  27. MIX_TEXTURE(2)
  28. #endif
  29. // Combine material with texture colors
  30. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texMixed;
  31. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texMixed;
  32. // Inverts the fragment normal if not FrontFacing
  33. vec3 fragNormal = Normal;
  34. if (!gl_FrontFacing) {
  35. fragNormal = -fragNormal;
  36. }
  37. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  38. vec3 Ambdiff, Spec;
  39. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  40. // Final fragment color
  41. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  42. }