standard_fragment.glsl 1.6 KB

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