Sky.shader 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. shader_type canvas_item;
  2. uniform float iTime;
  3. uniform int iFrame;
  4. uniform sampler2D iChannel0;
  5. uniform bool disable_panorama;
  6. uniform float sun_pos;
  7. //using https://www.shadertoy.com/view/XdBSWd
  8. vec3 rotate_y(vec3 v, float angle)
  9. {
  10. float ca = cos(angle); float sa = sin(angle);
  11. return v*mat3(
  12. vec3(+ca, +.0, -sa),
  13. vec3(+.0,+1.0, +.0),
  14. vec3(+sa, +.0, +ca));
  15. }
  16. vec3 rotate_x(vec3 v, float angle)
  17. {
  18. float ca = cos(angle); float sa = sin(angle);
  19. return v*mat3(
  20. vec3(+1.0, +.0, +.0),
  21. vec3(+.0, +ca, -sa),
  22. vec3(+.0, +sa, +ca));
  23. }
  24. void panorama_uv(vec2 fragCoord, out vec3 ro, out vec3 rd, in vec2 iResolution){
  25. float M_PI = 3.1415926535;
  26. float ymul = 2.0; float ydiff = -1.0;
  27. vec2 uv = fragCoord.xy / iResolution.xy;
  28. ///uv.x=1.-uv.x;
  29. uv.x = 2.0 * uv.x - 1.0;
  30. uv.y = ymul * uv.y + ydiff;
  31. //uv.x=-uv.x;
  32. //uv.x+=-01.25;
  33. ro = vec3(0., 5., 0.);
  34. rd = normalize(rotate_y(rotate_x(vec3(0.0, 0.0, 1.0),-uv.y*M_PI/2.0),-uv.x*M_PI));
  35. }
  36. float plane( in vec3 ro, in vec3 rd, vec3 c, vec3 u, vec3 v )
  37. {
  38. vec3 q = ro - c;
  39. vec3 n = cross(u,v);
  40. return -dot(n,q)/dot(rd,n);
  41. }
  42. float nz(in vec2 p){return textureLod(iChannel0, p*.01,0.).x;}
  43. float fbm(in vec2 p, in float d)
  44. {
  45. mat2 m2 = mat2( vec2(0.80, 0.60), vec2(-0.60, 0.80) );
  46. float time=iTime*2.;
  47. d = smoothstep(0.,100.,d);
  48. p *= .3/(d+0.2);
  49. float z=2.;
  50. float rz = 0.;
  51. p -= time*0.02;
  52. for (float i= 1.;i <=5.;i++ )
  53. {
  54. rz+= (sin(nz(p)*6.5)*0.5+0.5)*1.25/z; //clouds sin(iTime/10.)*
  55. z *= 2.1;
  56. p *= 2.15;
  57. p += time*0.027*(mod(i,2.)-1.5);
  58. p *= m2;
  59. }
  60. return pow(abs(rz),2.-d);
  61. }
  62. vec3 pldec(vec2 p)
  63. {
  64. float t = iTime*.13;
  65. p*=0.25;
  66. float a = t*.5;
  67. float s=sin(a), c=cos(a);
  68. p*=mat2(vec2(c,s),vec2(-s,c));
  69. p += .21*sin(p.yx*6.+t);
  70. p = abs(p);
  71. p*=mat2(vec2(c,s),vec2(-s,c));
  72. vec3 col =0.5 + 0.5*cos(t*1.4+vec3(p.xy,p.x)*5.+vec3(0,2,4)) + .3*sin(p.x*(1.1+.2*sin(t*.9))*20.+t*.4)+.3;
  73. col.g=0.25*max(0.65*col.r,0.5*col.b);
  74. col.r=0.5*max(col.r,0.85*col.b);
  75. col.r=max(col.r,.28-col.g);
  76. col.b=max(0.65*col.b,.6-col.g);
  77. return (pow(clamp(col*(1.-length(p*1.6)),vec3(0.),vec3(1.)),vec3(02.5)));
  78. }
  79. vec3 clouds(in vec3 ro, in vec3 rd, in bool wtr)
  80. {
  81. vec3 lgt = normalize(vec3(-1.0,0.1,.50));
  82. float tval=-.2+0.15*iTime;
  83. tval=-0.;
  84. tval=0.025*sun_pos;
  85. lgt = normalize(vec3(sin(tval),-sin(tval),cos(tval)));
  86. vec3 hor = vec3(0);
  87. vec3 col1=vec3(0.70,1.0,1.0);
  88. vec3 col2=vec3(1.3,0.55,0.15);
  89. vec3 col3=vec3(0.5,0.75,1.);
  90. vec3 scol1=vec3(1.0,0.8,0.7);
  91. vec3 scol2=vec3(1.0,0.4,0.2);
  92. float hval=min(smoothstep(-0.0,0.15,abs(rd.y)),1.) ;
  93. float nval=lgt.y/(normalize(vec3(1.)).x);
  94. float mval2=smoothstep(-.9,-0.2,nval)*smoothstep(-0.8,-.1,nval);
  95. nval=max(lgt.y/(normalize(vec3(1.)).x),0.);
  96. float sun = clamp(dot(lgt,rd),0.0,1.0 );
  97. float mval=max(max(nval,sun),0.21);
  98. float v = 1.0/( 2. * ( 1. + rd.y ) );
  99. vec2 xy = vec2(rd.x * v, rd.z * v);
  100. vec3 exc=vec3(0.);
  101. vec3 col=vec3(0.);
  102. col2*=mval;
  103. col3*=mval;
  104. exc=(pldec(xy));
  105. scol1=mix(2.*exc,scol1,max(nval,sun));
  106. scol2=mix(scol2*0.5+1.5*exc,scol2,max(nval,sun));
  107. if (!wtr)
  108. {
  109. col += 0.8*scol1*pow(sun,512.0);
  110. col += 0.2*scol2*pow(sun,32.0);
  111. }
  112. else
  113. {
  114. //col3*=(1.-hval);
  115. col3=1.5*vec3(0.5509,0.42117,0.61725)*hval;
  116. col3=mix(col3/2.5,col3,nval);
  117. col += .625*scol1*pow(sun,512.0)*(1.-hval);
  118. col += 0.13*scol2*pow(sun,32.0)*(1.-hval);
  119. }
  120. hor = mix( col1, col2, 0.25+0.75*sun );//*(0.85+0.25*mval2);
  121. col += 0.4*mix( col3, hor, exp(-(4.+ 2.*(1.-sun))*max(0.0,rd.y-0.05)) );
  122. col += 0.1*scol2*pow(sun,4.0);
  123. float pt = (90.0-ro.y)/rd.y;
  124. vec3 bpos = ro + pt*rd;
  125. float dist = sqrt(distance(ro,bpos));
  126. float s2p = distance(bpos,lgt*100.);
  127. float cls = 0.005;
  128. float bz = 0.;
  129. float tot = bz;
  130. float ds = 2.;
  131. if (!wtr){
  132. bz=fbm(bpos.xz*cls,dist);
  133. tot=bz;
  134. tot = smoothstep(0.,1.15,tot);
  135. for (float i=0.;i<=3.;i++)
  136. {
  137. vec3 pp = bpos + ds*lgt;
  138. float vl = fbm(pp.xz*cls,dist);
  139. vl = smoothstep(0.,1.15,vl);
  140. tot += vl;
  141. ds *= .14*dist;
  142. }}
  143. bz*=hval;
  144. tot*=hval;
  145. col = mix(col,vec3(.5)*0.2*nval,pow(bz,1.5));
  146. tot = smoothstep(-7.5,-0.,1.-tot);
  147. vec3 sccol = mix(col3*vec3(0.1,0.1,0.2),scol1*vec3(.2,0.,0.1),smoothstep(0.,900.,s2p));
  148. col = mix(col,sccol,1.-tot)*1.6;
  149. vec3 sncol = mix(scol2*1.75,scol2*1.25,smoothstep(0.,1200.,s2p)); //ночь
  150. float sd = pow(sun,10.)+.7;
  151. col += sncol*bz*bz*bz*tot*tot*tot*sd;
  152. if (!wtr)col+=(1.-bz)*01.15*exc*(1.-mval);
  153. else col+=01.15*exc*(1.-mval)*(1.-hval);
  154. col=mix(col*col,col,nval);
  155. return col;
  156. }
  157. vec3 clouds_col(vec3 ro, vec3 rd){
  158. float pln = plane(ro, rd, vec3(0.,-4.,0), vec3(1.,0.,0.), vec3(0.0,.0,1.0));
  159. bool wtr = false;
  160. vec3 bm = vec3(0);
  161. if (pln < 500. && pln > 0.)
  162. {
  163. vec3 n = vec3(0,1,0);
  164. rd = reflect(rd,n);
  165. wtr = true;
  166. }
  167. vec3 clo = clouds(ro, rd, wtr);
  168. return clo;
  169. }
  170. void mainImage( out vec4 fragColor, in vec2 fragCoord ,vec2 iResolution)
  171. {
  172. vec3 ro,rd;
  173. panorama_uv(fragCoord,ro,rd,iResolution);
  174. vec3 col = vec3(0.);
  175. col=clouds_col(ro,rd);
  176. //col=col/4.+col*col/2.;
  177. //col=col*col;
  178. fragColor = vec4(col, 1);
  179. }
  180. void fragment(){
  181. vec2 iResolution=1./TEXTURE_PIXEL_SIZE;
  182. if(disable_panorama) COLOR=vec4(0.8,0.8,0.8,1.);
  183. else mainImage(COLOR,UV*iResolution,iResolution);
  184. }