Sky.shader 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. shader_type canvas_item;
  2. // USING https://www.shadertoy.com/view/XtBXDw (base on it)
  3. uniform float iTime;
  4. uniform int iFrame;
  5. uniform sampler2D iChannel0;
  6. uniform float COVERAGE :hint_range(0,1); //0.5
  7. uniform float THICKNESS :hint_range(0,100); //25.
  8. uniform float ABSORPTION :hint_range(0,10); //1.030725
  9. uniform int STEPS :hint_range(0,100); //25
  10. float noise( in vec3 x )
  11. {
  12. x*=0.01;
  13. float z = x.z*256.0;
  14. vec2 offz = vec2(0.317,0.123);
  15. vec2 uv1 = x.xy + offz*floor(z);
  16. vec2 uv2 = uv1 + offz;
  17. return mix(textureLod( iChannel0, uv1 ,0.0).x,textureLod( iChannel0, uv2 ,0.0).x,fract(z));
  18. }
  19. float fbm(vec3 pos,float lacunarity){
  20. vec3 p = pos;
  21. float
  22. t = 0.51749673 * noise(p); p *= lacunarity;
  23. t += 0.25584929 * noise(p); p *= lacunarity;
  24. t += 0.12527603 * noise(p); p *= lacunarity;
  25. t += 0.06255931 * noise(p);
  26. return t;
  27. }
  28. float get_noise(vec3 x)
  29. {
  30. float FBM_FREQ=2.76434;
  31. return fbm(x, FBM_FREQ);
  32. }
  33. vec3 render_sky_color(vec3 rd){
  34. vec3 sun_color = vec3(1., .7, .55);
  35. vec3 SUN_DIR = normalize(vec3(0, abs(sin( .3)), -1));
  36. float sun_amount = max(dot(rd, SUN_DIR), 0.0);
  37. vec3 sky = mix(vec3(.0, .1, .4), vec3(.3, .6, .8), 1.0 - rd.y);
  38. sky = sky + sun_color * min(pow(sun_amount, 1500.0) * 5.0, 1.0);
  39. sky = sky + sun_color * min(pow(sun_amount, 10.0) * .6, 1.0);
  40. return sky;
  41. }
  42. bool SphereIntersect(vec3 SpPos, float SpRad, vec3 ro, vec3 rd, out float t, out vec3 norm) {
  43. ro -= SpPos;
  44. float A = dot(rd, rd);
  45. float B = 2.0*dot(ro, rd);
  46. float C = dot(ro, ro)-SpRad*SpRad;
  47. float D = B*B-4.0*A*C;
  48. if (D < 0.0) return false;
  49. D = sqrt(D);
  50. A *= 2.0;
  51. float t1 = (-B+D)/A;
  52. float t2 = (-B-D)/A;
  53. if (t1 < 0.0) t1 = t2;
  54. if (t2 < 0.0) t2 = t1;
  55. t1 = min(t1, t2);
  56. if (t1 < 0.0) return false;
  57. norm = ro+t1*rd;
  58. t = t1;
  59. //norm = normalize(norm);
  60. return true;
  61. }
  62. float density(vec3 pos,vec3 offset,float t){
  63. vec3 p = pos * .0212242 + offset;
  64. float dens = get_noise(p);
  65. float cov = 1. - COVERAGE;
  66. dens *= smoothstep (cov, cov + .05, dens);
  67. return clamp(dens, 0., 1.);
  68. }
  69. vec4 render_clouds(vec3 ro,vec3 rd){
  70. vec3 apos=vec3(0, -450, 0);
  71. float arad=500.;
  72. vec3 WIND=vec3(0, 0, -iTime * .2);
  73. vec3 C = vec3(0, 0, 0);
  74. float alpha = 0.;
  75. vec3 n;
  76. float tt;
  77. if(SphereIntersect(apos,arad,ro,rd,tt,n)){
  78. float thickness = THICKNESS;
  79. int steps = STEPS;
  80. float march_step = thickness / float(steps);
  81. vec3 dir_step = rd / rd.y * march_step;
  82. vec3 pos =n;
  83. float T = 1.;
  84. for (int i = 0; i < steps; i++) {
  85. float h = float(i) / float(steps);
  86. float dens = density (pos, WIND, h);
  87. float T_i = exp(-ABSORPTION * dens * march_step);
  88. T *= T_i;
  89. if (T < .01) break;
  90. C += T * (exp(h) / 1.75) *dens * march_step;
  91. alpha += (1. - T_i) * (1. - alpha);
  92. pos += dir_step;
  93. if (length(pos) > 1e3) break;
  94. }
  95. return vec4(C, alpha);
  96. }
  97. return vec4(C, alpha);
  98. }
  99. float fbm2(in vec3 p)
  100. {
  101. float f = 0.;
  102. f += .50000 * noise(.5 * (p+vec3(0.,0.,-iTime*0.275)));
  103. f += .25000 * noise(1. * (p+vec3(0.,0.,-iTime*0.275)));
  104. f += .12500 * noise(2. * (p+vec3(0.,0.,-iTime*0.275)));
  105. f += .06250 * noise(4. * (p+vec3(0.,0.,-iTime*0.275)));
  106. return f;
  107. }
  108. vec3 cube_bot(vec3 d, vec3 c1, vec3 c2)
  109. {
  110. return fbm2(d) * mix(c1, c2, d * .5 + .5);
  111. }
  112. vec3 rotate_y(vec3 v, float angle)
  113. {
  114. float ca = cos(angle); float sa = sin(angle);
  115. return v*mat3(
  116. vec3(+ca, +.0, -sa),
  117. vec3(+.0,+1.0, +.0),
  118. vec3(+sa, +.0, +ca));
  119. }
  120. vec3 rotate_x(vec3 v, float angle)
  121. {
  122. float ca = cos(angle); float sa = sin(angle);
  123. return v*mat3(
  124. vec3(+1.0, +.0, +.0),
  125. vec3(+.0, +ca, -sa),
  126. vec3(+.0, +sa, +ca));
  127. }
  128. void panorama_uv(vec2 fragCoord, out vec3 ro,out vec3 rd, in vec2 iResolution){
  129. float M_PI = 3.1415926535;
  130. float ymul = 2.0; float ydiff = -1.0;
  131. vec2 uv = fragCoord.xy / iResolution.xy;
  132. uv.x = 2.0 * uv.x - 1.0;
  133. uv.y = ymul * uv.y + ydiff;
  134. ro = vec3(0., 5., 0.);
  135. rd = normalize(rotate_y(rotate_x(vec3(0.0, 0.0, 1.0),-uv.y*M_PI/2.0),-uv.x*M_PI));
  136. }
  137. void mainImage( out vec4 fragColor, in vec2 fragCoord, in vec2 iResolution)
  138. {
  139. vec3 ro = vec3 (0.,0.,0.);
  140. vec3 rd = vec3(0.);
  141. vec3 col=vec3(0.);
  142. panorama_uv(fragCoord,ro,rd,iResolution);
  143. vec3 sky = render_sky_color(rd);
  144. vec4 cld = vec4(0.);
  145. float skyPow = dot(rd, vec3(0.0, -1.0, 0.0));
  146. float horizonPow =1.-pow(1.0-abs(skyPow), 5.0);
  147. if(rd.y>0.)
  148. {cld=render_clouds(ro,rd);
  149. cld=clamp(cld,vec4(0.),vec4(1.));
  150. cld.rgb+=0.04*cld.rgb*horizonPow;
  151. cld*=clamp(( 1.0 - exp(-2.3 * pow(max((0.0), horizonPow), (2.6)))),0.,1.);
  152. }
  153. else{
  154. cld.rgb = cube_bot(rd,vec3(1.5,1.49,1.71), vec3(1.1,1.15,1.5));
  155. cld*=cld;
  156. //cld=clamp(cld,vec4(0.),vec4(1.));
  157. cld.a=1.;
  158. cld*=clamp(( 1.0 - exp(-1.3 * pow(max((0.0), horizonPow), (2.6)))),0.,1.);
  159. }
  160. col=mix(sky, cld.rgb/(0.0001+cld.a), cld.a);
  161. //col*=col;
  162. fragColor = vec4(col,1.0);
  163. }
  164. void fragment(){
  165. vec2 iResolution=1./TEXTURE_PIXEL_SIZE;
  166. mainImage(COLOR,UV*iResolution,iResolution);
  167. }