Advertisement
Guest User

Untitled

a guest
Mar 10th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.64 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. // Simple 2d interpolated noise for mercator projection, using global seed.
  10. float sphereNoise2D(vec2 bl,float scale)
  11. {
  12. vec2 x = bl;
  13. vec2 xc = vec2(floor(x.y*scale)) + vec2(1.0,0.0);
  14. xc = floor(sin(3.1415 * xc/scale)* scale);
  15. vec4 p = vec4(xc.x*x.x,xc.y*x.x,x.y*scale,x.y*scale+1.0);
  16. vec4 f = fract(p);
  17. f=f*f*(3.0-2.0*f);
  18. p = floor(p);
  19. 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.z+scale,p.w+scale);
  20. p.zw += vec2(scale);
  21.  
  22. vec4 n= hash4(vec4(dot(p.yz,NC0),dot(pp.yz,NC0),dot(p.xw,NC0),dot(pp.xw,NC0))+seed);
  23. return mix(mix(n.x,n.y,f.y),mix(n.z,n.w,f.x),f.z);
  24. }
  25.  
  26. // Simple 2d interpolated noise for mercator projection, using local seed.
  27. float sphereNoise2Ds(vec2 bl,float scale,float s)
  28. {
  29. vec2 x = bl;
  30. vec2 xc = vec2(floor(x.y*scale)) + vec2(1.0,0.0);
  31. xc = floor(sin(3.1415 * xc/scale)* scale);
  32. vec4 p = vec4(xc.x*x.x,xc.y*x.x,x.y*scale,x.y*scale+1.0);
  33. vec4 f = fract(p);
  34. f=f*f*(3.0-2.0*f);
  35. p = floor(p);
  36. 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.z+scale,p.w+scale);
  37. p.zw += vec2(scale);
  38.  
  39. vec4 n= hash4(vec4(dot(p.yz,NC0),dot(pp.yz,NC0),dot(p.xw,NC0),dot(pp.xw,NC0))+s);
  40. return mix(mix(n.x,n.y,f.y),mix(n.z,n.w,f.x),f.z);
  41. }
  42.  
  43. // Simple 2d interpolated noise for mercator projection, using global seed animated.
  44. float sphereNoise2D(vec3 blt,float scale)
  45. {
  46. float z = floor(blt.z)*54.0;
  47. float f = fract(blt.z);
  48. //f=f*f*(3.0-2.0*f);
  49. return mix(sphereNoise2Ds(blt.xy,scale,seed+z),
  50. sphereNoise2Ds(blt.xy,scale,seed+z+54.0),f);
  51. }
  52.  
  53. // Simple 2d interpolated distortion noise for mercator projection, using global seed.
  54. float sphereNoise2Do(vec2 bl,float scale,float force)
  55. {
  56. bl = bl+vec2(sphereNoise2Ds(bl,scale*2.0f,3.0),sphereNoise2Ds(bl,scale*2.0f,20.0))*force/scale;
  57. bl.x = bl.x>1.0 ? bl.x - 2.0 : bl.x;
  58. bl.y = clamp(bl.y,0.0,1.0);
  59. return sphereNoise2D(bl,scale);
  60. }
  61.  
  62. float sphereNoise2Do2(vec2 bl,float scale,float force, float s)
  63. {
  64. bl = bl+vec2(sphereNoise2Ds(bl,scale*s,3.0),sphereNoise2Ds(bl,scale*s,20.0))*force/scale;
  65. bl.x = bl.x>1.0 ? bl.x - 2.0 : bl.x;
  66. bl.y = clamp(bl.y,0.0,1.0);
  67. return sphereNoise2D(bl,scale);
  68. }
  69.  
  70. vec2 sphereIntersect(vec3 ray,vec3 pos,float r)
  71. {
  72. float r2=r*r;
  73. float cd = dot(pos,ray);
  74. vec3 p = ray * cd-pos;
  75. float t2 = dot(p,p);
  76. if (t2>=r2) return vec2(-1.0, -1.0);
  77. float d = sqrt(r2-t2);
  78. return cd+vec2(- d,d);
  79. }
  80.  
  81. float sphereIntersectIn(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 -1.0;
  88. return cd + sqrt(r2-t2);
  89. }
  90.  
  91. vec3 sphereNormal(vec3 pos, vec3 surface)
  92. {
  93. return normalize(surface-pos);
  94. }
  95.  
  96. float spherePerlin(vec2 bl,float scale)
  97. {
  98. return (sphereNoise2D(bl,512.0*scale)*0.8+sphereNoise2D(bl,256.0*scale)*0.8
  99. +sphereNoise2D(bl,128.0*scale)+sphereNoise2D(bl,64.0*scale)
  100. +sphereNoise2D(bl,32.0*scale)+sphereNoise2D(bl,16.0*scale)
  101. +sphereNoise2D(bl,8.0*scale)+sphereNoise2D(bl,4.0*scale)
  102. +sphereNoise2D(bl,2.0*scale)*1.2+sphereNoise2D(bl,scale)*1.2)*0.1;
  103. }
  104.  
  105. float spherePerlinA(vec3 blt,float scale)
  106. {
  107. return (sphereNoise2D(blt,16.0*scale)
  108. +sphereNoise2D(blt,8.0*scale)+sphereNoise2D(blt,4.0*scale)
  109. +sphereNoise2D(blt,2.0*scale)*1.2+sphereNoise2D(blt,scale)*1.2)*0.2;
  110. }
  111.  
  112. float spherePerlinHalf(vec2 bl,float scale)
  113. {
  114. return (sphereNoise2D(bl,16.0*scale)+sphereNoise2D(bl,8.0*scale)+sphereNoise2D(bl,4.0*scale)
  115. +sphereNoise2D(bl,2.0*scale)+sphereNoise2D(bl,scale))*0.2;
  116. }
  117.  
  118. // Perlin distortion noise
  119. float spherePerlinHalfd(vec2 bl,float scale,float force)
  120. {
  121. return (sphereNoise2Do(bl,16.0*scale,force)+sphereNoise2Do(bl,8.0*scale,force)
  122. +sphereNoise2Do(bl,4.0*scale,force)
  123. +sphereNoise2Do(bl,2.0*scale,force)+sphereNoise2Do(bl,scale,force))*0.2;
  124. }
  125.  
  126. // Perlin distortion noise with increasing coefficient
  127. float spherePerlinHalfds(vec2 bl,float scale,float force)
  128. {
  129. return sphereNoise2Do(bl,16.0*scale,force)*0.05
  130. +sphereNoise2Do(bl,8.0*scale,force)*0.15+sphereNoise2Do(bl,4.0*scale,force)*0.2
  131. +sphereNoise2Do(bl,2.0*scale,force)*0.25+sphereNoise2Do(bl,scale,force)*0.35;
  132. }
  133.  
  134. float spherePerlinHalfds2(vec2 bl,float scale,float force,float s)
  135. {
  136. return sphereNoise2Do2(bl,16.0*scale,force,16.*s)*0.05
  137. +sphereNoise2Do2(bl,8.0*scale,force,8.*s)*0.15+sphereNoise2Do2(bl,4.0*scale,force,4.*s)*0.2
  138. +sphereNoise2Do2(bl,2.0*scale,force,2.*s)*0.25+sphereNoise2Do2(bl,scale,force,s)*0.35;
  139. }
  140.  
  141. vec2 twirls3D(vec3 pos,float rings,float skip)
  142. {
  143. vec3 xv = normalize(cross(pos,vec3(0.,1.,0.)));
  144. vec3 yv = normalize(cross(pos,xv));
  145. vec3 f = fract(pos)-0.5;
  146. pos = floor(pos);
  147. float r = 0.0;
  148. float s = 0.0;
  149.  
  150. for (int z = 0;z<2;z++)
  151. for (int y = 0;y<2;y++)
  152. for (int x = 0;x<2;x++) {
  153. vec3 off = vec3(float(x),float(y),float(z));
  154. vec3 xyz = pos + off;
  155. float p = dot(xyz,NC1);
  156. vec3 add = hash3(vec3(p,p+30.0,p+23.0));
  157. if (add.z<skip) continue;
  158. vec3 fxyz = f + add*0.8-off;
  159. float l = length(fxyz);
  160. float a = abs(0.5-fract(l*rings*(0.8+add.y*0.2)*2.0+atan(dot(fxyz,xv),dot(fxyz,yv))/3.1415*0.5));//+add.x*10.0
  161.  
  162. float al = l*2.0;
  163. float sf = clamp(1.0-al*al*al,0.,1.);
  164. r += mix(1.0,a,clamp(l*6.0,0.,1.))*sf;
  165. s += sf;
  166. }
  167. return clamp(vec2(r,s),vec2(0.),vec2(1.));
  168. }
  169.  
  170. float atmosphere(vec3 pos, vec3 atmosSurface, vec3 ray, vec3 lnorm, float planetRadius, float atmosRadius, float atmosDepth)
  171. {
  172. float wall = sqrt(atmosRadius*atmosRadius - planetRadius*planetRadius);
  173. float acc = clamp(atmosDepth/wall,0.0,1.0);
  174. acc = acc*clamp(dot(lnorm,normalize(atmosSurface-pos)),0.0,1.0);
  175. acc = acc*acc;
  176. acc = acc*acc;
  177.  
  178. return acc;
  179. }
  180.  
  181. vec3 sphereTexture(vec3 realSurface,vec3 ray, vec3 snorm,vec3 lnorm,float time)
  182. {
  183. seed =1.0;
  184. vec2 bl = vec2(atan(realSurface.x,realSurface.z),atan(length(realSurface.xz),realSurface.y))/3.1415;
  185.  
  186. seed = 2.0;
  187. float d = spherePerlinHalfds(bl,8.0,0.5);
  188. d = clamp(1.0-abs(d-0.5)*10.0,0.0,1.0);
  189. seed = 10.0;
  190. float mc = clamp(spherePerlinHalfds(bl,6.0,0.5)*6.0-2.4, 0.0, 1.0);
  191.  
  192. seed = 20.0;
  193. float c = spherePerlinHalfds(bl,2.0,0.5);
  194. c = c-d*0.05;
  195.  
  196. seed = 5.0;
  197. float fa = (spherePerlinA(vec3(bl,time),200.0)+spherePerlinA(vec3(bl,time+2.5),200.0))*0.5;
  198.  
  199. vec3 water = mix(vec3(0.2,0.0,0.7),vec3(0.0,0.7,1.0),c+mc*0.2);
  200.  
  201. float light = clamp(dot(snorm,lnorm),0.0,1.0);
  202. float ref = clamp(dot(reflect(ray,normalize(snorm+0.6*cross(ray,snorm)*(0.5-fa))),lnorm),0.0,1.0);
  203. ref = ref*0.9;
  204.  
  205. vec3 col = water;
  206.  
  207. float l = pow(ref,8.0)*(fa+1.0);
  208. float wl = clamp((light+pow(ref,6.0)) * mix((abs(0.5-fa)*2.0),0.0,0.3),0.0,1.0);
  209.  
  210. vec3 lcolor = l*mix(vec3(1.0),vec3(1.0,0.9,0.8),clamp(dot(ray,lnorm),0.,1.));
  211. return clamp(mix(col*light+lcolor,vec3(1.0),wl*1.0*light),vec3(0.,0.,0.),vec3(1.,1.,1.));
  212. }
  213.  
  214. float cloudTexture(vec3 realSurface,float time)
  215. {
  216. seed = 14.0;
  217. vec2 bl = vec2(atan(realSurface.x,realSurface.z),atan(length(realSurface.xz),realSurface.y))/3.1415;
  218.  
  219. float d = spherePerlinHalfd(bl, 1.0, 1.5);
  220.  
  221. float f = clamp((d*2.0-spherePerlinHalfd(bl,20.0,0.6)*0.4-0.53)*4.0,0.0,1.0);
  222. seed = 24.0;
  223.  
  224. float c = spherePerlinA(vec3(bl,time),50.0);
  225. vec2 t = twirls3D(realSurface*2.,1.,0.);
  226. t += twirls3D(realSurface*1.,1.5,0.);
  227. t += twirls3D(realSurface*6.,1.5,0.5)*0.6;
  228. f = clamp(mix(mix(f,0.0,c),t.x*mix(f,0.0,c),t.y),0.,1.);
  229. return f;
  230. }
  231.  
  232. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  233. {
  234. float res = 1.0 / iResolution.y;
  235. vec2 p = (-iResolution.xy + 2.0*fragCoord.xy) *res;
  236.  
  237. float time=iTime*1.0;
  238. vec3 ray = normalize(vec3(p,2.0));
  239. vec3 col = vec3(0.0);
  240.  
  241. float mx = iMouse.z>0.0 ? iMouse.x/iResolution.x*10.0:0.0;
  242. float my = iMouse.z>0.0 ? iMouse.y/iResolution.y*4.0-2.0:-0.6;
  243. vec2 rotate = vec2(mx,my);
  244. float protate = time*0.025;
  245. vec2 sins=sin(rotate);
  246. vec2 coss=cos(rotate);
  247. vec2 pcs=vec2(cos(protate),sin(protate));
  248. mat3 mr=mat3(vec3(coss.x,0.0,sins.x),vec3(0.0,1.0,0.0),vec3(-sins.x,0.0,coss.x));
  249. mr=mat3(vec3(1.0,0.0,0.0),vec3(0.0,coss.y,sins.y),vec3(0.0,-sins.y,coss.y))*mr;
  250. mat3 pmr=mat3(vec3(pcs.x,0.0,pcs.y),vec3(0.0,1.0,0.0),vec3(-pcs.y,0.0,pcs.x));
  251. mat3 cmr=mat3(vec3(pcs.x,0.0,pcs.y),vec3(0.0,1.0,0.0),vec3(-pcs.y,0.0,pcs.x));
  252.  
  253. float dist=3.0;
  254. float rplanet = 1.0;
  255. float ratmos = 1.15;
  256. float rcloud = 1.005;
  257. vec3 spos = vec3(0.0,0.0,3.0);
  258. vec3 lpos = vec3(1.5,1.5,0.0);
  259. lpos = mr*(lpos-spos) + spos;
  260. vec3 lnorm = normalize(lpos - spos);
  261.  
  262. float dplanet = sphereIntersect(ray,spos,rplanet).x;
  263. vec2 dcloud = sphereIntersect(ray,spos,rcloud);
  264. float datmos = sphereIntersect(ray,spos,ratmos).x;
  265. if (dplanet>0.0) {
  266. vec3 ssurface = ray*dplanet;
  267. vec3 snorm = sphereNormal(spos,ssurface);
  268. vec3 lsnorm = normalize(lpos - ssurface);
  269. float dcs = sphereIntersectIn(lnorm,spos-ssurface,rcloud);
  270. vec3 scloud = ssurface+lnorm*dcs;
  271. float cs = clamp(1.0-cloudTexture((scloud-spos)*mr*pmr*cmr,time*0.2)*2.6,0.0,1.0);
  272.  
  273. col = sphereTexture((ssurface-spos)*mr*pmr,ray,snorm,lsnorm,time);
  274. col = mix(col,col*cs,0.7);
  275. }
  276. if (datmos>0.0) {
  277. float adepth = dplanet>0.0 ? (dplanet-datmos) : abs(dot(ray,spos)-datmos);
  278. vec3 ssurface = ray*datmos;
  279. float ap = atmosphere(spos, ssurface, ray, lnorm, rplanet, ratmos, adepth);
  280. col = mix(col,vec3(0.3,0.76,1.0),ap);
  281. }
  282. if (dcloud.x>0.0) {
  283. vec3 sback = ray*dcloud.y;
  284. if (dplanet<0.0) {
  285. vec3 bnorm = sphereNormal(spos,sback);
  286. col = mix(col, vec3(0.0), cloudTexture((sback-spos)*mr*pmr*cmr,time*0.2) * clamp(dot(bnorm,lnorm),0.0,1.0));
  287. }
  288. vec3 ssurface = ray*dcloud.x;
  289. vec3 snorm = sphereNormal(spos,ssurface);
  290. vec3 lsnorm = normalize(lpos - ssurface);
  291. float a = cloudTexture((ssurface-spos)*mr*pmr*cmr,time*0.2);
  292. float light = clamp(dot(snorm,lsnorm),0.0,1.0);
  293. col = mix(col+a*light,mix(vec3(0.93,0.9,1.0),vec3(0.94,1.0,1.0),clamp(1.0*a,0.,1.)),clamp(1.5*a*light,0.,1.));
  294. }
  295. // Output to screen
  296. fragColor = vec4(col,1.0);
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement