p3.shader 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. shader_type spatial;
  2. render_mode blend_mix,depth_draw_opaque, cull_back,diffuse_burley,specular_schlick_ggx,unshaded,shadows_disabled;
  3. uniform vec3 dir_angle;
  4. uniform float iTime;
  5. //https://www.shadertoy.com/view/WtBXWw
  6. //Based on Naty Hoffmann and Arcot J. Preetham. Rendering out-door light scattering in real time.
  7. //http://renderwonk.com/publications/gdm-2002/GDM_August_2002.pdf
  8. vec3 ACESFilm( vec3 x )
  9. {
  10. float tA = 2.51;
  11. float tB = 0.03;
  12. float tC = 2.43;
  13. float tD = 0.59;
  14. float tE = 0.14;
  15. return clamp((x*(tA*x+tB))/(x*(tC*x+tD)+tE),0.0,1.0);
  16. }
  17. vec3 sky( in vec3 rd) {
  18. float Gamma=2.2;
  19. float Rayleigh=1.;
  20. float Mie=1.;
  21. float RayleighAtt=1.;
  22. float MieAtt=1.2;
  23. //float g = -0.84;
  24. //float g = -0.97;
  25. float g = -0.97;
  26. vec3 ds = -normalize(dir_angle); //sun
  27. vec3 _betaR = vec3(0.0195,0.11,0.294);
  28. vec3 _betaM = vec3(0.04);
  29. vec3 c3=vec3(0.01,0.05,0.15); // bot color
  30. vec3 col = vec3(0.);
  31. vec3 ord=rd;
  32. if (rd.y < 0.) {
  33. rd.y = -rd.y;
  34. rd = normalize(rd);
  35. }
  36. float sR = RayleighAtt / rd.y ;
  37. float sM = MieAtt / rd.y ;
  38. float cosine = clamp(dot(rd,ds),0.0,1.0);
  39. vec3 extinction = exp(-(_betaR * sR + _betaM * sM));
  40. float g2 = g * g;
  41. float fcos2 = cosine * cosine;
  42. if(ord.y<0.)cosine=clamp(dot(ord,ds),0.0,1.0);
  43. float miePhase = Mie * pow(1. + g2 + 2. * g * cosine, -1.5) * (1. - g2) / (2. + g2);
  44. float rayleighPhase = Rayleigh;
  45. vec3 inScatter = (1. + fcos2) * vec3(rayleighPhase + _betaM / _betaR * miePhase);
  46. col = inScatter*(1.0-extinction); // *vec3(1.6,1.4,1.0)
  47. // sun
  48. col += 0.47*vec3(1.6,1.4,1.0)*pow( cosine, 350.0 ) * extinction;
  49. // sun haze
  50. col += 0.4*vec3(0.8,0.9,1.0)*pow( cosine, 2.0 )* extinction;
  51. col = ACESFilm(col);
  52. col = pow(col, vec3(Gamma));
  53. float hor=1.;
  54. if(ord.y<0.)
  55. hor=1.-max(-ord.y,0.);
  56. float hor_power=pow(hor,3.0); //horizont, remove if not needed
  57. return mix(c3,clamp(col,vec3(0.),vec3(1.)),hor_power);
  58. }
  59. void fragment() {
  60. vec3 rd=normalize(((CAMERA_MATRIX*vec4(normalize(-VERTEX),0.0)).xyz));
  61. rd=-rd;
  62. vec3 sky_col=sky(rd);
  63. ALBEDO=sky_col;
  64. //ALBEDO=ALBEDO*ALBEDO; //to use in Godot GLES3 add this color correction
  65. }