Metaballs
SetErrorMode(2)
// set window properties
SetWindowTitle( "Metaballs" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
CreateSprite(1,0)
SetSpriteSize(1,1024,768)
LoadSpriteShader(2, "metaballs.ps")
SetShaderConstantByName(2,"iResolution",1024,768,0,0)
SetSpriteShader( 1,2 )
do
SetShaderConstantByName(2,"iMouse",GetPointerX(),GetPointerY(),0,0)
SetShaderConstantByName(2,"iGlobalTime",timer(),0,0,0)
sync()
loop
metaballs.ps
#define occlusion_enabled
#define occlusion_quality 4
//#define occlusion_preview
#define noise_use_smoothstep
#define light_color vec3(0.1,0.4,0.6)
#define light_direction normalize(vec3(.2,1.0,-0.2))
#define light_speed_modifier 1.0
#define object_color vec3(0.9,0.1,0.1)
#define object_count 9
#define object_speed_modifier 1.0
#define render_steps 33
uniform mediump float iGlobalTime;
uniform mediump vec2 iResolution;
uniform mediump vec2 iMouse;
mediump float hash(float x)
{
return fract(sin(x*.0127863)*17143.321);
}
mediump float hash(vec2 x)
{
return fract(cos(dot(x.xy,vec2(2.31,53.21))*124.123)*412.0);
}
mediump vec3 cc(vec3 color, float factor,float factor2) //a wierd color modifier
{
mediump float w = color.x+color.y+color.z;
return mix(color,vec3(w)*factor,w*factor2);
}
mediump float hashmix(float x0, float x1, float interp)
{
x0 = hash(x0);
x1 = hash(x1);
#ifdef noise_use_smoothstep
interp = smoothstep(0.0,1.0,interp);
#endif
return mix(x0,x1,interp);
}
mediump float noise(float p) // 1D noise
{
mediump float pm = mod(p,1.0);
mediump float pd = p-pm;
return hashmix(pd,pd+1.0,pm);
}
mediump vec3 rotate_y(vec3 v, float angle)
{
mediump float ca = cos(angle); mediump float sa = sin(angle);
return v*mat3(
+ca, +.0, -sa,
+.0,+1.0, +.0,
+sa, +.0, +ca);
}
mediump vec3 rotate_x(vec3 v, float angle)
{
mediump float ca = cos(angle); mediump float sa = sin(angle);
return v*mat3(
+1.0, +.0, +.0,
+.0, +ca, -sa,
+.0, +sa, +ca);
}
mediump float max3(float a, float b, float c)//returns the maximum of 3 values
{
return max(a,max(b,c));
}
mediump vec3 bpos[object_count];//position for each metaball
mediump float dist(vec3 p)//distance function
{
mediump float d=1024.0;
mediump float nd;
for (int i=0 ;i<object_count; i++)
{
mediump vec3 np = p+bpos[i];
mediump float shape0 = max3(abs(np.x),abs(np.y),abs(np.z))-1.0;
mediump float shape1 = length(np)-1.0;
nd = shape0+(shape1-shape0)*2.0;
d = mix(d,nd,smoothstep(-1.0,+1.0,d-nd));
}
return d;
}
mediump vec3 normal(vec3 p,float e) //returns the normal, uses the distance function
{
mediump float d=dist(p);
return normalize(vec3(dist(p+vec3(e,0,0))-d,dist(p+vec3(0,e,0))-d,dist(p+vec3(0,0,e))-d));
}
mediump vec3 light = light_direction; //global variable that holds light direction
mediump vec3 background(vec3 d)//render background
{
mediump float t=iGlobalTime*0.5*light_speed_modifier;
mediump float qq = dot(d,light)*.5+.5;
mediump float bgl = qq;
mediump float q = (bgl+noise(bgl*6.0+t)*.85+noise(bgl*12.0+t)*.85);
q+= pow(qq,32.0)*2.0;
mediump vec3 sky = vec3(0.1,0.4,0.6)*q;
return sky;
}
mediump float occlusion(vec3 p, vec3 d)//returns how much a point is visible from a given direction
{
mediump float occ = 1.0;
p=p+d;
for (int i=0; i<occlusion_quality; i++)
{
mediump float dd = dist(p);
p+=d*dd;
occ = min(occ,dd);
}
return max(.0,occ);
}
mediump vec3 object_material(vec3 p, vec3 d)
{
mediump vec3 color = normalize(object_color*light_color);
mediump vec3 n = normal(p,0.1);
mediump vec3 r = reflect(d,n);
mediump float reflectance = dot(d,r)*.5+.5;reflectance=pow(reflectance,2.0);
mediump float diffuse = dot(light,n)*.5+.5; diffuse = max(.0,diffuse);
#ifdef occlusion_enabled
mediump float oa = occlusion(p,n)*.4+.6;
mediump float od = occlusion(p,light)*.95+.05;
mediump float os = occlusion(p,r)*.95+.05;
#else
mediump float oa=1.0;
mediump float ob=1.0;
mediump float oc=1.0;
#endif
#ifndef occlusion_preview
color =
color*oa*.2 + //ambient
color*diffuse*od*.7 + //diffuse
background(r)*os*reflectance*.7; //reflection
#else
color=vec3((oa+od+os)*.3);
#endif
return color;
}
#define offset1 4.7
#define offset2 4.6
void main()
{
mediump vec2 uv = gl_FragCoord.xy / iResolution.xy - 0.5;
uv.x *= iResolution.x/iResolution.y; //fix aspect ratio
mediump vec2 mouse = iMouse.xy / iResolution.xy;
mediump float t = iGlobalTime*.5*object_speed_modifier + 2.0;
for (int i=0 ;i<object_count; i++) //position for each metaball
{
bpos[i] = 1.3*vec3(
sin(t*0.967+float(i)*42.0),
sin(t*.423+float(i)*152.0),
sin(t*.76321+float(i)));
}
//setup the camera
mediump vec3 p = vec3(.0,0.0,-4.0);
p = rotate_x(p,mouse.y*9.0+offset1);
p = rotate_y(p,mouse.x*9.0+offset2);
mediump vec3 d = vec3(uv,1.0);
d.z -= length(d)*.5; //lens distort
d = normalize(d);
d = rotate_x(d,mouse.y*9.0+offset1);
d = rotate_y(d,mouse.x*9.0+offset2);
//and action!
mediump float dd;
mediump vec3 color;
for (int i=0; i<render_steps; i++) //raymarch
{
dd = dist(p);
p+=d*dd*.7;
if (dd<.04 || dd>4.0) break;
}
if (dd<0.5) //close enough
color = object_material(p,d);
else
color = background(d);
//post procesing
color *=.85;
color = mix(color,color*color,0.3);
color -= hash(color.xy+uv.xy)*.015;
color -= length(uv)*.1;
color =cc(color,.5,.6);
gl_FragColor = vec4(color,1.0);
}