standard_fragment.glsl 1.7 KB

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