gmain.fs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #START VS
  2. #version 330 core
  3. layout (location = 0) in vec2 in_position;
  4. layout (location = 1) in vec2 in_uv;
  5. out vec2 uv;
  6. void main()
  7. {
  8. gl_Position = vec4(in_position.x, in_position.y, 0.0f, 1.0f);
  9. uv = in_uv;
  10. }
  11. #END VS
  12. #START FS
  13. #version 330 core
  14. in vec2 uv;
  15. out vec4 color;
  16. uniform sampler2D u_position;
  17. uniform sampler2D u_norm;
  18. uniform sampler2D u_colorspec;
  19. uniform sampler2D u_ssao;
  20. uniform mat4 u_projection;
  21. uniform mat4 u_view;
  22. uniform mat4 u_inverse_view;
  23. uniform bool u_ambient_pass;
  24. /* spot lights */
  25. const int MAX_SL = 32;
  26. struct spot_light {
  27. vec3 position;
  28. vec3 direction;
  29. vec3 color;
  30. float inner;
  31. float outer;
  32. bool is_shadow;
  33. float far;
  34. };
  35. // for dynamic spotlights
  36. uniform spot_light u_spot_light;
  37. uniform sampler2D u_spot_depth;
  38. // for static spotlights
  39. uniform spot_light u_spot_lights[MAX_SL];
  40. uniform int u_spot_count;
  41. uniform bool u_spot_active;
  42. /* ------------ */
  43. /* dir light */
  44. struct dir_light {
  45. vec3 position;
  46. vec3 target;
  47. vec3 color;
  48. float far;
  49. };
  50. uniform dir_light u_dir_light;
  51. uniform sampler2D u_dir_depth;
  52. uniform mat4 u_dir_transform;
  53. uniform bool u_dir_active;
  54. /* ------------ */
  55. /* point light */
  56. const int MAX_PL = 64;
  57. struct point_light {
  58. vec3 position;
  59. vec3 color;
  60. bool is_shadow;
  61. float far;
  62. };
  63. // for dynamic lights
  64. uniform point_light u_point_light;
  65. uniform samplerCube u_point_depth;
  66. // for static ones done in a single render pass
  67. uniform point_light u_point_lights[MAX_PL];
  68. uniform int u_point_count;
  69. uniform bool u_point_active;
  70. /* ------------ */
  71. vec3 pcf_offset[20] = vec3[]
  72. (
  73. vec3( 1, 1, 1), vec3( 1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1),
  74. vec3( 1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
  75. vec3( 1, 1, 0), vec3( 1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
  76. vec3( 1, 0, 1), vec3(-1, 0, 1), vec3( 1, 0, -1), vec3(-1, 0, -1),
  77. vec3( 0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1)
  78. );
  79. vec3 calc_point_light(point_light light)
  80. {
  81. point_light l = light;
  82. l.position = vec3(u_view * vec4(l.position, 1.0));
  83. // point light
  84. vec3 fragpos = texture(u_position, uv).rgb;
  85. vec3 normals = texture(u_norm, uv).rgb * 2.0 - 1.0;
  86. vec3 diff = texture(u_colorspec, uv).rgb;
  87. float spec = texture(u_colorspec, uv).a*1.0f;
  88. vec3 view_dir = normalize(-fragpos);
  89. vec3 light_dir = l.position - fragpos;
  90. float dist = length(light_dir);
  91. light_dir = normalize(light_dir);
  92. // diffuse
  93. vec3 diffuse = max(dot(light_dir, normals), 0.0) * diff * l.color;
  94. // specular
  95. vec3 halfwayd = normalize(light_dir + view_dir);
  96. float specs = pow(max(dot(normals, halfwayd), 0.0), 64.0f);
  97. vec3 specular = l.color * specs * spec;
  98. // attenuation
  99. // float attenuation = 1.0f / (1.0f + 0.1f * (dist * dist));
  100. float attenuation = 1.0f / (1.0f + 0.14f * dist + 0.07f * (dist * dist));
  101. // float attenuation = 1.0f / (1.0f + dist);
  102. // float attenuation = max(0.0, 4.0 - pow(dist, 1.0/2.0));
  103. diffuse *= attenuation;
  104. specular *= attenuation;
  105. // shadows
  106. float costheta = clamp(dot(normals, light_dir), 0.0, 1.0);
  107. float bias = 0.2*tan(acos(costheta));
  108. bias = clamp(bias, 0.1, 0.2);
  109. float shadow = 0.0f;
  110. if (l.is_shadow) {
  111. vec3 frag_to_light = mat3(u_inverse_view) * (fragpos - l.position);
  112. float current_depth = length(frag_to_light);
  113. float view_dist = length(-fragpos);
  114. // PCF smoothing
  115. float radius = (1.0 + (view_dist / l.far)) / l.far;
  116. float offset = 0.1;
  117. int samples = 20;
  118. float closest_depth = 0.0f;
  119. for (int i=0; i<samples; ++i) {
  120. closest_depth = texture(u_point_depth, frag_to_light + pcf_offset[i] * radius).r;
  121. closest_depth *= l.far;
  122. if (current_depth - bias > closest_depth)
  123. shadow += 1.0;
  124. }
  125. shadow /= float(samples);
  126. }
  127. return vec3((1.0 - shadow) * (diffuse + specular));
  128. }
  129. vec3 aces_tonemap(vec3 x) {
  130. float a = 2.51;
  131. float b = 0.03;
  132. float c = 2.43;
  133. float d = 0.59;
  134. float e = 0.14;
  135. return clamp((x*(a*x+b))/(x*(c*x+d)+e), 0.0, 1.0);
  136. }
  137. void main()
  138. {
  139. vec3 diffuse = vec3(0.0f);
  140. vec3 reflection = vec3(0.0f);
  141. float ao = texture(u_ssao, uv).r;
  142. if (u_ambient_pass) {
  143. diffuse += texture(u_colorspec, uv).rgb * 0.06;
  144. // vec3 normals = normalize(mat3(u_inverse_view) * normalize(texture(u_norm, uv).rgb));
  145. // vec3 fragpos = mat3(u_inverse_view) * texture(u_position, uv).rgb;
  146. // float spec = texture(u_colorspec, uv).a;
  147. // vec3 eye = normalize(u_eye_dir);
  148. // eye = normalize(fragpos);
  149. // eye.y = -eye.y;
  150. // vec3 reflected = normalize(reflect(eye, normals));
  151. // reflection = texture(u_reflection, reflected).rgb * 5.5;
  152. // diffuse *= reflection * spec;
  153. } else {
  154. // shadow casters
  155. if (u_point_active && u_point_count <= 0)
  156. diffuse += calc_point_light(u_point_light);
  157. // if (u_spot_active && u_spot_count <= 0)
  158. // diffuse += calc_spot_light(u_spot_light);
  159. }
  160. // non shadow casters
  161. if (u_point_count > 0)
  162. for (int i=0; i<u_point_count; i++)
  163. diffuse += calc_point_light(u_point_lights[i]);
  164. vec3 tex_color = vec3(1.0) - exp(-diffuse * 1.0);
  165. color = vec4(aces_tonemap(tex_color), 2.5);
  166. color *= ao;
  167. color *= min(100.0 / length(texture(u_position, uv).rgb), 1.0);
  168. // color = vec4(diffuse * ao, 1.0f);
  169. }
  170. #END FS