striped.shader 5.2 KB

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