# 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
// Compiled in Unity 2018.1.0f2
// https://github.com/przemyslawzaworski/Unity3D-CG-programming

{
{
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
}
}
}

## Create an account

Register a new account

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633662
• Total Posts
3013229
×