shadow.glslv 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef SHADOW_GLSLV
  2. #define SHADOW_GLSLV
  3. // #import u_normal_offset u_v_light_ts u_v_light_r u_v_light_tsr\
  4. // u_p_light_matrix0 u_p_light_matrix1 u_p_light_matrix2 u_p_light_matrix3
  5. // #import v_shadow_coord0 v_shadow_coord1 v_shadow_coord2 v_shadow_coord3
  6. /*==============================================================================
  7. VARS
  8. ==============================================================================*/
  9. #var MAC_OS_SHADOW_HACK 0
  10. #var SHADOW_TEX_RES 2048.0
  11. #var SHADOW_USAGE NO_SHADOWS
  12. #var NUM_CAST_LAMPS 0
  13. #var CSM_SECTION1 0
  14. #var CSM_SECTION2 0
  15. #var CSM_SECTION3 0
  16. /*============================================================================*/
  17. #include <std.glsl>
  18. #if SHADOW_USAGE == SHADOW_MASK_GENERATION || SHADOW_USAGE == SHADOW_MAPPING_BLEND
  19. #include <math.glslv>
  20. vec4 get_shadow_coords_shifted(mat4 light_proj_matrix, vec4 pos_light_space, mat4 v_light_matrix) {
  21. // NOTE: shift coords to remove shadow map panning
  22. //NOTE: v_light_matrix[3] is world space point (0.0, 0.0, 0.0, 1.0)
  23. // translated into light view space
  24. vec2 shift = (light_proj_matrix * v_light_matrix[3]).xy;
  25. float half_tex_res = SHADOW_TEX_RES / 2.0;
  26. shift = floor(shift * half_tex_res + 0.5) / half_tex_res - shift;
  27. vec4 shadow_coord = light_proj_matrix * pos_light_space;
  28. shadow_coord.xy += shift;
  29. // moving from unit cube [-1,1] to [0,1]
  30. // NOTE scaling by 0.5 and adding 0.5 produces the same result
  31. // as multiplying by the bias matrix
  32. shadow_coord.xyz = 0.5 * (shadow_coord.xyz + shadow_coord.w);
  33. return shadow_coord;
  34. }
  35. void get_shadow_coords(vec3 pos, vec3 nor) {
  36. # if MAC_OS_SHADOW_HACK
  37. mat4 v_light_matrix = tsr_to_mat4(u_v_light_tsr[0]);
  38. # else
  39. mat4 v_light_matrix = tsr_to_mat4(mat3(u_v_light_ts[0], u_v_light_r[0], 0.0));
  40. # endif
  41. // apply normal offset to prevent shadow acne
  42. vec4 pos_light = v_light_matrix * vec4(pos + u_normal_offset * nor,
  43. 1.0);
  44. v_shadow_coord0 = get_shadow_coords_shifted(u_p_light_matrix0, pos_light, v_light_matrix);
  45. // NUM_CAST_LAMPS and CSM_SECTION are mutually exclusive directives
  46. # if NUM_CAST_LAMPS > 1
  47. # if MAC_OS_SHADOW_HACK
  48. v_light_matrix = tsr_to_mat4(u_v_light_tsr[1]);
  49. # else
  50. v_light_matrix = tsr_to_mat4(mat3(u_v_light_ts[1], u_v_light_r[1], 0.0));
  51. # endif
  52. pos_light = v_light_matrix * vec4(pos + u_normal_offset * nor, 1.0);
  53. v_shadow_coord1 = get_shadow_coords_shifted(u_p_light_matrix1, pos_light, v_light_matrix);
  54. # endif
  55. # if NUM_CAST_LAMPS > 2
  56. # if MAC_OS_SHADOW_HACK
  57. v_light_matrix = tsr_to_mat4(u_v_light_tsr[2]);
  58. # else
  59. v_light_matrix = tsr_to_mat4(mat3(u_v_light_ts[2], u_v_light_r[2], 0.0));
  60. # endif
  61. pos_light = v_light_matrix * vec4(pos + u_normal_offset * nor, 1.0);
  62. v_shadow_coord2 = get_shadow_coords_shifted(u_p_light_matrix2, pos_light, v_light_matrix);
  63. # endif
  64. # if NUM_CAST_LAMPS > 3
  65. # if MAC_OS_SHADOW_HACK
  66. v_light_matrix = tsr_to_mat4(u_v_light_tsr[3]);
  67. # else
  68. v_light_matrix = tsr_to_mat4(mat3(u_v_light_ts[3], u_v_light_r[3], 0.0));
  69. # endif
  70. pos_light = v_light_matrix * vec4(pos + u_normal_offset * nor, 1.0);
  71. v_shadow_coord3 = get_shadow_coords_shifted(u_p_light_matrix3, pos_light, v_light_matrix);
  72. # endif
  73. # if CSM_SECTION1
  74. v_shadow_coord1 = get_shadow_coords_shifted(u_p_light_matrix1, pos_light, v_light_matrix);
  75. # endif
  76. # if CSM_SECTION2
  77. v_shadow_coord2 = get_shadow_coords_shifted(u_p_light_matrix2, pos_light, v_light_matrix);
  78. # endif
  79. # if CSM_SECTION3
  80. v_shadow_coord3 = get_shadow_coords_shifted(u_p_light_matrix3, pos_light, v_light_matrix);
  81. # endif
  82. }
  83. #endif
  84. #endif