Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

118 rader
3.0 KiB

  1. [vert.glsl]
  2. #version 130
  3. in vec3 in_position;
  4. in vec3 in_Normal;
  5. in vec4 in_color;
  6. uniform mat4 u_modelview;
  7. uniform mat4 u_view;
  8. uniform mat4 u_projection;
  9. uniform mat3 u_normalmat;
  10. uniform float u_damage;
  11. out vec4 pass_vertex; /* View space */
  12. out vec3 pass_tnormal;
  13. out vec4 pass_color;
  14. void main(void)
  15. {
  16. vec4 vertex = vec4(in_position + (in_Normal * u_damage), 1.0);
  17. float dam_perc = u_damage / (0.1 * 40);
  18. vec3 vGravity = vec3(0.0, -0.981, 0.0) * 2.0;
  19. float k = pow(dam_perc, 3);
  20. vertex = u_modelview * vertex + u_view * vec4(vGravity * k, 1.0);
  21. vec3 tnorm = normalize(u_normalmat * in_Normal);
  22. pass_vertex = vertex;
  23. pass_tnormal = tnorm;
  24. pass_color = in_color;
  25. gl_Position = u_projection * vertex;
  26. }
  27. [frag.glsl]
  28. #version 130
  29. #if defined GL_ES
  30. precision highp float;
  31. #endif
  32. in vec4 pass_vertex; /* View space */
  33. in vec3 pass_tnormal;
  34. in vec4 pass_color;
  35. uniform mat4 u_view;
  36. uniform mat4 u_inv_view;
  37. uniform mat4 u_inv_modelview;
  38. uniform float u_damage;
  39. //Light list
  40. uniform vec4 u_lights[8 * 2];
  41. void main(void)
  42. {
  43. /* Material properties */
  44. vec3 specular_reflect = vec3(0.8, 0.75, 0.4);
  45. float specular_power = 60.0;
  46. /* World properties */
  47. vec3 ambient = vec3(0.7, 0.7, 0.7);
  48. vec3 specular = vec3(0.0, 0.0, 0.0);
  49. vec3 diffuse = vec3(0.0, 0.0, 0.0);
  50. /* Light precalculations */
  51. vec3 v = normalize(-pass_vertex.xyz);
  52. /* Apply lighting */
  53. for (int i = 0; i < 8; i++)
  54. {
  55. vec4 pos = u_lights[i * 2];
  56. vec4 color = u_lights[i * 2 + 1];
  57. vec3 s, r;
  58. if (pos.w > 0.0)
  59. {
  60. /* Point light -- no attenuation yet */
  61. s = normalize((u_view * pos).xyz - pass_vertex.xyz);
  62. r = reflect(-s, pass_tnormal);
  63. }
  64. else
  65. {
  66. /* Directional light */
  67. s = normalize(-pos.xyz);
  68. r = reflect(s, pass_tnormal);
  69. }
  70. float sdotn = max(dot(s, pass_tnormal), 0.0);
  71. diffuse += color.xyz * sdotn;
  72. if (sdotn > 0.0)
  73. specular += color.xyz * specular_reflect
  74. * pow(max(dot(r, v), 0.0), specular_power);
  75. }
  76. vec3 light = ambient + diffuse + specular;
  77. vec4 world_vertex = u_inv_modelview * pass_vertex;
  78. vec4 world_normal = u_inv_modelview * vec4(pass_tnormal, 1.0);
  79. float dam_perc = u_damage / (0.1 * 40);
  80. float PI = 3.14159265358979323846264;
  81. dam_perc = (sin(PI * (dam_perc - 0.5)) * 0.5 + 0.5);
  82. dam_perc *= dam_perc;
  83. dam_perc = 1.0 - dam_perc;
  84. float mod = 2.0;
  85. float transp = dam_perc;
  86. /*
  87. abs(sin(world_vertex.x * mod) * dam_perc * 0.5 + 0.5) *
  88. abs(sin(world_vertex.y * mod) * dam_perc * 0.5 + 0.5) *
  89. abs(sin(world_vertex.z * mod) * dam_perc * 0.5 + 0.5)
  90. */
  91. //vec4 real_color = mix(pass_color, vec4(1.2, 1.2, 1.2, 1.0), u_damage);
  92. // - u_damage
  93. gl_FragColor = pass_color * vec4(light, (u_damage == 0)?(1.0):(transp));
  94. }