Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // nincsneve
- // 4k intro
- // by Caiwan
- // Industrial Revolutoners
- // 2012
- #version 150
- // ezeket a parametereket a frameworktol kapja
- uniform vec4 params[8];
- // Az intro alapja egy egyszeru raymarcher
- // Felhasznalt anyag + forras:
- // - raymarcher sandbox: http://glsl.heroku.com/e#1996.0
- // - IQ leirasai a distant fuggvenekhez: http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
- // - IQ menger szivacsa: http://www.iquilezles.org/www/articles/menger/menger.htm
- #define PI 3.151592
- #define DEBUG
- //--------------------------------------------------------------------------------------------------------------------
- // Object utils
- float smoothcurve(float f) {return .5*(1+cos(PI*f));}
- float maxcomp(in vec3 p ) { return max(p.x,max(p.y,p.z));}
- // ?
- float lengthn(in vec3 p, float n){return pow((pow(p.x,n)+pow(p.y,n)+pow(p.z,n)),(1/n));}
- float length2(in vec3 p){return lengthn(p,2);}
- float length8(in vec3 p){return lengthn(p,8);}
- //Matrixok
- mat4 identity(vec3 v){
- return mat4(
- 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1
- );
- }
- mat4 translate(vec3 v){
- return mat4(
- 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- v.x, v.y, v.z, 1
- );
- }
- mat4 scale(vec3 v){
- return mat4(
- v.x, 0, 0, 0,
- 0, v.y, 0, 0,
- 0, 0, v.z, 0,
- 0, 0, 0, 1
- );
- }
- mat4 rotate(vec3 v, float a){
- float ca = cos(a);
- float sa = sin(a);
- vec3 vv = normalize(v);
- float x = vv.x,
- y = vv.y,
- z = vv.z;
- return mat4x4(
- x*x*(1-ca)+ca, x*y*(1-ca)-z*sa, x*z*(1-ca)+y*sa, 0,
- y*x*(1-ca)+z*sa, y*y*(1-ca)+ca, y*z*(1-ca)-x*sa, 0,
- z*x*(1-ca)-y*sa, z*y*(1-ca)+x*sa, z*z*(1-ca)+ca, 0,
- 0, 0, 0, 1
- );
- }
- //=================================================
- // Egyszeru primitivek IQ-tol
- float sdSphere( vec3 p, float s ){
- return length(p)-s;
- }
- float udBox( vec3 p, vec3 b ){
- return length(max(abs(p)-b,0.0));
- }
- float udRoundBox( vec3 p, vec3 b, float r ){
- return length(max(abs(p)-b,0.0))-r;
- }
- float sdPlane( vec3 p, vec4 n ){
- // n must be normalized
- return dot(p,n.xyz) + n.w;
- }
- /*
- float sdBox( vec3 p, vec3 b ){
- vec3 di = abs(p) - b;
- float mc = maxcomp(di);
- return min(mc,length(max(di,0.0)));
- }
- */
- float sdTorus82( vec3 p, vec2 t )
- {
- vec3 q = vec3(length2(vec3(p.xz,0))-t.x,p.y,0);
- return length8(q)-t.y;
- }
- float sdTorus88( vec3 p, vec2 t )
- {
- vec3 q = vec3(length8(vec3(p.xz,0))-t.x,p.y,0);
- return length8(q)-t.y;
- }
- //=================================================
- // Primitiv muveletek
- //osszeadas
- float opU( float d1, float d2 ){
- return min(d1,d2);
- }
- //kulonbseg
- float opS( float d1, float d2 )
- {
- return max(-d1,d2);
- }
- //translation
- vec3 opTx( vec3 p, mat4 m ){
- return (inverse(m)*vec4(p,1)).xyz;
- }
- vec3 opRep( vec3 p, vec3 c ){
- return mod(p,c)-0.5*c;
- }
- //blend
- /*
- float opBlend(float pa, float pb){
- //float d1 = primitiveA(p);
- //float d2 = primitiveB(p);
- float dd = smoothcurve(pa-pb);
- return mix(pa,pb,dd);
- }
- */
- //hajlitas (nem mukodik)
- /*
- vec3 opCheapBend( vec3 p, float shift){
- float c = cos(shift*p.y);
- float s = sin(shift*p.y);
- mat2 m = mat2(c,-s,s,c);
- return vec3(m*p.xy,p.z);
- }
- */
- //--------------------------------------------------------------------------------------------------------------------
- // Scenek
- /*
- vec3 tex0(in vec3 p){
- vec3 col = vec3(1,1,1);
- vec2 pos = vec2((p.x/5.), (p.z/5.)); //transform (or fract)
- return vec3(length(pos));
- }
- */
- mat4 kocka1Forog;
- mat4 forog;
- void matrixok_kiszamol(){
- float scene = params[3].w;
- vec4 scenep = params[4];
- forog = rotate((vec4(1,0,0,0)*rotate(vec3(.371,.557,.742), params[1].x*.7)).xyz, params[1].x*.2);
- if (scene == 0){
- kocka1Forog = translate(mix(vec3(20,30,-20), vec3(7,6,-7), scenep.w));
- kocka1Forog *= forog;
- } else if (scene == 1) {
- // ...
- }
- }
- float inObj(in vec3 p){
- float f = 1.;
- float scene = params[3].w;
- vec4 scenep = params[4];
- float plane = sdPlane(p, vec4(0,1,0,0));
- float vagosik = opU(sdPlane(p, vec4(0,-1,0,0)), sdPlane(opTx(p,translate(vec3(-7))), vec4(0,1,0,0)));
- if (scene == 0){
- float pyramid = 1.;
- int h = 5;
- for (int i=1; i<=h; i++){
- pyramid = opU(pyramid, udBox(p, vec3(h-i, i, h-i)));
- }
- f = pyramid;
- //f = opU(f, udBox(opTx(p,kocka1Forog), vec3(4)));
- //f = opU(f, plane);
- } else if (scene == 1){
- f = opS(vagosik, udRoundBox( opRep(p, vec3(10, 10, 10)) , vec3(3.5), .2));
- } else if (scene == 2){
- f = opS(vagosik, sdTorus88( opRep(p, vec3(10, 10, 10)) , vec2(1,2)));
- f = opU(f, plane);
- } else {
- }
- return f;
- }
- vec3 inTexture(float m, vec3 p){
- return vec3(0.1,0.15,0.2);
- }
- //--------------------------------------------------------------------------------------------------------------------
- float intersect( in vec3 E, in vec3 M, out vec3 p)
- {
- float t = 0.0;
- // amikor release van akkor a nagyobb ertek kell
- #ifndef DEBUG
- for(int i=0;i<256;i++)
- #else
- for(int i=0;i<64;i++)
- #endif
- {
- p = E + M*t;
- float h = inObj(p);
- #ifndef DEBUG
- if( h<0.0002 )
- #else
- if( h<0.02 )
- #endif
- //return vec4(t,h.yzw);
- return t;
- t += h;
- }
- //return vec4(-1.0);
- return -1;
- }
- vec3 calcNormal(in vec3 pos)
- {
- vec3 eps = vec3(.001,0.0,0.0);
- return normalize(vec3(
- inObj(pos+eps.xyy) - inObj(pos-eps.xyy),
- inObj(pos+eps.yxy) - inObj(pos-eps.yxy),
- inObj(pos+eps.yyx) - inObj(pos-eps.yyx)
- ));
- }
- //--------------------------------------------------------------------------------------------------------------------
- // main fuggveny
- void main(void)
- {
- //hal_num(2, 3);
- vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / params[0].xy;
- p.x *= 1.33;
- //scene
- //melyik scene + 4 szabad param.
- float scene = params[3].w;
- vec4 scenep = params[4];
- // light
- vec3 light = normalize(vec3(1,.8,-.6));
- float ctime = params[1].x;
- // camera
- vec3 cE = params[2].xyz;
- vec3 cC = params[3].xyz;
- float rot = PI*(params[2].w/180.); //rotation
- vec3 ww = normalize(cC - cE); //EC vektor
- vec3 cU = (rotate(ww, rot)*vec4(0,1,0,0)).xyz ; //up vektor
- vec3 uu = normalize(cross(cU , ww ));
- vec3 vv = normalize(cross(ww,uu));
- vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );
- vec3 col = vec3(0.0); // background
- //col = vec3(.5,.7,1);
- matrixok_kiszamol();
- // Raymarch
- vec3 P = vec3(p,0);
- //vec4 tmat = intersect(cE,rd,P);
- float tmat = intersect(cE,rd,P);
- //if( tmat>0.0 )
- {
- vec3 pos = cE + tmat*rd;
- vec3 nor = calcNormal(pos);
- float dif1 = max(0.4 + 0.6*dot(nor,light),0.0);
- float dif2 = max(0.4 + 0.6*dot(nor,vec3(-light.x,light.y,-light.z)),0.0);
- // shadow
- //#if 0
- //float ldis = 4.0;
- //vec3 SP;
- //float shadow = intersect( pos + light*ldis, -light, SP);
- //if( shadow>0.0 && shadow<(ldis-0.01) ) dif1=0.0;
- //#endif
- // textura hozzaad
- //col = vec3(1.0,0.97,0.85);
- float lum = 2.0*dif1 + 0.2*dif2 + (.5+0.5*nor.y);
- col = inTexture(tmat, P)*lum;
- //col = vec3(0.2,0.2,0.2) * lum;
- // gamma lighting
- col = col*0.5+0.5*sqrt(col)*2.2;
- // gamma kod
- float g = scenep.y; if (g<.001) g = 1;
- if (scene < 2) col *= .75*exp((-0.5*1./g)*tmat); else col *= .75*exp(-0.05*tmat);
- //col *= .75*exp((-0.5*1./scenep.y)*tmat.x);
- }
- //else {
- // col.r = 1;
- //}
- //if (scene < 1) col = mix(col, vec3(1), pow(scenep.x,5));
- col = mix(col, vec3(1), pow(scenep.x,5));
- gl_FragColor = vec4(col,1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement