Es meg mindig lehet fokozni
#ifdef GL_ES
precision highp float;
#endif
//#define aliasing
#define moreball
uniform vec2 resolution;
uniform float time;
uniform sampler2D tex0;
//uniform sampler2D tex1;
struct pack
{
vec3 src;
vec3 ray;
vec3 col;
vec3 obj;
vec3 norm;
float tmin;
int hit;
};
float sgn(float n)
{
if(n<0.0) return -1.0;
if(n>0.0) return 1.0;
return 0.0;
}
vec3 floorv(vec3 v1)
{
v1.x=floor(v1.x);
v1.y=floor(v1.y);
v1.z=floor(v1.z);
return v1;
}
float noise( float n )
{
return (sin(n+time*0.945)*43348.5453);//fract!
}
vec3 lerp(vec3 v1,vec3 v2,float t)
{
return v1+(v2-v1)*t;
}
int boxchk3(pack ee,vec3 box,vec3 box2)
{
if(ee.src.x>=box.x)
if(ee.src.y>=box.y)
if(ee.src.z>=box.z)
if(ee.src.x<=box2.x)
if(ee.src.y<=box2.y)
if(ee.src.z<=box2.z) return 1;//inside
float t1=0.0;
vec3 w1,norm, obj;
if(ee.ray.x<0.0) t1=(box2.x-ee.src.x)/ee.ray.x;
else t1=(box.x-ee.src.x)/ee.ray.x;
if(t1>0.01)
// if(t1<ee.tmin)
{
obj=ee.src + ee.ray*t1;
if(obj.y>=box.y)
if(obj.y<=box2.y)
if(obj.z>=box.z)
if(obj.z<=box2.z) return 1;
}
if(ee.ray.y<0.0) t1=(box2.y-ee.src.y)/ee.ray.y;
else t1=(box.y-ee.src.y)/ee.ray.y;
if(t1>0.01)
// if(t1<ee.tmin)
{
obj=ee.src + ee.ray*t1;
if(obj.x>=box.x)
if(obj.x<=box2.x)
if(obj.z>=box.z)
if(obj.z<=box2.z) return 1;
}
if(ee.ray.z<0.0) t1=(box2.z-ee.src.z)/ee.ray.z;
else t1=(box.z-ee.src.z)/ee.ray.z;
if(t1>0.01)
// if(t1<ee.tmin)
{
obj=ee.src + ee.ray*t1;
if(obj.y>=box.y)
if(obj.y<=box2.y)
if(obj.x>=box.x)
if(obj.x<=box2.x) return 1;
}
return 0;
}
pack boxchk(pack ee,vec3 box,vec3 box2,vec3 col)
{
box2+=box;//max
float t1=0.0;
vec3 w1,norm, obj;
if(ee.ray.x<0.0) t1=(box2.x-ee.src.x)/ee.ray.x;
else t1=(box.x-ee.src.x)/ee.ray.x;
if(t1>0.01)
if(t1<ee.tmin)
{
obj=ee.src + ee.ray*t1;
if(obj.y>=box.y)
if(obj.y<=box2.y)
if(obj.z>=box.z)
if(obj.z<=box2.z)
{
ee.col=col;
if(ee.ray.x<0.0) norm=vec3( 1,0,0);
else norm=vec3(-1,0,0);
ee.obj=obj;
ee.norm=norm;
ee.hit=1;
ee.tmin=t1;
}
}
if(ee.ray.y<0.0) t1=(box2.y-ee.src.y)/ee.ray.y;
else t1=(box.y-ee.src.y)/ee.ray.y;
if(t1>0.01)
if(t1<ee.tmin)
{
obj=ee.src + ee.ray*t1;
if(obj.x>=box.x)
if(obj.x<=box2.x)
if(obj.z>=box.z)
if(obj.z<=box2.z)
{
ee.col=col;
if(ee.ray.y<0.0) norm=vec3(0, 1,0);
else norm=vec3(0,-1,0);
ee.obj=obj;
ee.norm=norm;
ee.hit=1;
ee.tmin=t1;
}
}
if(ee.ray.z<0.0) t1=(box2.z-ee.src.z)/ee.ray.z;
else t1=(box.z-ee.src.z)/ee.ray.z;
if(t1>0.01)
if(t1<ee.tmin)
{
obj=ee.src + ee.ray*t1;
if(obj.y>=box.y)
if(obj.y<=box2.y)
if(obj.x>=box.x)
if(obj.x<=box2.x)
{
ee.col=col;
if(ee.ray.z<0.0) norm=vec3(0,0, 1);
else norm=vec3(0,0,-1);
ee.obj=obj;
ee.norm=norm;
ee.hit=1;
ee.tmin=t1;
}
}
return ee;
}
pack chk_ball2(vec3 ball,float r,vec3 col,pack ee)
{
ball.y+=10.0+10.0*sin(time + dot(ball,vec3(6.0,3.0,1.0)));
ball.y+=10.0+10.0*sin(time + dot(col,vec3(6.0,3.0,1.0)));
vec3 ball2eyes=ee.src-ball;
float a=dot(ee.ray,ee.ray);
float b=dot(ee.ray,ball2eyes)*2.0 ;
float c=dot(ball2eyes,ball2eyes)-r*r;
float D=b*b-a*c*4.0;
if(D>=0.0)
{
float t1=(-b-sqrt(D))/(a*2.0);
if(t1>0.01)
if(t1<ee.tmin)
{
ee.col=col;
ee.obj=ee.src + ee.ray*t1;
ee.norm=normalize(ee.obj-ball);
ee.tmin=t1;
ee.hit=1;
}
}
return ee;
}
pack chk_scene(pack ee,float tmin)
{
ee.tmin=1e16;
if(tmin!=0.0) ee.tmin=tmin;
ee.hit=0;
ee.col=vec3(0,0,0);
if(ee.ray.y<0.0)//ground
{
#if 0
ee.col=vec3(0,0,1);
float t1=(0.0-ee.src.y)/ee.ray.y;
ee.obj=ee.src + ee.ray*t1;
vec3 w1=mod(ee.obj/90.0,1.0);
#if 1
if(w1.x>0.5) if(w1.z<0.5) ee.col=vec3(1,1,1);
if(w1.x<0.5) if(w1.z>0.5) ee.col=vec3(1,1,1);
#else
ee.col = texture2D(tex0,w1.xz/110.0).xyz*3.0;
#endif
ee.norm=vec3(0,1,0);
ee.tmin=t1;
ee.hit=1;
#endif
float t1=(0.0-ee.src.y)/ee.ray.y;
vec3 obj=ee.src + ee.ray*t1;
obj.y=0.0;
vec3 ball3=floor(obj/100.0)*100.0+vec3(50,-50,50);
for(int y=-1;y<2;y++)
for(int x=-1;x<2;x++)
{
ee=chk_ball2(ball3+vec3(x,0,y)*100.0,50.2,vec3(0.0,0.6,0.0),ee);
}
}
#ifndef moreball
ee=chk_ball2(vec3(250,50,-310),50.2,vec3(1,0,0),ee);
ee=chk_ball2(vec3(120,50,-210),50.2,vec3(0,1,0),ee);
ee=chk_ball2(vec3(10,50,-170),50.2,vec3(0,0,1),ee);
ee=chk_ball2(vec3(150,50,-410),50.2,vec3(1,0,1),ee);
ee=chk_ball2(vec3(250,50,-210),50.2,vec3(1,1,1),ee);
ee=boxchk(ee,vec3(-60.0,0.0,-430.0),vec3(100.0,100.0,100.0),vec3(0.7,0.6,0.2));
#else
vec3 dbase=vec3(400,400,400);
if(ee.ray.x<0.0) dbase.x=0.0;//benne lesz a dobozban, nem kell tovabblepni!
if(ee.ray.y<0.0) dbase.y=0.0;
if(ee.ray.z<0.0) dbase.z=0.0;
vec3 src3=ee.src;//kulon!
vec3 base=floorv(src3/400.0)*400.0;
for(int x=0;x<12;x++)//8 12,max 20 eleg
{
vec3 bmin=base;
vec3 bmax=bmin+vec3( 400,210,400);
if(src3.y>-50.0)
if(src3.y<350.0)
{
if(boxchk3(ee,bmin,bmax)==1)
{
ee=chk_ball2(bmin+vec3(250,50,310),49.0,vec3(1,0,0),ee);
ee=chk_ball2(bmin+vec3(120,50,210),49.0,vec3(0,1,0),ee);
ee=chk_ball2(bmin+vec3(80,50,170),49.0,vec3(0,0,1),ee);
ee=chk_ball2(bmin+vec3(150,50,60),49.0,vec3(1,0,1),ee);
ee=chk_ball2(bmin+vec3(310,50,210),49.0,vec3(1,1,1),ee);
}
}
vec3 base2=base+dbase;//kovetkezo 3 fal
vec3 t3=(base2-src3)/ee.ray;
t3.x=min(t3.x,min(t3.y,t3.z));
src3+=ee.ray*t3.x;//elore
src3+=ee.ray*0.01;//belep a dobozba
base=floorv(src3/400.0)*400.0;
if(src3.y>350.0) if(ee.ray.y>0.0) break;//up
if(src3.y<-50.0) if(ee.ray.y<0.0) break;//up
if(length(src3-ee.src)>400.0*12.0) break;//messze ment
}
#endif
return ee;
}
void main(void)
{
vec2 txc = gl_FragCoord.xy / resolution.xy;
txc.x *= resolution.x/resolution.y;
vec3 eye=vec3(100,250,300);
vec3 lookat=vec3(0,50,0);
vec3 up=vec3(0,1,0);
float r2=420.0;
#ifndef moreball
lookat=vec3(120,50,-210);
r2=320.0;
#endif
float aa=time*0.1;//lets move
eye=lookat+vec3(cos(aa)*r2,150.0,sin(aa)*r2);
vec3 dv=vec3(12000.0,0.0,12000.0);//negative voxel bug
eye+=dv;
lookat+=dv;
vec3 camz=normalize(lookat-eye);
vec3 camx=cross(camz,up);
vec3 camy=cross(camx,camz);
float rnd=123.124;
vec3 col4=vec3(0,0,0);
#ifdef aliasing
float alias=4.0;//12 ,4
for( int j=0; j<4; j++ )//12 ,4
#else
float alias=1.0;
#endif
{
pack ee;
float dx=0.0,dy=0.0;
#ifdef aliasing
rnd=noise( 340.123+rnd); dx=fract(rnd)/resolution.x;
rnd=noise( 2345.1423+rnd); dy=fract(rnd)/resolution.y;
#endif
ee.ray=camz*1.1 + camx*(txc.x-0.5+dx) + camy*(txc.y-0.5+dy);
ee.ray=normalize(ee.ray);
ee.src=eye;
vec3 w1;
vec3 light=vec3(200,300,-100)+dv;
aa=time*0.7;//lets move
light+=vec3(cos(aa)*350.0,0.0,sin(aa)*350.0);
float sz=1.0;
vec3 col3=vec3(0,0,0);
for( int i=0; i<3; i++ )//N=3 reflection
{
ee=chk_scene(ee,0.0);
if(i!=0)
if(length(ee.src-eye)>400.0*12.0) ee.hit=0;//stop//messze
if(ee.hit==0)
{
float t1=ee.ray.y;
t1=sqrt(t1);
col3+=lerp(vec3(0.5,0.7,0.99)*18.0, vec3(0.0,0.2,0.99)*5.0, t1)*sz;//5
break;
}
float shadow=1.0,t6=length(light-ee.src);
if(t6<1000.0)
{
pack ee2=ee;//chk shadow
ee2.ray=normalize(light-ee.obj);
ee2.src=ee.obj+ee2.ray*0.02;
ee2=chk_scene(ee2,t6);
if(ee2.hit==1) shadow=0.0;
}
#if 0
vec3 rndv;
rnd=noise(rnd+234.254); rndv.x=fract(rnd);
rnd=noise(rnd+2234.54); rndv.y=fract(rnd);
rnd=noise(rnd+24.25); rndv.z=fract(rnd);
ee.norm+=rndv*0.02;//smooth reflect
ee.norm=normalize(ee.norm);
#endif
w1=(light-ee.obj);
float lt=dot(w1,w1);
w1=normalize(w1);
col3+=ee.col*vec3(0.99,0.9,0.7)*max(dot(ee.norm,w1),0.0)*61e4*sz*shadow/(lt);
float t2=0.15+0.85*max(ee.norm.y,0.0);//ambient
col3+=ee.col*vec3(0.6,0.7,0.9)*t2*0.9*sz;
col3+=vec3(0.6,0.7,0.9)*t2*0.34*sz;
w1=normalize(ee.obj-ee.src);
vec3 ref=w1-ee.norm*dot(ee.norm,w1)*2.0;//reflection
w1=normalize(light-ee.obj);
float spec=max(dot(ref,w1),0.0);
col3+=vec3(0.99,0.9,0.7)*pow(spec,12.0)*7.0*sz*shadow;//specular
ee.ray=ref;//new ray src
ee.src=ee.obj+ee.ray*0.02;
sz*=0.4;//0.1 0.4
}
col3=col3*0.15;//0.25
col4+=col3;
}
gl_FragColor = vec4( col4/alias,1);
}
//http://www.iquilezles.org/apps/shadertoy/presets/tex1.jpg