netgrind

HYPRGLTCH

Nov 9th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.73 KB | None | 0 0
  1. Shader "Camp Cult/Rays/HYPRGLTCH" {
  2. Properties {
  3.     _Color1("Color 1", Color) = (1,1,1,1)
  4.     _Color2("Color 2", Color) = (1,0,0,1)
  5.     _Shape("X-curve Y-shift Z-mul",Vector)=(.75,1.5,.75,0)
  6.     _x("X-curve Y-shift Z-mul",Vector)=(.75,1.5,.75,0)
  7.     _Fog("Fog",Float) = 10
  8.  }
  9. Category {
  10. Blend SrcAlpha OneMinusSrcAlpha
  11. Tags {"Queue"="Transparent"}
  12. SubShader {
  13. Pass {
  14. CGPROGRAM
  15. #include "UnityCG.cginc"
  16. #include "ray.cginc"
  17. #pragma vertex vert_img
  18. #pragma fragment frag
  19. #define count 1.
  20. #define pi 3.14159
  21. uniform float4 _Color1;
  22. uniform float4 _Color2;
  23. uniform float4 _x;
  24. uniform float4 _Shape;
  25. uniform float _Fog;
  26.  
  27. // Hacked apart by cale bradbury - netgrind.net/2015
  28. // Based on work by inigo quilez - iq/2013
  29. // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  30. // More info here: http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
  31.  
  32. float fr(float3 p, float v){
  33.     return sdBox(p,v.rrr);
  34. }
  35.  
  36. float2 map( in float3 pos, float t )
  37. {
  38.     float space = _Shape.y;
  39.     float size = _Shape.z+sin(length(pos)*.2-_Time.y*pi*2.+pos.x*.2)*.1+.1;
  40.     float2 res = float2(pos.y>-100.,1.1);//float2( pos.y+.2+sin(pos.x*sin(1.+pos.x*4.+pos.z*3.)+cos(pos.z*2.+_Time.y*3.141*2.0)+_Time.y*3.141*2.0)*.03, 0. );  
  41.  
  42.     float3 p = pos;
  43.     p.xy = opSpin(pos.xy,_Time.y*pi*_x.z + pos.z*_x.x*pi);
  44.     p.x = fmod(abs(p.x),space)-space*.5;
  45.     p.z = fmod(abs(p.z),space)-space*.5;
  46.     p.y = fmod(abs(p.y),space)-space*.5;
  47.    
  48.    
  49.     float vert = fr(float3(p.x,0.0,p.z),size);
  50.     float hori = fr(float3(0.0,p.y,p.z),size);
  51.     float dept = fr(float3(p.x,p.y,0.0),size);
  52.    
  53.     float2 s = float2(smin3(vert,hori,dept,_x.w),1.);
  54.     /*
  55.     pos.xy = opSpin(pos.xy,pi*.25);
  56.     pos.xz = opSpin(pos.xz,pi*.25);
  57.     pos.yz = opSpin(pos.yz,_Time.y*pi*.5);
  58.     float2 s = float2(sdBox(pos,float(1.0).rrr),1.);*/
  59.    
  60.     res = opU( res, s );
  61.     return res;
  62. }
  63.  
  64. float2 castRay( in float3 ro, in float3 rd, float time )
  65. {
  66.     float tmin = 0.01;
  67.     float tmax = 110.0;
  68.    
  69. #if 0
  70.     float tp1 = (0.0-ro.y)/rd.y; if( tp1>0.0 ) tmax = min( tmax, tp1 );
  71.     float tp2 = (1.6-ro.y)/rd.y; if( tp2>0.0 ) { if( ro.y>1.6 ) tmin = max( tmin, tp2 );
  72.                                                  else           tmax = min( tmax, tp2 ); }
  73. #endif
  74.  
  75.     float precis = 0.00000001;
  76.     float t = tmin;
  77.     float m = -1.0;
  78.     float3 glitch = float3(0.,0.,0.);
  79.     for( int i=0; i<100; i++ )
  80.     {
  81.         float2 res = map( ro+rd*t+glitch, time );
  82.         glitch.x = floor(cos(floor(rd.z*t*343.36234)/1324.36234+_Time.y*pi*2.)*4.)/4.;
  83.         glitch.y = floor(cos(floor(rd.z*t*134.36)/1428.36234+floor(_Time.y*pi*4.)*pi*2.)*4.)/4.;
  84.         glitch = fmod(glitch,_Shape.yyy);
  85.         res = opU(res,float2(map( ro+rd*t+glitch, time ).x,.5));
  86.         glitch.x = floor(sin(floor(rd.z*t*134.36234)/14.36234+floor(_Time.y*pi*4.)*pi*2.)*4.)/4.;
  87.         glitch.y = floor(cos(floor(rd.z*t*268.234)/28.36234+(_Time.y*pi*6.)*pi*2.)*4.)/4.;
  88.         glitch = fmod(glitch,_Shape.yyy);
  89.         res = opU(res,float2(map( ro+rd*t+glitch, time ).x,0.));
  90.         //if( res.x<precis || t>tmax ) break;
  91.         if(t>tmax)break;
  92.         t += res.x;
  93.         m = res.y;
  94.         glitch.x = floor(sin(floor(rd.z*t*344.234)/324.36234+floor(_Time.y*pi*6.)*pi*2.)*4.)/4.;
  95.         glitch.y = floor(sin(floor(rd.z*t*856.3234)/428.36234+floor(_Time.y*pi*4.)*pi*2.)*4.)/4.;
  96.         glitch = fmod(glitch,_Shape.yyy);
  97.     }
  98.  
  99.     if( t>tmax ) m=m;
  100.     return float2( t, m );
  101. }
  102.  
  103. float3 calcNormal( in float3 pos, in float time )
  104. {
  105.     float3 eps = float3( 0.1, 0.0,0.0 );
  106.     float3 nor = float3(
  107.         map(pos+eps.xyy, time).x - map(pos-eps.xyy, time).x,
  108.         map(pos+eps.yxy, time).x - map(pos-eps.yxy, time).x,
  109.         map(pos+eps.yyx, time).x - map(pos-eps.yyx, time).x );
  110.     return normalize(nor);
  111. }
  112.  
  113. float3 render( in float3 ro, in float3 rd, float time )
  114. {
  115.     float3 col = float3(1.0,1.0, 1.0);
  116.     float2 res = castRay(ro,rd, time);
  117.     float t = res.x;
  118.     float m = res.y;
  119.     if( m>-0.5 )
  120.     {
  121.         float3 pos = ro + t*rd;
  122.         float3 nor = calcNormal( pos, time);
  123.         //float3 ref = reflect( rd, nor );
  124.        
  125.         // material        
  126.         col = float3(1.0,1.0,1.0);
  127.         //m = max(m,.05)*(nor.z*.5+.55);
  128.         col.r = floor(m+.1);
  129.         col.g = 1.0-(abs(m-.5)*2.);
  130.         col.b = 1.0-ceil(m);
  131.         col = abs(floor(fmod(abs(pos*.2),2.0.rrr))-col);
  132.         //col.rgb = sin(m*float3(0.,.33,.66)*pi*2.)*.5+.5;
  133.         //col.rgb = m.rrr;
  134.         col.rgb *= min(1.0,pow(max(0.,1.0-t/_Fog),2.));
  135.     }
  136.     //col*=pow(1.0-res.x*.01,8.);
  137.     return float3( clamp(col,0.0,1.0) );
  138. }
  139.  
  140. float3x3 setCamera( in float3 ro, in float3 ta, float cr )
  141. {
  142.     float3 cw = normalize(ta-ro);
  143.     float3 cp = float3(sin(cr), cos(cr),0.0);
  144.     float3 cu = normalize( cross(cw,cp) );
  145.     float3 cv = normalize( cross(cu,cw) );
  146.     return float3x3( cu, cv, cw );
  147. }
  148.  
  149. fixed4 frag (v2f_img i) : COLOR
  150. {
  151.     float2 q = i.uv;
  152.     float2 p = -1.0+2.0*q;
  153.     float time = _Time.y;
  154.  
  155.     // camera  
  156.     float3 ro = float3( 0.0,0.,10.+_Shape.w);
  157.     float3 ta = float3( 0, -3., 10.+_Shape.w+ 10.);
  158.    
  159.     // camera-to-world transformation
  160.     float3x3 ca = setCamera( ro, ta, 0.0 );
  161.     // ray direction
  162.     float velocity = _Shape.x;//sin(_Time.y*pi*.25)*.48+.48;
  163.    
  164.     float fov = 100.0;
  165.     float mu = 500.;
  166.     float rayZ = tan ((90.0 - 0.5 * fov) * 0.01745329252);
  167.     float3 rd = mul( normalize( float3(p.xy,-rayZ)*mu ),ca);
  168.     rd = float3(rd.xy,rsqrt (1.0 - velocity * velocity) * (rd.z + velocity));
  169.     rd = normalize(rd);
  170.    
  171.    
  172.     // render  
  173.     float3 col = float3(0.,0.,0.);
  174.    
  175.     float s = .001;
  176.     for(float i = 0.;i<count;i++){
  177.      col += render( ro, rd, time+s*i );
  178.     }
  179.     col/=count;
  180.     col.rgb = max(0.,min(1.,col));
  181.    
  182.     col = pow( col, float(0.4545).rrr );
  183.  
  184.     return lerp(_Color1,_Color2,float4( col, 1.0 ));
  185. }
  186. ENDCG
  187. }
  188. }
  189. }
  190. FallBack "Unlit"
  191. }
Advertisement
Add Comment
Please, Sign In to add comment