2
0

compose.fs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #version 120
  2. #define MAX_LIGHTS 32
  3. #define MAT_DISCARD 1.0
  4. #define MAT_REFLECT_MINOR 2.0
  5. #define MAT_REFLECT_MAJOR 3.0
  6. #define MAT_REFLECT_NONE 4.0
  7. #define MAT_FLAT 5.0
  8. #define MAT_LEAF 6.0
  9. #define MAT_SKIN 7.0
  10. #define MAT_CLOTH 8.0
  11. #define MAT_NONE -1.0
  12. uniform sampler2D diffuse_texture;
  13. uniform sampler2D positions_texture;
  14. uniform sampler2D normals_texture;
  15. uniform sampler2D random_texture;
  16. uniform sampler2D depth_texture;
  17. uniform sampler2D shadows_texture0;
  18. uniform sampler2D shadows_texture1;
  19. uniform sampler2D shadows_texture2;
  20. uniform sampler2D ssao_texture;
  21. uniform samplerCube env_texture;
  22. uniform sampler2D skin_lookup;
  23. uniform mat4 inv_view;
  24. uniform mat4 inv_proj;
  25. uniform float clip_near;
  26. uniform float clip_far;
  27. uniform vec3 camera_position;
  28. uniform int lights_num;
  29. uniform float light_power[MAX_LIGHTS];
  30. uniform float light_falloff[MAX_LIGHTS];
  31. uniform vec3 light_position[MAX_LIGHTS];
  32. uniform vec3 light_target[MAX_LIGHTS];
  33. uniform vec3 light_diffuse[MAX_LIGHTS];
  34. uniform vec3 light_ambient[MAX_LIGHTS];
  35. uniform vec3 light_specular[MAX_LIGHTS];
  36. uniform float light_start[3];
  37. uniform mat4 light_view[3];
  38. uniform mat4 light_proj[3];
  39. varying vec2 fTexcoord;
  40. #define SHADOW_BIAS 0.001
  41. #define SHADOW_SAMPLE_SPHERE vec3[32]( \
  42. vec3(-0.00, 0.02, -0.03), vec3( 0.35, -0.04, 0.31), vec3( 0.66, -0.32, 0.53), \
  43. vec3(-0.04, -0.04, 0.01), vec3( 0.24, -0.22, 0.89), vec3(-0.09, 0.10, -0.54), \
  44. vec3( 0.24, 0.04, 0.01), vec3( 0.37, 0.88, 0.05), vec3( 0.02, 0.11, -0.19), \
  45. vec3(-0.04, 0.83, -0.01), vec3( 0.33, 0.11, -0.44), vec3( 0.21, -0.17, 0.28), \
  46. vec3( 0.48, -0.30, 0.34), vec3( 0.39, -0.72, 0.43), vec3( 0.19, 0.20, 0.03), \
  47. vec3( 0.35, -0.04, -0.01), vec3(-0.00, -0.02, -0.25), vec3(-0.07, 0.12, -0.04), \
  48. vec3( 0.00, 0.01, -0.40), vec3(-0.27, 0.41, -0.44), vec3( 0.13, 0.26, -0.14), \
  49. vec3( 0.15, 0.19, -0.26), vec3(-0.32, 0.29, 0.56), vec3(-0.00, -0.00, 0.13), \
  50. vec3(-0.36, -0.18, 0.07), vec3( 0.70, 0.21, 0.39), vec3(-0.36, 0.17, 0.91), \
  51. vec3(-0.11, -0.12, 0.26), vec3(-0.59, -0.67, 0.14), vec3(-0.24, -0.75, 0.27), \
  52. vec3( 0.18, 0.04, -0.58), vec3(-0.16, 0.11, -0.26))
  53. float shadow_amount(vec3 position, mat4 light_view, mat4 light_proj, sampler2D light_depth, float kernel, vec2 seed) {
  54. vec4 light_pos = light_proj * light_view * vec4(position, 1.0);
  55. light_pos = light_pos / light_pos.w;
  56. float pixel_depth = light_pos.z / 2 + 0.5;
  57. vec2 pixel_coords = vec2(light_pos.x, light_pos.y) / 2.0 + 0.5;
  58. float shade = 1.0;
  59. vec2 offset0 = reflect(SHADOW_SAMPLE_SPHERE[0].xy, seed);
  60. vec2 offset1 = reflect(SHADOW_SAMPLE_SPHERE[1].xy, seed);
  61. vec2 offset2 = reflect(SHADOW_SAMPLE_SPHERE[2].xy, seed);
  62. vec2 offset3 = reflect(SHADOW_SAMPLE_SPHERE[3].xy, seed);
  63. float shadow_depth0 = texture2D( light_depth, pixel_coords + offset0 * kernel ).r;
  64. float shadow_depth1 = texture2D( light_depth, pixel_coords + offset1 * kernel ).r;
  65. float shadow_depth2 = texture2D( light_depth, pixel_coords + offset2 * kernel ).r;
  66. float shadow_depth3 = texture2D( light_depth, pixel_coords + offset3 * kernel ).r;
  67. shade = shade - sign(pixel_depth - shadow_depth0 - SHADOW_BIAS) * (float(1) / float(4));
  68. shade = shade - sign(pixel_depth - shadow_depth1 - SHADOW_BIAS) * (float(1) / float(4));
  69. shade = shade - sign(pixel_depth - shadow_depth2 - SHADOW_BIAS) * (float(1) / float(4));
  70. shade = shade - sign(pixel_depth - shadow_depth3 - SHADOW_BIAS) * (float(1) / float(4));
  71. return shade;
  72. }
  73. float when_eq(float x, float y) {
  74. return 1.0 - abs(sign(x - y));
  75. }
  76. vec4 when_eq(vec4 x, vec4 y) {
  77. return 1.0 - abs(sign(x - y));
  78. }
  79. float when_neq(float x, float y) {
  80. return abs(sign(x - y));
  81. }
  82. vec3 from_gamma(vec3 color) {
  83. return vec3(
  84. pow(color.r, 1.0 / 2.2),
  85. pow(color.g, 1.0 / 2.2),
  86. pow(color.b, 1.0 / 2.2));
  87. }
  88. float perspective_depth(float depth, float near, float far) {
  89. return (((2.0 * near) / depth) - far - near) / (near - far);
  90. }
  91. void main() {
  92. float depth = texture2D(depth_texture, fTexcoord).r;
  93. vec3 position_clip = vec3(fTexcoord.xy, perspective_depth(depth, clip_near, clip_far)) * 2.0 - 1.0;
  94. vec4 position = inv_view * inv_proj * vec4(position_clip, 1);
  95. position = position / position.w;
  96. vec4 diffuse_a = texture2D(diffuse_texture, fTexcoord );
  97. vec3 diffuse_amount = diffuse_a.rgb;
  98. float spec_amount = diffuse_a.a;
  99. vec4 normals = texture2D(normals_texture, fTexcoord);
  100. vec3 normal = normalize(normals.rgb);
  101. float glossiness = mod(normals.a, 1.0) * 1000;
  102. int material = int(normals.a);
  103. float curvature = glossiness;
  104. if (material == MAT_DISCARD) { discard; }
  105. if (material == MAT_FLAT) { gl_FragColor.rgb = diffuse_amount; return; }
  106. float noise_tile = 1.0;
  107. vec3 random =
  108. abs(normal.x) * texture2D(random_texture, position.yz * noise_tile).rgb +
  109. abs(normal.y) * texture2D(random_texture, position.xz * noise_tile).rgb +
  110. abs(normal.z) * texture2D(random_texture, position.xy * noise_tile).rgb;
  111. random = normalize(random * 2.0 - 1.0);
  112. float shadow0 = shadow_amount(position.xyz, light_view[0], light_proj[0], shadows_texture0, 0.00075, random.xy);
  113. float shadow1 = shadow_amount(position.xyz, light_view[1], light_proj[1], shadows_texture1, 0.00075, random.xy);
  114. float shadow2 = shadow_amount(position.xyz, light_view[2], light_proj[2], shadows_texture2, 0.00100, random.xy);
  115. float shadow = depth > light_start[2] ? shadow2 : (depth > light_start[1] ? shadow1 : shadow0);
  116. vec3 eye_dir = normalize(camera_position - position.xyz);
  117. float n_dot_c = dot(normal, eye_dir);
  118. vec3 ssao = texture2D(ssao_texture, fTexcoord).rgb;
  119. vec3 env = textureCube(env_texture, reflect(-eye_dir, normal)).rgb;
  120. vec4 materialsv = vec4(material, material, material, material);
  121. vec4 materials0 = vec4(MAT_REFLECT_MINOR, MAT_REFLECT_MAJOR, MAT_REFLECT_NONE, MAT_FLAT);
  122. vec4 materials1 = vec4(MAT_LEAF, MAT_SKIN, MAT_CLOTH, MAT_NONE);
  123. float reflect_glossiness = 1.0;
  124. float reflect_amount =
  125. dot(when_eq(materials0, materialsv), vec4(0.5, 1.5, 0.0, 0.0)) +
  126. dot(when_eq(materials1, materialsv), vec4(0.5, 0.5, 1.25, 0.0));
  127. float inner_rim_amount =
  128. dot(when_eq(materials0, materialsv), vec4(0.1, 0.05, 0.0, 0.0)) +
  129. dot(when_eq(materials1, materialsv), vec4(0.1, 0.1, 0.25, 0.0));
  130. float outer_rim_amount =
  131. dot(when_eq(materials0, materialsv), vec4(0.1, 0.25, 0.0, 0.0)) +
  132. dot(when_eq(materials1, materialsv), vec4(0.5, 0.1, 0.5, 0.0));
  133. float inner_rim_exp =
  134. dot(when_eq(materials0, materialsv), vec4(20.0, 30.0, 1.0, 1.0)) +
  135. dot(when_eq(materials1, materialsv), vec4(40.0, 5.0, 30.0, 1.0));
  136. float outer_rim_exp =
  137. dot(when_eq(materials0, materialsv), vec4(10.0, 5.0, 1.0, 1.0)) +
  138. dot(when_eq(materials1, materialsv), vec4(20.0, 1.0, 1.0, 1.0));
  139. float is_skin = when_eq(material, MAT_SKIN);
  140. glossiness = is_skin * 15.0 + (1-is_skin) * glossiness;
  141. vec3 diffuse = vec3(0.0, 0.0, 0.0);
  142. vec3 ambient = vec3(0.0, 0.0, 0.0);
  143. vec3 specular = vec3(0.0, 0.0, 0.0);
  144. vec3 reflection = vec3(0.0, 0.0, 0.0);
  145. vec3 inner_rim = vec3(0.0, 0.0, 0.0);
  146. vec3 outer_rim = vec3(0.0, 0.0, 0.0);
  147. for(int i = 0; i < lights_num; i++) {
  148. vec3 light_vector = light_position[i] - position.xyz;
  149. float power = light_power[i] / pow(length(light_vector), light_falloff[i]);
  150. vec3 light_dir = normalize(light_position[i] - light_target[i]);
  151. vec3 light_half = normalize(light_dir + eye_dir);
  152. float n_dot_l = dot(normal, light_dir);
  153. float n_dot_h = dot(normal, light_half);
  154. float n_dot_v = dot(normal, eye_dir);
  155. float v_dot_h = dot(eye_dir, light_half);
  156. vec3 light_diff = max(vec3(n_dot_l, n_dot_l, n_dot_l), 0.0);
  157. float light_spec = ((glossiness+2) / (8 * 3.141)) * max(pow(n_dot_h, glossiness), 0.0);
  158. float light_refl = ((reflect_glossiness+2) / (2 * 3.141)) * max(pow((1.0-n_dot_c), reflect_glossiness), 0.0);
  159. float light_inrim = pow(clamp(n_dot_v, 0, 1), inner_rim_exp);
  160. float light_outrim = pow(clamp(1-n_dot_v, 0, 1), outer_rim_exp);
  161. if (material == MAT_SKIN) {
  162. light_diff = texture2D( skin_lookup, clamp(vec2(n_dot_l * 0.5 + 0.5, curvature), 0.1, 0.9)).rgb;
  163. }
  164. light_diff *= clamp(shadow + i, 0, 1);
  165. light_spec *= clamp(shadow + i, 0, 1);
  166. light_inrim *= clamp(shadow + i, 0, 1);
  167. light_outrim *= clamp(shadow + i, 0, 1);
  168. ambient += power * light_ambient[i];
  169. diffuse += power * light_diffuse[i] * light_diff;
  170. specular += power * light_specular[i] * light_spec;
  171. reflection += power * light_ambient[i] * light_refl;
  172. inner_rim += power * light_specular[i] * light_inrim;
  173. outer_rim += power * light_specular[i] * light_outrim;
  174. }
  175. ambient = ambient * diffuse_amount * ssao;
  176. diffuse = diffuse * diffuse_amount;
  177. specular = specular * spec_amount;
  178. reflection = reflection * spec_amount * reflect_amount * env;
  179. inner_rim = inner_rim * spec_amount * inner_rim_amount;
  180. outer_rim = outer_rim * spec_amount * outer_rim_amount;
  181. gl_FragColor.rgb = ambient + diffuse + specular + reflection + inner_rim + outer_rim;
  182. }