The Code Therapy

Simple metaball in motion

A simple shader with metaball mechanics. (Port of the project made in ShaderToy)

Created by lpg2709 on Thu, 05 Jan 2023 15:44:43 GMT.


#version 300 es
precision highp float;

uniform vec2 resolution;
uniform float time;
out vec4 fragColor;

const float TAN30 = 1.0 / sqrt(3.0);

vec2 Ball1 = vec2(0.5);
vec2 Ball2 = vec2(0.5);
vec2 Ball3 = vec2(0.5);
vec2 Ball4 = vec2(0.5);
float radio = 0.03;

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  // Change position over time
  Ball1 = 0.5 + 0.1*cos(time+uv.xy+vec2(2));
  Ball2 = 0.5 + 0.2*cos(time+uv.xy+vec2(2));
  Ball3 = 0.5 + 0.3*cos(time+uv.xy+vec2(2));
  Ball4 = 0.5 + 0.4*cos(time+uv.xy+vec2(2));
  
  // Calculate distances of the pixel to the ball position
  float d1 = distance(uv, Ball1);
  float d2 = distance(uv, Ball2);
  float d3 = distance(uv, Ball3);
  float d4 = distance(uv, Ball4);
  
  // Sum all distances
  vec2 sum = vec2(radio/d1 + radio/d2 + radio/d3 + radio/d4);
    
  vec2 inf = vec2(0.3);
  // Get mouse Position
  // inf = vec2(iMouse.x / iResolution.x, iMouse.y / iResolution.y);
    
  vec3 color = vec3(sum.x*inf.x, sum.y*inf.y, inf.x*inf.y);
  // Output to screen
  fragColor = vec4(color, 1.0);
}