striped.shader 5.7 KB

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