Skip to content

Commit 66ba294

Browse files
committed
mandelbub
1 parent 15714cd commit 66ba294

File tree

4 files changed

+285
-3
lines changed

4 files changed

+285
-3
lines changed

contributed/data/java_args.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

contributed/decagon_grid.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Example of a grid of decagons with perlin noise after Lenny Herzog
2+
3+
load_library :pdf
4+
NOISE_STRENGTH = 80.0
5+
THETA = 36
6+
attr_reader :version, :save, :noise_generator
7+
8+
def setup
9+
sketch_title 'Decagon Grid'
10+
frame_rate 24
11+
@version = 0
12+
@save = false
13+
@noise_generator = lambda do |x, y, seed|
14+
NOISE_STRENGTH * noise(
15+
x / 150.0,
16+
y / 150.0 + seed * 2,
17+
seed
18+
) - 100
19+
end
20+
end
21+
22+
def draw
23+
begin_record(PDF, data_path("Line_#{version}.pdf")) if save
24+
background(255)
25+
no_fill
26+
stroke(0)
27+
stroke_weight(1)
28+
grid(height + 100, width + 100, 50, 50) do |cy, cx|
29+
begin_shape
30+
(0..360).step(THETA) do |angle|
31+
x = (DegLut.cos(angle) * 60) + cx
32+
y = (DegLut.sin(angle) * 60) + cy
33+
noise_value = noise_generator.call(x, y, millis / 5_000.0)
34+
x += noise_value
35+
y += noise_value
36+
vertex(x, y)
37+
end
38+
end_shape(CLOSE)
39+
end
40+
return unless save
41+
42+
end_record
43+
@version += 1
44+
@save = false
45+
end
46+
47+
def mouse_pressed
48+
@save = true
49+
end
50+
51+
def settings
52+
size(1000, 1000)
53+
end
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
// ----------------------
7+
// SHADERTOY UNIFORMS -
8+
// ----------------------
9+
10+
uniform vec3 iResolution; // viewport resolution (in pixels)
11+
uniform float iTime; // shader playback time (in seconds)
12+
13+
float stime, ctime;
14+
void ry(inout vec3 p, float a){
15+
float c,s;vec3 q=p;
16+
c = cos(a); s = sin(a);
17+
p.x = c * q.x + s * q.z;
18+
p.z = -s * q.x + c * q.z;
19+
}
20+
21+
float pixel_size = 0.0;
22+
23+
/*
24+
25+
z = r*(sin(theta)cos(phi) + i cos(theta) + j sin(theta)sin(phi)
26+
27+
zn+1 = zn^8 +c
28+
29+
z^8 = r^8 * (sin(8*theta)*cos(8*phi) + i cos(8*theta) + j sin(8*theta)*sin(8*theta)
30+
31+
zn+1' = 8 * zn^7 * zn' + 1
32+
33+
*/
34+
35+
vec3 mb(vec3 p) {
36+
p.xyz = p.xzy;
37+
vec3 z = p;
38+
vec3 dz=vec3(0.0);
39+
float power = 8.0;
40+
float r, theta, phi;
41+
float dr = 1.0;
42+
43+
float t0 = 1.0;
44+
for(int i = 0; i < 7; ++i) {
45+
r = length(z);
46+
if(r > 2.0) continue;
47+
theta = atan(z.y / z.x);
48+
#ifdef phase_shift_on
49+
phi = asin(z.z / r) + iTime*0.1;
50+
#else
51+
phi = asin(z.z / r);
52+
#endif
53+
54+
dr = pow(r, power - 1.0) * dr * power + 1.0;
55+
56+
r = pow(r, power);
57+
theta = theta * power;
58+
phi = phi * power;
59+
60+
z = r * vec3(cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi)) + p;
61+
62+
t0 = min(t0, r);
63+
}
64+
return vec3(0.5 * log(r) * r / dr, t0, 0.0);
65+
}
66+
67+
vec3 f(vec3 p){
68+
ry(p, iTime*0.2);
69+
return mb(p);
70+
}
71+
72+
73+
float softshadow(vec3 ro, vec3 rd, float k ){
74+
float akuma=1.0,h=0.0;
75+
float t = 0.01;
76+
for(int i=0; i < 50; ++i){
77+
h=f(ro+rd*t).x;
78+
if(h<0.001)return 0.02;
79+
akuma=min(akuma, k*h/t);
80+
t+=clamp(h,0.01,2.0);
81+
}
82+
return akuma;
83+
}
84+
85+
vec3 nor( in vec3 pos )
86+
{
87+
vec3 eps = vec3(0.001,0.0,0.0);
88+
return normalize( vec3(
89+
f(pos+eps.xyy).x - f(pos-eps.xyy).x,
90+
f(pos+eps.yxy).x - f(pos-eps.yxy).x,
91+
f(pos+eps.yyx).x - f(pos-eps.yyx).x ) );
92+
}
93+
94+
vec3 intersect( in vec3 ro, in vec3 rd )
95+
{
96+
float t = 1.0;
97+
float res_t = 0.0;
98+
float res_d = 1000.0;
99+
vec3 c, res_c;
100+
float max_error = 1000.0;
101+
float d = 1.0;
102+
float pd = 100.0;
103+
float os = 0.0;
104+
float step = 0.0;
105+
float error = 1000.0;
106+
107+
for( int i=0; i<48; i++ )
108+
{
109+
if( error < pixel_size*0.5 || t > 20.0 )
110+
{
111+
}
112+
else{ // avoid broken shader on windows
113+
114+
c = f(ro + rd*t);
115+
d = c.x;
116+
117+
if(d > os)
118+
{
119+
os = 0.4 * d*d/pd;
120+
step = d + os;
121+
pd = d;
122+
}
123+
else
124+
{
125+
step =-os; os = 0.0; pd = 100.0; d = 1.0;
126+
}
127+
128+
error = d / t;
129+
130+
if(error < max_error)
131+
{
132+
max_error = error;
133+
res_t = t;
134+
res_c = c;
135+
}
136+
137+
t += step;
138+
}
139+
140+
}
141+
if( t>20.0/* || max_error > pixel_size*/ ) res_t=-1.0;
142+
return vec3(res_t, res_c.y, res_c.z);
143+
}
144+
145+
146+
147+
void main() {
148+
vec2 q=gl_FragCoord.xy/iResolution.xy;
149+
vec2 uv = -1.0 + 2.0*q;
150+
uv.x*=iResolution.x/iResolution.y;
151+
152+
pixel_size = 1.0/(iResolution.x * 3.0);
153+
// camera
154+
stime=0.7+0.3*sin(iTime*0.4);
155+
ctime=0.7+0.3*cos(iTime*0.4);
156+
157+
vec3 ta=vec3(0.0,0.0,0.0);
158+
vec3 ro = vec3(0.0, 3.*stime*ctime, 3.*(1.-stime*ctime));
159+
160+
vec3 cf = normalize(ta-ro);
161+
vec3 cs = normalize(cross(cf,vec3(0.0,1.0,0.0)));
162+
vec3 cu = normalize(cross(cs,cf));
163+
vec3 rd = normalize(uv.x*cs + uv.y*cu + 3.0*cf); // transform from view to world
164+
165+
vec3 sundir = normalize(vec3(0.1, 0.8, 0.6));
166+
vec3 sun = vec3(1.64, 1.27, 0.99);
167+
vec3 skycolor = vec3(0.6, 1.5, 1.0);
168+
169+
vec3 bg = exp(uv.y-2.0)*vec3(0.4, 1.6, 1.0);
170+
171+
float halo=clamp(dot(normalize(vec3(-ro.x, -ro.y, -ro.z)), rd), 0.0, 1.0);
172+
vec3 col=bg+vec3(1.0,0.8,0.4)*pow(halo,17.0);
173+
174+
175+
float t=0.0;
176+
vec3 p=ro;
177+
178+
vec3 res = intersect(ro, rd);
179+
if(res.x > 0.0){
180+
p = ro + res.x * rd;
181+
vec3 n=nor(p);
182+
float shadow = softshadow(p, sundir, 10.0 );
183+
184+
float dif = max(0.0, dot(n, sundir));
185+
float sky = 0.6 + 0.4 * max(0.0, dot(n, vec3(0.0, 1.0, 0.0)));
186+
float bac = max(0.3 + 0.7 * dot(vec3(-sundir.x, -1.0, -sundir.z), n), 0.0);
187+
float spe = max(0.0, pow(clamp(dot(sundir, reflect(rd, n)), 0.0, 1.0), 10.0));
188+
189+
vec3 lin = 4.5 * sun * dif * shadow;
190+
lin += 0.8 * bac * sun;
191+
lin += 0.6 * sky * skycolor*shadow;
192+
lin += 3.0 * spe * shadow;
193+
194+
res.y = pow(clamp(res.y, 0.0, 1.0), 0.55);
195+
vec3 tc0 = 0.5 + 0.5 * sin(3.0 + 4.2 * res.y + vec3(0.0, 0.5, 1.0));
196+
col = lin *vec3(0.9, 0.8, 0.6) * 0.2 * tc0;
197+
col=mix(col,bg, 1.0-exp(-0.001*res.x*res.x));
198+
}
199+
200+
// post
201+
col=pow(clamp(col,0.0,1.0),vec3(0.45));
202+
col=col*0.6+0.4*col*col*(3.0-2.0*col); // contrast
203+
col=mix(col, vec3(dot(col, vec3(0.33))), -0.5); // satuation
204+
col*=0.5+0.5*pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7); // vigneting
205+
gl_FragColor = vec4(col.xyz, smoothstep(0.55, .76, 1.-res.x/5.));
206+
}
207+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# Animated Mandelbub Using Shadertoy shader created by evilryu
3+
# https://www.shadertoy.com/view/MdXSWn
4+
# License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported
5+
# License. Adapted for JRubyArt by Martin Prout
6+
7+
attr_reader :mandelbub
8+
9+
def setup
10+
sketch_title 'Mandelbub'
11+
no_stroke
12+
@mandelbub = load_shader(data_path('mandelbub.glsl'))
13+
mandelbub.set('iResolution', width.to_f, height.to_f, 0.0)
14+
end
15+
16+
def draw
17+
puts frame_rate if (frame_count % 300).zero?
18+
mandelbub.set('iTime', millis / 1000.0)
19+
shader(mandelbub)
20+
rect(0, 0, width, height)
21+
end
22+
23+
def settings
24+
size(640, 360, P2D)
25+
end

0 commit comments

Comments
 (0)