transparent_object.shader 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_vertex;
  11. varying highp vec3 v_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. v_vertex = world_space_vert.xyz;
  17. v_normal = (u_normalMatrix * normalize(a_normal)).xyz;
  18. }
  19. fragment =
  20. uniform mediump vec4 u_ambientColor;
  21. uniform mediump vec4 u_diffuseColor;
  22. uniform highp vec3 u_lightPosition;
  23. uniform mediump float u_opacity;
  24. varying highp vec3 v_vertex;
  25. varying highp vec3 v_normal;
  26. void main()
  27. {
  28. mediump vec4 finalColor = vec4(0.0);
  29. /* Ambient Component */
  30. finalColor += u_ambientColor;
  31. highp vec3 normal = normalize(v_normal);
  32. highp vec3 lightDir = normalize(u_lightPosition - v_vertex);
  33. /* Diffuse Component */
  34. highp float NdotL = clamp(abs(dot(normal, lightDir)), 0.0, 1.0);
  35. finalColor += (NdotL * u_diffuseColor);
  36. //Premultiply the alpha component into the colour. Combine this with additive blending.
  37. //See https://apoorvaj.io/alpha-compositing-opengl-blending-and-premultiplied-alpha/ for a layout of theory.
  38. gl_FragColor = finalColor * u_opacity;
  39. gl_FragColor.a = u_opacity;
  40. }
  41. vertex41core =
  42. #version 410
  43. uniform highp mat4 u_modelMatrix;
  44. uniform highp mat4 u_viewMatrix;
  45. uniform highp mat4 u_projectionMatrix;
  46. uniform highp mat4 u_normalMatrix;
  47. in highp vec4 a_vertex;
  48. in highp vec4 a_normal;
  49. in highp vec2 a_uvs;
  50. out highp vec3 v_vertex;
  51. out highp vec3 v_normal;
  52. void main()
  53. {
  54. vec4 world_space_vert = u_modelMatrix * a_vertex;
  55. gl_Position = u_projectionMatrix * u_viewMatrix * world_space_vert;
  56. v_vertex = world_space_vert.xyz;
  57. v_normal = (u_normalMatrix * normalize(a_normal)).xyz;
  58. }
  59. fragment41core =
  60. #version 410
  61. uniform mediump vec4 u_ambientColor;
  62. uniform mediump vec4 u_diffuseColor;
  63. uniform highp vec3 u_lightPosition;
  64. uniform mediump float u_opacity;
  65. in highp vec3 v_vertex;
  66. in highp vec3 v_normal;
  67. out vec4 frag_color;
  68. void main()
  69. {
  70. mediump vec4 finalColor = vec4(0.0);
  71. /* Ambient Component */
  72. finalColor += u_ambientColor;
  73. highp vec3 normal = normalize(v_normal);
  74. highp vec3 lightDir = normalize(u_lightPosition - v_vertex);
  75. /* Diffuse Component */
  76. highp float NdotL = clamp(abs(dot(normal, lightDir)), 0.0, 1.0);
  77. finalColor += (NdotL * u_diffuseColor);
  78. //Premultiply the alpha component into the colour. Combine this with additive blending.
  79. //See https://apoorvaj.io/alpha-compositing-opengl-blending-and-premultiplied-alpha/ for a layout of theory.
  80. frag_color = finalColor * u_opacity;
  81. frag_color.a = u_opacity;
  82. }
  83. [defaults]
  84. u_ambientColor = [0.1, 0.1, 0.1, 1.0]
  85. u_diffuseColor = [0.4, 0.4, 0.4, 1.0]
  86. u_opacity = 0.5
  87. [bindings]
  88. u_modelMatrix = model_matrix
  89. u_viewMatrix = view_matrix
  90. u_projectionMatrix = projection_matrix
  91. u_normalMatrix = normal_matrix
  92. u_lightPosition = light_0_position
  93. u_diffuseColor = diffuse_color
  94. [attributes]
  95. a_vertex = vertex
  96. a_normal = normal