Skip to content

Instantly share code, notes, and snippets.

@FVSHaLuan
Created August 26, 2015 02:49
Show Gist options
  • Save FVSHaLuan/888d9b66542aa0d5f1a4 to your computer and use it in GitHub Desktop.
Save FVSHaLuan/888d9b66542aa0d5f1a4 to your computer and use it in GitHub Desktop.
**Function: shades a cube with a different color for each edge.
/* by FVS - Ha Luan */
// Usage:
// 1. Create a material using this shader
// 2. Apply the new material to a cube with correct normal vectors defined (a Unity's built-in cube is enough)
Shader "FVS/Color cube" {
SubShader
{
cull off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata_t
{
float4 vertex : POSITION;
float4 normal : NORMAL;
fixed4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert(appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
float r;
float g;
float b;
// r,x
if (v.normal.x > 0)
{
r = 1;
}
else if (v.normal.x < 0)
{
r = 0;
}
else
{
r = 0.5;
}
// g,y
if (v.normal.y > 0)
{
g = 1;
}
else if (v.normal.y < 0)
{
g = 0;
}
else
{
g = 0.5;
}
// b,z
if (v.normal.z > 0)
{
b = 1;
}
else if (v.normal.z < 0)
{
b = 0;
}
else
{
b = 0.5;
}
///
if (v.normal.y == 0)
{
o.color = fixed4(r, g + v.vertex.y, b, 1);
}
else
{
o.color = fixed4(r, g + v.vertex.z, b, 1);
}
///
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return i.color;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment