overhang.shader 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. varying highp vec3 f_vertex;
  29. varying highp vec3 f_normal;
  30. void main()
  31. {
  32. mediump vec4 finalColor = vec4(0.0);
  33. // Ambient Component
  34. finalColor += u_ambientColor;
  35. highp vec3 normal = normalize(f_normal);
  36. highp vec3 lightDir = normalize(u_lightPosition - f_vertex);
  37. // Diffuse Component
  38. highp float NdotL = clamp(abs(dot(normal, lightDir)), 0.0, 1.0);
  39. finalColor += (NdotL * u_diffuseColor);
  40. // Specular Component
  41. // TODO: We should not do specularity for fragments facing away from the light.
  42. highp vec3 reflectedLight = reflect(-lightDir, normal);
  43. highp vec3 viewVector = normalize(u_viewPosition - f_vertex);
  44. highp float NdotR = clamp(dot(viewVector, reflectedLight), 0.0, 1.0);
  45. finalColor += pow(NdotR, u_shininess) * u_specularColor;
  46. finalColor = (-normal.y > u_overhangAngle) ? u_overhangColor : finalColor;
  47. gl_FragColor = finalColor;
  48. gl_FragColor.a = 1.0;
  49. }
  50. vertex41core =
  51. #version 410
  52. uniform highp mat4 u_modelMatrix;
  53. uniform highp mat4 u_viewMatrix;
  54. uniform highp mat4 u_projectionMatrix;
  55. uniform highp mat4 u_normalMatrix;
  56. in highp vec4 a_vertex;
  57. in highp vec4 a_normal;
  58. in highp vec2 a_uvs;
  59. out highp vec3 f_vertex;
  60. out highp vec3 f_normal;
  61. void main()
  62. {
  63. vec4 world_space_vert = u_modelMatrix * a_vertex;
  64. gl_Position = u_projectionMatrix * u_viewMatrix * world_space_vert;
  65. f_vertex = world_space_vert.xyz;
  66. f_normal = (u_normalMatrix * normalize(a_normal)).xyz;
  67. }
  68. fragment41core =
  69. #version 410
  70. uniform mediump vec4 u_ambientColor;
  71. uniform mediump vec4 u_diffuseColor;
  72. uniform mediump vec4 u_specularColor;
  73. uniform highp vec3 u_lightPosition;
  74. uniform mediump float u_shininess;
  75. uniform highp vec3 u_viewPosition;
  76. uniform lowp float u_overhangAngle;
  77. uniform lowp vec4 u_overhangColor;
  78. in highp vec3 f_vertex;
  79. in highp vec3 f_normal;
  80. out vec4 frag_color;
  81. void main()
  82. {
  83. mediump vec4 finalColor = vec4(0.0);
  84. // Ambient Component
  85. finalColor += u_ambientColor;
  86. highp vec3 normal = normalize(f_normal);
  87. highp vec3 lightDir = normalize(u_lightPosition - f_vertex);
  88. // Diffuse Component
  89. highp float NdotL = clamp(abs(dot(normal, lightDir)), 0.0, 1.0);
  90. finalColor += (NdotL * u_diffuseColor);
  91. // Specular Component
  92. // TODO: We should not do specularity for fragments facing away from the light.
  93. highp vec3 reflectedLight = reflect(-lightDir, normal);
  94. highp vec3 viewVector = normalize(u_viewPosition - f_vertex);
  95. highp float NdotR = clamp(dot(viewVector, reflectedLight), 0.0, 1.0);
  96. finalColor += pow(NdotR, u_shininess) * u_specularColor;
  97. finalColor = (-normal.y > u_overhangAngle) ? u_overhangColor : finalColor;
  98. frag_color = finalColor;
  99. frag_color.a = 1.0;
  100. }
  101. [defaults]
  102. u_ambientColor = [0.3, 0.3, 0.3, 1.0]
  103. u_diffuseColor = [1.0, 0.79, 0.14, 1.0]
  104. u_specularColor = [0.4, 0.4, 0.4, 1.0]
  105. u_overhangColor = [1.0, 0.0, 0.0, 1.0]
  106. u_shininess = 20.0
  107. [bindings]
  108. u_modelMatrix = model_matrix
  109. u_viewMatrix = view_matrix
  110. u_projectionMatrix = projection_matrix
  111. u_normalMatrix = normal_matrix
  112. u_viewPosition = view_position
  113. u_lightPosition = light_0_position
  114. u_diffuseColor = diffuse_color
  115. [attributes]
  116. a_vertex = vertex
  117. a_normal = normal