overhang.shader 4.2 KB

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