Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Shader "ShaderMan/simple hexagon raymarch"
  3.     {
  4.  
  5.     Properties{
  6.     _iMouse ("iMouse", Vector) = (0,0,0,0)
  7.     }
  8.  
  9.     SubShader
  10.     {
  11.     Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
  12.  
  13.     Pass
  14.     {
  15.     ZWrite Off
  16.     Blend SrcAlpha OneMinusSrcAlpha
  17.  
  18.     CGPROGRAM
  19.     #pragma vertex vert
  20.     #pragma fragment frag
  21.     #include "UnityCG.cginc"
  22.  
  23.     struct VertexInput {
  24.     fixed4 vertex : POSITION;
  25.     fixed2 uv:TEXCOORD0;
  26.     fixed4 tangent : TANGENT;
  27.     fixed3 normal : NORMAL;
  28.     //VertexInput
  29.     };
  30.  
  31.  
  32.     struct VertexOutput {
  33.     fixed4 pos : SV_POSITION;
  34.     fixed2 uv:TEXCOORD0;
  35.     //VertexOutput
  36.     };
  37.  
  38.     //Variables
  39. float4 _iMouse;
  40.  
  41.     // tweaked copy of https://www.shadertoy.com/view/Xds3zN by inigo quilez - iq/2013
  42. // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  43.  
  44. fixed3 hue(fixed3 color4, fixed shift) {
  45.  
  46.     const fixed3  kRGBToYPrime = fixed3 (0.299, 0.587, 0.114);
  47.     const fixed3  kRGBToI     = fixed3 (0.596, -0.275, -0.321);
  48.     const fixed3  kRGBToQ     = fixed3 (0.212, -0.523, 0.311);
  49.  
  50.     const fixed3  kYIQToR   = fixed3 (1.0, 0.956, 0.621);
  51.     const fixed3  kYIQToG   = fixed3 (1.0, -0.272, -0.647);
  52.     const fixed3  kYIQToB   = fixed3 (1.0, -1.107, 1.704);
  53.  
  54.     // Convert to YIQ
  55.     fixed   YPrime  = dot (color4, kRGBToYPrime);
  56.     fixed   I      = dot (color4, kRGBToI);
  57.     fixed   Q      = dot (color4, kRGBToQ);
  58.  
  59.     // Calculate the hue and chroma
  60.     fixed   hue     = atan2 (Q, I);
  61.     fixed   chroma  = sqrt (I * I + Q * Q);
  62.  
  63.     // Make the user's adjustments
  64.     hue += shift;
  65.  
  66.     // Convert back to YIQ
  67.     Q = chroma * sin (hue);
  68.     I = chroma * cos (hue);
  69.  
  70.     // Convert back to RGB
  71.     fixed3    yIQ   = fixed3 (YPrime, I, Q);
  72.     color4.r = dot (yIQ, kYIQToR);
  73.     color4.g = dot (yIQ, kYIQToG);
  74.     color4.b = dot (yIQ, kYIQToB);
  75.  
  76.     return color4;
  77. }
  78.  
  79. fixed sdHexPrism( fixed3 p, fixed2 h )
  80. {
  81.     fixed3 q = abs(p);
  82.     return max(q.z-h.y,max((q.x*0.866025+q.y*0.5),q.y)-h.x);
  83. }
  84.  
  85. fixed sdBox( fixed3 p, fixed3 b )
  86. {
  87.   fixed3 d = abs(p) - b;
  88.   return min(max(d.x,max(d.y,d.z)),0.0) +
  89.          length(max(d,0.0));
  90. }
  91.  
  92. fixed opS( fixed d1, fixed d2 )
  93. {
  94.     return max(-d1,d2);
  95. }
  96.  
  97. fixed2 opU( fixed2 d1, fixed2 d2 )
  98. {
  99.     return (d1.x<d2.x) ? d1 : d2;
  100. }
  101.  
  102. fixed2 mapper( in fixed3 pos )
  103. {
  104.     //pos.x += sin(pos.z+_Time.y)*0.2;
  105.     //pos.y += cos(pos.z+_Time.y)*0.2;
  106.    
  107.     fixed height = .42;
  108.     fixed depth = .75;
  109.     fixed t = 0.02 + sin(_Time.y)*0.01;
  110.     pos.z = fmod(pos.z,depth*2.)-0.5*depth*2.;
  111.  
  112.     fixed cyl = sdHexPrism( pos, fixed2(height-t, depth+t));
  113.     fixed scyl = sdHexPrism( pos, fixed2(height-t*2.0, depth+t+.001));
  114.    
  115.     fixed2 res = fixed2(opS(scyl,cyl),1.5);
  116.     fixed2 final = res;
  117.    
  118.     for (int i = 1; i < 3; i++) {
  119.  
  120. //        fixed c = cos(pos.z+_Time.y*0.5);
  121. //    fixed s = sin(pos.z+_Time.y*0.5);
  122. //    fixed2x2  m = fixed2x2(c,-s,s,c);
  123. //    pos = fixed3(m*pos.xy,pos.z);
  124.        
  125.         height -= 0.1;
  126.         depth -= 0.19;
  127.         cyl = sdHexPrism( pos, fixed2(height-t, depth+t));
  128.         scyl = sdHexPrism( pos, fixed2(height-t*2.0, depth+t+.001));
  129.    
  130.        final = opU(final, fixed2(opS(scyl,cyl),2.5));
  131.    
  132.     }
  133.      
  134.    return final;
  135. }
  136.  
  137. fixed2 castRay( in fixed3 ro, in fixed3 rd )
  138. {
  139.     fixed tmin = 0.0;
  140.     fixed tmax = 100.0;
  141.    
  142.     fixed t = tmin;
  143.     fixed m = -1.0;
  144.     [unroll(100)]
  145. for( int i=0; i<100; i++ )
  146.     {
  147.    fixed2 res = mapper( ro+rd*t );
  148.         if(  t>tmax ) break;
  149.         t += res.x;
  150.    m = res.y;
  151.     }
  152.  
  153.     if( t>tmax ) m=-1.0;
  154.     return fixed2( t, m );
  155. }
  156.  
  157. fixed3 calcNormal( in fixed3 pos )
  158. {
  159. fixed3 eps = fixed3( 0.01, 0.0, 0.0 );
  160. fixed3 nor = fixed3(
  161.     mapper(pos+eps.xyy).x - mapper(pos-eps.xyy).x,
  162.     mapper(pos+eps.yxy).x - mapper(pos-eps.yxy).x,
  163.     mapper(pos+eps.yyx).x - mapper(pos-eps.yyx).x );
  164. return normalize(nor);
  165. }
  166.  
  167. fixed calcAO( in fixed3 pos, in fixed3 nor )
  168. {
  169. fixed occ = 0.0;
  170.     fixed sca = 1.0;
  171.     [unroll(100)]
  172. for( int i=0; i<5; i++ )
  173.     {
  174.         fixed hr = 0.01 + 0.12*fixed(i)/4.0;
  175.         fixed3 aopos =  nor * hr + pos;
  176.         fixed dd = mapper( aopos ).x;
  177.         occ += -(dd-hr)*sca;
  178.         sca *= .95;
  179.     }
  180.     return clamp( 1.0 - 3.0*occ, 0.0, 1.0 );    
  181. }
  182.  
  183.  
  184.  
  185.  
  186. fixed3 render( in fixed3 ro, in fixed3 rd )
  187. {
  188.     fixed3 col = fixed3(1.0,1.0,1.0);
  189.     fixed2 res = castRay(ro,rd);
  190.     fixed t = res.x;
  191.     fixed m = res.y;
  192.    
  193.     if( m>-0.5 )
  194.     {
  195.         fixed3 pos = ro + t*rd;
  196.         fixed3 nor = calcNormal( pos );
  197.         fixed3 ref = reflect( rd, nor );
  198.        
  199.         // material        
  200.         fixed occ = calcAO( pos, nor );
  201.         col = 1.0 - hue(fixed3(0.0,1.0,1.0),_Time.y*0.02+pos.z)*occ;
  202.     }
  203.  
  204. return fixed3( clamp(col,0.0,1.0) );
  205. }
  206.  
  207. fixed3x3 setCamera( in fixed3 ro, in fixed3 ta, fixed cr )
  208. {
  209. fixed3 cw = normalize(ta-ro);
  210. fixed3 cp = fixed3(sin(cr), cos(cr),0.0);
  211. fixed3 cu = normalize( cross(cw,cp) );
  212. fixed3 cv = normalize( cross(cu,cw) );
  213.     return fixed3x3( cu, cv, cw );
  214. }
  215.  
  216.  
  217.  
  218.  
  219.  
  220.     VertexOutput vert (VertexInput v)
  221.     {
  222.     VertexOutput o;
  223.     o.pos = UnityObjectToClipPos (v.vertex);
  224.     o.uv = v.uv;
  225.     //VertexFactory
  226.     return o;
  227.     }
  228.     fixed4 frag(VertexOutput i) : SV_Target
  229.     {
  230.    
  231.     fixed2 q = i.uv/1;
  232.     fixed2 p = -1.0+2.0*q;
  233.     p.x *= 1/1;
  234.     fixed2 mo = _iMouse.xy/1;
  235.  
  236.     // camera
  237.     fixed3 ro = fixed3(0., 0.,_Time.y );
  238.    
  239.     fixed3 ta = ro+fixed3( 0., 0.,1. );
  240.    
  241.     // camera-to-world transformation
  242.     //fixed3x3 ca = setCamera( ro, ta, 3.14159/2.0 );   //Skipping the camera to world stuff
  243.  
  244.     // ray direction
  245.     //fixed3 rd = ca * normalize(fixed3(p.xy,4.5));   //Skipping the camera to world stuff, and normalization
  246.     fixed3 rd = fixed3(p.xy,4.5);
  247.  
  248.     // render
  249.     fixed3 col = render( ro, rd );
  250.  
  251.     SV_Target=fixed4( col, 1.0 );
  252.  
  253.  
  254.     }
  255.     ENDCG
  256.     }
  257.   }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement