-
Notifications
You must be signed in to change notification settings - Fork 687
/
Copy pathEdgeDetectionRoberts.shader
210 lines (146 loc) · 4.91 KB
/
EdgeDetectionRoberts.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
198
199
200
201
202
203
204
205
206
207
208
209
210
//----------------------------------------------------------------------------------------------------------
// X-PostProcessing Library
// https://github.com/QianMo/X-PostProcessing-Library
// Copyright (C) 2020 QianMo. All rights reserved.
// Licensed under the MIT License
// You may not use this file except in compliance with the License.You may obtain a copy of the License at
// http://opensource.org/licenses/MIT
//----------------------------------------------------------------------------------------------------------
Shader "Hidden/X-PostProcessing/EdgeDetectionRoberts"
{
HLSLINCLUDE
#include "../../../Shaders/StdLib.hlsl"
#include "../../../Shaders/XPostProcessing.hlsl"
half2 _Params;
half4 _EdgeColor;
half4 _BackgroundColor;
#define _EdgeWidth _Params.x
#define _BackgroundFade _Params.y
float intensity(in float4 color)
{
return sqrt((color.x * color.x) + (color.y * color.y) + (color.z * color.z));
}
float sobel(float stepx, float stepy, float2 center)
{
// get samples around pixel
float topLeft = intensity(SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, center + float2(-stepx, stepy)));
float bottomLeft = intensity(SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, center + float2(-stepx, -stepy)));
float topRight = intensity(SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, center + float2(stepx, stepy)));
float bottomRight = intensity(SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, center + float2(stepx, -stepy)));
// Roberts Operator
//X = -1 0 Y = 0 -1
// 0 1 1 0
// Gx = sum(kernelX[i][j]*image[i][j])
float Gx = -1.0 * topLeft + 1.0 * bottomRight;
// Gy = sum(kernelY[i][j]*image[i][j]);
float Gy = -1.0 * topRight + 1.0 * bottomLeft;
float sobelGradient = sqrt((Gx * Gx) + (Gy * Gy));
return sobelGradient;
}
half4 Frag(VaryingsDefault i): SV_Target
{
half4 sceneColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
float sobelGradient = sobel(_EdgeWidth / _ScreenParams.x, _EdgeWidth / _ScreenParams.y, i.texcoord);
half4 backgroundColor = lerp(sceneColor, _BackgroundColor, _BackgroundFade);
float3 edgeColor = lerp(backgroundColor.rgb, _EdgeColor.rgb, sobelGradient);
return float4(edgeColor, 1);
}
ENDHLSL
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment Frag
ENDHLSL
}
}
//HLSLINCLUDE
//
//#include "../../../Shaders/StdLib.hlsl"
//#include "../../../Shaders/XPostProcessing.hlsl"
//
//
//
//half _Float1;
//half _Float2;
//half _Float3;
//half4 _Color1;
//
//struct v2f
//{
// float2 uvRoberts[5] : TEXCOORD0;
// float4 vertex : SV_POSITION;
//};
//v2f vert_Roberts(appdata v)
//{
// v2f o;
// o.vertex = UnityObjectToClipPos(v.vertex);
// o.uvRoberts[0] = v.uv + float2(-1, -1) * _MainTex_TexelSize * _SampleRange;
// o.uvRoberts[1] = v.uv + float2(1, -1) * _MainTex_TexelSize * _SampleRange;
// o.uvRoberts[2] = v.uv + float2(-1, 1) * _MainTex_TexelSize * _SampleRange;
// o.uvRoberts[3] = v.uv + float2(1, 1) * _MainTex_TexelSize * _SampleRange;
// o.uvRoberts[4] = v.uv;
// return o;
//}
//float Roberts(v2f i)
//{
// const float Gx[4] =
// {
// -1, 0,
// 0, 1
// };
// const float Gy[4] =
// {
// 0, -1,
// 1, 0
// };
// float edgex, edgey;
// for (int j = 0; j < 4; j++)
// {
// fixed4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uvRoberts[j]);
// float lum = Luminance(col.rgb);
// edgex += lum * Gx[j];
// edgey += lum * Gy[j];
// }
// return 1 - abs(edgex) - abs(edgey);
//}
//fixed4 frag_Roberts(v2f i) : SV_Target
//{
// fixed4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uvRoberts[4]);
// float g = Roberts(i);
// g = pow(g, _EdgePower);
// col.rgb = lerp(_EdgeColor, _NonEdgeColor, g);
// return col;
//}
////half4 Frag(VaryingsDefault i): SV_Target
////{
////
//// half4 sceneColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
//// half3 col = 0.5 + 0.5 * cos(_Time.y + i.texcoord.xyx + float3(0, 2, 4));
////
//// half3 finalColor = lerp(sceneColor.rgb, col, _Float1 * 0.1);
////
//// return half4(finalColor, 1.0);
////}
////
//ENDHLSL
//
//SubShader
//{
// Cull Off ZWrite Off ZTest Always
//
// Pass
// {
// HLSLPROGRAM
//
// #pragma vertex vert_Roberts
// #pragma fragment frag_Roberts
//
// ENDHLSL
//
// }
//}
}