-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsample-edge-1.shader
197 lines (163 loc) · 4.36 KB
/
sample-edge-1.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
Shader "Unite-2019/CelColoredOutline"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_RampTex("Ramp", 2D) = "white" {}
_Color("Color", Color) = (1, 1, 1, 1)
_OutlineExtrusion("Outline Extrusion", float) = 0
_OutlineColor("Outline Color", Color) = (0, 0, 0, 1)
}
SubShader
{
// Regular color & lighting pass
Pass
{
Tags
{
"LightMode" = "ForwardBase" // allows shadow rec/cast
}
// Write to Stencil buffer (so that outline pass can read)
Stencil
{
Ref 4
Comp always
Pass replace
ZFail keep
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase // shadows
#include "AutoLight.cginc"
#include "UnityCG.cginc"
// Properties
sampler2D _MainTex;
sampler2D _RampTex;
float4 _Color;
float4 _LightColor0; // provided by Unity
struct vertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float3 texCoord : TEXCOORD0;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float3 normal : NORMAL;
float3 texCoord : TEXCOORD0;
LIGHTING_COORDS(1,2) // shadows
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
// convert input to world space
output.pos = UnityObjectToClipPos(input.vertex);
float4 normal4 = float4(input.normal, 0.0); // need float4 to mult with 4x4 matrix
output.normal = normalize(mul(normal4, unity_WorldToObject).xyz);
output.texCoord = input.texCoord;
TRANSFER_VERTEX_TO_FRAGMENT(output); // shadows
return output;
}
float4 frag(vertexOutput input) : COLOR
{
// lighting mode
// convert light direction to world space & normalize
// _WorldSpaceLightPos0 provided by Unity
float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
// finds location on ramp texture that we should sample
// based on angle between surface normal and light direction
float ramp = clamp(dot(input.normal, lightDir), 0, 1.0);
float3 lighting = tex2D(_RampTex, float2(ramp, 0.5)).rgb;
// sample texture for color
float4 albedo = tex2D(_MainTex, input.texCoord.xy);
float attenuation = LIGHT_ATTENUATION(input); // shadow value
float3 rgb = albedo.rgb * _LightColor0.rgb * lighting * _Color.rgb * attenuation;
return float4(rgb, 1.0);
}
ENDCG
}
// Shadow pass
Pass
{
Tags
{
"LightMode" = "ShadowCaster"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#include "UnityCG.cginc"
struct v2f {
V2F_SHADOW_CASTER;
};
v2f vert(appdata_base v)
{
v2f o;
TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
return o;
}
float4 frag(v2f i) : SV_Target
{
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
// Outline pass
Pass
{
// Won't draw where it sees ref value 4
Cull OFF
ZWrite OFF
ZTest ON
Stencil
{
Ref 4
Comp notequal
Fail keep
Pass replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// Properties
uniform float4 _OutlineColor;
uniform float _OutlineSize;
uniform float _OutlineExtrusion;
sampler2D _MainTex;
struct vertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float3 texCoord : TEXCOORD0;
float4 color : TEXCOORD1;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float4 color : TEXCOORD0;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
float4 newPos = input.vertex;
// normal extrusion technique
float3 normal = normalize(input.normal);
newPos += float4(normal, 0.0) * _OutlineExtrusion;
// convert to world space
output.pos = UnityObjectToClipPos(newPos);
output.color = tex2Dlod(_MainTex, float4(input.texCoord.xy, 0, 0));
output.color *= _OutlineColor;
return output;
}
float4 frag(vertexOutput input) : COLOR
{
//float4 color = input.color * _OutlineColor;
return input.color;
}
ENDCG
}
}
}