Skip to content

Commit 3b2a59d

Browse files
committed
shadertoy sierpinski
1 parent 66ba294 commit 3b2a59d

File tree

4 files changed

+212
-15
lines changed

4 files changed

+212
-15
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// The MIT License
2+
// Copyright © 2013 Inigo Quilez
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4+
5+
6+
#ifdef GL_ES
7+
precision mediump float;
8+
precision mediump int;
9+
#endif
10+
11+
// ----------------------
12+
// SHADERTOY UNIFORMS -
13+
// ----------------------
14+
15+
uniform vec3 iResolution; // viewport resolution (in pixels)
16+
uniform float iTime; // shader playback time (in seconds)
17+
uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click
18+
19+
void mainImage( out vec4 fragColor, in vec2 fragCoord );
20+
21+
void main() {
22+
mainImage(gl_FragColor,gl_FragCoord.xy);
23+
}
24+
25+
const vec3 va = vec3( 0.0, 0.57735, 0.0 );
26+
const vec3 vb = vec3( 0.0, -1.0, 1.15470 );
27+
const vec3 vc = vec3( 1.0, -1.0, -0.57735 );
28+
const vec3 vd = vec3( -1.0, -1.0, -0.57735 );
29+
30+
// return distance and address
31+
vec2 map( vec3 p )
32+
{
33+
float a = 0.0;
34+
float s = 1.0;
35+
float r = 1.0;
36+
float dm;
37+
vec3 v;
38+
for( int i=0; i<8; i++ )
39+
{
40+
float d, t;
41+
d = dot(p-va,p-va); v=va; dm=d; t=0.0;
42+
d = dot(p-vb,p-vb); if( d<dm ) { v=vb; dm=d; t=1.0; }
43+
d = dot(p-vc,p-vc); if( d<dm ) { v=vc; dm=d; t=2.0; }
44+
d = dot(p-vd,p-vd); if( d<dm ) { v=vd; dm=d; t=3.0; }
45+
p = v + 2.0*(p - v); r*= 2.0;
46+
a = t + 4.0*a; s*= 4.0;
47+
}
48+
49+
return vec2( (sqrt(dm)-1.0)/r, a/s );
50+
}
51+
52+
const float precis = 0.0002;
53+
54+
vec3 intersect( in vec3 ro, in vec3 rd )
55+
{
56+
vec3 res = vec3( 1e20, 0.0, 0.0 );
57+
58+
float maxd = 5.0;
59+
60+
// sierpinski
61+
float h = 1.0;
62+
float t = 0.5;
63+
float m = 0.0;
64+
vec2 r;
65+
for( int i=0; i<100; i++ )
66+
{
67+
r = map( ro+rd*t );
68+
if( r.x<precis || t>maxd ) break;
69+
m = r.y;
70+
t += r.x;
71+
}
72+
73+
if( t<maxd && r.x<precis )
74+
res = vec3( t, 2.0, m );
75+
76+
return res;
77+
}
78+
79+
vec3 calcNormal( in vec3 pos )
80+
{
81+
vec3 eps = vec3(precis,0.0,0.0);
82+
return normalize( vec3(
83+
map(pos+eps.xyy).x - map(pos-eps.xyy).x,
84+
map(pos+eps.yxy).x - map(pos-eps.yxy).x,
85+
map(pos+eps.yyx).x - map(pos-eps.yyx).x ) );
86+
}
87+
88+
float calcOcclusion( in vec3 pos, in vec3 nor )
89+
{
90+
float ao = 0.0;
91+
float sca = 1.0;
92+
for( int i=0; i<8; i++ )
93+
{
94+
float h = 0.001 + 0.5*pow(float(i)/7.0,1.5);
95+
float d = map( pos + h*nor ).x;
96+
ao += -(d-h)*sca;
97+
sca *= 0.95;
98+
}
99+
return clamp( 1.0 - 0.8*ao, 0.0, 1.0 );
100+
}
101+
102+
vec3 lig = normalize(vec3(1.0,0.7,0.9));
103+
104+
vec3 render( in vec3 ro, in vec3 rd )
105+
{
106+
vec3 col = vec3(0.0);
107+
108+
// raymarch
109+
vec3 tm = intersect(ro,rd);
110+
if( tm.y>0.5 )
111+
{
112+
// geometry
113+
vec3 pos = ro + tm.x*rd;
114+
vec3 nor = calcNormal( pos );
115+
vec3 maa = vec3( 0.0 );
116+
117+
maa = 0.5 + 0.5*cos( 6.2831*tm.z + vec3(0.0,1.0,2.0) );
118+
119+
float occ = calcOcclusion( pos, nor );
120+
121+
// lighting
122+
float amb = (0.5 + 0.5*nor.y);
123+
float dif = max(dot(nor,lig),0.0);
124+
125+
// lights
126+
vec3 lin = 1.5*amb*vec3(1.0) * occ;
127+
128+
// surface-light interacion
129+
col = maa * lin;
130+
131+
}
132+
133+
// gamma
134+
col = pow( clamp(col,0.0,1.0), vec3(0.45) );
135+
136+
return col;
137+
}
138+
void mainImage( out vec4 fragColor, in vec2 fragCoord )
139+
{
140+
vec2 q = fragCoord.xy / iResolution.xy;
141+
vec2 p = -1.0 + 2.0 * q;
142+
p.x *= iResolution.x/iResolution.y;
143+
vec2 m = vec2(0.5);
144+
if( iMouse.z>0.0 ) m = iMouse.xy/iResolution.xy;
145+
146+
//-----------------------------------------------------
147+
// camera
148+
//-----------------------------------------------------
149+
150+
float an = 3.2 + 0.5*iTime - 6.2831*(m.x-0.5);
151+
152+
vec3 ro = vec3(2.5*sin(an),0.0,2.5*cos(an));
153+
vec3 ta = vec3(0.0,-0.5,0.0);
154+
vec3 ww = normalize( ta - ro );
155+
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
156+
vec3 vv = normalize( cross(uu,ww));
157+
vec3 rd = normalize( p.x*uu + p.y*vv + 5.0*ww*m.y );
158+
159+
vec3 col = render( ro, rd );
160+
161+
fragColor = vec4( col, 1.0 );
162+
}
163+
164+
void mainVR( out vec4 fragColor, in vec2 fragCoord, in vec3 fragRayOri, in vec3 fragRayDir )
165+
{
166+
vec3 col = render( fragRayOri + vec3(0.0,-0.1,2.0), fragRayDir );
167+
168+
fragColor = vec4( col, 1.0 );
169+
}
170+
// ----------------------------
171+
// SHADERTOY CODE ENDS HERE -
172+
// ----------------------------

processing_app/topics/shaders/data/voronoi_distance.glsl

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ precision mediump int;
99

1010
uniform vec3 iResolution;
1111
uniform float iTime;
12-
13-
void mainImage( out vec4 fragColor, in vec2 fragCoord );
14-
15-
void main() {
16-
mainImage(gl_FragColor,gl_FragCoord.xy);
17-
}
18-
1912
vec2 hash2( vec2 p )
2013
{
2114
// procedural white noise
@@ -64,12 +57,9 @@ vec3 voronoi( in vec2 x )
6457
return vec3( md, mr );
6558
}
6659

67-
void mainImage( out vec4 fragColor, in vec2 fragCoord )
68-
{
69-
vec2 p = fragCoord/iResolution.xx;
70-
60+
void main() {
61+
vec2 p = gl_FragCoord.xy/iResolution.xx;
7162
vec3 c = voronoi( 8.0*p );
72-
7363
// isolines
7464
vec3 col = c.x*(0.5 + 0.5*sin(64.0*c.x))*vec3(1.0);
7565
// borders
@@ -78,6 +68,5 @@ void mainImage( out vec4 fragColor, in vec2 fragCoord )
7868
float dd = length( c.yz );
7969
col = mix( vec3(1.0,0.6,0.1), col, smoothstep( 0.0, 0.12, dd) );
8070
col += vec3(1.0,0.6,0.1)*(1.0-smoothstep( 0.0, 0.04, dd));
81-
82-
fragColor = vec4(col,1.0);
71+
gl_FragColor = vec4(col,1.0);
8372
}

processing_app/topics/shaders/mandelbub.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ def draw
2121
end
2222

2323
def settings
24-
size(640, 360, P2D)
24+
size(1200, 580, P2D)
2525
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
attr_reader :last_mouse_position, :mouse_click_state, :wrapper, :start
2+
3+
def settings
4+
size(640, 360, P2D)
5+
end
6+
7+
def setup
8+
sketch_title '3D Sierpinski'
9+
@previous_time = 0.0
10+
@mouse_dragged = false
11+
@mouse_click_state = 0.0
12+
# Load the shader file from the "data" folder
13+
@wrapper = load_shader(data_path('sierpinski.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+
@last_mouse_position = Vec2D.new(mouse_x, mouse_y)
17+
@start = java.lang.System.current_time_millis
18+
end
19+
20+
def draw
21+
puts frame_rate if (frame_count % 300).zero?
22+
# shader playback time (in seconds)
23+
current_time = (java.lang.System.current_time_millis - start) / 1000.0
24+
wrapper.set('iTime', current_time)
25+
# mouse pixel coords. xy: current (if MLB down), zw: click
26+
if mouse_pressed?
27+
@last_mouse_position = Vec2D.new(mouse_x, mouse_y)
28+
@mouse_click_state = 1.0
29+
else
30+
@mouse_click_state = 0.0
31+
end
32+
wrapper.set('iMouse', last_mouse_position.x, last_mouse_position.y, mouse_click_state, mouse_click_state)
33+
shader(wrapper)
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)