-
Notifications
You must be signed in to change notification settings - Fork 211
/
Chessboard.shader
64 lines (52 loc) · 1.75 KB
/
Chessboard.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Shader "Tutorial/011_Chessboard"
{
//show values to edit in inspector
Properties{
_Scale ("Pattern Size", Range(0,10)) = 1
_EvenColor("Color 1", Color) = (0,0,0,1)
_OddColor("Color 2", Color) = (1,1,1,1)
}
SubShader{
//the material is completely non-transparent and is rendered at the same time as the other opaque geometry
Tags{ "RenderType"="Opaque" "Queue"="Geometry"}
Pass{
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
float _Scale;
float4 _EvenColor;
float4 _OddColor;
struct appdata{
float4 vertex : POSITION;
};
struct v2f{
float4 position : SV_POSITION;
float3 worldPos : TEXCOORD0;
};
v2f vert(appdata v){
v2f o;
//calculate the position in clip space to render the object
o.position = UnityObjectToClipPos(v.vertex);
//calculate the position of the vertex in the world
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_TARGET{
//scale the position to adjust for shader input and floor the values so we have whole numbers
float3 adjustedWorldPos = floor(i.worldPos / _Scale);
//add different dimensions
float chessboard = adjustedWorldPos.x + adjustedWorldPos.y + adjustedWorldPos.z;
//divide it by 2 and get the fractional part, resulting in a value of 0 for even and 0.5 for off numbers.
chessboard = frac(chessboard * 0.5);
//multiply it by 2 to make odd values white instead of grey
chessboard *= 2;
//interpolate between color for even fields (0) and color for odd fields (1)
float4 color = lerp(_EvenColor, _OddColor, chessboard);
return color;
}
ENDCG
}
}
FallBack "Standard" //fallback adds a shadow pass so we get shadows on other objects
}