striped.shader 5.5 KB

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