Skip to content

Commit dfbed9d

Browse files
committed
More algorithmic examples
1 parent 51c60d5 commit dfbed9d

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

contributed/worley_noise.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Worley Noise
2+
# After
3+
# The Coding Train / Daniel Shiffman
4+
attr_reader :points
5+
6+
NPOINTS = 20
7+
8+
def settings
9+
size(400, 400)
10+
end
11+
12+
def setup
13+
sketch_title 'Worley Noise'
14+
@points = (0..NPOINTS).map { Vec3D.new(rand(width), rand(height), rand(width)) }
15+
end
16+
17+
def draw
18+
load_pixels
19+
grid(width, height) do |x, y|
20+
distances = []
21+
NPOINTS.times do |i|
22+
v = points[i]
23+
z = frame_count % width
24+
distances << dist(x, y, z, v.x, v.y, v.z)
25+
end
26+
sorted = distances.sort!
27+
r = map1d(sorted[0], 0..150, 0..255)
28+
g = map1d(sorted[1], 0..50, 255..0)
29+
b = map1d(sorted[2], 0..200, 255..0)
30+
pixels[x + y * width] = color(r, g, b)
31+
end
32+
update_pixels
33+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
uniform vec3 iResolution; // viewport resolution (in pixels)
7+
uniform float iTime; // shader playback time (in seconds)
8+
9+
10+
float opSmoothUnion( float d1, float d2, float k )
11+
{
12+
float h = clamp( 0.5 + 0.5*(d2-d1)/k, 0.0, 1.0 );
13+
return mix( d2, d1, h ) - k*h*(1.0-h);
14+
}
15+
16+
float sdSphere( vec3 p, float s )
17+
{
18+
return length(p)-s;
19+
}
20+
21+
float map(vec3 p)
22+
{
23+
float d = 2.0;
24+
for (int i = 0; i < 16; i++) {
25+
float fi = float(i);
26+
float time = iTime * (fract(fi * 412.531 + 0.513) - 0.5) * 2.0;
27+
d = opSmoothUnion(
28+
sdSphere(p + sin(time + fi * vec3(52.5126, 64.62744, 632.25)) * vec3(2.0, 2.0, 0.8), mix(0.5, 1.0, fract(fi * 412.531 + 0.5124))),
29+
d,
30+
0.4
31+
);
32+
}
33+
return d;
34+
}
35+
36+
vec3 calcNormal( in vec3 p )
37+
{
38+
const float h = 1e-5; // or some other value
39+
const vec2 k = vec2(1,-1);
40+
return normalize( k.xyy*map( p + k.xyy*h ) +
41+
k.yyx*map( p + k.yyx*h ) +
42+
k.yxy*map( p + k.yxy*h ) +
43+
k.xxx*map( p + k.xxx*h ) );
44+
}
45+
46+
void main(void)
47+
{
48+
vec2 uv = gl_FragCoord.xy/iResolution.xy;
49+
50+
// screen size is 6m x 6m
51+
vec3 rayOri = vec3((uv - 0.5) * vec2(iResolution.x/iResolution.y, 1.0) * 6.0, 3.0);
52+
vec3 rayDir = vec3(0.0, 0.0, -1.0);
53+
54+
float depth = 0.0;
55+
vec3 p;
56+
57+
for(int i = 0; i < 64; i++) {
58+
p = rayOri + rayDir * depth;
59+
float dist = map(p);
60+
depth += dist;
61+
if (dist < 1e-6) {
62+
break;
63+
}
64+
}
65+
66+
depth = min(6.0, depth);
67+
vec3 n = calcNormal(p);
68+
float b = max(0.0, dot(n, vec3(0.577)));
69+
vec3 col = (0.5 + 0.5 * cos((b + iTime * 3.0) + uv.xyx * 2.0 + vec3(0,2,4))) * (0.85 + b * 0.35);
70+
col *= exp( -depth * 0.15 );
71+
72+
// maximum thickness is 2m in alpha channel
73+
gl_FragColor = vec4(col, 1.0 - (depth - 0.5) / 2.0);
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# from shadertoy example by Edan Kwan
2+
# https://www.shadertoy.com/view/3sySRK
3+
4+
attr_reader :previous_time, :wrapper, :start
5+
6+
def settings
7+
size(640, 360, P2D)
8+
end
9+
10+
def setup
11+
sketch_title 'Metaballs Two'
12+
@previous_time = 0.0
13+
@wrapper = load_shader(data_path('metaballs_two.glsl'))
14+
# Assume the dimension of the window will not change over time
15+
wrapper.set('iResolution', width.to_f, height.to_f, 0.0)
16+
@start = java.lang.System.current_time_millis
17+
end
18+
19+
def playback_time_seconds
20+
(java.lang.System.current_time_millis - start) / 1000.0
21+
end
22+
23+
def render_time
24+
playback_time_seconds - previous_time
25+
end
26+
27+
def draw
28+
wrapper.set('iTime', playback_time_seconds)
29+
@previous_time = playback_time_seconds
30+
# Apply the specified shader to any geometry drawn from this point
31+
shader(wrapper)
32+
# Draw the output of the shader onto a rectangle that covers the whole viewport.
33+
rect(0, 0, width, height)
34+
end

0 commit comments

Comments
 (0)