Duck1.frag 1.3 KB

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