Adding a few more shader examples

This commit is contained in:
codeanticode
2012-09-03 01:31:49 +00:00
parent d09d1d461f
commit 7709d70d47
10 changed files with 526 additions and 0 deletions
@@ -0,0 +1,19 @@
PImage tex;
PShader deform;
void setup() {
size(512, 384, P2D);
textureWrap(REPEAT);
tex = loadImage("tex1.jpg");
deform = loadShader("deform.glsl");
deform.set("resolution", float(width), float(height));
}
void draw() {
deform.set("time", millis() / 1000.0);
deform.set("mouse", float(mouseX), float(mouseY));
shader(deform);
image(tex, 0, 0, width, height);
}
@@ -0,0 +1,24 @@
uniform sampler2D textureSampler;
uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
void main(void) {
vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
vec2 m = -1.0 + 2.0 * mouse.xy / resolution.xy;
float a1 = atan(p.y - m.y, p.x - m.x);
float r1 = sqrt(dot(p - m, p - m));
float a2 = atan(p.y + m.y, p.x + m.x);
float r2 = sqrt(dot(p + m, p + m));
vec2 uv;
uv.x = 0.2 * time + (r1 - r2) * 0.25;
uv.y = sin(2.0 * (a1 - a2));
float w = r1 * r2 * 0.8;
vec3 col = texture2D(textureSampler, 0.5 - 0.495 * uv).xyz;
gl_FragColor = vec4(col / (0.1 + w), 1.0);
}
@@ -0,0 +1,16 @@
PShader landscape;
void setup() {
size(320, 240, P2D);
landscape = loadShader("landscape.glsl");
landscape.set("resolution", float(width), float(height));
noStroke();
}
void draw() {
landscape.set("time", millis() / 1000.0);
landscape.set("mouse", float(mouseX), height - float(mouseY));
shader(landscape);
rect(0, 0, width, height);
}
@@ -0,0 +1,220 @@
// Some hills...
// rotwang: @mod* lowered cam for better flight feeling
// @mod+ mouse y controls flight height
// @mod* some color tests
// @mod+ Canyon
// @emackey: Simple sky blue (no clouds...)
// @rotwang: mod* sky gradient, different terrain front and backcolor
// @mod* stripes texture
// @mod* terrain variation
// @psonice flyover adjusted so height depends on terrain. WOuld be better with spline interpolation, if you have the time go do it :)
#ifdef GL_ES
precision highp float;
#endif
uniform vec2 resolution;
uniform float time;
uniform vec2 mouse;
//Simple raymarching sandbox with camera
//Raymarching Distance Fields
//About http://www.iquilezles.org/www/articles/raymarchingdf/raymarchingdf.htm
//Also known as Sphere Tracing
//Original seen here: http://twitter.com/#!/paulofalcao/statuses/134807547860353024
//Util Start
mat2 m = mat2( 0.80, 0.60, -0.60, 0.80 );
float hash( float n )
{
return fract(sin(n)*43758.5453);
}
float noise( in vec2 x )
{
vec2 p = floor(x);
vec2 f = fract(x);
f = f*f*(3.0-2.0*f);
float n = p.x + p.y*57.0;
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);
return res;
}
float fbm_5oct( vec2 p )
{
float f = 0.0;
f += 0.50000*noise( p ); p = m*p*2.02;
f += 0.25000*noise( p ); p = m*p*2.03;
f += 0.12500*noise( p ); p = m*p*2.01;
f += 0.06250*noise( p ); p = m*p*2.04;
f += 0.03125*noise( p );
return f/0.984375;
}
float fbm_3oct( vec2 p )
{
float f = 0.0;
f += 0.50000*noise( p ); p = m*p*2.02;
f += 0.25000*noise( p ); p = m*p*2.03;
f += 0.12500*noise( p ); p = m*p*2.01;
return f/0.875;
}
vec2 ObjUnion(in vec2 obj_floor,in vec2 obj_roundBox){
if (obj_floor.x<obj_roundBox.x)
return obj_floor;
else
return obj_roundBox;
}
//Util End
//Scene Start
//Torus
float torus(in vec3 p, in vec2 t){
vec2 q = vec2(length(vec2(p.x,p.z))-t.x, p.y);
return length(q) - t.y;
}
//Sphere
float sphere(in vec3 p, float radius){
float length = sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
return length-radius;
}
vec2 terrain(in vec3 p){
float da = sin(0.9*p.y)*fbm_3oct(p.xz);
vec2 vd = vec2(p.y+fbm_3oct(p.xz / 9.0) * 9.33 ,0);
vd.x += da;
return vd;
}
vec3 hsv2rgb(float h,float s,float v) {
return mix(vec3(1.),clamp((abs(fract(h+vec3(3.,2.,1.)/3.)*6.-3.)-1.),0.,1.),s)*v;
}
/**
@rotwang:
smoothes between 2 vectors a and b (eg colors)
using a source value and a smooth amount
about the base as center.
*/
vec3 smoothmix(vec3 a, vec3 b, float base, float smooth, float source)
{
float f = smoothstep(base-smooth, base+smooth, source );
vec3 vec = mix(a, b, f);
return vec;
}
//Terrain Color
vec3 terrain_clr(in vec3 p){
vec3 clr_a = vec3(0.4, 0.5, 0.2);
vec3 clr_b = vec3(0.3, 0.3, 0.3);
float m = fract(p.y*4.0);
float g = fbm_3oct(p.xz * 28.0);
clr_b += g;
vec3 clr = smoothmix(clr_a, clr_b,0.76, 0.3, m );
clr = mix( clr, g*clr, 0.6);
return clr;
}
//IQs RoundBox (try other objects http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm)
vec2 obj_roundBox(in vec3 p){
return vec2(length(max(abs(p)-vec3(1,1,1),0.0))-0.25,1);
}
vec2 obj_sphere(in vec3 p){
return vec2(length(p)-2.0);
}
//RoundBox with simple solid color
vec3 obj_roundBox_c(in vec3 p){
return vec3(1.0,0.5,0.2);
}
//Objects union
vec2 inObj(in vec3 p){
return terrain(p);
}
//Scene End
void main(void){
//Camera animation
vec3 U=vec3(0,1,0);//Camera Up Vector
float speed = time*2.0;
float my = sin(time/8.0); // mouse.y*4.0;
float camy = -1.0+my;
float tary = -1.0+my;
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
E.y += .2;
vec3 viewDir=vec3(E.x-sin(mouse.x*0.0),tary,E.z+cos(mouse.x*0.0)); //Change camere view vector here
//vec3 E=vec3(mouse.x*0.0, 4, mouse.y*4.0); //Camera location; Change camera path position here
//Camera setup
vec3 C=normalize(viewDir-E);
vec3 A=cross(C, U);
vec3 B=cross(A, C);
vec3 M=(E+C);
vec2 vPos=2.0*gl_FragCoord.xy/resolution.xy - 1.0; // (2*Sx-1) where Sx = x in screen space (between 0 and 1)
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)
vec3 scp=normalize(scrCoord-E);
// scp.z += 1.;
scp.y -=0.25;
E.y -= (terrain(E).x + terrain(E + vec3(0., 0., 0.1)).x + terrain(E + vec3(0., 0., -0.1)).x) / 3.;
E.y += 2.;
//Raymarching
const vec3 e=vec3(0.1,0,0);
const float MAX_DEPTH=60.0; //Max depth0.
vec2 s=vec2(0.1,0.0);
vec3 c,p,n;
float f=1.0;
for(int i=0;i<100;i++){
if (abs(s.x)<.01||f>MAX_DEPTH) break;
f+=s.x;
p=E+scp*f;
s=inObj(p);
}
if (f<MAX_DEPTH){
if (s.y==0.0)
c=terrain_clr(p);
vec3 cc = vec3(0.7, c.g, c.b);
float m = smoothstep(3.0, 20.0, f);
c = mix(c,cc, 1.0-m);
n=normalize(
vec3(s.x-inObj(p-e.xyy).x,
s.x-inObj(p-e.yxy).x,
s.x-inObj(p-e.yyx).x));
float b=dot(n,normalize(E-p));
gl_FragColor=vec4(b*c*(2.0-f*.01),1.0);//simple phong LightPosition=CameraPosition
}
else {
float invy = 1.0-vPos.y;
float r = invy*0.8;
float g = 0.2 + invy*0.7;
vec3 sky_clr = vec3(r,g,0.9);
gl_FragColor=vec4( sky_clr,1.0); //background color
}
}
@@ -0,0 +1,13 @@
PShader life;
void setup() {
size(640, 480, P2D);
life = loadShader("Life.glsl");
noStroke();
}
void draw() {
life.set("time", millis() / 1000.0f);
life.set("mouse", (float)mouseX, height - (float)mouseY);
shader(life);
rect(0, 0, width, height);
}
@@ -0,0 +1,60 @@
// Conway's game of life
#ifdef GL_ES
precision highp float;
#endif
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
uniform sampler2D backbufferSampler;
vec4 live = vec4(0.796, 0.933, 0.141, 1.0);
vec4 dead = vec4(0.384, 0.792, 0.086, 1.0);
vec4 dirt = vec4(0.478, 0.31, 0.208, 1.0);
vec4 decay = vec4(0.349, 0.31, 0.118, 1.0);
void main( void ) {
vec2 position = ( gl_FragCoord.xy / resolution.xy );
vec2 pixel = 1./resolution;
if (length(position-mouse/resolution) < 0.01) {
float rnd1 = mod(fract(sin(dot(position + time * 0.001, vec2(14.9898,78.233))) * 43758.5453), 1.0);
if (rnd1 > 0.5) {
gl_FragColor = live;
} else {
gl_FragColor = dead;
}
} else {
int sum = 0;
sum += (texture2D(backbufferSampler, position + pixel * vec2(-1., -1.)).g > 0.9) ? 1 : 0;
sum += (texture2D(backbufferSampler, position + pixel * vec2(-1., 0.)).g > 0.9) ? 1 : 0;
sum += (texture2D(backbufferSampler, position + pixel * vec2(-1., 1.)).g > 0.9) ? 1 : 0;
sum += (texture2D(backbufferSampler, position + pixel * vec2(1., -1.)).g > 0.9) ? 1 : 0;
sum += (texture2D(backbufferSampler, position + pixel * vec2(1., 0.)).g > 0.9) ? 1 : 0;
sum += (texture2D(backbufferSampler, position + pixel * vec2(1., 1.)).g > 0.9) ? 1 : 0;
sum += (texture2D(backbufferSampler, position + pixel * vec2(0., -1.)).g > 0.9) ? 1 : 0;
sum += (texture2D(backbufferSampler, position + pixel * vec2(0., 1.)).g > 0.9) ? 1 : 0;
vec4 me = texture2D(backbufferSampler, position);
if (me.g <= 0.9) {
if (sum == 3) {
gl_FragColor = live;
} else if ((me.b > 0.2) || (me.b < 0.01)) {
gl_FragColor = dirt;
} else {
if (me.r > decay.r) { me.r -= 0.004; }
else if (me.g > decay.g) { me.g -= 0.004; }
else if (me.b < decay.b) { me.b += 0.004; }
else { me = decay; }
gl_FragColor = me;
}
} else {
if ((sum == 2) || (sum == 3)) {
gl_FragColor = live;
} else {
gl_FragColor = dead;
}
}
}
}
@@ -0,0 +1,18 @@
PShader monjori;
void setup() {
size(512, 384, P2D);
noStroke();
monjori = loadShader("monjori.glsl");
monjori.set("resolution", float(width), float(height));
}
void draw() {
monjori.set("time", millis() / 1000.0);
shader(monjori);
rect(0, 0, width, height);
}
@@ -0,0 +1,29 @@
uniform vec2 resolution;
uniform float time;
void main(void)
{
vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
float a = time*40.0;
float d,e,f,g=1.0/40.0,h,i,r,q;
e=400.0*(p.x*0.5+0.5);
f=400.0*(p.y*0.5+0.5);
i=200.0+sin(e*g+a/150.0)*20.0;
d=200.0+cos(f*g/2.0)*18.0+cos(e*g)*7.0;
r=sqrt(pow(i-e,2.0)+pow(d-f,2.0));
q=f/r;
e=(r*cos(q))-a/2.0;f=(r*sin(q))-a/2.0;
d=sin(e*g)*176.0+sin(e*g)*164.0+r;
h=((f+d)+a/2.0)*g;
i=cos(h+r*p.x/1.3)*(e+e+a)+cos(q*g*6.0)*(r+h/3.0);
h=sin(f*g)*144.0-sin(e*g)*212.0*p.x;
h=(h+(f-e)*q+sin(r-(a+h)/7.0)*10.0+i/4.0)*g;
i+=cos(h*2.3*sin(a/350.0-q))*184.0*sin(q-(r*4.3+a/12.0)*g)+tan(r*g+h)*184.0*cos(r*g+h);
i=mod(i/5.6,256.0)/64.0;
if(i<0.0) i+=4.0;
if(i>=2.0) i=4.0-i;
d=r/350.0;
d+=sin(d*d*8.0)*0.52;
f=(sin(a*g)+1.0)/2.0;
gl_FragColor=vec4(vec3(f*i/1.6,i/2.0+d/13.0,i)*d*p.x+vec3(i/1.3+d/8.0,i/2.0+d/18.0,i)*d*(1.0-p.x),1.0);
}
@@ -0,0 +1,17 @@
PShader nebula;
void setup() {
size(320, 240, P3D);
noStroke();
nebula = loadShader("nebula.glsl");
nebula.set("resolution", float(width), float(height));
}
void draw() {
nebula.set("time", millis() / 500.0);
shader(nebula);
fill(0);
rect(0, 0, width, height);
}
@@ -0,0 +1,110 @@
uniform float time;
uniform vec2 resolution;
// NEBULA - CoffeeBreakStudios.com (CBS)
// Work in progress...
//
// 3148.26: Switched from classic to simplex noise
// 3148.27: Reduced number of stars
// 3249.0: Switched to fast computed 3D noise. Less quality but ~ 2x faster
// 3249.5: Removed use of random number generator to gain performance
// 3265.0: Added rotation: glsl.heroku.com/e#3005.1
//Utility functions
vec3 fade(vec3 t) {
return vec3(1.0,1.0,1.0);//t*t*t*(t*(t*6.0-15.0)+10.0);
}
vec2 rotate(vec2 point, float rads) {
float cs = cos(rads);
float sn = sin(rads);
return point * mat2(cs, -sn, sn, cs);
}
vec4 randomizer4(const vec4 x)
{
vec4 z = mod(x, vec4(5612.0));
z = mod(z, vec4(3.1415927 * 2.0));
return(fract(cos(z) * vec4(56812.5453)));
}
// Fast computed noise
// http://www.gamedev.net/topic/502913-fast-computed-noise/
const float A = 1.0;
const float B = 57.0;
const float C = 113.0;
const vec3 ABC = vec3(A, B, C);
const vec4 A3 = vec4(0, B, C, C+B);
const vec4 A4 = vec4(A, A+B, C+A, C+A+B);
float cnoise4(const in vec3 xx)
{
vec3 x = mod(xx + 32768.0, 65536.0);
vec3 ix = floor(x);
vec3 fx = fract(x);
vec3 wx = fx*fx*(3.0-2.0*fx);
float nn = dot(ix, ABC);
vec4 N1 = nn + A3;
vec4 N2 = nn + A4;
vec4 R1 = randomizer4(N1);
vec4 R2 = randomizer4(N2);
vec4 R = mix(R1, R2, wx.x);
float re = mix(mix(R.x, R.y, wx.y), mix(R.z, R.w, wx.y), wx.z);
return 1.0 - 2.0 * re;
}
float surface3 ( vec3 coord, float frequency ) {
float n = 0.0;
n += 1.0 * abs( cnoise4( coord * frequency ) );
n += 0.5 * abs( cnoise4( coord * frequency * 2.0 ) );
n += 0.25 * abs( cnoise4( coord * frequency * 4.0 ) );
n += 0.125 * abs( cnoise4( coord * frequency * 8.0 ) );
n += 0.0625 * abs( cnoise4( coord * frequency * 16.0 ) );
return n;
}
void main( void ) {
float rads = radians(time*3.15);
vec2 position = gl_FragCoord.xy / resolution.xy;
position += rotate(position, rads);
float n = surface3(vec3(position*sin(time*0.1), time * 0.05)*mat3(1,0,0,0,.8,.6,0,-.6,.8),0.9);
float n2 = surface3(vec3(position*cos(time*0.1), time * 0.04)*mat3(1,0,0,0,.8,.6,0,-.6,.8),0.8);
float lum = length(n);
float lum2 = length(n2);
vec3 tc = pow(vec3(1.0-lum),vec3(sin(position.x)+cos(time)+4.0,8.0+sin(time)+4.0,8.0));
vec3 tc2 = pow(vec3(1.1-lum2),vec3(5.0,position.y+cos(time)+7.0,sin(position.x)+sin(time)+2.0));
vec3 curr_color = (tc*0.8) + (tc2*0.5);
//Let's draw some stars
float scale = sin(0.3 * time) + 5.0;
vec2 position2 = (((gl_FragCoord.xy / resolution) - 0.5) * scale);
float gradient = 0.0;
vec3 color = vec3(0.0);
float fade = 0.0;
float z = 0.0;
vec2 centered_coord = position2 - vec2(sin(time*0.1),sin(time*0.1));
centered_coord = rotate(centered_coord, rads);
for (float i=1.0; i<=60.0; i++)
{
vec2 star_pos = vec2(sin(i) * 250.0, sin(i*i*i) * 250.0);
float z = mod(i*i - 10.0*time, 256.0);
float fade = (256.0 - z) /256.0;
vec2 blob_coord = star_pos / z;
gradient += ((fade / 384.0) / pow(length(centered_coord - blob_coord), 1.5)) * ( fade);
}
curr_color += gradient;
gl_FragColor = vec4(curr_color, 1.0);
}