FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Adding a few more shader examples · whyi/processing@ea40104 · GitHub

Repository navigation

Commit ea40104

Browse files
committed
Adding a few more shader examples
1 parent e177092 commit ea40104

11 files changed

Lines changed: 526 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
PImage tex;
2+
PShader deform;
3+
4+
void setup() {
5+
size(512, 384, P2D);
6+
7+
textureWrap(REPEAT);
8+
tex = loadImage("tex1.jpg");
9+
10+
deform = loadShader("deform.glsl");
11+
deform.set("resolution", float(width), float(height));
12+
}
13+
14+
void draw() {
15+
deform.set("time", millis() / 1000.0);
16+
deform.set("mouse", float(mouseX), float(mouseY));
17+
shader(deform);
18+
image(tex, 0, 0, width, height);
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
uniform sampler2D textureSampler;
2+
3+
uniform float time;
4+
uniform vec2 resolution;
5+
uniform vec2 mouse;
6+
7+
void main(void) {
8+
vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
9+
vec2 m = -1.0 + 2.0 * mouse.xy / resolution.xy;
10+
11+
float a1 = atan(p.y - m.y, p.x - m.x);
12+
float r1 = sqrt(dot(p - m, p - m));
13+
float a2 = atan(p.y + m.y, p.x + m.x);
14+
float r2 = sqrt(dot(p + m, p + m));
15+
16+
vec2 uv;
17+
uv.x = 0.2 * time + (r1 - r2) * 0.25;
18+
uv.y = sin(2.0 * (a1 - a2));
19+
20+
float w = r1 * r2 * 0.8;
21+
vec3 col = texture2D(textureSampler, 0.5 - 0.495 * uv).xyz;
22+
23+
gl_FragColor = vec4(col / (0.1 + w), 1.0);
24+
}
31.9 KB
LoadingViewer requires iframe.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PShader landscape;
2+
3+
void setup() {
4+
size(320, 240, P2D);
5+
6+
landscape = loadShader("landscape.glsl");
7+
landscape.set("resolution", float(width), float(height));
8+
noStroke();
9+
}
10+
11+
void draw() {
12+
landscape.set("time", millis() / 1000.0);
13+
landscape.set("mouse", float(mouseX), height - float(mouseY));
14+
shader(landscape);
15+
rect(0, 0, width, height);
16+
}
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
// Some hills...
2+
// rotwang: @mod* lowered cam for better flight feeling
3+
// @mod+ mouse y controls flight height
4+
// @mod* some color tests
5+
// @mod+ Canyon
6+
// @emackey: Simple sky blue (no clouds...)
7+
// @rotwang: mod* sky gradient, different terrain front and backcolor
8+
// @mod* stripes texture
9+
// @mod* terrain variation
10+
// @psonice flyover adjusted so height depends on terrain. WOuld be better with spline interpolation, if you have the time go do it :)
11+
#ifdef GL_ES
12+
precision highp float;
13+
#endif
14+
15+
16+
uniform vec2 resolution;
17+
uniform float time;
18+
uniform vec2 mouse;
19+
//Simple raymarching sandbox with camera
20+
21+
//Raymarching Distance Fields
22+
//About http://www.iquilezles.org/www/articles/raymarchingdf/raymarchingdf.htm
23+
//Also known as Sphere Tracing
24+
//Original seen here: http://twitter.com/#!/paulofalcao/statuses/134807547860353024
25+
26+
//Util Start
27+
28+
mat2 m = mat2( 0.80, 0.60, -0.60, 0.80 );
29+
30+
float hash( float n )
31+
{
32+
return fract(sin(n)*43758.5453);
33+
}
34+
35+
float noise( in vec2 x )
36+
{
37+
vec2 p = floor(x);
38+
vec2 f = fract(x);
39+
f = f*f*(3.0-2.0*f);
40+
float n = p.x + p.y*57.0;
41+
float res = mix(mix( hash(n+ 0.0), hash(n+ 1.0),f.x), mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y);
42+
return res;
43+
}
44+
45+
float fbm_5oct( vec2 p )
46+
{
47+
float f = 0.0;
48+
f += 0.50000*noise( p ); p = m*p*2.02;
49+
f += 0.25000*noise( p ); p = m*p*2.03;
50+
f += 0.12500*noise( p ); p = m*p*2.01;
51+
f += 0.06250*noise( p ); p = m*p*2.04;
52+
f += 0.03125*noise( p );
53+
return f/0.984375;
54+
}
55+
56+
float fbm_3oct( vec2 p )
57+
{
58+
float f = 0.0;
59+
f += 0.50000*noise( p ); p = m*p*2.02;
60+
f += 0.25000*noise( p ); p = m*p*2.03;
61+
f += 0.12500*noise( p ); p = m*p*2.01;
62+
63+
return f/0.875;
64+
}
65+
66+
vec2 ObjUnion(in vec2 obj_floor,in vec2 obj_roundBox){
67+
if (obj_floor.x<obj_roundBox.x)
68+
return obj_floor;
69+
else
70+
return obj_roundBox;
71+
}
72+
//Util End
73+
74+
//Scene Start
75+
76+
//Torus
77+
float torus(in vec3 p, in vec2 t){
78+
vec2 q = vec2(length(vec2(p.x,p.z))-t.x, p.y);
79+
return length(q) - t.y;
80+
}
81+
82+
//Sphere
83+
float sphere(in vec3 p, float radius){
84+
float length = sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
85+
return length-radius;
86+
}
87+
88+
89+
vec2 terrain(in vec3 p){
90+
91+
92+
float da = sin(0.9*p.y)*fbm_3oct(p.xz);
93+
vec2 vd = vec2(p.y+fbm_3oct(p.xz / 9.0) * 9.33 ,0);
94+
vd.x += da;
95+
96+
return vd;
97+
}
98+
99+
vec3 hsv2rgb(float h,float s,float v) {
100+
return mix(vec3(1.),clamp((abs(fract(h+vec3(3.,2.,1.)/3.)*6.-3.)-1.),0.,1.),s)*v;
101+
}
102+
103+
/**
104+
@rotwang:
105+
smoothes between 2 vectors a and b (eg colors)
106+
using a source value and a smooth amount
107+
about the base as center.
108+
*/
109+
vec3 smoothmix(vec3 a, vec3 b, float base, float smooth, float source)
110+
{
111+
float f = smoothstep(base-smooth, base+smooth, source );
112+
vec3 vec = mix(a, b, f);
113+
return vec;
114+
}
115+
116+
117+
//Terrain Color
118+
vec3 terrain_clr(in vec3 p){
119+
120+
121+
vec3 clr_a = vec3(0.4, 0.5, 0.2);
122+
vec3 clr_b = vec3(0.3, 0.3, 0.3);
123+
float m = fract(p.y*4.0);
124+
float g = fbm_3oct(p.xz * 28.0);
125+
clr_b += g;
126+
vec3 clr = smoothmix(clr_a, clr_b,0.76, 0.3, m );
127+
clr = mix( clr, g*clr, 0.6);
128+
129+
130+
return clr;
131+
}
132+
133+
//IQs RoundBox (try other objects http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm)
134+
vec2 obj_roundBox(in vec3 p){
135+
return vec2(length(max(abs(p)-vec3(1,1,1),0.0))-0.25,1);
136+
}
137+
138+
vec2 obj_sphere(in vec3 p){
139+
return vec2(length(p)-2.0);
140+
}
141+
142+
//RoundBox with simple solid color
143+
vec3 obj_roundBox_c(in vec3 p){
144+
return vec3(1.0,0.5,0.2);
145+
}
146+
147+
//Objects union
148+
vec2 inObj(in vec3 p){
149+
return terrain(p);
150+
}
151+
152+
//Scene End
153+
154+
void main(void){
155+
//Camera animation
156+
vec3 U=vec3(0,1,0);//Camera Up Vector
157+
158+
float speed = time*2.0;
159+
float my = sin(time/8.0); // mouse.y*4.0;
160+
float camy = -1.0+my;
161+
float tary = -1.0+my;
162+
vec3 E=vec3(0., camy, speed);//vec3(-sin(time/4.0)*8.0,4,cos(time/4.0)*8.0); //Camera location; Change camera path position here
163+
E.y += .2;
164+
vec3 viewDir=vec3(E.x-sin(mouse.x*0.0),tary,E.z+cos(mouse.x*0.0)); //Change camere view vector here
165+
//vec3 E=vec3(mouse.x*0.0, 4, mouse.y*4.0); //Camera location; Change camera path position here
166+
167+
//Camera setup
168+
vec3 C=normalize(viewDir-E);
169+
vec3 A=cross(C, U);
170+
vec3 B=cross(A, C);
171+
vec3 M=(E+C);
172+
173+
vec2 vPos=2.0*gl_FragCoord.xy/resolution.xy - 1.0; // (2*Sx-1) where Sx = x in screen space (between 0 and 1)
174+
vec3 scrCoord=M + vPos.x*A*resolution.x/resolution.y + vPos.y*B; //normalize resolution in either x or y direction (ie resolution.x/resolution.y)
175+
vec3 scp=normalize(scrCoord-E);
176+
// scp.z += 1.;
177+
scp.y -=0.25;
178+
E.y -= (terrain(E).x + terrain(E + vec3(0., 0., 0.1)).x + terrain(E + vec3(0., 0., -0.1)).x) / 3.;
179+
E.y += 2.;
180+
//Raymarching
181+
const vec3 e=vec3(0.1,0,0);
182+
const float MAX_DEPTH=60.0; //Max depth0.
183+
184+
vec2 s=vec2(0.1,0.0);
185+
vec3 c,p,n;
186+
187+
float f=1.0;
188+
for(int i=0;i<100;i++){
189+
if (abs(s.x)<.01||f>MAX_DEPTH) break;
190+
f+=s.x;
191+
p=E+scp*f;
192+
s=inObj(p);
193+
}
194+
195+
if (f<MAX_DEPTH){
196+
if (s.y==0.0)
197+
c=terrain_clr(p);
198+
vec3 cc = vec3(0.7, c.g, c.b);
199+
200+
float m = smoothstep(3.0, 20.0, f);
201+
c = mix(c,cc, 1.0-m);
202+
203+
204+
n=normalize(
205+
vec3(s.x-inObj(p-e.xyy).x,
206+
s.x-inObj(p-e.yxy).x,
207+
s.x-inObj(p-e.yyx).x));
208+
float b=dot(n,normalize(E-p));
209+
210+
gl_FragColor=vec4(b*c*(2.0-f*.01),1.0);//simple phong LightPosition=CameraPosition
211+
}
212+
else {
213+
float invy = 1.0-vPos.y;
214+
float r = invy*0.8;
215+
float g = 0.2 + invy*0.7;
216+
vec3 sky_clr = vec3(r,g,0.9);
217+
gl_FragColor=vec4( sky_clr,1.0); //background color
218+
}
219+
220+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
PShader life;
2+
void setup() {
3+
size(640, 480, P2D);
4+
life = loadShader("Life.glsl");
5+
noStroke();
6+
}
7+
8+
void draw() {
9+
life.set("time", millis() / 1000.0f);
10+
life.set("mouse", (float)mouseX, height - (float)mouseY);
11+
shader(life);
12+
rect(0, 0, width, height);
13+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Conway's game of life
2+
3+
#ifdef GL_ES
4+
precision highp float;
5+
#endif
6+
7+
uniform float time;
8+
uniform vec2 mouse;
9+
uniform vec2 resolution;
10+
uniform sampler2D backbufferSampler;
11+
12+
vec4 live = vec4(0.796, 0.933, 0.141, 1.0);
13+
vec4 dead = vec4(0.384, 0.792, 0.086, 1.0);
14+
vec4 dirt = vec4(0.478, 0.31, 0.208, 1.0);
15+
vec4 decay = vec4(0.349, 0.31, 0.118, 1.0);
16+
17+
void main( void ) {
18+
vec2 position = ( gl_FragCoord.xy / resolution.xy );
19+
vec2 pixel = 1./resolution;
20+
21+
if (length(position-mouse/resolution) < 0.01) {
22+
float rnd1 = mod(fract(sin(dot(position + time * 0.001, vec2(14.9898,78.233))) * 43758.5453), 1.0);
23+
if (rnd1 > 0.5) {
24+
gl_FragColor = live;
25+
} else {
26+
gl_FragColor = dead;
27+
}
28+
} else {
29+
int sum = 0;
30+
sum += (texture2D(backbufferSampler, position + pixel * vec2(-1., -1.)).g > 0.9) ? 1 : 0;
31+
sum += (texture2D(backbufferSampler, position + pixel * vec2(-1., 0.)).g > 0.9) ? 1 : 0;
32+
sum += (texture2D(backbufferSampler, position + pixel * vec2(-1., 1.)).g > 0.9) ? 1 : 0;
33+
sum += (texture2D(backbufferSampler, position + pixel * vec2(1., -1.)).g > 0.9) ? 1 : 0;
34+
sum += (texture2D(backbufferSampler, position + pixel * vec2(1., 0.)).g > 0.9) ? 1 : 0;
35+
sum += (texture2D(backbufferSampler, position + pixel * vec2(1., 1.)).g > 0.9) ? 1 : 0;
36+
sum += (texture2D(backbufferSampler, position + pixel * vec2(0., -1.)).g > 0.9) ? 1 : 0;
37+
sum += (texture2D(backbufferSampler, position + pixel * vec2(0., 1.)).g > 0.9) ? 1 : 0;
38+
vec4 me = texture2D(backbufferSampler, position);
39+
40+
if (me.g <= 0.9) {
41+
if (sum == 3) {
42+
gl_FragColor = live;
43+
} else if ((me.b > 0.2) || (me.b < 0.01)) {
44+
gl_FragColor = dirt;
45+
} else {
46+
if (me.r > decay.r) { me.r -= 0.004; }
47+
else if (me.g > decay.g) { me.g -= 0.004; }
48+
else if (me.b < decay.b) { me.b += 0.004; }
49+
else { me = decay; }
50+
gl_FragColor = me;
51+
}
52+
} else {
53+
if ((sum == 2) || (sum == 3)) {
54+
gl_FragColor = live;
55+
} else {
56+
gl_FragColor = dead;
57+
}
58+
}
59+
}
60+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
PShader monjori;
2+
3+
void setup() {
4+
size(512, 384, P2D);
5+
noStroke();
6+
7+
monjori = loadShader("monjori.glsl");
8+
monjori.set("resolution", float(width), float(height));
9+
10+
}
11+
12+
void draw() {
13+
monjori.set("time", millis() / 1000.0);
14+
15+
shader(monjori);
16+
rect(0, 0, width, height);
17+
}
18+

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL