Skip to content

Commit 51c60d5

Browse files
committed
Metaballs by atomek
1 parent b2dc547 commit 51c60d5

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
/** ----------------------
7+
After a sketch on shadertoy https://www.shadertoy.com/user/atomek
8+
**/
9+
10+
uniform vec3 iResolution; // viewport resolution (in pixels)
11+
uniform float iTime; // shader playback time (in seconds)
12+
uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click
13+
14+
void main()
15+
{
16+
// Hold the mouse and drag to adjust
17+
18+
float ratio = iResolution.y / iResolution.x;
19+
float divider = float(iMouse.x / iResolution.x * 10.0) + 1.0;
20+
float intensity = float(iMouse.y / iResolution.y * 10.0) + 1.0;
21+
22+
float coordX = gl_FragCoord.x / iResolution.x;
23+
float coordY = gl_FragCoord.y / iResolution.x;
24+
25+
float ball1x = sin(iTime * 2.1) * 0.5 + 0.5;
26+
float ball1y = cos(iTime * 1.0) * 0.5 + 0.5;
27+
float ball1z = sin(iTime * 2.0) * 0.1 + 0.2;
28+
29+
float ball2x = sin(iTime * 1.0) * 0.5 + 0.5;
30+
float ball2y = cos(iTime * 1.8) * 0.5 + 0.5;
31+
float ball2z = cos(iTime * 2.0) * 0.1 + 0.2;
32+
33+
float ball3x = sin(iTime * 0.7) * 0.5 + 0.5;
34+
float ball3y = cos(iTime * 1.5) * 0.5 + 0.5;
35+
float ball3z = cos(iTime * 1.0) * 0.1 + 0.2;
36+
37+
vec3 ball1 = vec3(ball1x, ball1y * ratio, ball1z);
38+
vec3 ball2 = vec3(ball2x, ball2y * ratio, ball2z);
39+
vec3 ball3 = vec3(ball3x, ball3y * ratio, ball3z);
40+
41+
float sum = 0.0;
42+
sum += ball1.z / distance(ball1.xy, vec2(coordX, coordY));
43+
sum += ball2.z / distance(ball2.xy, vec2(coordX, coordY));
44+
sum += ball3.z / distance(ball3.xy, vec2(coordX, coordY));
45+
46+
sum = pow(sum / intensity, divider);
47+
48+
gl_FragColor = vec4(sum * 0.2, sum * 0.2, sum * 0.4, 1.0);
49+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
attr_reader :last_mouse_position, :mouse_click_state, :mouse_dragged, :shader_t
2+
attr_reader :start
3+
4+
def settings
5+
size(640, 360, P2D)
6+
end
7+
8+
def setup
9+
sketch_title 'Shadertoy Metaballs'
10+
@previous_time = 0.0
11+
@mouse_dragged = false
12+
@mouse_click_state = 0.0
13+
# Load the shader file from the "data" folder
14+
@shader_t = load_shader(data_path('metaballs.glsl'))
15+
# Assume the dimension of the window will not change over time
16+
shader_t.set('iResolution', width.to_f, height.to_f, 0.0)
17+
@last_mouse_position = Vec2D.new(mouse_x, mouse_y)
18+
@start = java.lang.System.current_time_millis
19+
end
20+
21+
def draw
22+
# shader playback time (in seconds)
23+
current_time = (java.lang.System.current_time_millis - start) / 1000.0
24+
shader_t.set('iTime', current_time)
25+
if mouse_pressed?
26+
@last_mouse_position = Vec2D.new(mouse_x, mouse_y)
27+
@mouse_click_state = 1.0
28+
else
29+
@mouse_click_state = 0.0
30+
end
31+
shader_t.set('iMouse', last_mouse_position.x, last_mouse_position.y, mouse_click_state, mouse_click_state)
32+
# Apply the specified shader to any geometry drawn from this point
33+
shader(shader_t)
34+
# Draw the output of the shader onto a rectangle that covers the whole viewport.
35+
rect(0, 0, width, height)
36+
end

0 commit comments

Comments
 (0)