overhang.shader 5.4 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 f_vertex;
  11. varying highp vec3 f_normal;
  12. void main()
  13. {
  14. vec4 world_space_vert = u_modelMatrix * a_vertex;
  15. gl_Position = u_projectionMatrix * u_viewMatrix * world_space_vert;
  16. f_vertex = world_space_vert.xyz;
  17. f_normal = (u_normalMatrix * normalize(a_normal)).xyz;
  18. }
  19. fragment =
  20. uniform mediump vec4 u_ambientColor;
  21. uniform mediump vec4 u_diffuseColor;
  22. uniform mediump vec4 u_specularColor;
  23. uniform highp vec3 u_lightPosition;
  24. uniform mediump float u_shininess;
  25. uniform highp vec3 u_viewPosition;
  26. uniform lowp float u_overhangAngle;
  27. uniform lowp vec4 u_overhangColor;
  28. uniform lowp float u_lowestPrintableHeight;
  29. uniform lowp vec4 u_faceColor;
  30. uniform highp int u_faceId;
  31. varying highp vec3 f_vertex;
  32. varying highp vec3 f_normal;
  33. uniform lowp float u_renderError;
  34. void main()
  35. {
  36. mediump vec4 finalColor = vec4(0.0);
  37. // Ambient Component
  38. finalColor += u_ambientColor;
  39. highp vec3 normal = normalize(f_normal);
  40. highp vec3 lightDir = normalize(u_lightPosition - f_vertex);
  41. // Diffuse Component
  42. highp float NdotL = clamp(abs(dot(normal, lightDir)), 0.0, 1.0);
  43. finalColor += (NdotL * u_diffuseColor);
  44. // Specular Component
  45. // TODO: We should not do specularity for fragments facing away from the light.
  46. highp vec3 reflectedLight = reflect(-lightDir, normal);
  47. highp vec3 viewVector = normalize(u_viewPosition - f_vertex);
  48. highp float NdotR = clamp(dot(viewVector, reflectedLight), 0.0, 1.0);
  49. finalColor += pow(NdotR, u_shininess) * u_specularColor;
  50. finalColor = (f_vertex.y >= 0.0 && -normal.y > u_overhangAngle) ? u_overhangColor : finalColor;
  51. highp vec3 grid = vec3(f_vertex.x - floor(f_vertex.x - 0.5), f_vertex.y - floor(f_vertex.y - 0.5), f_vertex.z - floor(f_vertex.z - 0.5));
  52. finalColor.a = (u_renderError > 0.5) && dot(grid, grid) < 0.245 ? 0.667 : 1.0;
  53. if (f_vertex.y <= u_lowestPrintableHeight)
  54. {
  55. finalColor.rgb = vec3(1.0, 1.0, 1.0) - finalColor.rgb;
  56. }
  57. gl_FragColor = finalColor;
  58. }
  59. vertex41core =
  60. #version 410
  61. uniform highp mat4 u_modelMatrix;
  62. uniform highp mat4 u_viewMatrix;
  63. uniform highp mat4 u_projectionMatrix;
  64. uniform highp mat4 u_normalMatrix;
  65. in highp vec4 a_vertex;
  66. in highp vec4 a_normal;
  67. in highp vec2 a_uvs;
  68. out highp vec3 f_vertex;
  69. out highp vec3 f_normal;
  70. void main()
  71. {
  72. vec4 world_space_vert = u_modelMatrix * a_vertex;
  73. gl_Position = u_projectionMatrix * u_viewMatrix * world_space_vert;
  74. f_vertex = world_space_vert.xyz;
  75. f_normal = (u_normalMatrix * normalize(a_normal)).xyz;
  76. }
  77. fragment41core =
  78. #version 410
  79. uniform mediump vec4 u_ambientColor;
  80. uniform mediump vec4 u_diffuseColor;
  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 lowp float u_renderError;
  86. uniform lowp float u_overhangAngle;
  87. uniform lowp vec4 u_overhangColor;
  88. uniform lowp float u_lowestPrintableHeight;
  89. uniform lowp vec4 u_faceColor;
  90. uniform highp int u_faceId;
  91. in highp vec3 f_vertex;
  92. in highp vec3 f_normal;
  93. out vec4 frag_color;
  94. void main()
  95. {
  96. mediump vec4 finalColor = vec4(0.0);
  97. // Ambient Component
  98. finalColor += u_ambientColor;
  99. highp vec3 normal = normalize(f_normal);
  100. highp vec3 lightDir = normalize(u_lightPosition - f_vertex);
  101. // Diffuse Component
  102. highp float NdotL = clamp(abs(dot(normal, lightDir)), 0.0, 1.0);
  103. finalColor += (NdotL * u_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 - f_vertex);
  108. highp float NdotR = clamp(dot(viewVector, reflectedLight), 0.0, 1.0);
  109. finalColor += pow(NdotR, u_shininess) * u_specularColor;
  110. finalColor = (u_faceId != gl_PrimitiveID) ? ((f_vertex.y >= 0.0 && -normal.y > u_overhangAngle) ? u_overhangColor : finalColor) : u_faceColor;
  111. frag_color = finalColor;
  112. if (f_vertex.y <= u_lowestPrintableHeight)
  113. {
  114. frag_color.rgb = vec3(1.0, 1.0, 1.0) - frag_color.rgb;
  115. }
  116. vec3 grid = f_vertex - round(f_vertex);
  117. frag_color.a = (u_renderError > 0.5) && dot(grid, grid) < 0.245 ? 0.667 : 1.0;
  118. }
  119. [defaults]
  120. u_ambientColor = [0.3, 0.3, 0.3, 1.0]
  121. u_diffuseColor = [1.0, 0.79, 0.14, 1.0]
  122. u_specularColor = [0.4, 0.4, 0.4, 1.0]
  123. u_overhangColor = [1.0, 0.0, 0.0, 1.0]
  124. u_faceColor = [0.0, 0.0, 1.0, 1.0]
  125. u_shininess = 20.0
  126. u_renderError = 1.0
  127. u_lowestPrintableHeight = 0.0
  128. [bindings]
  129. u_modelMatrix = model_matrix
  130. u_viewMatrix = view_matrix
  131. u_projectionMatrix = projection_matrix
  132. u_normalMatrix = normal_matrix
  133. u_viewPosition = view_position
  134. u_lightPosition = light_0_position
  135. u_diffuseColor = diffuse_color
  136. u_faceId = hover_face
  137. [attributes]
  138. a_vertex = vertex
  139. a_normal = normal