striped.shader 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. [shaders]
  2. vertex =
  3. uniform highp mat4 u_modelMatrix;
  4. uniform highp mat4 u_viewProjectionMatrix;
  5. uniform highp mat4 u_normalMatrix;
  6. attribute highp vec4 a_vertex;
  7. attribute highp vec4 a_normal;
  8. attribute highp vec2 a_uvs;
  9. varying highp vec3 v_position;
  10. varying highp vec3 v_vertex;
  11. varying highp vec3 v_normal;
  12. void main()
  13. {
  14. vec4 world_space_vert = u_modelMatrix * a_vertex;
  15. gl_Position = u_viewProjectionMatrix * world_space_vert;
  16. v_position = gl_Position.xyz;
  17. v_vertex = world_space_vert.xyz;
  18. v_normal = (u_normalMatrix * normalize(a_normal)).xyz;
  19. }
  20. fragment =
  21. uniform mediump vec4 u_ambientColor;
  22. uniform mediump vec4 u_diffuseColor1;
  23. uniform mediump vec4 u_diffuseColor2;
  24. uniform mediump vec4 u_specularColor;
  25. uniform highp vec3 u_lightPosition;
  26. uniform mediump float u_shininess;
  27. uniform highp vec3 u_viewPosition;
  28. uniform mediump float u_width;
  29. varying highp vec3 v_position;
  30. varying highp vec3 v_vertex;
  31. varying highp vec3 v_normal;
  32. void main()
  33. {
  34. mediump vec4 finalColor = vec4(0.0);
  35. mediump vec4 diffuseColor = (mod((-v_position.x + v_position.y), u_width) < (u_width / 2.)) ? u_diffuseColor1 : u_diffuseColor2;
  36. /* Ambient Component */
  37. finalColor += u_ambientColor;
  38. highp vec3 normal = normalize(v_normal);
  39. highp vec3 lightDir = normalize(u_lightPosition - v_vertex);
  40. /* Diffuse Component */
  41. highp float NdotL = clamp(abs(dot(normal, lightDir)), 0.0, 1.0);
  42. finalColor += (NdotL * diffuseColor);
  43. /* Specular Component */
  44. /* TODO: We should not do specularity for fragments facing away from the light.*/
  45. highp vec3 reflectedLight = reflect(-lightDir, normal);
  46. highp vec3 viewVector = normalize(u_viewPosition - v_vertex);
  47. highp float NdotR = clamp(dot(viewVector, reflectedLight), 0.0, 1.0);
  48. finalColor += pow(NdotR, u_shininess) * u_specularColor;
  49. gl_FragColor = finalColor;
  50. gl_FragColor.a = 1.0;
  51. }
  52. [defaults]
  53. u_ambientColor = [0.3, 0.3, 0.3, 1.0]
  54. u_diffuseColor1 = [1.0, 0.5, 0.5, 1.0]
  55. u_diffuseColor2 = [0.5, 0.5, 0.5, 1.0]
  56. u_specularColor = [0.4, 0.4, 0.4, 1.0]
  57. u_shininess = 20.0
  58. u_width = 5.0
  59. [bindings]
  60. u_modelMatrix = model_matrix
  61. u_viewProjectionMatrix = view_projection_matrix
  62. u_normalMatrix = normal_matrix
  63. u_viewPosition = view_position
  64. u_lightPosition = light_0_position
  65. u_diffuseColor = diffuse_color
  66. [attributes]
  67. a_vertex = vertex
  68. a_normal = normal