Advertisement
Zunesha

Shader Água movimentada piscina godot 4

Sep 9th, 2024 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 11.48 KB | None | 0 0
  1. Shader Água movimentada piscina godot 4
  2.  
  3. shader_type canvas_item;
  4.  
  5.  
  6. uniform vec4 WATER_COL : source_color =  vec4(0.176471, 0.635294, 0.85098, 1.0);
  7. uniform vec4 WATER2_COL : source_color =  vec4(0.0588235, 0.529412, 0.776471, 1.0);
  8. uniform vec4 FOAM_COL : source_color = vec4(0.87451, 0.988235, 1, 1.0);
  9.  
  10. uniform float distortion_speed = 2.0;
  11. uniform vec2 tile = vec2(5.0, 5.0);
  12. uniform vec2 offset = vec2(0, 0);
  13.  
  14. const float TWOPI = 6.283185307;
  15. const float SIXPI = 18.84955592;
  16.  
  17.  
  18. float circ(vec2 pos, vec2 c, float s)
  19. {
  20.     c = abs(pos - c);
  21.     c = min(c, 1.0 - c);
  22.  
  23.     return smoothstep(0.0, 0.002, sqrt(s) - sqrt(dot(c, c))) * -1.0;
  24. }
  25.  
  26.  
  27. float waterlayer(vec2 uv)
  28. {
  29.     uv = mod(uv, 1.0); // Clamp to [0..1]
  30.     float ret = 1.0;
  31.     ret += circ(uv, vec2(0.37378, 0.277169), 0.0268181);
  32.     ret += circ(uv, vec2(0.0317477, 0.540372), 0.0193742);
  33.     ret += circ(uv, vec2(0.430044, 0.882218), 0.0232337);
  34.     ret += circ(uv, vec2(0.641033, 0.695106), 0.0117864);
  35.     ret += circ(uv, vec2(0.0146398, 0.0791346), 0.0299458);
  36.     ret += circ(uv, vec2(0.43871, 0.394445), 0.0289087);
  37.     ret += circ(uv, vec2(0.909446, 0.878141), 0.028466);
  38.     ret += circ(uv, vec2(0.310149, 0.686637), 0.0128496);
  39.     ret += circ(uv, vec2(0.928617, 0.195986), 0.0152041);
  40.     ret += circ(uv, vec2(0.0438506, 0.868153), 0.0268601);
  41.     ret += circ(uv, vec2(0.308619, 0.194937), 0.00806102);
  42.     ret += circ(uv, vec2(0.349922, 0.449714), 0.00928667);
  43.     ret += circ(uv, vec2(0.0449556, 0.953415), 0.023126);
  44.     ret += circ(uv, vec2(0.117761, 0.503309), 0.0151272);
  45.     ret += circ(uv, vec2(0.563517, 0.244991), 0.0292322);
  46.     ret += circ(uv, vec2(0.566936, 0.954457), 0.00981141);
  47.     ret += circ(uv, vec2(0.0489944, 0.200931), 0.0178746);
  48.     ret += circ(uv, vec2(0.569297, 0.624893), 0.0132408);
  49.     ret += circ(uv, vec2(0.298347, 0.710972), 0.0114426);
  50.     ret += circ(uv, vec2(0.878141, 0.771279), 0.00322719);
  51.     ret += circ(uv, vec2(0.150995, 0.376221), 0.00216157);
  52.     ret += circ(uv, vec2(0.119673, 0.541984), 0.0124621);
  53.     ret += circ(uv, vec2(0.629598, 0.295629), 0.0198736);
  54.     ret += circ(uv, vec2(0.334357, 0.266278), 0.0187145);
  55.     ret += circ(uv, vec2(0.918044, 0.968163), 0.0182928);
  56.     ret += circ(uv, vec2(0.965445, 0.505026), 0.006348);
  57.     ret += circ(uv, vec2(0.514847, 0.865444), 0.00623523);
  58.     ret += circ(uv, vec2(0.710575, 0.0415131), 0.00322689);
  59.     ret += circ(uv, vec2(0.71403, 0.576945), 0.0215641);
  60.     ret += circ(uv, vec2(0.748873, 0.413325), 0.0110795);
  61.     ret += circ(uv, vec2(0.0623365, 0.896713), 0.0236203);
  62.     ret += circ(uv, vec2(0.980482, 0.473849), 0.00573439);
  63.     ret += circ(uv, vec2(0.647463, 0.654349), 0.0188713);
  64.     ret += circ(uv, vec2(0.651406, 0.981297), 0.00710875);
  65.     ret += circ(uv, vec2(0.428928, 0.382426), 0.0298806);
  66.     ret += circ(uv, vec2(0.811545, 0.62568), 0.00265539);
  67.     ret += circ(uv, vec2(0.400787, 0.74162), 0.00486609);
  68.     ret += circ(uv, vec2(0.331283, 0.418536), 0.00598028);
  69.     ret += circ(uv, vec2(0.894762, 0.0657997), 0.00760375);
  70.     ret += circ(uv, vec2(0.525104, 0.572233), 0.0141796);
  71.     ret += circ(uv, vec2(0.431526, 0.911372), 0.0213234);
  72.     ret += circ(uv, vec2(0.658212, 0.910553), 0.000741023);
  73.     ret += circ(uv, vec2(0.514523, 0.243263), 0.0270685);
  74.     ret += circ(uv, vec2(0.0249494, 0.252872), 0.00876653);
  75.     ret += circ(uv, vec2(0.502214, 0.47269), 0.0234534);
  76.     ret += circ(uv, vec2(0.693271, 0.431469), 0.0246533);
  77.     ret += circ(uv, vec2(0.415, 0.884418), 0.0271696);
  78.     ret += circ(uv, vec2(0.149073, 0.41204), 0.00497198);
  79.     ret += circ(uv, vec2(0.533816, 0.897634), 0.00650833);
  80.     ret += circ(uv, vec2(0.0409132, 0.83406), 0.0191398);
  81.     ret += circ(uv, vec2(0.638585, 0.646019), 0.0206129);
  82.     ret += circ(uv, vec2(0.660342, 0.966541), 0.0053511);
  83.     ret += circ(uv, vec2(0.513783, 0.142233), 0.00471653);
  84.     ret += circ(uv, vec2(0.124305, 0.644263), 0.00116724);
  85.     ret += circ(uv, vec2(0.99871, 0.583864), 0.0107329);
  86.     ret += circ(uv, vec2(0.894879, 0.233289), 0.00667092);
  87.     ret += circ(uv, vec2(0.246286, 0.682766), 0.00411623);
  88.     ret += circ(uv, vec2(0.0761895, 0.16327), 0.0145935);
  89.     ret += circ(uv, vec2(0.949386, 0.802936), 0.0100873);
  90.     ret += circ(uv, vec2(0.480122, 0.196554), 0.0110185);
  91.     ret += circ(uv, vec2(0.896854, 0.803707), 0.013969);
  92.     ret += circ(uv, vec2(0.292865, 0.762973), 0.00566413);
  93.     ret += circ(uv, vec2(0.0995585, 0.117457), 0.00869407);
  94.     ret += circ(uv, vec2(0.377713, 0.00335442), 0.0063147);
  95.     ret += circ(uv, vec2(0.506365, 0.531118), 0.0144016);
  96.     ret += circ(uv, vec2(0.408806, 0.894771), 0.0243923);
  97.     ret += circ(uv, vec2(0.143579, 0.85138), 0.00418529);
  98.     ret += circ(uv, vec2(0.0902811, 0.181775), 0.0108896);
  99.     ret += circ(uv, vec2(0.780695, 0.394644), 0.00475475);
  100.     ret += circ(uv, vec2(0.298036, 0.625531), 0.00325285);
  101.     ret += circ(uv, vec2(0.218423, 0.714537), 0.00157212);
  102.     ret += circ(uv, vec2(0.658836, 0.159556), 0.00225897);
  103.     ret += circ(uv, vec2(0.987324, 0.146545), 0.0288391);
  104.     ret += circ(uv, vec2(0.222646, 0.251694), 0.00092276);
  105.     ret += circ(uv, vec2(0.159826, 0.528063), 0.00605293);
  106.     return max(ret, 0.0);
  107. }
  108.  
  109. vec3 water(vec2 uv, vec3 cdir, float iTime)
  110. {
  111.     uv *= vec2(0.25);
  112.    
  113.    
  114.  
  115.     // Parallax height distortion with two directional waves at
  116.     // slightly different angles.
  117.     vec2 a = 0.025 * cdir.xz / cdir.y; // Parallax offset
  118.     float h = sin(uv.x + iTime); // Height at UV
  119.     uv += a * h;
  120.     h = sin(0.841471 * uv.x - 0.540302 * uv.y + iTime);
  121.     uv += a * h;
  122.    
  123.     // Texture distortion
  124.     float d1 = mod(uv.x + uv.y, TWOPI);
  125.     float d2 = mod((uv.x + uv.y + 0.25) * 1.3, SIXPI);
  126.     d1 = iTime * 0.07 + d1;
  127.     d2 = iTime * 0.5 + d2;
  128.     vec2 dist = vec2(
  129.         sin(d1) * 0.15 + sin(d2) * 0.05,
  130.         cos(d1) * 0.15 + cos(d2) * 0.05
  131.     );
  132.    
  133.     vec3 ret = mix(WATER_COL.rgb, WATER2_COL.rgb, waterlayer(uv + dist.xy));
  134.     ret = mix(ret, FOAM_COL.rgb, waterlayer(vec2(1.0) - uv - dist.yx));
  135.     return ret;
  136. }
  137.  
  138. varying vec2 globalposition;
  139. void vertex(){
  140.     globalposition = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
  141. }
  142.  
  143. void fragment() {
  144.     vec2 uv = globalposition / 32.0;
  145.     COLOR.rgb = vec3(water(uv * tile + offset, vec3(0,1,0), TIME * distortion_speed));
  146. }
  147.  
  148. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OUTRO SHADER INTERESSANTE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  149. shader_type canvas_item;
  150.  
  151. uniform int OCTAVE = 6;
  152. uniform float mulscale = 5.0;
  153. uniform float height = 0.6;
  154. uniform float tide = 0.1;
  155. uniform float foamthickness = 0.1;
  156. uniform float timescale = 1.0;
  157. uniform float waterdeep = 0.3;
  158. uniform vec4 WATER_COL : source_color =  vec4(0.04, 0.38, 0.88, 1.0);
  159. uniform vec4 WATER2_COL : source_color  =  vec4(0.04, 0.35, 0.78, 1.0);
  160. uniform vec4 FOAM_COL : source_color  = vec4(0.8125, 0.9609, 0.9648, 1.0);
  161.  
  162.  
  163. float rand(vec2 input){
  164.     return fract(sin(dot(input,vec2(23.53,44.0)))*42350.45);
  165. }
  166.  
  167. float perlin(vec2 input){
  168.     vec2 i = floor(input);
  169.     vec2 j = fract(input);
  170.     vec2 coord = smoothstep(0.,1.,j);
  171.    
  172.     float a = rand(i);
  173.     float b = rand(i+vec2(1.0,0.0));
  174.     float c = rand(i+vec2(0.0,1.0));
  175.     float d = rand(i+vec2(1.0,1.0));
  176.  
  177.     return mix(mix(a,b,coord.x),mix(c,d,coord.x),coord.y);
  178. }
  179.  
  180. float fbm(vec2 input){
  181.     float value = 0.0;
  182.     float scale = 0.5;
  183.    
  184.     for(int i = 0; i < OCTAVE; i++){
  185.         value += perlin(input)*scale;
  186.         input*=2.0;
  187.         scale*=0.5;
  188.     }
  189.     return value;
  190. }
  191.  
  192. void fragment(){
  193.     float newtime = TIME*timescale;
  194.     float fbmval = fbm(vec2(UV.x*mulscale+0.2*sin(0.3*newtime)+0.15*newtime,-0.05*newtime+UV.y*mulscale+0.1*cos(0.68*newtime)));
  195.     float fbmvalshadow = fbm(vec2(UV.x*mulscale+0.2*sin(-0.6*newtime + 25.0 * UV.y)+0.15*newtime+3.0,-0.05*newtime+UV.y*mulscale+0.13*cos(-0.68*newtime))-7.0+0.1*sin(0.43*newtime));
  196.     float myheight = height+tide*sin(newtime+5.0*UV.x-8.0*UV.y);
  197.     float shadowheight = height+tide*1.3*cos(newtime+2.0*UV.x-2.0*UV.y);
  198.     float withinFoam = step(myheight, fbmval)*step(fbmval, myheight + foamthickness);
  199.     float shadow = (1.0-withinFoam)*step(shadowheight, fbmvalshadow)*step(fbmvalshadow, shadowheight + foamthickness * 0.7);
  200.     COLOR = withinFoam*FOAM_COL + shadow*WATER2_COL + ((1.0-withinFoam)*(1.0-shadow))*WATER_COL;
  201.     //COLOR = vec4(1.0,1.0,1.0,fbmval);
  202. }
  203.  
  204.  
  205. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OUTRO SHADER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  206.  
  207. shader_type canvas_item;
  208.  
  209. #define iResolution 1.0/SCREEN_PIXEL_SIZE
  210. #define iTime TIME
  211. #define fragColor COLOR
  212.  
  213. uniform float uv_scale : hint_range(0.0, 10.0, 0.1) = 1.0;
  214. uniform float color_alpha : hint_range(0.0, 1.0, 0.1) = 1.0;
  215.  
  216. vec2 hash( vec2 p ) // replace this by something better
  217. {
  218.     p = vec2( dot(p,vec2(127.1,311.7)), dot(p,vec2(269.5,183.3)) );
  219.     return -1.0 + 2.0*fract(sin(p)*43758.5453123);
  220. }
  221.  
  222. float noise( in vec2 p )
  223. {
  224.     const float K1 = 0.366025404; // (sqrt(3)-1)/2;
  225.     const float K2 = 0.211324865; // (3-sqrt(3))/6;
  226.  
  227.     vec2  i = floor( p + (p.x+p.y)*K1 );
  228.     vec2  a = p - i + (i.x+i.y)*K2;
  229.     float m = step(a.y,a.x);
  230.     vec2  o = vec2(m,1.0-m);
  231.     vec2  b = a - o + K2;
  232.     vec2  c = a - 1.0 + 2.0*K2;
  233.     vec3  h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 );
  234.     vec3  n = h*h*h*h*vec3( dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1.0)));
  235.     return dot( n, vec3(70.0) );
  236. }
  237.  
  238. #define MAX_WAVES 4
  239. #define SUPERPOSITION 4
  240. #define TAU 6.28318
  241. #define PHI 1.618
  242.  
  243. // closed form normal would increase performance a lot
  244. float height(vec2 p, float t) {
  245.     float acc = 0.0;
  246.     for (int i = 0; i < MAX_WAVES; i++) {
  247.     for (int j = 0; j < SUPERPOSITION; j++) {
  248.         int seed = i + 5*j;
  249.         //float theta = TAU * noise(vec2(0.01 * t, 10.0*float(i)));
  250.         float theta = TAU * PHI * 10.0*float(seed);
  251.         float up = cos(theta) * p.x - sin(theta) * p.y;
  252.         float vp = sin(theta) * p.x + cos(theta) * p.y;
  253.         //float initial_phase = TAU * noise(vec2(0.0, 2.0*float(i))) + cos(vp);
  254.         float initial_phase = TAU * PHI * float(seed);
  255.         //float k = pow(2.0, float(i)*0.1) + 0.5;
  256.         //float k = pow(2.0, 1.0 + 0.4*float(i));
  257.         float k = pow(2.0, float(i));
  258.         //float k = float(i+1);
  259.         float phase = initial_phase + up*k + cos(vp) + 3.0*t + 0.5*k*t;
  260.         // its kinda like choose your artifacts, probably use noise for vp
  261.         float A = cos(phase)/(k*k);
  262.         acc += A;
  263.     }}
  264.     return acc;
  265. }
  266. vec4 hn_fdm(vec2 p, float t) {
  267.   float h = height(p, t);
  268.   vec2 vx = vec2(0.1, 0.0);
  269.   vec2 vy = vec2(0.0, 0.1);
  270.   float hx = height(p+vx, t);
  271.   float hy = height(p+vy, t);
  272.   float dx = (hx - h);
  273.   float dy = (hy - h);
  274.   // vec3 norm = normalize(vec3(-dx, -dy, dx+dy));
  275.   // vec3 norm = normalize(vec3(-dx/vx.x, -dy/vy.y, 1.0));
  276.  
  277.   vec3 v1 = normalize(vec3(vx.x, 0.0, dx));
  278.   vec3 v2 = normalize(vec3(0.0, vy.y, dy));
  279.   vec3 norm = cross(v1, v2);
  280.  
  281.   return vec4(norm, h);
  282. }
  283.  
  284. void fragment()
  285. {
  286.     vec2 uv = UV;
  287.     vec2 uv_screen = (uv - 0.5) * uv_scale;
  288.  
  289.     vec4 nh = hn_fdm(uv_screen* 10.0, iTime * 1.0);
  290.     float h = nh.w;
  291.     vec3 norm = nh.xyz;
  292.     vec3 sun_dir = normalize(vec3(-0.2, 0.4, 1.0));
  293.  
  294.     vec4 water_colour = vec4(0.2, 0.4, 0.6, 1.0);
  295.     vec4 foam_colour = vec4(0.8, 0.9, 1.0, 1.0);
  296.     vec4 sky_colour = vec4(0.2, 0.6, 0.8, 1.0);
  297.     vec4 specular_colour = vec4(1.0, 1.0, 1.0, 1.0);
  298.  
  299.     //fragColor = vec4(norm.xyz, 1.0); return;
  300.    
  301.     if (dot(sun_dir, norm) > 0.98) {
  302.         fragColor = specular_colour;
  303.     } else {
  304.         fragColor = mix(water_colour, sky_colour, dot(norm, normalize(vec3(0.0, 0.2, 1.0))));
  305.     }
  306.     fragColor.a = color_alpha;
  307. }
  308.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement