Jump to content
  • Advertisement
gtx660

3D [UNITY] Mastering HLSL and code golf

Recommended Posts

Hi.

For improving programming skills, recently I tried to create some tiny shaders, with more ambitious effect than just displaying single color (example below - animated 3D noise).

Could someone tell about even more interesting tricks for HLSL language (especially in context of writing shorter code) ? 

 

// 620 chars without whitespaces
// Apply material with shader to quad and that's all
// Compiled in Unity 2018.1.0f2
// https://github.com/przemyslawzaworski/Unity3D-CG-programming

Shader "I"
{
	Subshader
	{
		Pass
		{
			Cull Off
			CGPROGRAM
			#pragma vertex V
			#pragma fragment P  

			#define l lerp
			
			half k(half3 x)
			{
				half3 p=x-frac(x), f=x-p, n={1,0,0}, t={1,9,57}; f*=f*(3-2*f); 
				#define h(m) frac(cos(dot(m,t))*1e5)
				return l(l(l(h(p),h(p+n.xyy),f.x),l(h(p+n.yxy),h(p+n.xxy),f.x),f.y),l(l(h(p+n.yyx),h(p+n.xyx),f.x),l(h(p+n.yxx),h(p+1),f.x),f.y),f.z);
			}
			
			void V(uint i:SV_VertexID,out half4 c:POSITION) {c=half4((i<<1&2)*2-1.,1-2.*(i&2),1,1);}

			void P(half4 u:POSITION,out half s:COLOR) 
			{	
				u = half4(9*u.xy/_ScreenParams,_Time.g,0);
				for (half i;i<1;i+=.02,u.y-=.1,u.w=(k(u)+k(u+9.5))/2,s=l(s,u.w,smoothstep(0,1,(u.w-i)/fwidth(u.w)))){}
			}

			ENDCG
		}
	}
}

 

I.png

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!