Skip to content

Commit a769c74

Browse files
committed
shaders
1 parent 25501fa commit a769c74

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
uniform sampler2D texture;
7+
uniform vec2 texOffset;
8+
9+
varying vec4 vertColor;
10+
varying vec4 vertTexCoord;
11+
12+
const vec4 lumcoeff = vec4(0.299, 0.587, 0.114, 0);
13+
14+
void main() {
15+
vec2 tc0 = vertTexCoord.st + vec2(-texOffset.s, -texOffset.t);
16+
vec2 tc1 = vertTexCoord.st + vec2( 0.0, -texOffset.t);
17+
vec2 tc2 = vertTexCoord.st + vec2(+texOffset.s, -texOffset.t);
18+
vec2 tc3 = vertTexCoord.st + vec2(-texOffset.s, 0.0);
19+
vec2 tc4 = vertTexCoord.st + vec2( 0.0, 0.0);
20+
vec2 tc5 = vertTexCoord.st + vec2(+texOffset.s, 0.0);
21+
vec2 tc6 = vertTexCoord.st + vec2(-texOffset.s, +texOffset.t);
22+
vec2 tc7 = vertTexCoord.st + vec2( 0.0, +texOffset.t);
23+
vec2 tc8 = vertTexCoord.st + vec2(+texOffset.s, +texOffset.t);
24+
25+
vec4 col0 = texture2D(texture, tc0);
26+
vec4 col1 = texture2D(texture, tc1);
27+
vec4 col2 = texture2D(texture, tc2);
28+
vec4 col3 = texture2D(texture, tc3);
29+
vec4 col4 = texture2D(texture, tc4);
30+
vec4 col5 = texture2D(texture, tc5);
31+
vec4 col6 = texture2D(texture, tc6);
32+
vec4 col7 = texture2D(texture, tc7);
33+
vec4 col8 = texture2D(texture, tc8);
34+
35+
vec4 sum = 8.0 * col4 - (col0 + col1 + col2 + col3 + col5 + col6 + col7 + col8);
36+
gl_FragColor = vec4(sum.rgb, 1.0) * vertColor;
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
attr_reader :edges, :apply_filter
2+
3+
def setup
4+
sketch_title 'Edge Filter'
5+
@apply_filter = true
6+
@edges = load_shader(data_path('edges.glsl'))
7+
no_stroke
8+
end
9+
10+
def draw
11+
background(0)
12+
lights
13+
translate(width / 2, height / 2)
14+
push_matrix
15+
rotate_x(frameCount * 0.01)
16+
rotate_y(frameCount * 0.01)
17+
box(120)
18+
pop_matrix
19+
filter(edges) if (apply_filter)
20+
21+
# The sphere doesn't have the edge detection applied
22+
# on it because it is drawn after filter is called.
23+
rotate_y(frame_count * 0.02)
24+
translate(150, 0)
25+
sphere(40)
26+
end
27+
28+
def mouse_pressed
29+
@apply_filter = !apply_filter
30+
end
31+
32+
def settings
33+
size(640, 360, P3D)
34+
end

0 commit comments

Comments
 (0)