duck0FS.glsl 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. precision highp float;
  2. varying vec3 v_normal;
  3. uniform vec4 u_ambient;
  4. varying vec2 v_texcoord0;
  5. uniform sampler2D u_diffuse;
  6. uniform vec4 u_emission;
  7. uniform vec4 u_specular;
  8. uniform float u_shininess;
  9. varying vec3 v_light0Direction;
  10. varying vec3 v_position;
  11. uniform vec3 u_light0Color;
  12. void main(void) {
  13. vec3 normal = normalize(v_normal);
  14. vec4 color = vec4(0., 0., 0., 0.);
  15. vec4 diffuse = vec4(0., 0., 0., 1.);
  16. vec3 diffuseLight = vec3(0., 0., 0.);
  17. vec4 emission;
  18. vec4 ambient;
  19. vec4 specular;
  20. ambient = u_ambient;
  21. diffuse = texture2D(u_diffuse, v_texcoord0);
  22. emission = u_emission;
  23. specular = u_specular;
  24. vec3 specularLight = vec3(0., 0., 0.);
  25. {
  26. float specularIntensity = 0.;
  27. float attenuation = 1.0;
  28. vec3 l = normalize(v_light0Direction);
  29. vec3 viewDir = -normalize(v_position);
  30. vec3 h = normalize(l+viewDir);
  31. specularIntensity = max(0., pow(max(dot(normal,h), 0.) , u_shininess)) * attenuation;
  32. specularLight += u_light0Color * specularIntensity;
  33. diffuseLight += u_light0Color * max(dot(normal,l), 0.) * attenuation;
  34. }
  35. specular.xyz *= specularLight;
  36. color.xyz += specular.xyz;
  37. diffuse.xyz *= diffuseLight;
  38. color.xyz += diffuse.xyz;
  39. color.xyz += emission.xyz;
  40. color = vec4(color.rgb * diffuse.a, diffuse.a);
  41. gl_FragColor = color;
  42. }