p5.shader 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 angle;
  4. uniform sampler2D iChannel0;
  5. uniform float iTime;
  6. //credit //https://www.shadertoy.com/view/4sd3zf
  7. //moon https://opengameart.org/content/moon
  8. mat3 rotx(float a) { mat3 rot; rot[0] = vec3(1.0, 0.0, 0.0); rot[1] = vec3(0.0, cos(a), -sin(a)); rot[2] = vec3(0.0, sin(a), cos(a)); return rot; }
  9. mat3 roty(float a) { mat3 rot; rot[0] = vec3(cos(a), 0.0, sin(a)); rot[1] = vec3(0.0, 1.0, 0.0); rot[2] = vec3(-sin(a), 0.0, cos(a)); return rot; }
  10. mat3 rotz(float a) { mat3 rot; rot[0] = vec3(cos(a), -sin(a), 0.0); rot[1] = vec3(sin(a), cos(a), 0.0); rot[2] = vec3(0.0, 0.0, 1.0); return rot; }
  11. vec2 uv_sphere(vec3 v)
  12. {
  13. float pi = 3.1415926536;
  14. return vec2(0.5 + atan(v.z, v.x) / (2.0 * pi), acos(v.y) / pi);
  15. }
  16. float hash(float n) { return fract(sin(n)*753.5453123); }
  17. float noise(vec3 x)
  18. {
  19. vec3 p = floor(x);
  20. vec3 f = fract(x);
  21. f = f*f*(3.0-2.0*f);
  22. float n = p.x + p.y*157.0 + 113.0*p.z;
  23. return mix(mix(mix( hash(n+ 0.0), hash(n+ 1.0),f.x),
  24. mix( hash(n+157.0), hash(n+158.0),f.x),f.y),
  25. mix(mix( hash(n+113.0), hash(n+114.0),f.x),
  26. mix( hash(n+270.0), hash(n+271.0),f.x),f.y),f.z)-0.4;
  27. }
  28. float clouds(vec3 p) {
  29. vec3 q = p+vec3(-0.1,0.37,1.0)*2.0*iTime+vec3(0.0, sin(p.y)*100.0, 0.0);
  30. float v = 0.0;
  31. v += 0.550 * noise(q*0.051);
  32. v += 0.250 * noise(q*0.111);
  33. v += 0.125 * noise(q*0.211);
  34. return v;
  35. }
  36. vec3 sky(vec3 rd, vec2 scr_uv)
  37. {
  38. vec3 ord=rd;
  39. vec2 PX_RES=vec2(320.0,200.0);
  40. rd=floor(rd*PX_RES.y)/PX_RES.y;
  41. scr_uv=floor(scr_uv*PX_RES)/PX_RES;
  42. vec3 col;
  43. float t = (20.0 ) / rd.y;
  44. float colv = 0.0;
  45. if (rd.y>0.) {
  46. // clouds
  47. float cloudA = 1.0;
  48. cloudA *= pow(smoothstep(0.0, 1.0, 90.0/t), 1.5); // lower dim
  49. cloudA *= pow((smoothstep(0.0, 1.0, t/35.0)), 1.5); //upper dim
  50. colv += cloudA * smoothstep(0.0, 0.8, 0.2+clouds(t *1.3* rd));
  51. }
  52. colv*=1.5;
  53. colv *= min(1.0, colv);
  54. const float step1 = 1.0/4.;
  55. float testvar = mod(colv, step1);
  56. bool xgrid = mod(scr_uv.x*PX_RES.x, 2.0)<1.0;
  57. bool ygrid = mod(scr_uv.y*PX_RES.y, 2.0)<1.0;
  58. // dither
  59. colv += testvar>step1*0.20 && xgrid && ygrid ? step1:0.0;
  60. colv += testvar>step1*0.40 && !xgrid && !ygrid ? step1:0.0;
  61. colv += testvar>step1*0.60 && xgrid && !ygrid ? step1:0.0;
  62. colv += testvar>step1*0.80 && !xgrid && ygrid ? step1:0.0;
  63. //draw moon texture on position
  64. ord=rd;
  65. vec3 ta=angle.zyx;
  66. ta.y+=1.571;
  67. ord*=rotx(ta.x);
  68. ord*=roty(ta.y);
  69. ord*=rotz(ta.z);
  70. vec2 tuv=uv_sphere(ord);
  71. tuv=(tuv-0.5)*15.+0.5;
  72. tuv.x*=2.;
  73. if((abs(tuv.x-0.5)<.5)&&(abs(tuv.y-0.5)<.5)){
  74. vec4 tx=texture(iChannel0,tuv);
  75. colv=mix(colv,colv*(1.-tx.a)+tx.r*tx.a*1.3,clamp(colv,0.,1.))+tx.r*tx.a*0.25;
  76. }
  77. col=vec3(colv);
  78. col *= vec3(0.8, 0.9, 1.84);
  79. col += vec3(0.1, 0.15, 0.3);
  80. if(rd.y<0.)rd.y*=0.52;
  81. float hor=1.-max(abs(rd.y),0.);
  82. float hor_power=pow(hor,106.0); //horizont, remove if not needed
  83. if(rd.y<0.)
  84. col=mix(col+col*hor_power,vec3(col)*0.5+col*hor_power,1.-hor_power);
  85. else col+=col*hor_power;
  86. return clamp(col,0.,1.);
  87. }
  88. void fragment() {
  89. vec3 rd=normalize(((CAMERA_MATRIX*vec4(normalize(-VERTEX),0.0)).xyz));
  90. rd=-rd;
  91. vec3 sky_col=sky(rd,SCREEN_UV);
  92. ALBEDO=sky_col;
  93. //ALBEDO=ALBEDO*ALBEDO; //to use in Godot GLES3 add this color correction
  94. }