shadowMapRenderer.fp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 2020, Inria
  3. * GRAPHDECO research group, https://team.inria.fr/graphdeco
  4. * All rights reserved.
  5. *
  6. * This software is free for non-commercial, research and evaluation use
  7. * under the terms of the LICENSE.md file.
  8. *
  9. * For inquiries contact sibr@inria.fr and/or George.Drettakis@inria.fr
  10. */
  11. #version 420
  12. const float PI = 3.1415926535897932384626433832795;
  13. vec2 poissonDisk[64] = vec2[](
  14. vec2(-0.613392, 0.617481),
  15. vec2(0.170019, -0.040254),
  16. vec2(-0.299417, 0.791925),
  17. vec2(0.645680, 0.493210),
  18. vec2(-0.651784, 0.717887),
  19. vec2(0.421003, 0.027070),
  20. vec2(-0.817194, -0.271096),
  21. vec2(-0.705374, -0.668203),
  22. vec2(0.977050, -0.108615),
  23. vec2(0.063326, 0.142369),
  24. vec2(0.203528, 0.214331),
  25. vec2(-0.667531, 0.326090),
  26. vec2(-0.098422, -0.295755),
  27. vec2(-0.885922, 0.215369),
  28. vec2(0.566637, 0.605213),
  29. vec2(0.039766, -0.396100),
  30. vec2(0.751946, 0.453352),
  31. vec2(0.078707, -0.715323),
  32. vec2(-0.075838, -0.529344),
  33. vec2(0.724479, -0.580798),
  34. vec2(0.222999, -0.215125),
  35. vec2(-0.467574, -0.405438),
  36. vec2(-0.248268, -0.814753),
  37. vec2(0.354411, -0.887570),
  38. vec2(0.175817, 0.382366),
  39. vec2(0.487472, -0.063082),
  40. vec2(-0.084078, 0.898312),
  41. vec2(0.488876, -0.783441),
  42. vec2(0.470016, 0.217933),
  43. vec2(-0.696890, -0.549791),
  44. vec2(-0.149693, 0.605762),
  45. vec2(0.034211, 0.979980),
  46. vec2(0.503098, -0.308878),
  47. vec2(-0.016205, -0.872921),
  48. vec2(0.385784, -0.393902),
  49. vec2(-0.146886, -0.859249),
  50. vec2(0.643361, 0.164098),
  51. vec2(0.634388, -0.049471),
  52. vec2(-0.688894, 0.007843),
  53. vec2(0.464034, -0.188818),
  54. vec2(-0.440840, 0.137486),
  55. vec2(0.364483, 0.511704),
  56. vec2(0.034028, 0.325968),
  57. vec2(0.099094, -0.308023),
  58. vec2(0.693960, -0.366253),
  59. vec2(0.678884, -0.204688),
  60. vec2(0.001801, 0.780328),
  61. vec2(0.145177, -0.898984),
  62. vec2(0.062655, -0.611866),
  63. vec2(0.315226, -0.604297),
  64. vec2(-0.780145, 0.486251),
  65. vec2(-0.371868, 0.882138),
  66. vec2(0.200476, 0.494430),
  67. vec2(-0.494552, -0.711051),
  68. vec2(0.612476, 0.705252),
  69. vec2(-0.578845, -0.768792),
  70. vec2(-0.772454, -0.090976),
  71. vec2(0.504440, 0.372295),
  72. vec2(0.155736, 0.065157),
  73. vec2(0.391522, 0.849605),
  74. vec2(-0.620106, -0.328104),
  75. vec2(0.789239, -0.419965),
  76. vec2(-0.545396, 0.538133),
  77. vec2(-0.178564, -0.596057)
  78. );
  79. uniform vec3 lightDir;
  80. uniform float sun_app_radius;
  81. uniform mat4 depthMapMVPinv;
  82. uniform float depthMapRadius;
  83. uniform float biasControl;
  84. layout(binding=0) uniform sampler2D depthMap;
  85. in vec4 depthMapProj;
  86. in vec3 VtoF_normal;
  87. in vec3 VtoF_pos;
  88. out float out_val;
  89. void main(void) {
  90. vec2 texc = (depthMapProj.xy + 1.0) / 2.0;
  91. float depthImSpace = depthMapProj.z;
  92. float cosTheta = clamp(dot(VtoF_normal,lightDir),0.0,1.0);
  93. float bias = biasControl*tan(acos(cosTheta));
  94. bias = clamp(bias, 0.0, 5*biasControl);
  95. int textureWidth = textureSize(depthMap,0).x;
  96. // Compute the size of the shadow transition
  97. // The 2 account for the fact that we are treating the radius.
  98. int r_blocker = int(ceil(0.5*tan(sun_app_radius*PI/180.0)*textureWidth));
  99. float mean_blocker_val = 0.0;
  100. float blocker_num_val = 0.0;
  101. float sum_weight = 0.0;
  102. for(int i = 0; i<7 ; i++){
  103. float theta_rot=i;
  104. mat2 rotation_poisson =mat2(cos(theta_rot), sin(theta_rot), -sin(theta_rot), cos(theta_rot));
  105. for(int k = 0; k <64 ; k++){
  106. float pixDist = length(r_blocker*poissonDisk[k]/textureWidth);
  107. if(pixDist<=r_blocker){
  108. float depthMapVal = texture(depthMap, texc + r_blocker*rotation_poisson*poissonDisk[k]/textureWidth).x;
  109. float bias_with_dist = bias*(pixDist+1.0);
  110. if( depthImSpace-bias_with_dist > depthMapVal ){
  111. vec3 blocker_pos=(depthMapMVPinv*vec4(depthMapProj.x,depthMapProj.y,depthMapVal,1.0)).xyz;
  112. float weight = 0.01+exp(-pow(pixDist/(0.02*r_blocker),2.0));
  113. mean_blocker_val += weight*length(VtoF_pos-blocker_pos);
  114. sum_weight += weight;
  115. blocker_num_val += 1.0;
  116. }
  117. }
  118. }
  119. }
  120. if( dot(VtoF_normal,lightDir) <= -0.017 ){
  121. out_val = 0.0;
  122. }
  123. else if(blocker_num_val < 1.0){
  124. out_val = 1.0;
  125. }
  126. else{
  127. float d_blocker_receiver = mean_blocker_val/sum_weight;
  128. float wShadow=2*d_blocker_receiver*tan(sun_app_radius*PI/180.0);
  129. //float angleCompensation= dot(normalize(VtoF_normal),normalize(lightDir));
  130. //float wShadowAngle = wShadow/angleCompensation;
  131. float ratioShadowTexture = wShadow/(2.0*depthMapRadius);
  132. float pixelShadowTexture = ratioShadowTexture*textureWidth;
  133. const int r = int(ceil(pixelShadowTexture));
  134. float num_val = 0.0;
  135. float sum_val = 0.0;
  136. for(int i = 0; i<14 ; i++){
  137. float theta_rot=float(i);
  138. mat2 rotation_poisson =mat2(cos(theta_rot), sin(theta_rot), -sin(theta_rot), cos(theta_rot));
  139. for(int k = 0; k <64 ; k++){
  140. float depthMapVal = texture(depthMap, texc + r*rotation_poisson*poissonDisk[k]/textureWidth).x;
  141. float pixDist = length(r*poissonDisk[k]/textureWidth);
  142. float bias_with_dist = bias*(pixDist+1.0);
  143. if( depthImSpace-bias_with_dist > depthMapVal ){
  144. sum_val += 0.0;
  145. }
  146. else{
  147. sum_val += 1.0;
  148. }
  149. num_val +=1.0;
  150. }
  151. }
  152. out_val = sum_val/num_val;
  153. }
  154. }