HTML

speed

sc3n3 2011.12.29. 08:06

 Egy 560as geforce 4-5x gyorsabb, mint az enyem, igy 10-12 fps varhato 300000 haromszognel nem tul nagy felbontasban, ami jatekokhoz meg keves. A masik gond, amit mindenhol megemlitenek, a scene statikus. Mozgasnal elvileg ujra kellene epiteni a kdtree-t, amire az nvidianak van is egy megoldasa. 

De nem feltetlenul ez a legjobb ut,  Ha nem keyframe animaciorol van szo, akkor az egyik megoldas szinten itt van a blogban.

Lehet voxeles megoldassal a teret felosztani, amihez hasonlot mar lentebb bemutattam.A scene kisebb egysegekbol allhat, aminek csak a befoglalo box-at kell forgatni, mozgatni, skalazni, mint a vizesflakonnal. Azon belul, lokalis koordinatarendszerben, akar a kdtree is maradhat mindig ugyan az, nem kell az object-hez ujraszamolni. Mozgasnal csak egy uj voxelhez kell linkelni az objectet, ami minimalis szamolassal jar.

Egy masik gyorsitasi mod, hogy az elso melysegben nem kell raytracing, Hiszen a kapott kep teljesen megegyezik a polygon alapu renderrel. Ezt lehet a regi modszerrel szamolni, csak le kell tarolni a talalati pontot, a normalt, es a tukrozodo vektort. 

Igazabol egy keyframe animacional is alkalmazhato, amit fentebb leirtam, Egy mozgo karakter minden darabja befoglalhato egy boxba. Az egyetlen gond, a sulyozas miatt elmozgo haromszogek elmasznak a kdtree-ben ledefinialt helyukrol.

Ezt a kis problemat majd id-s baratunk megoldja. 

 

 

ui:

Elso gondolatom az volt, miutan ujraolvastam az irasom, hogy a megoldas biztosan nem modosithatja a kdtree-t. Ez a leggyengebb pont.

Marpedig akkor a sulyozast kell modositani. Azutan kell valahogy elvegezni, miutan a sulyozott vertex mindket csontbeli pozicioja megvan. Nade ingyen ennyi segitseg tobb mint eleg.  ,)

 

komment

dragon

sc3n3 2011.12.28. 20:31

 

komment

the future?

sc3n3 2011.12.28. 16:25

Nos, lehet, hogy nem ertek a raytracinghez, de akkor biztos csak veletlenul megy 2.5 fps-el majdnem 300000 haromszog egyszeru CG shaderben, egy 17000Ft-os videokartyan,

Olvasgatom, hogy egyesek szerint hogy kellene mindenfele nyakatekert modszerrel multiprocesszoros rendszerekre programot irni. Nos, ez a rajtrace egyetlen shader, es majdnem ugy nez ki, mint ami a CPU-n fut. A C nyelv tokeletes. Akik megszoktak a objektumok kenyelmet, most csunyakat mondanak, de kit erdekel. Ez az igazsag.

A CUDA es OpenCL semmi olyan pluszt nem tud, amit CG-ben ne lehetne megoldani.

 

A buddha file innen szerezheto meg.

http://graphics.stanford.edu/data/3Dscanrep/

ftp://graphics.stanford.edu/pub/3Dscanrep/happy/happy_recon.tar.gz

happy_vrip_res2.ply

ply
format ascii 1.0
comment generated by ply_writer
element vertex 144647
property float x
property float y
property float z
element face 293232
property list uchar int vertex_indices
end_header

 

Csak a shadert linkelem, a foprogram csak egyetlen lapot renderel. A kdtree epito nem publikus. Sorry.

int chk_bbox(float3 src,float3 ray,float3 bbox1,float3 bbox2,float tmin)
{
float t5=0.0;//01;

if(src.x>=bbox1.x)
if(src.x<=bbox2.x)
if(src.y>=bbox1.y)
if(src.y<=bbox2.y)
if(src.z>=bbox1.z)
if(src.z<=bbox2.z) return 1;


int o=0;
float3 obj;
float tx,ty,tz,t1;
if(ray.x>0.0) {tx=(bbox1.x - src.x)/ray.x;}
else {tx=(bbox2.x - src.x)/ray.x;}
if(ray.y>0.0) {ty=(bbox1.y - src.y)/ray.y;}
else {ty=(bbox2.y - src.y)/ray.y;}
if(ray.z>0.0) {tz=(bbox1.z - src.z)/ray.z;}
else {tz=(bbox2.z - src.z)/ray.z;}
if(tx>ty) if(tx>tz) {o=1;t1=tx;}
if(ty>tx) if(ty>tz) {o=2;t1=ty;}
if(tz>ty) if(tz>tx) {o=3;t1=tz;}

if(o==1)
if(t1>t5)
if(t1<tmin)
{
obj.y=src.y + ray.y*t1;

if(obj.y>=bbox1.y)
if(obj.y<=bbox2.y)
{
obj.z=src.z + ray.z*t1;
if(obj.z>=bbox1.z)
if(obj.z<=bbox2.z) return 1;
}
}

if(o==2)
if(t1>t5) / />
if(t1<tmin) //<
{
obj.x=src.x + ray.x*t1;

if(obj.x>=bbox1.x)
if(obj.x<=bbox2.x)
{
obj.z=src.z + ray.z*t1;
if(obj.z>=bbox1.z)
if(obj.z<=bbox2.z) return 1;
}
}

if(o==3)
if(t1>t5)
if(t1<tmin)
{
obj.x=src.x + ray.x*t1;

if(obj.x>=bbox1.x)
if(obj.x<=bbox2.x)
{
obj.y=src.y + ray.y*t1;
if(obj.y>=bbox1.y)
if(obj.y<=bbox2.y) return 1;
}
}
return 0;
}


void ps_realraytrace_ultrafast(float3 position : TEXCOORD0,
float3 normal : TEXCOORD1,
float2 txc : TEXCOORD2,
uniform sampler2D tx_bboxmap : TEX0,
uniform sampler2D tx_bbox1 : TEX1,
uniform sampler2D tx_bbox2 : TEX2,

uniform sampler2D tx_vert1 : TEX3,
uniform sampler2D tx_edge1 : TEX4,
uniform sampler2D tx_norm2 : TEX5,
uniform sampler2D tx_tang1 : TEX6,

uniform sampler2D tx_col5 : TEX7,
uniform sampler2D tx_rnd1 : TEX8,
uniform sampler2D tx_decal1 : TEX9,

uniform float3 eye,
uniform float3 camx,
uniform float3 camy,
uniform float3 camz,
uniform float3 xdir,
uniform float3 rnd1,
uniform float3 time3,

uniform float txsizexi,// 1/txsize!
uniform float txsizeyi,// 1/txsize!
uniform float3 dtx_aliasing,


// uniform int render_mode,
uniform int txc2x_limit,
uniform int txc2y_limit,

out float4 color0 : COLOR)
{
color0.w=1;

// float3 ray=camz*0.7+ camx*(txc.x-0.5)*800.0/600.0 + camy*(txc.y-0.5);
float3 ray=camz*0.7+ camx*(txc.x-0.5+dtx_aliasing.x)*500.0/400.0 + camy*(txc.y-0.5+dtx_aliasing.y);

ray=normalize(ray);


float3 ray2=float3(0,0,0);
float3 eye2=float3(0,0,0);


float3 snorm=float3(0,0,1);
float3 sobj=float3(0,0,0);
float3 scol=float3(0,0,0);

float3 sref=float3(0,0,0);
float3 col3=float3(0,0,0);
float3 col4=float3(0,0,0);
float3 gcol=float3(0,0,0);
float3 col5=tex2Dlod( tx_col5,float4(txc,0,0)).rgb;
#ifdef realtime
col5=0;//realt
#endif

float sz=1,sshadow=1,tmin2;

#define rt_def 0
#define rt_shad 1
#define rt_occl 2
int ttrend[]={rt_def,rt_shad,rt_occl, rt_def,rt_shad,rt_occl,0,0};


#ifdef occltest
for(int q5=0;q5<3;q5+=1)//occl test, no ref
#else
for(int q5=0;q5<6;q5+=1)//normal
#endif
{
float tmin=1e16;
float hit=0;
int render_type=ttrend[q5];

if(render_type==rt_shad) tmin=tmin2;

int4 txc2=int4(0,0,0,0);//sorba
int4 txc2b=int4(0,0,0,0);//linear
int4 seltxc=int4(0,0,0,0);

int j2=0,end=0;
while(end==0 )//!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
int3 bbox_data=tex2Dfetch( tx_bboxmap,txc2).rgb;
float3 bbox1=tex2Dfetch( tx_bbox1,txc2).rgb;
float3 bbox2=tex2Dfetch( tx_bbox2,txc2).rgb;


//is_tri==0 &&
if(chk_bbox(eye,ray,bbox1,bbox2,tmin)==0)//bbox skip 4 fps
{
txc2.xy=bbox_data.xy;//skip coords, !!recursion like!! thekey!!!!
if(txc2.y>txc2y_limit || (txc2.y==txc2y_limit && txc2.x>=txc2x_limit)) {end=1;}// *txsizeyi binarytree
}
else
{
int is_tri=(int)bbox_data.z;//4.27!!!!

if(is_tri!=0)
{
float3 norm2=tex2Dfetch( tx_norm2,txc2).rgb;//norm2
int4 txc22=txc2;
txc22.x<<=2;
float3 v1=tex2Dfetch( tx_vert1,txc22).rgb;


float t1=(dot(norm2,v1-eye))/dot(norm2,ray);
if(t1>0.01)//self 0.001
if(t1<tmin)
{
float3 obj=eye+ray*t1;
float3 edge1=(tex2Dfetch( tx_edge1,txc22).rgb);//edge


if(dot(edge1,obj-v1)>=0.0)//tri
{
txc22.x++;
float3 v2=tex2Dfetch( tx_vert1,txc22).rgb;
float3 edge2=(tex2Dfetch( tx_edge1,txc22).rgb);
if(dot(edge2,obj-v2)>=0.0)
{
txc22.x++;
float3 v3=tex2Dfetch( tx_vert1,txc22).rgb;
float3 edge3=(tex2Dfetch( tx_edge1,txc22).rgb);
if(dot(edge3,obj-v3)>=0.0)
{
tmin=t1;
if(render_type==rt_def)//reflect&occl-hoz nem kell semmi
{
sobj=obj;
snorm=norm2;
scol=float3(0.7,0.0,0.0);//0.7 0 0
seltxc=txc22;
}

hit=1;
} //tri
}//dot
}//dot
}//t1<min
}//is_tri

txc2.x+=1;
if(txc2.x>=512)
{
txc2.x=0;
txc2.y+=1;
}//txc2
if(txc2.y>txc2y_limit || (txc2.y==txc2y_limit && txc2.x>=txc2x_limit)) {end=1;}

}//chkbox else
}//end




// float3 light=(float3(1280,150,1240));//120,270,240));
// float3 light=(float3(1300-50,110,1200-50));//120,270,240));
// float3 light=(float3(1300+70,110,1200+30));//120,270,240));
float3 light=(float3(1300+200,310,1200+60));


if(render_type==rt_shad)
{
if(hit==1) sshadow=0.0;
else sshadow=1.0;

gcol.rgb+=col3*sshadow;

ray=ray2;
eye=eye2;
}
if(render_type==rt_occl)
{
#ifdef occltest
if(hit==0) gcol.rgb+=float3(0.7,0.7,0.7);//occl test
#else
if(hit==0) gcol.rgb+=col4;//afloat diffuse 4skyoccl
#endif
ray=ray2;
eye=eye2;
}
if(render_type==rt_def)
{
if(hit==0)
{
if(ray.y>0.0)
{

scol.x=0.4+pow(1.0-ray.y,3.0)*2.6;//16
scol.y=0.8+pow(1.0-ray.y,3.0)*2.6;
scol.z=0.9+pow(1.0-ray.y,3.0)*2.6;
scol*=0.6;//sotetit

gcol.rgb+=scol*sz;

gcol*=0.25;// !!!!!!!!!!!!!!!!!!!!!!! exit
color0.rgb=col5+saturate(gcol);
return;
// break;
}
else
{
float t1=(0.0-eye.y)/ray.y;
sobj=eye+ray*t1;
#if 0
float u=fmod(sobj.x/150.0,1.0); //kockas padlo
float v=fmod(sobj.z/150.0,1.0);
if((u>0.5&&v>0.5 )||(u<0.5&&v<0.5 )) scol=float3(1,1,1);
else scol=float3(0,0,1);
#else
scol=tex2Dlod(tx_decal1,float4(sobj.xz/100.0,0,0)).xyz;//wood
#endif
snorm=float3(0,1,0);
}
}
else//hit==1 tang->norm interp
{
#if 0
seltxc.x-=2;//!!
float3 v1=tex2Dfetch( tx_vert1,seltxc).rgb;
float3 tang1=(tex2Dfetch( tx_tang1,seltxc).rgb);seltxc.x++;
float3 tang2=(tex2Dfetch( tx_tang1,seltxc).rgb);seltxc.x++;
float3 tang3=(tex2Dfetch( tx_tang1,seltxc).rgb);seltxc.x++;
float3 tang4=(tex2Dfetch( tx_tang1,seltxc).rgb);

float3 w1=sobj - v1;
snorm.x=tang4.x + dot(tang1,w1);//tangent space ->normal interp
snorm.y=tang4.y + dot(tang2,w1);
snorm.z=tang4.z + dot(tang3,w1);
snorm=normalize(snorm);
#endif
}
if(dot(snorm,eye-sobj)<0.0) snorm=-snorm;//normal az eye fele!

//diffuse light
float3 w2=sobj-light;
float3 w1=normalize(w2);
float t1=length(w2);
col3=scol*fabs(dot(snorm,w1))*float3(1.0,0.9,0.7)*sz*35e4/(t1*t1);//diffuse
//ambient
col4=scol*float3(0.7,0.9,1.0)*1.4*sz;//ambient 0.3

//reflect ray
float3 w4=normalize(sobj-eye);
sref=w4-snorm*dot(w4,snorm)*2;
sref+=tex2Dlod(tx_rnd1,float4(txc+time3.xy,0,0)).xyz*0.01;//rnd 4ref
// sref+=(scol-float3(0.5,0.5,0.5))*0.3;//color rnd 4 ref
sref=normalize(sref);

//specular
float3 w3=-w1;//normalize(light-sobj)
col3+=saturate(float3(1.0,0.9,0.7)*pow(dot(sref,w3),12.0))*7.0;//specular

#ifdef occltest
col3=0;//occl test
#endif
ray=sref;
eye=sobj;

//setup shadow ray
if(render_type==rt_def)//next shadow
{
ray2=ray;//forender utan, store
eye2=eye;

ray=normalize(light-sobj);//to light
tmin2=length(light-sobj);

ray+=tex2Dlod(tx_rnd1,float4(txc+time3.xy,0,0)).xyz*0.01;//rnd 4shad
ray=normalize(ray);

sz*=0.6;//0.3
}

// gcol.rgb+=(col3+col4);//ref no shad & occl
}//shadow

//setup occl ray
if(render_type==rt_shad)//next occl
{
eye=eye2;
#if 1
ray=tex2Dlod(tx_rnd1,float4(txc+time3.xy,0,0)).xyz;//rnd4occl
#else
ray=tex2Dlod(tx_rnd1,float4(txc+time3.xy,0,0)).xyz*0.8;//rnd4occl 0.8
ray+=snorm;
#endif
ray=normalize(ray);
if(dot(ray,snorm)<0.0) ray=-ray;
}
}//q5

gcol*=0.25;// !!!!!!!!!!!!!!!!!!!!!!! exit
color0.rgb=col5+saturate(gcol);


}


 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

komment

mouse

sc3n3 2011.12.28. 11:26

Eleg volt a forgasbol, vegyuk at az iranyitast.

uniform vec4 mouse;

vec2 user_hand=mouse.xy/resolution.xy;
vec2 aa2=user_hand*pi*2.0;//lets move,
aa2.y/=2.0; aa2.y-=pi/2.0;
vec2 c1=cos(aa2);
vec2 s1=sin(aa2);
eye=lookat+vec3(c1.x*c1.y*r2, s1.y*r2, s1.x*c1.y*r2);

Kesobb kell egy float az aa=.... ele. Erdemes mindig vektorokkal sin-t, cos-t, meg altalaban minden szamolni, mert igy biztosan egyszerre szamolja a hardware, A forditotol fuggoen ha kulon irjuk, lehet hogy nem.
 

komment

polygonok

sc3n3 2011.12.28. 11:03

A polygon alapu raytracet egyenlore kihagyom, mert a model kd-fajat egy float3 texturaban kell tarolni, amit most sehogy nem tudok atadni a http://www.iquilezles.org/apps/shadertoy/  oldalnak.

Ami igazan vicces, hogy az a kod sokkal egyszerubb es rovidebb mint ez itt.

komment

transzformacio

sc3n3 2011.12.28. 10:47

Hogyan lehet elforgatni valamit? Matrix transzformacioval. 

Az  bemeno ertekeket a vizesflakon koordinatarendszerebe kell atalakitani, a kimeno ee.obj es ee.norm-okat vissza a vilag-koordinatarendszerbe,

 

A normalnal es a ray-nel nincs pozicio, igy eleg egy 3x3 matrixxal szorozni. A tobbit 4x4-essel szokas, de most egyszerubb az m11 matrix transzponaltjat /m22/ eloallitani, igy marad a pozicionalas es a 3x3-as szorzas. Az eredmeny ugyan az, de 4x4.essel szebb a kod.

Mostmar eleg komplex sceneket ossze lehet heggeszteni 3d editor nelkul, egy szimpla browserben.

Mint kitunik a blogbol, a hatar soha nem ott van, ahol hisszuk. Mindig akad valamilyen csel, amivel kitolhatoak a latszolag atlephetetlen hatarok.

 

pack chk_fig(vec3 pos,vec3 col,pack ee)
{

mat3 m11 = mat3( 0.00, 0.80, 0.60,
-0.80, 0.36, -0.48,
-0.60, -0.48, 0.64);
mat3 m22 = mat3( 0.00, -0.80, -0.60,
0.80, 0.36, -0.48,
0.60, -0.48, 0.64);


vec3 bbox1=vec3(-40,0,-40);//pos+
vec3 bbox2=vec3(40,120,40);//pos+

vec3 srco=ee.src;
vec3 rayo=ee.ray;
ee.src=(ee.src-pos)*m11; //boxchk4 hez!
ee.ray=ee.ray*m11;


vec3 pnt1=boxchk4(ee,bbox1,bbox2,0);
vec3 pnt2=boxchk4(ee,bbox1,bbox2,1);

// if(t1<ee.tmin) if(t2<ee.tmin)//nem biztos hogy lesz hit, bug
if(pnt1.x!=-1.0 || pnt2.x!=-1.0)
{
vec3 w1=pnt1-ee.src; float t1=dot(w1,w1);
w1=pnt2-ee.src; float t2=dot(w1,w1);
if(t2<t1) {w1=pnt1;pnt1=pnt2;pnt2=w1;}///xchg

vec3 axis=vec3(0,0,0);//pos;//forgastengely, magassag=y valtozik csak
vec3 pnt=pnt1;
vec3 dpnt=normalize(pnt2-pnt1);
vec3 spnt=vec3(0,0,0);
float tmin=1e16;
float step=12.0;//12 t3 atallitja!
int hit=0;

for(int i=0;i<16;i++)
{
axis.y=pnt.y;
vec3 dir=pnt-axis;// radialis vektor
float ld=dot(dir,dir);//length(dir);dir=normalize(dir);

float rd=getcurve(pnt.y);
float t1=rd*rd-ld;//pnt a feluleten belul
float t3=abs(t1)*9.0;//step korrekcio, felulet kozeleben korrigal, 2=szemcses, 5=durva 3^2
if(t3<step) step=t3;
if(t1>=0.0)//hit
{
pnt-=dpnt*step;
step*=0.5;//fele akkora lepessel tovabb

if(step<0.5) hit=1;//0.1 eleg kicsi, itt a felulet!
}
pnt+=dpnt*step;
}
spnt=pnt;
t2=length(spnt-ee.src);

if(hit==1)//real hit
if(t2<ee.tmin)
if(t2>0.1)//self, 0.1 kell!
{
ee.col=col;
ee.obj=spnt*m22+pos;
// ee.obj=spnt;
ee.tmin=t2;
ee.hit=1;
ee.type=2;

#if 1
//felulet normal
float rd=getcurve(spnt.y);
axis.y=spnt.y;
vec3 dir=spnt-axis;
dir=normalize(dir);
vec3 obj1=axis + dir*rd;

dir+=vec3(dir.z,0.0,-dir.x)*0.2;//meroleges , mellete levo pnt
dir=normalize(dir);
vec3 obj2=axis + dir*rd;//rd ua!

spnt.y+=0.2;//felette levo pnt
rd=getcurve(spnt.y);
axis.y=spnt.y;
dir=spnt-axis;
dir=normalize(dir);
vec3 obj3=axis + dir*rd;

ee.norm=normalize(cross(obj2-obj1, obj3-obj1))*m22;

#endif
}
}
ee.src=srco;//restore
ee.ray=rayo;

return ee;
}
 

 

 

komment

lol

sc3n3 2011.12.27. 17:35

float getcurve(float t)
{
float e=0.0;

e=30.0+3.0*sin(t*0.1+time*3.0)+1.0*sin(t*0.3435-time);

return e;
}

 

komment

bugfix

sc3n3 2011.12.27. 14:36

A boxchk4()-bol ki kell szedni a 3db  if(t1>0.01)-at, es a chk_fig()-ben a

if(t2>0.1)//self, 0.1 kell!

sort 0.01-rol 0.1 re

dir+=vec3(dir.z,0.0,-dir.x)*0.2;//meroleges , mellete levo pnt
 spnt.y+=0.2;//felette levo pnt 

szorzokat 2.0-rol 0.2-re atirni. Igy mar sokkal jobb, de lehet meg finomitani,.

 

 

komment

uveg

sc3n3 2011.12.27. 14:00

A chk_fig()ben levo felulet normal szamitasnal tul nagy volt a lepeskoz, ezert volt asszimetrikus a horizont a tukrozodesben. Erdemes az utolso kod alapjan atnezni a regieket, mert volt nehany bug. Az egeszet bemasolni nem jo otlet, mert kinyiffanhat az egesz program. Az ujraelesztes pedig egy kulon muveszet.

 

 

#ifdef GL_ES
precision highp float;
#endif


#define aliasing

uniform vec2 resolution;
uniform float time;
uniform sampler2D tex0;
//uniform sampler2D tex1;

#define pi 3.1415926

struct pack
{
vec3 src;
vec3 ray;

vec3 col;
vec3 obj;
vec3 norm;

float tmin;
int hit;
int type;
};


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;
}
vec3 boxchk4(pack ee,vec3 box,vec3 box2,int side)
{
// box2+=box;//max

float t1=0.0;
vec3 w1,norm, obj;

vec3 bbox1;
vec3 bbox2;
if(side==1) {bbox1=box;bbox2=box2;}
else {bbox1=box2;bbox2=box;}

if(ee.ray.x<0.0) t1=(bbox2.x-ee.src.x)/ee.ray.x;
else t1=(bbox1.x-ee.src.x)/ee.ray.x;

// if(t1>0.01) // benne is lehet!
// 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 obj;
}



if(ee.ray.y<0.0) t1=(bbox2.y-ee.src.y)/ee.ray.y;
else t1=(bbox1.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 obj;
}


if(ee.ray.z<0.0) t1=(bbox2.z-ee.src.z)/ee.ray.z;
else t1=(bbox1.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 obj;
}

return vec3(-1.0,0,0);
}
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;
}
float getcurve(float t)
{
float e=0.0,e2=0.0;
#if 1
if(t>70.0)//0-80-120
{
t-=95.0; t*=0.8;
float r=32.0;r*=r;
e=sqrt(r-t*t);
}
#else
if(t>95.0)//0-80-120
{
t-=95.0;
float r=38.0;r*=r;
e=sqrt(r-t*t);
}
#endif
else
{
e=20.0+15.0*sin(t*0.1);
e2=22.0+5.0*sin(t*0.3435);
e=max(e,e2);
}

return e;
}
pack chk_fig(vec3 pos,vec3 col,pack ee)
{
vec3 bbox1=pos-vec3(40,0,40);
vec3 bbox2=pos+vec3(40,120,40);

vec3 pnt1=boxchk4(ee,bbox1,bbox2,0);
vec3 pnt2=boxchk4(ee,bbox1,bbox2,1);

// if(t1<ee.tmin) if(t2<ee.tmin)//nem biztos hogy lesz hit, bug
if(pnt1.x!=-1.0 || pnt2.x!=-1.0)
{
vec3 w1=pnt1-ee.src; float t1=dot(w1,w1);
w1=pnt2-ee.src; float t2=dot(w1,w1);
if(t2<t1) {w1=pnt1;pnt1=pnt2;pnt2=w1;}///xchg

vec3 axis=pos;//forgastengely, magassag=y valtozik csak
vec3 pnt=pnt1;
vec3 dpnt=normalize(pnt2-pnt1);
vec3 spnt=vec3(0,0,0);
float tmin=1e16;
float step=12.0;//12 t3 atallitja!
int hit=0;

for(int i=0;i<16;i++)
{
axis.y=pnt.y;
vec3 dir=pnt-axis;// radialis vektor
float ld=dot(dir,dir);//length(dir);dir=normalize(dir);

float rd=getcurve(pnt.y);
float t1=rd*rd-ld;//pnt a feluleten belul
float t3=abs(t1)*9.0;//step korrekcio, felulet kozeleben korrigal, 2=szemcses, 5=durva 3^2
if(t3<step) step=t3;
if(t1>=0.0)//hit
{
pnt-=dpnt*step;
step*=0.5;//fele akkora lepessel tovabb

if(step<0.5) hit=1;//0.1 eleg kicsi, itt a felulet!
}
pnt+=dpnt*step;
}
spnt=pnt;
t2=length(spnt-ee.src);

if(hit==1)//real hit
if(t2<ee.tmin)
if(t2>0.1)//self, 0.1 kell!
{
ee.col=col;
ee.obj=spnt;
ee.tmin=t2;
ee.hit=1;
ee.type=2;

#if 1
//felulet normal
float rd=getcurve(spnt.y);
axis.y=spnt.y;
vec3 dir=spnt-axis;
dir=normalize(dir);
vec3 obj1=axis + dir*rd;

dir+=vec3(dir.z,0.0,-dir.x)*0.2;//meroleges , mellete levo pnt
dir=normalize(dir);
vec3 obj2=axis + dir*rd;//rd ua!

spnt.y+=0.2;//felette levo pnt
rd=getcurve(spnt.y);
axis.y=spnt.y;
dir=spnt-axis;
dir=normalize(dir);
vec3 obj3=axis + dir*rd;

ee.norm=normalize(cross(obj2-obj1, obj3-obj1));

#endif
}
}
return ee;
}
pack chk_scene(pack ee,float tmin)
{
ee.tmin=1e16;
if(tmin!=0.0) ee.tmin=tmin;
ee.hit=0;
ee.type=0;
ee.col=vec3(0,0,0);

if(ee.ray.y<0.0)//ground
{
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=fract(ee.obj/90.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;

//csempe letores
w1=mod(ee.obj,45.0);
if(w1.x<3.0)
{
float aa=w1.x*pi*0.5/3.0;
ee.norm=vec3(0.0,sin(aa),cos(aa)*0.6);//0 nal fekszik a normal!
ee.norm=normalize(ee.norm);
}
if(w1.x>45.0-3.0)
{
float aa=(w1.x-45.0-3.0)*pi*0.5/3.0;
ee.norm=vec3(0.0,sin(aa),-cos(aa)*0.6);
ee.norm=normalize(ee.norm);
}
if(w1.z<3.0)
{
float aa=w1.z*pi*0.5/3.0;
ee.norm=vec3(0.0,sin(aa),cos(aa)*0.6);//0 nal fekszik a normal!
ee.norm=normalize(ee.norm);
}
if(w1.z>45.0-3.0)
{
float aa=(w1.z-45.0-3.0)*pi*0.5/3.0;
ee.norm=vec3(0.0,sin(aa),-cos(aa)*0.6);
ee.norm=normalize(ee.norm);
}
}




#if 1
vec3 dv=vec3(12000.0,0.0,12000.0);
ee=chk_fig(vec3(0,0,0)+dv,vec3(0.2,0.5,0.7),ee);
ee=chk_ball2(vec3(100,50,100)+dv,50.0,vec3(0.5,0.3,0.2),ee); /////////////
#else
float hh=200.0;
vec3 dbase=vec3(hh,hh,hh);
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/hh)*hh;
for(int x=0;x<6;x++)
{
vec3 bmin=base;
vec3 bmax=bmin+vec3( hh,210,hh);


if(src3.y>-50.0)
if(src3.y<350.0)
{
if(boxchk3(ee,bmin,bmax)==1)
{
ee=chk_fig(bmin+vec3(50,0,50),vec3(0.1,0.6,0.2),ee);
// ee=chk_ball2(bmin+vec3(50,50,50),50.0,vec3(0.1,0.4,0.2),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/hh)*hh;

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)>hh*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;
//lookat=vec3(120,50,-210);
r2=320.0;

float aa=-time*0.3;//lets move, szedulok lol
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 al=0; al<4; al++ )//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
vec3 ray=camz*1.1 + camx*(txc.x-0.5+dx) + camy*(txc.y-0.5+dy);


vec3 w1;
vec3 light=vec3(200,300,-100)+dv;
aa=time*0.3;//lets move
light+=vec3(cos(aa)*350.0,0.0,sin(aa)*350.0);


for( int j=0; j<2; j++ )//j=0 reflect j=1 refract
{
ee.ray=normalize(ray);//restart
ee.src=eye;

float sz=1.0;
vec3 col3=vec3(0,0,0);
vec3 mat_col=vec3(1,1,1);
int type=0;

for( int i=0; i<5; i++ )//N=3 reflection
{

ee=chk_scene(ee,0.0);
if(i!=0)
if(length(ee.src-eye)>200.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)*6.0, vec3(0.0,0.2,0.99)*2.0, t1)*sz;//18 5

break;
}


float shadow=1.0,t6=length(light-ee.src);
// if(t6<1000.0)
if(j==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);
float pw=41e4;
if(ee.type==2) pw=1e4;
col3+=ee.col*vec3(0.99,0.9,0.7)*max(dot(ee.norm,w1),0.0)*pw*sz*shadow*mat_col/(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*mat_col*0.2;//0.9
col3+=vec3(0.6,0.7,0.9)*t2*0.34*sz*mat_col*0.2;//0.34

//ee.type!=2 &&

vec3 ref;
w1=normalize(ee.obj-ee.src);
if(j==0)
{
ref=w1-ee.norm*dot(ee.norm,w1)*2.0;//reflection
}
else
{
int ir=0;
if(dot(ee.norm,ee.src-ee.obj)<0.0) {ee.norm=-ee.norm;ir=1;}//normal az src fele!
vec3 ref_x=w1-ee.norm*dot(ee.norm,w1);//refraction
if(ir==0) ref=w1-ref_x/6.0;//0.04-0.4 0.07 /n
else ref=w1+ref_x*(1.0/5.0);//alulrol kapja *1/(n-1)
ref=normalize(ref);
}

w1=normalize(light-ee.obj);
float spec=max(dot(ref,w1),0.0);
if(j==0) col3+=vec3(0.99,0.9,0.7)*pow(spec,12.0)*12.0*sz*shadow*mat_col;//specular

ee.ray=ref;//new ray src
ee.src=ee.obj+ee.ray*0.02;
if(j==0) sz*=0.6;//7
else sz*=0.892;//6

type=ee.type;//next
mat_col=ee.col;
}//i

col3=col3*0.19;//0.25
col4+=col3*0.5;
}//j
}//al

gl_FragColor = vec4( col4/alias,1);
}



//http://www.iquilezles.org/apps/shadertoy/prese

 

komment

csempezes

sc3n3 2011.12.27. 11:06

A gorbe felulet metszete nem tokeletes, de 3 ora munkabol ennyire futotta,

Minden szabadon felhasznalhato, az itt levo kodokbol.


 



#ifdef GL_ES
precision highp float;
#endif


//#define aliasing

uniform vec2 resolution;
uniform float time;
uniform sampler2D tex0;
//uniform sampler2D tex1;

#define pi 3.1415926

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;
}
vec3 boxchk4(pack ee,vec3 box,vec3 box2,int side)
{
// box2+=box;//max

float t1=0.0;
vec3 w1,norm, obj;

vec3 bbox1;
vec3 bbox2;
if(side==1) {bbox1=box;bbox2=box2;}
else {bbox1=box2;bbox2=box;}

if(ee.ray.x<0.0) t1=(bbox2.x-ee.src.x)/ee.ray.x;
else t1=(bbox1.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 obj;
}



if(ee.ray.y<0.0) t1=(bbox2.y-ee.src.y)/ee.ray.y;
else t1=(bbox1.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 obj;
}


if(ee.ray.z<0.0) t1=(bbox2.z-ee.src.z)/ee.ray.z;
else t1=(bbox1.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 obj;
}

return vec3(-1.0,0,0);
}
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;
}
float getcurve(float t)
{
float e=0.0,e2=0.0;
#if 1
if(t>70.0)//0-80-120
{
t-=95.0; t*=0.8;
float r=32.0;r*=r;
e=sqrt(r-t*t);
}
#else
if(t>95.0)//0-80-120
{
t-=95.0;
float r=38.0;r*=r;
e=sqrt(r-t*t);
}
#endif
else
{
e=20.0+15.0*sin(t*0.1);
e2=22.0+5.0*sin(t*0.3435);
e=max(e,e2);
}

return e;
}
pack chk_fig(vec3 pos,vec3 col,pack ee)
{
vec3 bbox1=pos-vec3(40,0,40);
vec3 bbox2=pos+vec3(40,120,40);

vec3 pnt1=boxchk4(ee,bbox1,bbox2,0);
vec3 pnt2=boxchk4(ee,bbox1,bbox2,1);

// if(t1<ee.tmin) if(t2<ee.tmin)//nem biztos hogy lesz hit, bug
if(pnt1.x!=-1.0 || pnt2.x!=-1.0)
{
vec3 w1=pnt1-ee.src; float t1=dot(w1,w1);
w1=pnt2-ee.src; float t2=dot(w1,w1);
if(t2<t1) {w1=pnt1;pnt1=pnt2;pnt2=w1;}///xchg

vec3 axis=pos;//forgastengely, magassag=y valtozik csak
vec3 pnt=pnt1;
vec3 dpnt=normalize(pnt2-pnt1);
vec3 spnt=vec3(0,0,0);
float tmin=1e16;
float step=12.0;//12 t3 atallitja!
int hit=0;

for(int i=0;i<16;i++)
{
axis.y=pnt.y;
vec3 dir=pnt-axis;// radialis vektor
float ld=dot(dir,dir);//length(dir);dir=normalize(dir);

float rd=getcurve(pnt.y);
float t1=rd*rd-ld;//pnt a feluleten belul
float t3=abs(t1)*9.0;//step korrekcio, felulet kozeleben korrigal, 2=szemcses, 5=durva 3^2
if(t3<step) step=t3;
if(t1>=0.0)//hit
{
pnt-=dpnt*step;
step*=0.5;//fele akkora lepessel tovabb

if(step<0.5) hit=1;//0.1 eleg kicsi, itt a felulet!
}
pnt+=dpnt*step;
}
spnt=pnt;
t2=length(spnt-ee.src);

if(hit==1)//real hit
if(t2<ee.tmin)
if(t2>0.01)//self
{
ee.col=col;
ee.obj=spnt;
ee.tmin=t2;
ee.hit=1;

#if 1
//felulet normal
float rd=getcurve(spnt.y);
axis.y=spnt.y;
vec3 dir=spnt-axis;
dir=normalize(dir);
vec3 obj1=axis + dir*rd;

dir+=vec3(dir.z,0.0,-dir.x)*2.0;//meroleges , mellete levo pnt
dir=normalize(dir);
vec3 obj2=axis + dir*rd;//rd ua!

spnt.y+=2.0;//felette levo pnt
rd=getcurve(spnt.y);
axis.y=spnt.y;
dir=spnt-axis;
dir=normalize(dir);
vec3 obj3=axis + dir*rd;

ee.norm=normalize(cross(obj2-obj1, obj3-obj1));
#endif
}
}
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
{
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=fract(ee.obj/90.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;

//csempe letores
w1=mod(ee.obj,45.0);
if(w1.x<3.0)
{
float aa=w1.x*pi*0.5/3.0;
ee.norm=vec3(0.0,sin(aa),cos(aa)*0.6);//0 nal fekszik a normal!
ee.norm=normalize(ee.norm);
}
if(w1.x>45.0-3.0)
{
float aa=(w1.x-45.0-3.0)*pi*0.5/3.0;
ee.norm=vec3(0.0,sin(aa),-cos(aa)*0.6);
ee.norm=normalize(ee.norm);
}
if(w1.z<3.0)
{
float aa=w1.z*pi*0.5/3.0;
ee.norm=vec3(0.0,sin(aa),cos(aa)*0.6);//0 nal fekszik a normal!
ee.norm=normalize(ee.norm);
}
if(w1.z>45.0-3.0)
{
float aa=(w1.z-45.0-3.0)*pi*0.5/3.0;
ee.norm=vec3(0.0,sin(aa),-cos(aa)*0.6);
ee.norm=normalize(ee.norm);
}
}


float hh=200.0;
vec3 dbase=vec3(hh,hh,hh);
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/hh)*hh;
for(int x=0;x<8;x++)//8 12,max 20 eleg
{
vec3 bmin=base;
vec3 bmax=bmin+vec3( hh,210,hh);


if(src3.y>-50.0)
if(src3.y<350.0)
{
if(boxchk3(ee,bmin,bmax)==1)
{
ee=chk_fig(bmin+vec3(50,0,50),vec3(1,0,0),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/hh)*hh;

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)>hh*8.0) break;//messze ment
}

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;
lookat=vec3(120,50,-210);
r2=320.0;

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,500,-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)>200.0*8.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<3000.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)*91e4*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/prese 

komment

curve

sc3n3 2011.12.27. 10:29

Bonyolitsuk

 

 

#ifdef GL_ES
precision highp float;
#endif


//#define aliasing

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;
}
vec3 boxchk4(pack ee,vec3 box,vec3 box2,int side)
{
// box2+=box;//max

float t1=0.0;
vec3 w1,norm, obj;

vec3 bbox1;
vec3 bbox2;
if(side==1) {bbox1=box;bbox2=box2;}
else {bbox1=box2;bbox2=box;}

if(ee.ray.x<0.0) t1=(bbox2.x-ee.src.x)/ee.ray.x;
else t1=(bbox1.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 obj;
}



if(ee.ray.y<0.0) t1=(bbox2.y-ee.src.y)/ee.ray.y;
else t1=(bbox1.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 obj;
}


if(ee.ray.z<0.0) t1=(bbox2.z-ee.src.z)/ee.ray.z;
else t1=(bbox1.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 obj;
}

return vec3(-1.0,0,0);
}
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;
}
float getcurve(float t)
{
float e=0.0,e2=0.0;
#if 1
if(t>70.0)//0-80-120
{
t-=95.0; t*=0.8;
float r=32.0;r*=r;
e=sqrt(r-t*t);
}
#else
if(t>95.0)//0-80-120
{
t-=95.0;
float r=38.0;r*=r;
e=sqrt(r-t*t);
}
#endif
else
{
e=20.0+15.0*sin(t*0.1);
e2=22.0+5.0*sin(t*0.3435);
e=max(e,e2);
}

return e;
}
pack chk_fig(vec3 pos,vec3 col,pack ee)
{
vec3 bbox1=pos-vec3(40,0,40);
vec3 bbox2=pos+vec3(40,120,40);

vec3 pnt1=boxchk4(ee,bbox1,bbox2,0);
vec3 pnt2=boxchk4(ee,bbox1,bbox2,1);
vec3 w1=pnt1-ee.src; float t1=dot(w1,w1);
w1=pnt2-ee.src; float t2=dot(w1,w1);
if(t2<t1) {w1=pnt1;pnt1=pnt2;pnt2=w1;}///xchg

// if(t1<ee.tmin) if(t2<ee.tmin)//nem biztos hogy lesz hit, bug
if(pnt1.x!=-1.0 || pnt2.x!=-1.0)
{
vec3 axis=pos;//forgastengely, magassag=y valtozik csak
vec3 pnt=pnt1;
vec3 dpnt=normalize(pnt2-pnt1);
vec3 spnt=vec3(0,0,0);
float tmin=1e16;
float step=12.0;//12 t3 atallitja!
int hit=0;

for(int i=0;i<16;i++)
{
axis.y=pnt.y;
vec3 dir=pnt-axis;// radialis vektor
  float ld=dot(dir,dir);//length(dir);dir=normalize(dir);

float rd=getcurve(pnt.y);
  float t1=rd*rd-ld;//pnt a feluleten belul
  float t3=abs(t1)*9.0;//step korrekcio, felulet kozeleben korrigal, 2=szemcses, 5=durva  3^2
if(t3<step) step=t3;
if(t1>=0.0)//hit
{
pnt-=dpnt*step;
step*=0.5;//fele akkora lepessel tovabb

if(step<0.5) hit=1;//0.1 eleg kicsi, itt a felulet!
}
pnt+=dpnt*step;
}
spnt=pnt;
float t2=length(spnt-ee.src);

if(hit==1)//real hit
if(t2<ee.tmin)
if(t2>0.01)//self
{
ee.col=col;
ee.obj=spnt;
ee.tmin=t2;
ee.hit=1;

#if 1
//felulet normal
float rd=getcurve(spnt.y);
axis.y=spnt.y;
vec3 dir=spnt-axis;
dir=normalize(dir);
vec3 obj1=axis + dir*rd;

dir+=vec3(dir.z,0.0,-dir.x)*2.0;//meroleges , mellete levo pnt
dir=normalize(dir);
vec3 obj2=axis + dir*rd;//rd ua!

spnt.y+=2.0;//felette levo pnt
rd=getcurve(spnt.y);
axis.y=spnt.y;
dir=spnt-axis;
dir=normalize(dir);
vec3 obj3=axis + dir*rd;

ee.norm=normalize(cross(obj2-obj1, obj3-obj1));
#endif
}
}
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
{
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=fract(ee.obj/90.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;
}


float hh=200.0;
vec3 dbase=vec3(hh,hh,hh);
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/hh)*hh;
for(int x=0;x<8;x++)//8 12,max 20 eleg
{
vec3 bmin=base;
vec3 bmax=bmin+vec3( hh,210,hh);


if(src3.y>-50.0)
if(src3.y<350.0)
{
if(boxchk3(ee,bmin,bmax)==1)
{
ee=chk_fig(bmin+vec3(50,0,50),vec3(1,0,0),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/hh)*hh;

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)>hh*8.0) break;//messze ment
}

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;
lookat=vec3(120,50,-210);
r2=320.0;

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,500,-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)>200.0*8.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<3000.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)*91e4*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

 

komment

?

sc3n3 2011.12.26. 19:52

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

 

komment

scene

sc3n3 2011.12.26. 18:12

 

komment

www.iquilezles.org/apps/shadertoy/

sc3n3 2011.12.26. 17:12

http://www.iquilezles.org/apps/shadertoy/

 

 

#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;
};


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+=30.0+30.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
{
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;
}

#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<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;
if(t3.y<t3.x) t3.x=t3.y;
if(t3.z<t3.x) t3.x=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(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=length(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*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

 

komment

süti beállítások módosítása