vulume_lights_16.shader 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. shader_type spatial;
  2. render_mode blend_add,depth_draw_never,depth_test_disable,cull_back,unshaded;
  3. uniform sampler2D Front : hint_albedo;
  4. uniform sampler2D Right : hint_albedo;
  5. uniform sampler2D Left : hint_albedo;
  6. uniform sampler2D Back : hint_albedo;
  7. uniform sampler2D Up : hint_albedo;
  8. uniform sampler2D Down : hint_albedo;
  9. uniform vec3 light_pos;
  10. uniform vec4 colorx:hint_color;
  11. // minimal example of that logic https://www.shadertoy.com/view/XsKGRz
  12. // Licence: no licence, use it as you wish.
  13. // twitter.com/AruGL
  14. vec4 cubemap(in vec3 d)
  15. {
  16. vec3 a = abs(d);
  17. bvec3 ip =greaterThan(d,vec3(0.));
  18. vec2 uvc;
  19. if (ip.x && a.x >= a.y && a.x >= a.z) {uvc.x = -d.z;uvc.y = d.y;
  20. return texture(Front,0.5 * (uvc / a.x + 1.));
  21. }else
  22. if (!ip.x && a.x >= a.y && a.x >= a.z) {uvc.x = d.z;uvc.y = d.y;
  23. return texture(Back,0.5 * (uvc / a.x + 1.));
  24. }else
  25. if (ip.y && a.y >= a.x && a.y >= a.z) {uvc.x = d.x;uvc.y = -d.z;
  26. return texture(Up,0.5 * (uvc / a.y + 1.));
  27. }else
  28. if (!ip.y && a.y >= a.x && a.y >= a.z) {uvc.x = d.x;uvc.y = d.z;
  29. return texture(Down,0.5 * (uvc / a.y + 1.));
  30. }else
  31. if (ip.z && a.z >= a.x && a.z >= a.y) {uvc.x = d.x;uvc.y = d.y;
  32. return texture(Right,0.5 * (uvc / a.z + 1.));
  33. }else
  34. if (!ip.z && a.z >= a.x && a.z >= a.y) {uvc.x = -d.x;uvc.y = d.y;
  35. return texture(Left,0.5 * (uvc / a.z + 1.));
  36. }
  37. return vec4(0.);
  38. }
  39. float rand(vec3 co){
  40. return fract(sin(dot(co*0.123,vec3(12.9898,78.233,112.166))) * 43758.5453);
  41. }
  42. void mainImage( out vec4 fragColor, vec3 rd, float gd)
  43. {
  44. vec3 ro = vec3 (0.,0.,0.);
  45. vec3 mx=vec3(1.,-1.,-1);
  46. rd*=mx;
  47. float maxdepth=max(0.01,10.*gd);
  48. float LIGHT_FALLOFF=50.;
  49. //correct value is 10, but 10 start before object
  50. float depth_mult=12.35; //set 12-14 to make it more nice(atleast not that bad) on flat objects
  51. const float steps=16.; //GLES2 does not allow not const in loop
  52. vec3 col=vec3(0.0);
  53. float dt=maxdepth/steps;
  54. float t=dt*rand(rd); //(rd+iTime)
  55. for(int i=0;i<int(steps);i++){
  56. vec3 p=ro+t*rd;
  57. vec3 L=(p-light_pos*mx);//light direction for shadow lookup
  58. float d=length(L);
  59. if(d<LIGHT_FALLOFF){//ignore if light is too far away
  60. L/=d;
  61. if(d<(clamp(cubemap(L).r,0.,1.))*depth_mult){
  62. float rangef=10.; //set 1 to have bright at light source
  63. col+=colorx.rgb/(rangef+10.0*d*d);
  64. }
  65. }
  66. t+=dt;
  67. if(t>maxdepth)break;
  68. }
  69. clamp(col,0.0,1.0);
  70. fragColor = vec4(col,1.0);
  71. }
  72. vec3 decodeSRGB(vec3 screenRGB)
  73. {
  74. vec3 a = screenRGB / 12.92;
  75. vec3 b = pow((screenRGB + 0.055) / 1.055, vec3(2.4));
  76. vec3 c = step(vec3(0.04045), screenRGB);
  77. return mix(a, b, c);
  78. }
  79. void fragment(){
  80. vec3 rd=normalize(((CAMERA_MATRIX*vec4(normalize(-VERTEX),0.0)).xyz));
  81. vec4 col=vec4(0.);
  82. float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r;
  83. depth = depth * 2.0 - 1.0;
  84. float z = -PROJECTION_MATRIX[3][2] / (depth + PROJECTION_MATRIX[2][2]);
  85. z*=0.1;
  86. depth=1.+z;
  87. depth=1.-clamp(depth,0.,1.);
  88. mainImage(col, rd, depth);
  89. ALBEDO=col.rgb;
  90. if(!OUTPUT_IS_SRGB){
  91. ALBEDO=decodeSRGB(ALBEDO);
  92. }
  93. ALBEDO*=3.5;
  94. //ALBEDO=ALBEDO*depth;
  95. }