FragShader.fsh 935 B

12345678910111213141516171819202122232425
  1. uniform sampler2D sBaseTex;
  2. uniform sampler2D sNormalMap;
  3. varying lowp vec3 LightVec;
  4. varying mediump vec2 TexCoord;
  5. void main()
  6. {
  7. // read the per-pixel normal from the normal map and expand to [-1, 1]
  8. lowp vec3 normal = texture2D(sNormalMap, TexCoord).rgb * 2.0 - 1.0;
  9. // linear interpolations of normals may cause shortened normals and thus
  10. // visible artifacts on low-poly models.
  11. // We omit the normalization here for performance reasons
  12. // calculate diffuse lighting as the cosine of the angle between light
  13. // direction and surface normal (both in surface local/tangent space)
  14. // We don't have to clamp to 0 here because the framebuffer write will be clamped
  15. lowp float lightIntensity = dot(LightVec, normal);
  16. // read base texture and modulate with light intensity
  17. lowp vec3 texColor = texture2D(sBaseTex, TexCoord).rgb;
  18. gl_FragColor = vec4(texColor * lightIntensity, 1.0);
  19. }