striped.shader 5.1 KB

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