phong_fragment.glsl 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #if MAT_TEXTURES==1
  19. texMixed = MIX_TEXTURE(texMixed, FragTexcoord, 0);
  20. #elif MAT_TEXTURES==2
  21. texMixed = MIX_TEXTURE(texMixed, FragTexcoord, 0);
  22. texMixed = MIX_TEXTURE(texMixed, FragTexcoord, 1);
  23. #elif MAT_TEXTURES==3
  24. texMixed = MIX_TEXTURE(texMixed, FragTexcoord, 0);
  25. texMixed = MIX_TEXTURE(texMixed, FragTexcoord, 1);
  26. texMixed = MIX_TEXTURE(texMixed, FragTexcoord, 2);
  27. #endif
  28. // Combine material with texture colors
  29. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texMixed;
  30. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texMixed;
  31. // Inverts the fragment normal if not FrontFacing
  32. vec3 fragNormal = Normal;
  33. if (!gl_FrontFacing) {
  34. fragNormal = -fragNormal;
  35. }
  36. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  37. vec3 Ambdiff, Spec;
  38. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  39. // Final fragment color
  40. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  41. }