striped.shader 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. vertex41core =
  53. #version 410
  54. uniform highp mat4 u_modelMatrix;
  55. uniform highp mat4 u_viewProjectionMatrix;
  56. uniform highp mat4 u_normalMatrix;
  57. in highp vec4 a_vertex;
  58. in highp vec4 a_normal;
  59. in highp vec2 a_uvs;
  60. out highp vec3 v_position;
  61. out highp vec3 v_vertex;
  62. out highp vec3 v_normal;
  63. void main()
  64. {
  65. vec4 world_space_vert = u_modelMatrix * a_vertex;
  66. gl_Position = u_viewProjectionMatrix * world_space_vert;
  67. v_position = gl_Position.xyz;
  68. v_vertex = world_space_vert.xyz;
  69. v_normal = (u_normalMatrix * normalize(a_normal)).xyz;
  70. }
  71. fragment41core =
  72. #version 410
  73. uniform mediump vec4 u_ambientColor;
  74. uniform mediump vec4 u_diffuseColor1;
  75. uniform mediump vec4 u_diffuseColor2;
  76. uniform mediump vec4 u_specularColor;
  77. uniform highp vec3 u_lightPosition;
  78. uniform mediump float u_shininess;
  79. uniform highp vec3 u_viewPosition;
  80. uniform mediump float u_width;
  81. in highp vec3 v_position;
  82. in highp vec3 v_vertex;
  83. in highp vec3 v_normal;
  84. out vec4 frag_color;
  85. void main()
  86. {
  87. mediump vec4 finalColor = vec4(0.0);
  88. mediump vec4 diffuseColor = (mod((-v_position.x + v_position.y), u_width) < (u_width / 2.)) ? u_diffuseColor1 : u_diffuseColor2;
  89. /* Ambient Component */
  90. finalColor += u_ambientColor;
  91. highp vec3 normal = normalize(v_normal);
  92. highp vec3 lightDir = normalize(u_lightPosition - v_vertex);
  93. /* Diffuse Component */
  94. highp float NdotL = clamp(abs(dot(normal, lightDir)), 0.0, 1.0);
  95. finalColor += (NdotL * diffuseColor);
  96. /* Specular Component */
  97. /* TODO: We should not do specularity for fragments facing away from the light.*/
  98. highp vec3 reflectedLight = reflect(-lightDir, normal);
  99. highp vec3 viewVector = normalize(u_viewPosition - v_vertex);
  100. highp float NdotR = clamp(dot(viewVector, reflectedLight), 0.0, 1.0);
  101. finalColor += pow(NdotR, u_shininess) * u_specularColor;
  102. frag_color = finalColor;
  103. frag_color.a = 1.0;
  104. }
  105. [defaults]
  106. u_ambientColor = [0.3, 0.3, 0.3, 1.0]
  107. u_diffuseColor1 = [1.0, 0.5, 0.5, 1.0]
  108. u_diffuseColor2 = [0.5, 0.5, 0.5, 1.0]
  109. u_specularColor = [0.4, 0.4, 0.4, 1.0]
  110. u_shininess = 20.0
  111. u_width = 5.0
  112. [bindings]
  113. u_modelMatrix = model_matrix
  114. u_viewProjectionMatrix = view_projection_matrix
  115. u_normalMatrix = normal_matrix
  116. u_viewPosition = view_position
  117. u_lightPosition = light_0_position
  118. u_diffuseColor = diffuse_color
  119. [attributes]
  120. a_vertex = vertex
  121. a_normal = normal