-
Notifications
You must be signed in to change notification settings - Fork 6
/
FastEdgeDetection.fs
171 lines (154 loc) · 4.1 KB
/
FastEdgeDetection.fs
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
/*{
"CREDIT" : "FastEdgeDetection by nimitz",
"CATEGORIES" : [
"ci"
],
"DESCRIPTION": "",
"INPUTS": [
{
"NAME": "inputImage",
"TYPE" : "image"
},
{
"NAME": "iZoom",
"TYPE" : "float",
"MIN" : 0.0,
"MAX" : 1.0,
"DEFAULT" : 1.0
},
{
"NAME": "iSteps",
"TYPE" : "float",
"MIN" : 2.0,
"MAX" : 75.0,
"DEFAULT" : 19.0
},
{
"NAME" :"iMouse",
"TYPE" : "point2D",
"DEFAULT" : [0.0, 0.0],
"MAX" : [640.0, 480.0],
"MIN" : [0.0, 0.0]
},
{
"NAME": "iColor",
"TYPE" : "color",
"DEFAULT" : [
0.9,
0.6,
0.0,
1.0
]
}
],
}
*/
// https://www.shadertoy.com/view/4s2XRd
//Fast Edge detection by nimitz (twitter: @stormoid)
#define EDGE_SIZE 0.07
#define SMOOTH 0.025
#define ITR 80
#define FAR 40.
#define time TIME
float hash( float n ) { return fract(sin(n)*43758.5453); }
vec3 rotx(vec3 p, float a)
{
float s = sin(a), c = cos(a);
return vec3(p.x, c*p.y - s*p.z, s*p.y + c*p.z);
}
vec3 roty(vec3 p, float a)
{
float s = sin(a), c = cos(a);
return vec3(c*p.x + s*p.z, p.y, -s*p.x + c*p.z);
}
vec2 map(vec3 p)
{
vec3 id = floor( (p+3.)/6.0);
p = mod( p+3., 6.0 ) - 3.;
float rid = hash(dot(id,vec3(7.,43,113)));
p = rotx(p,time*.5+rid+sin(rid*5.+time));
p = roty(p,time*0.5+rid*1.1);
float d = mix((max(abs(p.x),max(abs(p.y),abs(p.z)))-0.5),max(length(p)-1.,-(length(p)-0.4)),rid);
return vec2(d*.85,rid);
}
/*
Keeping track of min distance, then, when the min distance
is both under a given threshold and starts increasing (meaning that
a fold was just passed) then I mark that pixel as an edge. The min
distance can then be smoothed allowing for arbitrarily smooth edges.
*/
vec4 march(in vec3 ro, in vec3 rd)
{
float precis = 0.001;
float h=precis*2.0;
vec2 d = vec2(0.,10000.);
float md = 1.;
float id = 0.;;
bool stp = false;
for( int i=0; i<ITR; i++ )
{
if( abs(h)<precis || d.x>=FAR ) break;
d.x += h;
vec2 res = map(ro+rd*d.x);
if (!stp)
{
md = min(md,res.x);
if (h < EDGE_SIZE && h < res.x && i>0)
{
stp = true;
d.y = d.x;
}
}
h = res.x;
id = res.y;
}
if (stp) md = smoothstep(EDGE_SIZE-SMOOTH, EDGE_SIZE+0.01, md);
else md = 1.;
return vec4(d, md, id);
}
vec3 normal(in vec3 p, in float d)
{
vec2 e = vec2(-1., 1.)*0.003*d;
return normalize(e.yxx*map(p + e.yxx).x + e.xxy*map(p + e.xxy).x +
e.xyx*map(p + e.xyx).x + e.yyy*map(p + e.yyy).x );
}
void main(void)
{
vec2 q = gl_FragCoord.xy/RENDERSIZE.xy;
vec2 p = -1.0 + 2.0*q;
p.x*=RENDERSIZE.x/RENDERSIZE.y;
vec2 mo = iMouse.xy/RENDERSIZE.xy*2.-1.;
mo = (mo==vec2(-1.))?mo=vec2(0.):mo;
mo.x *= RENDERSIZE.x/RENDERSIZE.y;
//camera
vec3 ro = vec3(4.1,-time*2.,time-0.5);
vec3 rd = normalize(vec3(p,2.5));
rd = roty(rd,sin(time*0.2)*0.5+0.9+mo.x);
rd = rotx(rd,sin(time*0.12+sin(time*.5)*1.)+0.9);
vec4 rz = march(ro,rd);
vec3 ligt = normalize( vec3(-.5, 0.2, -0.2) );
float sun = dot(rd,ligt);
vec3 bg = vec3(0.5,0.6,.9)*sun*0.5+0.6;
vec3 col = bg;
if ( rz.x < FAR )
{
vec3 pos = ro+rz.x*rd;
float d = distance(ro,pos);
vec3 nor= normal(pos,d);
vec3 h = normalize(ligt - rd);
col = sqrt(col);
col = mix(sin(vec3(1,2,3)*rz.w*1.9)*0.5+0.35,col,0.2);
float dif = clamp( dot(nor, ligt), 0., 1.);
float spe = pow(clamp(dot(nor,h), 0., 1.),70.);
float fre = 0.1*pow(clamp(1. + dot(nor, rd), 0., 1.), 2.);
vec3 brdf = 1.5*vec3(.10, .11, .11);
brdf += 1.30*dif*vec3(1., .9, .75);
col = col*brdf + col*spe + fre*col;
}
col = mix(col,bg,smoothstep(30.,40.,rz.x)); //Distance fog
col *= mix(rz.z,1.,smoothstep(30.,40.,rz.y)); //Edges + Fog (using edge-eye distance)
col = pow(col, vec3(.8))*1.;
//vignetting from iq
col *= 0.4 + 0.6*pow( 16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y), 0.2 );
gl_FragColor = vec4( col, 1.0 );
}