overhang.shader 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 vec4 u_faceColor;
  29. uniform highp int u_faceId;
  30. varying highp vec3 f_vertex;
  31. varying highp vec3 f_normal;
  32. uniform lowp float u_renderError;
  33. float round(float f)
  34. {
  35. return sign(f) * floor(abs(f) + 0.5);
  36. }
  37. void main()
  38. {
  39. mediump vec4 finalColor = vec4(0.0);
  40. // Ambient Component
  41. finalColor += u_ambientColor;
  42. highp vec3 normal = normalize(f_normal);
  43. highp vec3 lightDir = normalize(u_lightPosition - f_vertex);
  44. // Diffuse Component
  45. highp float NdotL = clamp(abs(dot(normal, lightDir)), 0.0, 1.0);
  46. finalColor += (NdotL * u_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 - f_vertex);
  51. highp float NdotR = clamp(dot(viewVector, reflectedLight), 0.0, 1.0);
  52. finalColor += pow(NdotR, u_shininess) * u_specularColor;
  53. finalColor = (-normal.y > u_overhangAngle) ? u_overhangColor : finalColor;
  54. if(u_renderError > 0.5)
  55. {
  56. vec3 grid = vec3(f_vertex.x - round(f_vertex.x), f_vertex.y - round(f_vertex.y), f_vertex.z - round(f_vertex.z));
  57. finalColor.a = dot(grid, grid) < 0.245 ? 0.667 : 1.0;
  58. }
  59. gl_FragColor = finalColor;
  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 f_vertex;
  71. out highp vec3 f_normal;
  72. void main()
  73. {
  74. vec4 world_space_vert = u_modelMatrix * a_vertex;
  75. gl_Position = u_projectionMatrix * u_viewMatrix * world_space_vert;
  76. f_vertex = world_space_vert.xyz;
  77. f_normal = (u_normalMatrix * normalize(a_normal)).xyz;
  78. }
  79. fragment41core =
  80. #version 410
  81. uniform mediump vec4 u_ambientColor;
  82. uniform mediump vec4 u_diffuseColor;
  83. uniform mediump vec4 u_specularColor;
  84. uniform highp vec3 u_lightPosition;
  85. uniform mediump float u_shininess;
  86. uniform highp vec3 u_viewPosition;
  87. uniform lowp float u_renderError;
  88. uniform lowp float u_overhangAngle;
  89. uniform lowp vec4 u_overhangColor;
  90. uniform lowp vec4 u_faceColor;
  91. uniform highp int u_faceId;
  92. in highp vec3 f_vertex;
  93. in highp vec3 f_normal;
  94. out vec4 frag_color;
  95. void main()
  96. {
  97. mediump vec4 finalColor = vec4(0.0);
  98. // Ambient Component
  99. finalColor += u_ambientColor;
  100. highp vec3 normal = normalize(f_normal);
  101. highp vec3 lightDir = normalize(u_lightPosition - f_vertex);
  102. // Diffuse Component
  103. highp float NdotL = clamp(abs(dot(normal, lightDir)), 0.0, 1.0);
  104. finalColor += (NdotL * u_diffuseColor);
  105. // Specular Component
  106. // TODO: We should not do specularity for fragments facing away from the light.
  107. highp vec3 reflectedLight = reflect(-lightDir, normal);
  108. highp vec3 viewVector = normalize(u_viewPosition - f_vertex);
  109. highp float NdotR = clamp(dot(viewVector, reflectedLight), 0.0, 1.0);
  110. finalColor += pow(NdotR, u_shininess) * u_specularColor;
  111. finalColor = (u_faceId != gl_PrimitiveID) ? ((-normal.y > u_overhangAngle) ? u_overhangColor : finalColor) : u_faceColor;
  112. frag_color = finalColor;
  113. if(u_renderError > 0.5)
  114. {
  115. vec3 grid = f_vertex - round(f_vertex);
  116. frag_color.a = dot(grid, grid) < 0.245 ? 0.667 : 1.0;
  117. }
  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. [bindings]
  128. u_modelMatrix = model_matrix
  129. u_viewMatrix = view_matrix
  130. u_projectionMatrix = projection_matrix
  131. u_normalMatrix = normal_matrix
  132. u_viewPosition = view_position
  133. u_lightPosition = light_0_position
  134. u_diffuseColor = diffuse_color
  135. u_faceId = hover_face
  136. [attributes]
  137. a_vertex = vertex
  138. a_normal = normal