Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "ShaderMan/simple hexagon raymarch"
- {
- Properties{
- _iMouse ("iMouse", Vector) = (0,0,0,0)
- }
- SubShader
- {
- Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
- Pass
- {
- ZWrite Off
- Blend SrcAlpha OneMinusSrcAlpha
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #include "UnityCG.cginc"
- struct VertexInput {
- fixed4 vertex : POSITION;
- fixed2 uv:TEXCOORD0;
- fixed4 tangent : TANGENT;
- fixed3 normal : NORMAL;
- //VertexInput
- };
- struct VertexOutput {
- fixed4 pos : SV_POSITION;
- fixed2 uv:TEXCOORD0;
- //VertexOutput
- };
- //Variables
- float4 _iMouse;
- // tweaked copy of https://www.shadertoy.com/view/Xds3zN by inigo quilez - iq/2013
- // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
- fixed3 hue(fixed3 color4, fixed shift) {
- const fixed3 kRGBToYPrime = fixed3 (0.299, 0.587, 0.114);
- const fixed3 kRGBToI = fixed3 (0.596, -0.275, -0.321);
- const fixed3 kRGBToQ = fixed3 (0.212, -0.523, 0.311);
- const fixed3 kYIQToR = fixed3 (1.0, 0.956, 0.621);
- const fixed3 kYIQToG = fixed3 (1.0, -0.272, -0.647);
- const fixed3 kYIQToB = fixed3 (1.0, -1.107, 1.704);
- // Convert to YIQ
- fixed YPrime = dot (color4, kRGBToYPrime);
- fixed I = dot (color4, kRGBToI);
- fixed Q = dot (color4, kRGBToQ);
- // Calculate the hue and chroma
- fixed hue = atan2 (Q, I);
- fixed chroma = sqrt (I * I + Q * Q);
- // Make the user's adjustments
- hue += shift;
- // Convert back to YIQ
- Q = chroma * sin (hue);
- I = chroma * cos (hue);
- // Convert back to RGB
- fixed3 yIQ = fixed3 (YPrime, I, Q);
- color4.r = dot (yIQ, kYIQToR);
- color4.g = dot (yIQ, kYIQToG);
- color4.b = dot (yIQ, kYIQToB);
- return color4;
- }
- fixed sdHexPrism( fixed3 p, fixed2 h )
- {
- fixed3 q = abs(p);
- return max(q.z-h.y,max((q.x*0.866025+q.y*0.5),q.y)-h.x);
- }
- fixed sdBox( fixed3 p, fixed3 b )
- {
- fixed3 d = abs(p) - b;
- return min(max(d.x,max(d.y,d.z)),0.0) +
- length(max(d,0.0));
- }
- fixed opS( fixed d1, fixed d2 )
- {
- return max(-d1,d2);
- }
- fixed2 opU( fixed2 d1, fixed2 d2 )
- {
- return (d1.x<d2.x) ? d1 : d2;
- }
- fixed2 mapper( in fixed3 pos )
- {
- //pos.x += sin(pos.z+_Time.y)*0.2;
- //pos.y += cos(pos.z+_Time.y)*0.2;
- fixed height = .42;
- fixed depth = .75;
- fixed t = 0.02 + sin(_Time.y)*0.01;
- pos.z = fmod(pos.z,depth*2.)-0.5*depth*2.;
- fixed cyl = sdHexPrism( pos, fixed2(height-t, depth+t));
- fixed scyl = sdHexPrism( pos, fixed2(height-t*2.0, depth+t+.001));
- fixed2 res = fixed2(opS(scyl,cyl),1.5);
- fixed2 final = res;
- for (int i = 1; i < 3; i++) {
- // fixed c = cos(pos.z+_Time.y*0.5);
- // fixed s = sin(pos.z+_Time.y*0.5);
- // fixed2x2 m = fixed2x2(c,-s,s,c);
- // pos = fixed3(m*pos.xy,pos.z);
- height -= 0.1;
- depth -= 0.19;
- cyl = sdHexPrism( pos, fixed2(height-t, depth+t));
- scyl = sdHexPrism( pos, fixed2(height-t*2.0, depth+t+.001));
- final = opU(final, fixed2(opS(scyl,cyl),2.5));
- }
- return final;
- }
- fixed2 castRay( in fixed3 ro, in fixed3 rd )
- {
- fixed tmin = 0.0;
- fixed tmax = 100.0;
- fixed t = tmin;
- fixed m = -1.0;
- [unroll(100)]
- for( int i=0; i<100; i++ )
- {
- fixed2 res = mapper( ro+rd*t );
- if( t>tmax ) break;
- t += res.x;
- m = res.y;
- }
- if( t>tmax ) m=-1.0;
- return fixed2( t, m );
- }
- fixed3 calcNormal( in fixed3 pos )
- {
- fixed3 eps = fixed3( 0.01, 0.0, 0.0 );
- fixed3 nor = fixed3(
- mapper(pos+eps.xyy).x - mapper(pos-eps.xyy).x,
- mapper(pos+eps.yxy).x - mapper(pos-eps.yxy).x,
- mapper(pos+eps.yyx).x - mapper(pos-eps.yyx).x );
- return normalize(nor);
- }
- fixed calcAO( in fixed3 pos, in fixed3 nor )
- {
- fixed occ = 0.0;
- fixed sca = 1.0;
- [unroll(100)]
- for( int i=0; i<5; i++ )
- {
- fixed hr = 0.01 + 0.12*fixed(i)/4.0;
- fixed3 aopos = nor * hr + pos;
- fixed dd = mapper( aopos ).x;
- occ += -(dd-hr)*sca;
- sca *= .95;
- }
- return clamp( 1.0 - 3.0*occ, 0.0, 1.0 );
- }
- fixed3 render( in fixed3 ro, in fixed3 rd )
- {
- fixed3 col = fixed3(1.0,1.0,1.0);
- fixed2 res = castRay(ro,rd);
- fixed t = res.x;
- fixed m = res.y;
- if( m>-0.5 )
- {
- fixed3 pos = ro + t*rd;
- fixed3 nor = calcNormal( pos );
- fixed3 ref = reflect( rd, nor );
- // material
- fixed occ = calcAO( pos, nor );
- col = 1.0 - hue(fixed3(0.0,1.0,1.0),_Time.y*0.02+pos.z)*occ;
- }
- return fixed3( clamp(col,0.0,1.0) );
- }
- fixed3x3 setCamera( in fixed3 ro, in fixed3 ta, fixed cr )
- {
- fixed3 cw = normalize(ta-ro);
- fixed3 cp = fixed3(sin(cr), cos(cr),0.0);
- fixed3 cu = normalize( cross(cw,cp) );
- fixed3 cv = normalize( cross(cu,cw) );
- return fixed3x3( cu, cv, cw );
- }
- VertexOutput vert (VertexInput v)
- {
- VertexOutput o;
- o.pos = UnityObjectToClipPos (v.vertex);
- o.uv = v.uv;
- //VertexFactory
- return o;
- }
- fixed4 frag(VertexOutput i) : SV_Target
- {
- fixed2 q = i.uv/1;
- fixed2 p = -1.0+2.0*q;
- p.x *= 1/1;
- fixed2 mo = _iMouse.xy/1;
- // camera
- fixed3 ro = fixed3(0., 0.,_Time.y );
- fixed3 ta = ro+fixed3( 0., 0.,1. );
- // camera-to-world transformation
- //fixed3x3 ca = setCamera( ro, ta, 3.14159/2.0 ); //Skipping the camera to world stuff
- // ray direction
- //fixed3 rd = ca * normalize(fixed3(p.xy,4.5)); //Skipping the camera to world stuff, and normalization
- fixed3 rd = fixed3(p.xy,4.5);
- // render
- fixed3 col = render( ro, rd );
- SV_Target=fixed4( col, 1.0 );
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement