Advertisement
Guest User

Untitled

a guest
Mar 11th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.32 KB | None | 0 0
  1. float waterline = 1.0;
  2.  
  3. vec4 hash4( vec4 n ) { return fract(abs(sin(n))*1399763.5453123); }
  4. vec3 hash3( vec3 n ) { return fract(abs(sin(n))*1399763.5453123); }
  5. vec2 NC0=vec2(1.0,136.0);
  6. vec3 NC1=vec3(1.0,136.0,31.0);
  7. float seed = 0.0;
  8.  
  9. vec2 getBL(vec3 pos) { return vec2(atan(pos.x,pos.z),atan(length(pos.xz),pos.y))/3.1415; }
  10. vec3 setBL(vec2 bl) { bl*=3.1415; return vec3(cos(bl.y)*cos(bl.x),sin(bl.y),cos(bl.y)*sin(bl.x)); }
  11.  
  12. // Simple 2d interpolated noise for mercator projection, using local seed.
  13. float sphereNoise2Ds(vec2 bl,float scale,float s)
  14. {
  15. vec2 x = bl;
  16. x.x = clamp(x.x,-1.0,0.9999);
  17. vec2 xc = vec2(floor(x.y*scale)) + vec2(1.0,0.0);
  18. xc = floor(sin(3.1415 * xc/scale)* scale);
  19. vec4 p = vec4(xc.xy*x.x,x.y*scale,x.y*scale+1.0);
  20. vec4 f = fract(p);
  21. f=f*f*(3.0-2.0*f);
  22. p = floor(p);
  23. p.zw += vec2(scale);
  24. vec4 pp = vec4(p.x+1.0>=xc.x? -xc.x:p.x+1.0,p.y+1.0>=xc.y? -xc.y:p.y+1.0,p.zw);
  25.  
  26. vec4 n= hash4(vec4(dot(p.yz,NC0),dot(pp.yz,NC0),dot(p.xw,NC0),dot(pp.xw,NC0))+s);
  27. n.xy = mix(n.xz,n.yw,f.yx);
  28. return mix(n.x,n.y,f.z);
  29. }
  30.  
  31. vec2 sphereNoise2D2s(vec2 bl,float scale,vec2 s)
  32. {
  33. vec2 x = bl;
  34. x.x = clamp(x.x,-1.0,0.9999);
  35. vec2 xc = vec2(floor(x.y*scale)) + vec2(1.0,0.0);
  36. xc = floor(sin(3.1415 * xc/scale)* scale);
  37. vec4 p = vec4(xc.xy*x.x,x.y*scale,x.y*scale+1.0);
  38. vec4 f = fract(p);
  39. f=f*f*(3.0-2.0*f);
  40. p = floor(p);
  41. p.zw += vec2(scale);
  42. vec4 pp = vec4(p.x+1.0>=xc.x? -xc.x:p.x+1.0,p.y+1.0>=xc.y? -xc.y:p.y+1.0,p.zw);
  43.  
  44. vec4 n1= hash4(vec4(dot(p.yz,NC0),dot(pp.yz,NC0),dot(p.xw,NC0),dot(pp.xw,NC0))+s.x);
  45. vec4 n2= hash4(vec4(dot(p.yz,NC0),dot(pp.yz,NC0),dot(p.xw,NC0),dot(pp.xw,NC0))+s.y);
  46. n1 = vec4(mix(n1.xz,n1.yw,f.yx),mix(n2.xz,n2.yw,f.yx));
  47.  
  48. return mix(n1.xz,n1.yw,f.zz);
  49. }
  50.  
  51. // Simple 2d interpolated noise for mercator projection, using global seed.
  52. float sphereNoise2D(vec2 bl,float scale) { return sphereNoise2Ds(bl,scale,seed); }
  53.  
  54. // Simple 2d interpolated noise for mercator projection, using global seed animated.
  55. float sphereNoise2D(vec3 blt,float scale)
  56. {
  57. float z = floor(blt.z)*54.0;
  58. float f = fract(blt.z);
  59. //f=f*f*(3.0-2.0*f);
  60. return mix(sphereNoise2Ds(blt.xy,scale,seed+z),
  61. sphereNoise2Ds(blt.xy,scale,seed+z+54.0),f);
  62. }
  63.  
  64. // Simple 2d interpolated distortion noise for mercator projection, using global seed.
  65. float sphereNoise2Do(vec2 bl,float scale,float force)
  66. {
  67. bl = bl+vec2(sphereNoise2Ds(bl,scale*2.0f,3.0),sphereNoise2Ds(bl,scale*2.0f,20.0))*force/scale;
  68. bl.x = bl.x>=1.0 ? bl.x - 2.0 : bl.x;
  69. bl.y = clamp(bl.y,0.0,1.0);
  70. return sphereNoise2D(bl,scale);
  71. }
  72.  
  73. float sphereNoise2Do2(vec2 bl,float scale,float force, float s)
  74. {
  75. bl = bl+vec2(sphereNoise2Ds(bl,scale*s,3.0),sphereNoise2Ds(bl,scale*s,20.0))*force/scale;
  76. bl.x = bl.x>=1.0 ? bl.x - 2.0 : bl.x;
  77. bl.y = clamp(bl.y,0.0,1.0);
  78. return sphereNoise2D(bl,scale);
  79. }
  80.  
  81. vec2 sphereIntersect(vec3 ray,vec3 pos,float r)
  82. {
  83. float r2=r*r;
  84. float cd = dot(pos,ray);
  85. vec3 p = ray * cd-pos;
  86. float t2 = dot(p,p);
  87. if (t2>=r2) return vec2(-1.0, -1.0);
  88. float d = sqrt(r2-t2);
  89. return cd+vec2(- d,d);
  90. }
  91.  
  92. float sphereIntersectIn(vec3 ray,vec3 pos,float r)
  93. {
  94. float r2=r*r;
  95. float cd = dot(pos,ray);
  96. vec3 p = ray * cd-pos;
  97. float t2 = dot(p,p);
  98. if (t2>=r2) return -1.0;
  99. return cd + sqrt(r2-t2);
  100. }
  101.  
  102. vec3 sphereNormal(vec3 pos, vec3 surface)
  103. {
  104. return normalize(surface-pos);
  105. }
  106.  
  107. float cloud(vec2 bl,float scale)
  108. {
  109. float f = 0.0;
  110. float m = 1.0;
  111. float s = 0.32;
  112. for (int i=0;i<20;i++) {
  113. f += sphereNoise2D(bl,m*scale)*s;
  114. m = m * 2.0;
  115. s = s * 0.7;
  116. }
  117. f = f;
  118. return clamp(f,0.,1.);
  119. }
  120.  
  121. float spherePerlin(vec2 bl,float scale)
  122. {
  123. return (sphereNoise2D(bl,512.0*scale)*0.8+sphereNoise2D(bl,256.0*scale)*0.8
  124. +sphereNoise2D(bl,128.0*scale)+sphereNoise2D(bl,64.0*scale)
  125. +sphereNoise2D(bl,32.0*scale)+sphereNoise2D(bl,16.0*scale)
  126. +sphereNoise2D(bl,8.0*scale)+sphereNoise2D(bl,4.0*scale)
  127. +sphereNoise2D(bl,2.0*scale)*1.2+sphereNoise2D(bl,scale)*1.2)*0.1;
  128. }
  129.  
  130. float spherePerlinA(vec3 blt,float scale)
  131. {
  132. return (sphereNoise2D(blt,16.0*scale)
  133. +sphereNoise2D(blt,8.0*scale)+sphereNoise2D(blt,4.0*scale)
  134. +sphereNoise2D(blt,2.0*scale)*1.2+sphereNoise2D(blt,scale)*1.2)*0.2;
  135. }
  136.  
  137. float spherePerlinHalf(vec2 bl,float scale)
  138. {
  139. return (sphereNoise2D(bl,16.0*scale)+sphereNoise2D(bl,8.0*scale)+sphereNoise2D(bl,4.0*scale)
  140. +sphereNoise2D(bl,2.0*scale)+sphereNoise2D(bl,scale))*0.2;
  141. }
  142.  
  143. float spherePerlinHalfs(vec2 bl,float scale, float seed)
  144. {
  145. return (sphereNoise2Ds(bl,16.0*scale,seed)+sphereNoise2Ds(bl,8.0*scale,seed)+sphereNoise2Ds(bl,4.0*scale,seed)
  146. +sphereNoise2Ds(bl,2.0*scale,seed)+sphereNoise2Ds(bl,scale,seed))*0.2;
  147. }
  148.  
  149. vec2 spherePerlinHalf2s(vec2 bl,float scale, vec2 seed)
  150. {
  151. return (sphereNoise2D2s(bl,16.0*scale,seed)+sphereNoise2D2s(bl,8.0*scale,seed)+sphereNoise2D2s(bl,4.0*scale,seed)
  152. +sphereNoise2D2s(bl,2.0*scale,seed)+sphereNoise2D2s(bl,scale,seed))*0.2;
  153. }
  154.  
  155. // Perlin distortion noise
  156. float spherePerlinHalfd(vec2 bl,float scale,float force)
  157. {
  158. return (sphereNoise2Do(bl,16.0*scale,force)+sphereNoise2Do(bl,8.0*scale,force)
  159. +sphereNoise2Do(bl,4.0*scale,force)
  160. +sphereNoise2Do(bl,2.0*scale,force)+sphereNoise2Do(bl,scale,force))*0.2;
  161. }
  162.  
  163. // Perlin distortion noise with increasing coefficient
  164. float spherePerlinHalfds(vec2 bl,float scale,float force)
  165. {
  166. return sphereNoise2Do(bl,16.0*scale,force)*0.05
  167. +sphereNoise2Do(bl,8.0*scale,force)*0.15+sphereNoise2Do(bl,4.0*scale,force)*0.2
  168. +sphereNoise2Do(bl,2.0*scale,force)*0.25+sphereNoise2Do(bl,scale,force)*0.35;
  169. }
  170.  
  171. float spherePerlinHalfds2(vec2 bl,float scale,float force,float s)
  172. {
  173. return sphereNoise2Do2(bl,16.0*scale,force,16.*s)*0.05
  174. +sphereNoise2Do2(bl,8.0*scale,force,8.*s)*0.15+sphereNoise2Do2(bl,4.0*scale,force,4.*s)*0.2
  175. +sphereNoise2Do2(bl,2.0*scale,force,2.*s)*0.25+sphereNoise2Do2(bl,scale,force,s)*0.35;
  176. }
  177.  
  178. vec3 wind3D(vec3 pos,float rings,float skip)
  179. {
  180. vec3 xv = normalize(cross(pos,vec3(0.,1.,0.)));
  181. vec3 yv = normalize(cross(pos,xv));
  182. vec3 f = fract(pos)-0.5;
  183. vec3 px = floor(pos);
  184. float r = length(pos);
  185. vec3 wind =vec3(0.);
  186.  
  187. for (int z = -0;z<2;z++)
  188. for (int y = -0;y<2;y++)
  189. for (int x = -0;x<2;x++) {
  190. vec3 off = vec3(float(x),float(y),float(z));
  191. vec3 xyz = px + off;
  192. float p = dot(xyz,NC1);
  193. vec3 add = hash3(vec3(p,p+30.0,p+23.0));
  194. if (add.z<skip) continue;
  195. vec3 fxyz = f + add*0.8-off;
  196. float ml = clamp(1.0-length(fxyz)*1.5,0.,1.);
  197. vec3 wp = normalize(pos - fxyz) * r;
  198. fxyz = wp-pos;
  199. float l1 = length(fxyz);
  200. float l2 = 1.-l1;
  201. vec3 w = vec3(0.);
  202. if (l1>0.) {
  203. w = normalize(cross(fxyz,pos));
  204. }
  205. wind +=w*ml*clamp(0.5-l1,0.,1.)*clamp(1.0-l2*l2*l2,0.,1.);
  206. }
  207. return wind;
  208. }
  209.  
  210. float atmosphere(vec3 pos, vec3 atmosSurface, vec3 ray, vec3 lnorm, float planetRadius, float atmosRadius, float atmosDepth)
  211. {
  212. float wall = sqrt(atmosRadius*atmosRadius - planetRadius*planetRadius);
  213. float acc = clamp(atmosDepth/wall,0.0,1.0);
  214. acc = acc*clamp(dot(lnorm,normalize(atmosSurface-pos)),0.0,1.0);
  215. acc = acc*acc;
  216. acc = acc*acc;
  217.  
  218. return acc;
  219. }
  220.  
  221. vec3 sphereTexture(vec3 realSurface,vec3 ray, vec3 snorm,vec3 lnorm,float time)
  222. {
  223. seed = 1.0;
  224. vec2 bl = getBL(realSurface);
  225.  
  226. seed = 2.0;
  227. float d = spherePerlinHalfds(bl,8.0,0.5);
  228. float c = spherePerlin(bl,2.5);
  229. float can = clamp(1.0-abs(d-0.5)*10.0,0.0,1.0) * clamp(c*5.0-1.6,0.0,1.0);
  230. float ll = clamp(1.0-abs(d-0.5)*4.0,0.0,1.0);
  231. float can2 = can*can;//*0.34;
  232. float can3 = can2*can;
  233.  
  234. seed = 10.0;
  235. float e = spherePerlinHalfds(bl,6.0,0.5);
  236. float mc = clamp(e*6.0-2.4, 0.0, 1.0);
  237.  
  238. seed = 5.0;
  239. float fa = (spherePerlinA(vec3(bl,time),200.0)+spherePerlinA(vec3(bl,time+2.5),200.0))*0.5;
  240.  
  241. seed = 5.0;
  242. float f = spherePerlin(bl,4.0);
  243. f = mix(1.0-pow(1.0-f*1.7,2.0),f*1.7,0.3);
  244. f = f-can3*0.34;
  245.  
  246. float canl = (can3+fa-0.5);
  247.  
  248. float light = clamp(dot(snorm,lnorm),0.0,1.0);
  249. vec3 col = clamp(mix(vec3(2.0,0.2,0.0),vec3(0.02,0.0,0.0),f)*f,vec3(0.,0.,0.),vec3(1.,1.,1.));
  250. col = mix(col,mix(vec3(1.0,0.5,0.0),vec3(1.0,0.8,0.33),mc),0.2);
  251. vec3 lav = mix(vec3(1.,0.0,0.),mix(vec3(1.,1.0,0.),vec3(1.,1.,1.),clamp(abs(0.5-canl)*3.0,0.,1.)),clamp(canl,0.,1.));
  252.  
  253. return clamp(mix(col*light,lav*2.0,can3),vec3(0.,0.,0.),vec3(1.,1.,1.));//mix(col*light,mix(lav,col*light,clamp((1.0-ll*ll),0.,1.)),ll);
  254.  
  255. //vec3 col = clamp(mix(vec3(0.94,0.63,0.33),vec3(1.0,0.85,0.57),f)*f,vec3(0.,0.,0.),vec3(1.,1.,1.));
  256. //return mix(col,mix(vec3(1.0,0.5,0.0),vec3(1.0,0.8,0.33),mc),0.2);
  257. //return vec3(0.5);
  258. }
  259.  
  260. vec2 cloudTexture(vec3 realSurface, float time)
  261. {
  262. seed = 14.0;
  263. vec2 bl = getBL(realSurface);
  264.  
  265. vec3 w = wind3D(realSurface*1.,1.5,0.);
  266. w += wind3D(realSurface*2.,1.5,0.)*0.2;
  267. w += wind3D(realSurface*6.,1.5,0.)*0.1;
  268. vec2 t = time*vec2(0.2,0.5);
  269. vec2 bld1 = getBL(normalize(realSurface-w*(1.4+0.4*fract(t.x))));
  270. vec2 bld2 = getBL(normalize(realSurface-w*(1.4+0.4*fract(t.x+0.5))));
  271. vec2 blc1 = getBL(normalize(realSurface-w*(1.4+0.4*fract(t.y))));
  272. vec2 blc2 = getBL(normalize(realSurface-w*(1.4+0.4*fract(t.y+0.5))));
  273.  
  274. seed = 14.0+floor(t.x)*0.1;
  275. float d1 = spherePerlinHalf(bld1, 6.0);
  276. seed = 14.05+floor(t.x+0.5)*0.1;
  277. float d2 = spherePerlinHalf(bld2, 6.0);
  278.  
  279. seed = 14.0+floor(t.y)*2.0;
  280. float c1 = spherePerlinHalf(blc1, 192.0);
  281. seed = 15.0+floor(t.y+0.5)*2.0;
  282. float c2 = spherePerlinHalf(blc2, 192.0);
  283. vec2 fdc = abs(0.5-fract(t))*2.0;
  284. float d = mix(d1,d2,fdc.x);
  285. float c = mix(c1,c2,fdc.y);
  286.  
  287. float f = clamp((d*d)*4.0-c-0.5,0.0,1.0);
  288. f = 1.-f;
  289. return vec2(1.-f*f,0);
  290. //return vec2(0.);
  291. }
  292.  
  293. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  294. {
  295. // resolution
  296. float res = 1.0 / iResolution.y;
  297. vec2 p = (-iResolution.xy + 2.0*fragCoord.xy) *res;
  298.  
  299. // bace const
  300. float time=iTime*1.0;
  301. vec3 ray = normalize(vec3(p,2.0));
  302. vec3 col = vec3(0.0);
  303. vec2 mouse = (iMouse.xy*2.0-iResolution.xy)*res;
  304.  
  305. // segments
  306. vec2 scroll = iMouse.z>0.0 ? mouse : vec2(floor(p.x*2.5)*0.4+0.2,0.0);
  307. scroll = clamp(scroll*0.5+0.5,vec2(0.),vec2(1.));
  308. // test scroll
  309. //const vec3 pal[6] = vec3[6](vec3(1.0,0.0,0.0),vec3(1.0,1.0,0.0),vec3(0.0,1.0,0.0),vec3(0.0,1.0,1.0),vec3(0.0,0.0,1.0),vec3(1.0,0.0,1.0));
  310. //col = mix(pal[int(floor(scroll.x*5.0))],pal[int(floor(scroll.x*5.0))+1],fract(scroll.x*5.0));
  311.  
  312. // transforms
  313. vec2 rotate = iMouse.z>0.0 ? mouse*2. : vec2(0.,-0.6);
  314. //vec2 rotate = vec2(0.,-0.6);
  315. float protate = time*0.025;
  316. vec4 mcs=vec4(sin(rotate),cos(rotate));
  317. vec2 pcs=vec2(cos(protate),sin(protate)); // planet rotation
  318. mat3 mr=mat3(vec3(mcs.z,0.0,mcs.x),vec3(0.0,1.0,0.0),vec3(-mcs.x,0.0,mcs.z));
  319. mr=mat3(vec3(1.0,0.0,0.0),vec3(0.0,mcs.w,mcs.y),vec3(0.0,-mcs.y,mcs.w))*mr;
  320. mat3 pmr=mat3(vec3(pcs.x,0.0,pcs.y),vec3(0.0,1.0,0.0),vec3(-pcs.y,0.0,pcs.x));
  321.  
  322. // object data
  323. float dist=2.3;
  324. float rplanet = 1.0;
  325. float ratmos = 1.15;
  326. float rcloud = 1.005;
  327. vec3 spos = vec3(0.0,0.0,dist);
  328. vec3 lpos = vec3(4.5,4.5,-9.0+dist);
  329. lpos = mr*(lpos-spos) + spos;
  330. vec3 lnorm = normalize(lpos - spos);
  331.  
  332. // render calc
  333. float dplanet = sphereIntersect(ray,spos,rplanet).x;
  334. vec2 dcloud = sphereIntersect(ray,spos,rcloud);
  335. float datmos = sphereIntersect(ray,spos,ratmos).x;
  336. if (dplanet>0.0) {
  337. vec3 ssurface = ray*dplanet;
  338. vec3 snorm = sphereNormal(spos,ssurface);
  339. vec3 lsnorm = normalize(lpos - ssurface);
  340. float dcs = sphereIntersectIn(lnorm,spos-ssurface,rcloud);
  341. vec3 scloud = ssurface+lnorm*dcs;
  342. float cs = dot(snorm,lnorm)>0. ? clamp(1.0-cloudTexture((scloud-spos)*mr*pmr,time*0.2).x,0.0,1.0) : 1.0;
  343.  
  344. col = sphereTexture((ssurface-spos)*mr*pmr,ray,snorm,lsnorm,time);
  345. col = mix(col,col*cs,0.7);
  346. }
  347. if (datmos>0.0) {
  348. float adepth = dplanet>0.0 ? (dplanet-datmos) : abs(dot(ray,spos)-datmos);
  349. vec3 ssurface = ray*datmos;
  350. float ap = atmosphere(spos, ssurface, ray, lnorm, rplanet, ratmos, adepth);
  351. col = mix(col,mix(vec3(1.0,0.3,0.0),vec3(1.0,0.99,0.94),ap),ap);
  352. }
  353. if (dcloud.x>0.0) {
  354. vec3 sback = ray*dcloud.y;
  355. /*if (dplanet<0.0) {
  356. vec3 bnorm = sphereNormal(spos,sback);
  357. col = mix(col, vec3(0.0), cloudTexture((sback-spos)*mr*pmr,time*0.2) * clamp(dot(bnorm,lnorm),0.0,1.0));
  358. }*/
  359. vec3 ssurface = ray*dcloud.x;
  360. vec3 snorm = sphereNormal(spos,ssurface);
  361. vec3 lsnorm = normalize(lpos - ssurface);
  362. vec2 al = cloudTexture((ssurface-spos+lsnorm*0.01)*mr*pmr,time*0.2);
  363. vec2 a = cloudTexture((ssurface-spos)*mr*pmr,time*0.2);
  364. float light = clamp(dot(snorm,lsnorm),0.0,1.0);
  365. col = mix(col,mix(vec3(0.0,0.0,0.0),vec3(0.0,0.0,0.0),a.y)*light,clamp(a.x,0.,1.))
  366. + vec3(clamp((a.x-al.x)*0.7,0.,1.)+clamp((a.y-al.y)*1.*a.x+a.x*0.3*light*light,0.,1.))*light;
  367. }
  368. // Output to screen
  369. fragColor = vec4(col,1.0);
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement