Advertisement
Guest User

Untitled

a guest
May 13th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct material{
  2.     vec3 color;
  3. };
  4.  
  5. struct sphere{
  6.     vec3 pos;
  7.     float radius;
  8.     float radiussq;
  9.     material material;
  10. };
  11.    
  12. struct panel{
  13.     vec3 pos;
  14.     vec3 normal;
  15.     float radius;
  16.     vec3 uvn1; //normal for uv
  17.     vec3 uvn2;
  18.     material material;
  19. };
  20.  
  21. struct rayresult{
  22.     bool hit; //if hit
  23.     vec3 N; //normal
  24.     vec3 P; //hit pos
  25.     vec3 o; //origin
  26.     vec3 v; //direction
  27.     float dist; //distance
  28.     vec2 uv; //material texture uv pos
  29.     material material;
  30. };
  31.  
  32. struct light{
  33.     vec3 pos;
  34.     float brightness;
  35.     vec3 color;
  36. };
  37.  
  38. const int sphere_count=20;
  39. sphere spheres[sphere_count];
  40.  
  41. const int panel_count=1;
  42. panel panels[panel_count];
  43.  
  44. const int light_count=3;
  45. light lights[light_count];
  46.  
  47.    
  48.  
  49. mat4 cam = mat4(
  50.     1,0,0,0,
  51.     0,1,0,0,
  52.     0,0,1,0,
  53.     0,0,0,1
  54. );
  55.  
  56. mat4 addmv(mat4 mat, vec3 v){
  57.     mat4 n = mat;
  58.     n[3]=n[3]+vec4(v,0);
  59.     return n;
  60. }
  61.  
  62. mat4 mat4v(vec3 v){
  63.     return mat4(
  64.         1,0,0,v.x,
  65.         0,1,0,v.y,
  66.         0,0,1,v.z,
  67.         0,0,0,1
  68.     );
  69. }
  70.  
  71. vec3 matpos(mat4 mat){
  72.     return mat[3].xyz;
  73. }
  74.  
  75. const float planedist = 1.;
  76.  
  77. //float tick = iTime/5.0;
  78.  
  79. bool raycheck(vec3 pos, vec3 dir){
  80.     for (int i=0;i<sphere_count;i++){
  81.         sphere c=spheres[i];
  82.         vec3 L = c.pos-pos;
  83.         float tca = dot(L,dir);
  84.         if (tca>0.){
  85.             float d2 = dot(L,L)-tca*tca;
  86.             if (d2<c.radiussq){
  87.                 return true;
  88.             }
  89.         }
  90.     }
  91.    
  92.     for (int i=0;i<panel_count;i++){
  93.         panel p=panels[i];
  94.         vec3 op=p.pos-pos;
  95.         if (dot(p.normal,dir)<0.){
  96.             float dist = dot(op,p.normal)/dot(dir,p.normal);
  97.             vec3 P = pos+dist*dir;
  98.             if (length(P-p.pos)<p.radius){
  99.                 return true;
  100.             }
  101.         }
  102.     }
  103.    
  104.    
  105.     return false;
  106. }
  107.  
  108. rayresult ray(vec3 pos, vec3 dir){
  109.    
  110.     rayresult ray;
  111.     float zbuffer = 9999999999.;
  112.    
  113.     ray.hit=false;
  114.     ray.o=pos;
  115.     ray.v=dir;
  116.    
  117.     for (int i=0;i<sphere_count;i++){
  118.         sphere c=spheres[i];
  119.         vec3 L = c.pos-pos;
  120.         float tca = dot(L,dir);
  121.         if (tca>0.){
  122.             float d2 = dot(L,L)-tca*tca;
  123.             if (d2<c.radiussq){
  124.                 float dist = tca-sqrt(c.radiussq-d2);
  125.                 if (dist<zbuffer){
  126.                     zbuffer=dist;
  127.                     ray.P = pos+dist*dir;
  128.                     ray.N = normalize(ray.P-c.pos);
  129.                     ray.material=c.material;
  130.                     ray.dist=dist;
  131.                     ray.hit=true;
  132.                     ray.uv=vec2(atan(ray.N.z,ray.N.x),acos(ray.N.y));
  133.                 }
  134.             }
  135.         }
  136.     }
  137.    
  138.     for (int i=0;i<panel_count;i++){
  139.         panel p=panels[i];
  140.         vec3 op=p.pos-pos;
  141.         if (dot(p.normal,dir)<0.){
  142.             float dist = dot(op,p.normal)/dot(dir,p.normal);
  143.             vec3 P = pos+dist*dir;
  144.             if ( dist<zbuffer && length(P-p.pos)<p.radius){
  145.                 zbuffer = dist;
  146.                 ray.P = P;
  147.                 ray.N = p.normal;
  148.                 ray.material=p.material;
  149.                 ray.dist=dist;
  150.                 ray.hit=true;
  151.                 vec3 normop = normalize(p.pos-P);
  152.                 ray.uv=vec2(atan(dot(p.uvn1,normop),dot(p.uvn2,normop)),length(p.pos-P)/p.radius*2.);
  153.             }
  154.         }
  155.     }
  156.    
  157.     return ray;
  158. }
  159.  
  160. const float ambient = 0.05;
  161.  
  162. vec3 doLight(rayresult o,float dit,bool next){//Returns color
  163.     vec3 col = vec3(0,0,0);
  164.     for (int i=0;i<light_count;i++){
  165.         vec3 v = lights[i].pos-o.P;
  166.         vec3 dir = normalize(v);
  167.         v += dir*dit;
  168.         if (!raycheck(o.P,dir)){
  169.             col +=  texture(iChannel1,o.uv).xyz
  170.                     *o.material.color
  171.                     *lights[i].color
  172.                     *lights[i].brightness
  173.                     /length(v)/length(v)
  174.                     *dot(v,o.N);
  175.         }
  176.     }
  177.     if (!next){
  178.         col+=texture(iChannel0,reflect(o.v,o.N)).xyz*ambient*o.material.color;
  179.     }
  180.     return col;
  181. }
  182.  
  183. const int maxrec = 5; //maximum recursion
  184.  
  185. vec3 manager(rayresult o){
  186.     vec3 col = vec3(0,0,0);
  187.     bool en = true;
  188.     float dit=0.;
  189.     int rec = 1;
  190.     while (en && maxrec>rec){
  191.         col += doLight(o,dit,rec<=1)/float(rec);
  192.         vec3 ndir = reflect(o.v,o.N);
  193.         o = ray(o.P+ndir*0.1,ndir);
  194.         en = o.hit;
  195.         dit+=o.dist;
  196.         rec++;
  197.     }
  198.     return col;
  199. }
  200.  
  201. void updatescene(float tick){
  202.     for (int i=0;i<sphere_count;i++){
  203.         float ii = float(i);
  204.         spheres[i].pos=vec3(cos(tick+ii)*29.,sin(tick*5.+ii)*12.,50.1+ii*2.9+sin(ii*4.4567));
  205.         spheres[i].radius = cos(tick*2.+ii)*2.+5.;
  206.         spheres[i].radiussq = spheres[i].radius*spheres[i].radius;
  207.         spheres[i].material.color=vec3(1,sin(ii),cos(ii));
  208.     }
  209.     lights[0].pos = vec3(0,20,45);
  210.     lights[0].brightness = 25.;
  211.     lights[0].color = vec3(1,1,1);
  212.    
  213.     //lights[1].pos = vec3(0,-20,45);
  214.     //lights[1].brightness = 25.;
  215.     //lights[1].color = vec3(1,1,1);
  216.    
  217.     //lights[2].pos = vec3(10,10,25);
  218.     //lights[2].brightness = 25.;
  219.     //lights[2].color = vec3(1,1,1);
  220.     //for (int i=1;i<light_count;i++){
  221.        
  222.     //}
  223.     panels[0].pos = vec3(0,-30,80);
  224.     panels[0].normal = vec3(0,1,0);
  225.     panels[0].radius = 30.;
  226.     panels[0].material.color = vec3(1,1,1);
  227.     panels[0].uvn1 = cross(panels[0].normal,normalize(vec3(454,957,234)));
  228.     panels[0].uvn2 = cross(panels[0].normal,panels[0].uvn1);
  229. }
  230.  
  231. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  232. {
  233.     float tick = iTime/15.;
  234.    
  235.    
  236.     updatescene(tick);
  237.    
  238.     vec2 mouseuv = (iMouse.xy-iResolution.xy/2.0)/iResolution.xx*2.;
  239.    
  240.    
  241.     mat4 ncam = cam
  242.         *build_transform(vec3(0,0,0),vec3(0,-mouseuv.x*3.1415928,0))
  243.         *build_transform(vec3(0,0,0),vec3(-mouseuv.y*3.1415928,0,0))
  244.         ;
  245.     ncam = ncam
  246.         *build_transform(vec3(0,0,-80),vec3(0,0,0))
  247.         ;
  248.     ncam[3].xyz = ncam[3].xyz + vec3(0,0,80);
  249.    
  250.    
  251.     vec3 campos = matpos(ncam);
  252.    
  253.     vec2 uv = (fragCoord-iResolution.xy/2.0)/iResolution.xx*2.;
  254.    
  255.     vec3 v = normalize(uv.x*ncam[0].xyz+uv.y*ncam[1].xyz+ncam[2].xyz*planedist);
  256.    
  257.     vec3 col = vec3(0,0,0);
  258.    
  259.     rayresult o = ray(campos,v);
  260.    
  261.    
  262.     if (o.hit){
  263.         col += manager(o);
  264.     }else{
  265.         col = texture(iChannel0,o.v).xyz;
  266.     }
  267.  
  268.  
  269.     fragColor = vec4(col,1.0);
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement