Giter Club home page Giter Club logo

Comments (1)

smkplus avatar smkplus commented on May 13, 2024

Hi, I converted this shader
but it's black! I don't know what's the problem!

Shader "Hidden/Dance"
{
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            
            #define USE_IQ_SMIN 0

float time;

float2 leg0[3];
float2 leg1[3];

float2 arm0[3];
float2 arm1[3];

float wlen=15.0;
float bob;
float wc_scale=0.5;
float scroll;
float scene_scale=15.0;

// Finds the entry and exit points of a 2D ray with a circle of radius 1
// centered at the origin.
float2 intersectCircle(float2 ro, float2 rd)
{
	float a = dot(rd, rd);
	float b = 2.0 * dot(rd, ro);
	float ds = b * b - 4.0 * a * (dot(ro, ro) - 1.0);
	
	if(ds < 0.0)
		return float2(1e3,1e3);
	
	return ((-b - sqrt(ds) * float2(-1.0, 1.0))) / (2.0 * a);
}

float3x3 rotateXMat(float a)
{
	return float3x3(1.0, 0.0, 0.0, 0.0, cos(a), -sin(a), 0.0, sin(a), cos(a));
}

float3x3 rotateYMat(float a)
{
	return float3x3(cos(a), 0.0, -sin(a), 0.0, 1.0, 0.0, sin(a), 0.0, cos(a));
}

// Adapted from https://www.shadertoy.com/view/ldlGR7   
float2 solve( float2 p, float l1, float l2, float side )
{
	float2 q = p*( 0.5 + 0.5*(l1*l1-l2*l2)/dot(p,p) );
	
	float s = l1*l1/dot(q,q) - 1.0;
	
	if( s<0.0 ) return float2(-100.0,-100.0);
	
	return q + q.yx*float2(-1.0,1.0)*side*sqrt( s );
}

// Returns a pyramid-like periodic signal.
float pyramid(float x)
{
	x = frac(x);
	return min(x * 2.0, (1.0 - x) * 2.0);
}

// Returns a semicircular periodic signal.
float circ(float x)
{
	x = frac(x) * 2.0 - 1.0;
	return sqrt(1.0 - x * x);
}

#if USE_IQ_SMIN
float smin(float a,float b,float k){ return -log(exp(-k*a)+exp(-k*b))/k;}//from iq
#else
// http://www.johndcook.com/blog/2010/01/20/how-to-compute-the-soft-maximum/
float smin(in float a, in float b, in float k) { return a - log(1.0+exp(k*(a-b))) / k; }
#endif

float mp(float x)
{
	float y=0.3;
	return clamp((pyramid(x)-0.5)*2.0-0.4,-y,y);
}

float mosaic(float3 p)
{
	// Disabled because it causes a compilation failure due to time-out or size limit.
	return 0.0;//max(mp(p.y*10.0),mp(p.z*10.0))*0.01;
}
/*
float3x3 transpose(float3x3 m)
{
	return float3x3(float3(m[0].x,m[1].x,m[2].x),
				float3(m[0].y,m[1].y,m[2].y),
				float3(m[0].z,m[1].z,m[2].z));
}*/

float capsuleDist(float3 p,float3 o,float3 d,float h0,float h1,float r0,float r1)
{
	float3 u=cross(d,float3(1.0,0.0,0.0));
	float3 v=cross(u,d);
	u=cross(v,d);
	float3x3 m=transpose(float3x3(normalize(u),normalize(v),normalize(d)));
	d=normalize(d);
	float t=clamp(dot(p-o,d),h0,h1);
	float3 np=o+t*d;
	return distance(np,p)-lerp(r0,r1,t)+mosaic(mul(m,(p-o)));
}

float boxDist(float3 p,float3 s,float r)
{
	return length(max(float3(0,0,0),abs(p)-s))-r+mosaic(p);
}

float sphereDist(float3 p,float3 o,float r)
{
	return distance(p,o)-r+mosaic(p-o);
}

float sceneDist(float3 p)
{
	float d=1e3;
	
	p+=float3(0.0,0.07,0.0)*scene_scale;
	p=mul(rotateYMat(3.1415926*0.5),p);
	
	p.z+=cos(p.y*2.0+time)*0.1;
	float tm=frac(time*wc_scale*2.0-0.1);
	p.x-=(smoothstep(0.0,0.3,tm)-smoothstep(0.4,1.0,tm))*smoothstep(0.5,2.0,p.y)*0.2+scroll;
	
	// Leg 0
	{
		float g=0.08;
		float3 o=float3(0.0,0.0,0.2);
		float d0=capsuleDist(p+o,float3(leg0[0],0.0),float3(leg0[1]-leg0[0],0.0),0.0,1.0-g,0.1,0.1);
		float d1=capsuleDist(p+o,float3(leg0[1],0.0),float3(leg0[2]-leg0[1],0.0),g,1.0,0.1,0.2);
		d=min(d,smin(d0,d1,15.0));
	}
	
	// Leg 1
	{
		float g=0.08;
		float3 o=float3(0.0,0.0,-0.2);
		float d0=capsuleDist(p+o,float3(leg1[0],0.0),float3(leg1[1]-leg1[0],0.0),0.0,1.0-g,0.1,0.1);
		float d1=capsuleDist(p+o,float3(leg1[1],0.0),float3(leg1[2]-leg1[1],0.0),g,1.0,0.1,0.2);
		d=min(d,smin(d0,d1,15.0));
	}
	
	p.y-=bob;
	
	// Arm 0
	{
		float g=0.08;
		float3 o=float3(0.0,0.0,0.4);
		float3x3 m=rotateXMat(-0.3)*rotateYMat((cos((time*wc_scale+0.5)*3.1415926*2.0)-0.6)*0.5);
		float d0=capsuleDist(p+o,float3(arm0[0],0.0),mul(m,float3(arm0[1]-arm0[0],0.0)),0.0,0.7-g,0.03,0.03);
		float d1=capsuleDist(p+o,float3(arm0[0],0.0)+mul(m,float3(arm0[1]-arm0[0],0.0)),mul(m,float3(arm0[2]-arm0[1],0.0)),g,0.7,0.03,0.06);
		d=min(d,smin(d0,d1,15.0));
	}
	
	// Arm 1
	{
		float g=0.08;
		float3 o=float3(0.0,0.0,-0.4);
		float3x3 m=rotateXMat(0.3)*rotateYMat(-(cos(time*wc_scale*3.1415926*2.0)-0.6)*0.5);
		float d0=capsuleDist(p+o,float3(arm1[0],0.0),mul(m,float3(arm1[1]-arm1[0],0.0)),0.0,0.7-g,0.03,0.03);
		float d1=capsuleDist(p+o,float3(arm1[0],0.0)+mul(m,float3(arm1[1]-arm1[0],0.0)),mul(m,float3(arm1[2]-arm1[1],0.0)),g,0.7,0.03,0.06);
		d=min(d,smin(d0,d1,15.0));
	}
	
	// Torso   
	d=smin(d,boxDist(p+float3(0.0,-0.7,0.0),float3(0.05,0.7,0.15),0.1),15.0);
	d=smin(d,boxDist(p+float3(-0.1,-1.1,0.0),float3(0.05,0.2,0.15)*0.1,0.1),5.0);
	
	// Head
	d=smin(d,sphereDist(p,float3(0.0,1.825,0.0),0.2),15.0);
	
	
	return d;
}

float3 sceneNorm(float3 p)
{
	p*=scene_scale;
	float c=sceneDist(p);
	float e=1e-3;
	return normalize(float3(sceneDist(p+float3(e,0,0))-c,
						  sceneDist(p+float3(0,e,0))-c,
						  sceneDist(p+float3(0,0,e))-c));
}

float robot(float3 ro,float3 rd)
{
	float t=0.0;
	float tm;
	
	tm=time*wc_scale;
	
	leg0[0]=float2(0.0,bob);
	leg0[2]=float2(pyramid(tm)-0.3,-1.8+0.3*circ(tm*2.0)*step(frac(tm),0.5));
	leg0[1]=(leg0[0]+solve(leg0[2]-leg0[0],1.0,1.0,1.0));
	
	arm1[0]=float2(0.0,1.4);
	arm1[2]=float2(pyramid(tm)-0.3,0.1+pow(pyramid(tm),2.0)*0.7);
	arm1[1]=(arm1[0]+solve(arm1[2]-arm1[0],0.7,0.7,-1.0));
	
	tm+=0.5;
	
	leg1[0]=float2(0.0,bob);
	leg1[2]=float2(pyramid(tm)-0.3,-1.8+0.3*circ(tm*2.0)*step(frac(tm),0.5));
	leg1[1]=(leg1[0]+solve(leg1[2]-leg1[0],1.0,1.0,1.0));
	
	arm0[0]=float2(0.0,1.4);
	arm0[2]=float2(pyramid(tm)-0.3,0.1+pow(pyramid(tm),2.0)*0.7);
	arm0[1]=(arm0[0]+solve(arm0[2]-arm0[0],0.7,0.7,-1.0));
	
	float rt=1e4;
	
	ro*=scene_scale;
	rd*=scene_scale;
	
	for(int i=0;i<15;i+=1)
	{
		float3 rp=ro+rd*t;
		
		float d=sceneDist(rp);
		
		if(d<1e-2)
		{
			rt=t;
		}
		
		t+=d/scene_scale;
	}
	
	
	return rt;
}


float2 unitSquareInterval(float2 ro, float2 rd)
{	
	float2 slabs0 = (float2(+1.0,+1.0) - ro) / rd;	
	float2 slabs1 = (float2(-1.0,-1.0) - ro) / rd;
	
	float2 mins = min(slabs0, slabs1);
	float2 maxs = max(slabs0, slabs1);
	
	return float2(max(mins.x, mins.y),
				min(maxs.x, maxs.y));
}

float3 squaresColours(float2 p)
{
	p+=float2(time*0.2,time*0.2);
	
	float3 orange=float3(1.0,0.4,0.1)*2.0;
	float3 purple=float3(1.0,0.2,0.5)*0.8;
	
	float l=pow(0.5+0.5*cos(p.x*7.0+cos(p.y)*8.0)*sin(p.y*2.0),4.0)*2.0;
	float3 c=pow(l*(lerp(orange,purple,0.5+0.5*cos(p.x*40.0+sin(p.y*10.0)*3.0))+
				  lerp(orange,purple,0.5+0.5*cos(p.x*20.0+sin(p.y*3.0)*3.0))),float3(1.2,1.2,1.2))*0.7;
	
	c+=float3(1.0,0.8,0.4)*pow(0.5+0.5*cos(p.x*20.0)*sin(p.y*12.0),20.0)*2.0;
	
	c+=float3(0.1,0.5+0.5*cos(p*20.0))*float3(0.05,0.1,0.4).bgr*0.7;
	
	return c;
}

float3 squaresTex(float2 p,float border)
{
	float sm=0.02;
	float2 res=float2(8.0,8.0);
	float2 ip=floor(p*res)/res;
	float2 fp=frac(p*res);
	float m=1.0-max(smoothstep(border-sm,border,abs(fp.x-0.5)),smoothstep(border-sm,border,abs(fp.y-0.5)));
	m+=1.0-smoothstep(0.0,0.56,distance(fp,float2(0.5,0.5)));
	return m*squaresColours(ip);
}

float3 room(float3 ro,float3 rd,out float3 rp,out float3 n)
{
	float2 box_size=float2(1.0,5.0+3.0/8.0);
	
	float2 cp=float2(0.0,0.0),ct=float2(1e3,1e3);
	
	for(int i=0;i<4;i+=1)
	{
		float cr=0.03;
		float2 tcp=float2(2.5/8.0*float(-1),float(i)-2.0+0.5/8.0);
		float2 tct=intersectCircle((ro.xz-tcp)/cr,rd.xz/cr);
		
		if(tct.y > 0.0 && tct.y<ct.y)
		{
			ct=tct;
			cp=tcp;
		}
	}

	for(int i=0;i<4;i+=1)
	{
		float cr=0.03;
		float2 tcp=float2(2.5/8.0*float(+1),float(i)-2.0+0.5/8.0);
		float2 tct=intersectCircle((ro.xz-tcp)/cr,rd.xz/cr);
		
		if(tct.y > 0.0 && tct.y<ct.y)
		{
			ct=tct;
			cp=tcp;
		}
	}
	
	ct.y=max(0.0,ct.y);
	
	float3 ci=ro+rd*ct.y;
	float2 cu=float2(atan2(ci.z-cp.y,ci.x-cp.x)/3.1415926*0.5,(ci.y+0.5/8.0)*4.0);
	
	float wt=max(0.0,unitSquareInterval(ro.xy * box_size, rd.xy * box_size).y);
	float t=min(ct.y,wt);
	
	rp=ro+rd*(t-1e-4);
	
	n.z=0.0;
	if(abs(rp.x*box_size.x)>abs(rp.y*box_size.y))
		n.xy=float2(rp.x/abs(rp.x),0.0);
	else
		n.xy=float2(0.0,rp.y/abs(rp.y));
	
	if(ct.y<wt)
	{
		n.y=0.0;
		n.xz=normalize(rp.xz-ci.xz);
	}
	
	float l=1.0-smoothstep(0.0,3.0,abs(rp.z-ro.z));
	
	float3 wc=lerp(squaresTex(rp.zy+float2(0.0,0.5/8.0),0.5),squaresTex(rp.xz,0.44),step(0.999/box_size.y,abs(rp.y)));
	float3 cc=squaresTex(cu,0.45)+0.8*float3(1,1,1)*smoothstep(0.83/5.0,0.86/5.0,abs(rp.y));
	
	return l*lerp(cc,wc,step(wt,t));
}
float3 mod(float3 x, float3 y)
{
return x - y * floor(x/y);
}
float3 scene(float2 p)
{
	float3x3 cam = rotateXMat(cos(time * 0.2) * 0.1) * rotateYMat(time * 0.5);
	float lt=mod(time*wc_scale,wlen)/wlen;
	
	float3 ro = mul(cam,float3(0.0,-0.15+lt*0.15, 0.15+lt*0.2))+float3(0.0,0.0,scroll/scene_scale);
	float3 rd =  mul(cam,float3(p, -1.0));
	
	rd=normalize(rd);
	
	float robot_t=robot(ro,rd);
	
	float3 n,rp;
	
	float3 c;
	float3 c0=room(ro,rd,rp,n);
	
	if(robot_t < distance(ro,rp))
	{
		rp=ro+rd*robot_t;
		n=sceneNorm(rp);
		float3 r=reflect(rd,n);
		c=float3(0.5+0.5*n.y,0.5+0.5*n.y,0.5+0.5*n.y)*0.5*float3(1.0,0.8,0.5);
		float3 c1=room(rp,r,rp,n);
		c+=c1*0.5;
	}
	else
	{
		float3 r=reflect(rd,n);
		float3 c1=room(rp,r,rp,n);
		c=c0+c1*c0*0.4;
	}
		
	float3 ll=float3(1.0-(smoothstep(0.0,0.07,lt)-smoothstep(0.93,1.0,lt)),1.0-(smoothstep(0.0,0.07,lt)-smoothstep(0.93,1.0,lt)),1.0-(smoothstep(0.0,0.07,lt)-smoothstep(0.93,1.0,lt)));
	
	return ll+c+
		0.6*((sin(p.y)*cos(p.x+time*2.0)*0.5+0.5)*
			 pow(lerp(float3(1.0,0.7,0.1),float3(1.0,0.2,0.6),0.5+0.5*cos(p.x+sin(time*3.0+p.y*2.0))),float3(2.0,2.0,2.0)));
}

            fixed4 frag (v2f i) : SV_Target
            {
	time=_Time.y+1.0;
	bob=cos(time*12.0)*0.05;
	scroll=-15.0+mod(time*wc_scale,wlen)*2.0;
	float4 fragColor = float4(0,0,0,0);
	//float2 uv = fragCoord.xy / iResolution.xy;
	float2 uv = i.uv;
	float2 q=uv;
	float2 t=uv*2.0-float2(1.0,1.0);
	//t.x*=iResolution.x/iResolution.y;
	fragColor.rgb = scene(t.xy) * 1.3;
	
	// vignet
	fragColor.rgb *= 0.5 + 0.5*pow( 16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y), 0.1 );
	return fragColor;
            }
            ENDCG
        }
    }
}

from shaderman.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.