Skip to content

Commit ba4a714

Browse files
author
monkstone
committed
use new video_event library in examples
1 parent 336a6ea commit ba4a714

13 files changed

+352
-38
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Example-Sketches
22
================
33

44
Many of the vanilla processing example sketches have been translated to ruby-processing, and they are mainly written as 'bare' sketches (ie not class wrapped) in keeping with the original processing. At runtime these sketches the get wrapped into a Sketch class. Should you prefer you can still write class wrapped sketches, these will work equally well with ruby-processing. Certain sketches must be run with JRuby-Complete (mainly `load_image` and `shader` sketches), this is a [java permissions thing with jruby][]. You should also checkout the [Nature of Code Examples in ruby][] and for the beginner [Learning Processing with Ruby][] for even more examples.
5+
Includes autorun Rakefiles, in a console cd outer directory and 'rake' to run all autorun examples or eg 'rake shaders' to just run the shader examples.
56

67
### Partial Catalogue (for the lazy)
78

Rakefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
root = File.expand_path(__dir__)
44

55
desc 'run contributed samples'
6-
task :default => [:contributed]
6+
task :default => [:all]
7+
8+
desc 'run all autorun samples'
9+
task :all do
10+
Rake::Task[:contributed].execute
11+
Rake::Task[:vecmath].execute
12+
Rake::Task[:shaders].execute
13+
end
714

815
desc 'run contributed samples'
916
task :contributed do

samples/processing_app/library/video/capture/ascii_capture.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77
# This sketch is a simple homage that re-interprets live video as ASCII text.
88
# See the key_pressed function for more options, like changing the font size.
99
#
10-
load_library :video
10+
load_library :video, :video_event
1111
include_package 'processing.video'
1212

1313
attr_reader :bright, :char, :cheat_screen, :font, :font_size, :letters, :video
1414

1515
# All ASCII characters, sorted according to their visual density
1616
LETTER_STRING = %q{ .`-_':,;^=+/\"|)\\<>)iv%xclrs{*}I?!][1taeo7zjLunT#JCwfy325Fp6mqSghVd4EgXPGZbYkOA&8U$@KHDBWNMR0Q}
1717
LETTER_ORDER = LETTER_STRING.scan(/./)
18+
1819
def setup
1920
size(640, 480)
2021
init_video
2122
@font_size = 1.5
22-
@font = load_font(data_path('UniversLTStd-Light-48.vlw'))
23+
@font = load_font('UniversLTStd-Light-48.vlw')
2324
# for the 256 levels of brightness, distribute the letters across
2425
# the an array of 256 elements to use for the lookup
2526
@letters = (0...256).map do |i|
@@ -38,14 +39,12 @@ def init_video
3839
@cheat_screen = false
3940
end
4041

41-
def capture_event(c)
42+
def captureEvent(c)
4243
c.read
43-
background 0
4444
end
4545

4646
def draw
47-
return unless (video.available == true)
48-
capture_event(video)
47+
background 0
4948
push_matrix
5049
hgap = width / video.width
5150
vgap = height / video.height

samples/processing_app/library/video/capture/background_subtraction.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# background-subtraction technique. To initialize the background, press a key.
77
#
88

9-
load_library :video
9+
load_libraries :video, :video_event
1010
include_package 'processing.video'
1111

1212
attr_reader :background_pixels, :number_of_pixels, :video
@@ -35,8 +35,6 @@ def capture # captureEvent does not work like vanilla processing
3535
end
3636

3737
def draw
38-
return unless (video.available == true)
39-
capture
4038
video.load_pixels # Make the pixels of video available
4139
# Difference between the current frame and the stored background
4240
# current_sum = 0
@@ -66,6 +64,10 @@ def draw
6664
# p current_sum # Print out the total amount of movement
6765
end
6866

67+
def captureEvent(c)
68+
c.read
69+
end
70+
6971
def key_pressed
7072
video.load_pixels
7173
@background_pixels = video.pixels.clone
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Because this sketch uses a glsl shader it needs to run using
2+
# jruby-complete (typically rp5 --nojruby sketch.rb)
3+
# hold down mouse to see unfiltered output
4+
load_libraries :video, :video_event
5+
include_package 'processing.video'
6+
attr_reader :cam, :my_shader
7+
8+
def setup
9+
size(640, 480, P2D)
10+
@my_shader = load_shader('bwfrag.glsl')
11+
start_capture(width, height)
12+
end
13+
14+
def start_capture(w, h)
15+
@cam = Capture.new(self, w, h)
16+
cam.start
17+
end
18+
19+
def draw
20+
image(cam, 0, 0)
21+
return if mouse_pressed?
22+
filter(my_shader)
23+
end
24+
25+
# using snake case to match java reflect method
26+
def captureEvent(c)
27+
c.read
28+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
#define PROCESSING_TEXTURE_SHADER
7+
8+
uniform sampler2D texture;
9+
10+
varying vec4 vertColor;
11+
varying vec4 vertTexCoord;
12+
13+
const vec4 lumcoeff = vec4(0.299, 0.587, 0.114, 0);
14+
15+
void main() {
16+
vec4 col = texture2D(texture, vertTexCoord.st);
17+
float lum = dot(col, lumcoeff);
18+
if (0.5 < lum) {
19+
gl_FragColor = vertColor;
20+
} else {
21+
gl_FragColor = vec4(0, 0, 0, 1);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
2+
// Original shader by Ken Slade
3+
// https://www.shadertoy.com/view/ldsSWr
4+
5+
// Ported to Processing by Raphaël de Courville <twitter: @sableRaph>
6+
7+
#ifdef GL_ES
8+
precision highp float;
9+
#endif
10+
11+
// This line is optional from Processing 2.1 and up
12+
#define PROCESSING_TEXTURE_SHADER
13+
14+
uniform sampler2D texture; // iChannel0 in Shadertoy
15+
uniform vec2 sketchSize; // iResolution in Shadertoy
16+
17+
18+
19+
// * Code from Shadertoy *
20+
21+
// Note: Uniforms have been renamed (see above)
22+
23+
// Note 2: void mainImage( out vec4 fragColor, in vec2 fragCoord )
24+
// was changed into
25+
// void main( void )
26+
27+
// Note 3: fragCoord and fragColor had to be renamed into
28+
// gl_FragCoord and gl_FragColor
29+
30+
31+
32+
33+
// Basic edge detection via convolution
34+
// Ken Slade - [email protected]
35+
// at https://www.shadertoy.com/view/ldsSWr
36+
37+
// Based on original Sobel shader by:
38+
// Jeroen Baert - [email protected] (www.forceflow.be)
39+
// at https://www.shadertoy.com/view/Xdf3Rf
40+
41+
//options are edge, colorEdge, or trueColorEdge
42+
#define EDGE_FUNC edge
43+
44+
//options are KAYYALI_NESW, KAYYALI_SENW, PREWITT, ROBERTSCROSS, SCHARR, or SOBEL
45+
#define SOBEL
46+
47+
// Use these parameters to fiddle with settings
48+
#ifdef SCHARR
49+
#define STEP 0.15
50+
#else
51+
#define STEP 1.0
52+
#endif
53+
54+
55+
#ifdef KAYYALI_NESW
56+
const mat3 kayyali_NESW = mat3(-6.0, 0.0, 6.0,
57+
0.0, 0.0, 0.0,
58+
6.0, 0.0, -6.0);
59+
#endif
60+
#ifdef KAYYALI_SENW
61+
const mat3 kayyali_SENW = mat3(6.0, 0.0, -6.0,
62+
0.0, 0.0, 0.0,
63+
-6.0, 0.0, 6.0);
64+
#endif
65+
#ifdef PREWITT
66+
// Prewitt masks (see http://en.wikipedia.org/wiki/Prewitt_operator)
67+
const mat3 prewittKernelX = mat3(-1.0, 0.0, 1.0,
68+
-1.0, 0.0, 1.0,
69+
-1.0, 0.0, 1.0);
70+
71+
const mat3 prewittKernelY = mat3(1.0, 1.0, 1.0,
72+
0.0, 0.0, 0.0,
73+
-1.0, -1.0, -1.0);
74+
#endif
75+
#ifdef ROBERTSCROSS
76+
// Roberts Cross masks (see http://en.wikipedia.org/wiki/Roberts_cross)
77+
const mat3 robertsCrossKernelX = mat3(1.0, 0.0, 0.0,
78+
0.0, -1.0, 0.0,
79+
0.0, 0.0, 0.0);
80+
const mat3 robertsCrossKernelY = mat3(0.0, 1.0, 0.0,
81+
-1.0, 0.0, 0.0,
82+
0.0, 0.0, 0.0);
83+
#endif
84+
#ifdef SCHARR
85+
// Scharr masks (see http://en.wikipedia.org/wiki/Sobel_operator#Alternative_operators)
86+
const mat3 scharrKernelX = mat3(3.0, 10.0, 3.0,
87+
0.0, 0.0, 0.0,
88+
-3.0, -10.0, -3.0);
89+
90+
const mat3 scharrKernelY = mat3(3.0, 0.0, -3.0,
91+
10.0, 0.0, -10.0,
92+
3.0, 0.0, -3.0);
93+
#endif
94+
#ifdef SOBEL
95+
// Sobel masks (see http://en.wikipedia.org/wiki/Sobel_operator)
96+
const mat3 sobelKernelX = mat3(1.0, 0.0, -1.0,
97+
2.0, 0.0, -2.0,
98+
1.0, 0.0, -1.0);
99+
100+
const mat3 sobelKernelY = mat3(-1.0, -2.0, -1.0,
101+
0.0, 0.0, 0.0,
102+
1.0, 2.0, 1.0);
103+
#endif
104+
105+
//performs a convolution on an image with the given kernel
106+
float convolve(mat3 kernel, mat3 image) {
107+
float result = 0.0;
108+
for (int i = 0; i < 3; i++) {
109+
for (int j = 0; j < 3; j++) {
110+
result += kernel[i][j]*image[i][j];
111+
}
112+
}
113+
return result;
114+
}
115+
116+
//helper function for colorEdge()
117+
float convolveComponent(mat3 kernelX, mat3 kernelY, mat3 image) {
118+
vec2 result;
119+
result.x = convolve(kernelX, image);
120+
result.y = convolve(kernelY, image);
121+
return clamp(length(result), 0.0, 255.0);
122+
}
123+
124+
//returns color edges using the separated color components for the measure of intensity
125+
//for each color component instead of using the same intensity for all three. This results
126+
//in false color edges when transitioning from one color to another, but true colors when
127+
//the transition is from black to color (or color to black).
128+
vec4 colorEdge(float stepx, float stepy, vec2 center, mat3 kernelX, mat3 kernelY) {
129+
//get samples around pixel
130+
vec4 colors[9];
131+
colors[0] = texture2D(texture,center + vec2(-stepx,stepy));
132+
colors[1] = texture2D(texture,center + vec2(0,stepy));
133+
colors[2] = texture2D(texture,center + vec2(stepx,stepy));
134+
colors[3] = texture2D(texture,center + vec2(-stepx,0));
135+
colors[4] = texture2D(texture,center);
136+
colors[5] = texture2D(texture,center + vec2(stepx,0));
137+
colors[6] = texture2D(texture,center + vec2(-stepx,-stepy));
138+
colors[7] = texture2D(texture,center + vec2(0,-stepy));
139+
colors[8] = texture2D(texture,center + vec2(stepx,-stepy));
140+
141+
mat3 imageR, imageG, imageB, imageA;
142+
for (int i = 0; i < 3; i++) {
143+
for (int j = 0; j < 3; j++) {
144+
imageR[i][j] = colors[i*3+j].r;
145+
imageG[i][j] = colors[i*3+j].g;
146+
imageB[i][j] = colors[i*3+j].b;
147+
imageA[i][j] = colors[i*3+j].a;
148+
}
149+
}
150+
151+
vec4 color;
152+
color.r = convolveComponent(kernelX, kernelY, imageR);
153+
color.g = convolveComponent(kernelX, kernelY, imageG);
154+
color.b = convolveComponent(kernelX, kernelY, imageB);
155+
color.a = convolveComponent(kernelX, kernelY, imageA);
156+
157+
return color;
158+
}
159+
160+
//finds edges where fragment intensity changes from a higher value to a lower one (or
161+
//vice versa).
162+
vec4 edge(float stepx, float stepy, vec2 center, mat3 kernelX, mat3 kernelY){
163+
// get samples around pixel
164+
mat3 image = mat3(length(texture2D(texture,center + vec2(-stepx,stepy)).rgb),
165+
length(texture2D(texture,center + vec2(0,stepy)).rgb),
166+
length(texture2D(texture,center + vec2(stepx,stepy)).rgb),
167+
length(texture2D(texture,center + vec2(-stepx,0)).rgb),
168+
length(texture2D(texture,center).rgb),
169+
length(texture2D(texture,center + vec2(stepx,0)).rgb),
170+
length(texture2D(texture,center + vec2(-stepx,-stepy)).rgb),
171+
length(texture2D(texture,center + vec2(0,-stepy)).rgb),
172+
length(texture2D(texture,center + vec2(stepx,-stepy)).rgb));
173+
vec2 result;
174+
result.x = convolve(kernelX, image);
175+
result.y = convolve(kernelY, image);
176+
177+
float color = clamp(length(result), 0.0, 255.0);
178+
return vec4(color);
179+
}
180+
181+
//Colors edges using the actual color for the fragment at this location
182+
vec4 trueColorEdge(float stepx, float stepy, vec2 center, mat3 kernelX, mat3 kernelY) {
183+
vec4 edgeVal = edge(stepx, stepy, center, kernelX, kernelY);
184+
return edgeVal * texture2D(texture,center);
185+
}
186+
187+
void main( void ){
188+
vec2 uv = gl_FragCoord.xy / sketchSize.xy;
189+
vec4 color = texture2D(texture, uv.xy);
190+
#ifdef KAYYALI_NESW
191+
gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1],
192+
uv,
193+
kayyali_NESW, kayyali_NESW);
194+
#endif
195+
#ifdef KAYYALI_SENW
196+
gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1],
197+
uv,
198+
kayyali_SENW, kayyali_SENW);
199+
#endif
200+
#ifdef PREWITT
201+
gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1],
202+
uv,
203+
prewittKernelX, prewittKernelY);
204+
#endif
205+
#ifdef ROBERTSCROSS
206+
gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1],
207+
uv,
208+
robertsCrossKernelX, robertsCrossKernelY);
209+
#endif
210+
#ifdef SOBEL
211+
gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1],
212+
uv,
213+
sobelKernelX, sobelKernelY);
214+
#endif
215+
#ifdef SCHARR
216+
gl_FragColor = EDGE_FUNC(STEP/sketchSize[0], STEP/sketchSize[1],
217+
uv,
218+
scharrKernelX, scharrKernelY);
219+
#endif
220+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load_library :video, :video_event
2+
include_package 'processing.video'
3+
attr_reader :cam, :my_shader
4+
5+
def setup
6+
size(640, 480, P2D)
7+
@my_shader = load_shader('edge_detect.glsl')
8+
my_shader.set('sketchSize', width.to_f, height.to_f)
9+
start_capture(width, height)
10+
end
11+
12+
def start_capture(w, h)
13+
@cam = Capture.new(self, w, h)
14+
cam.start
15+
end
16+
17+
def draw
18+
image(cam, 0, 0)
19+
return if mouse_pressed?
20+
filter(my_shader)
21+
end
22+
23+
# use snake case here to match java reflect method
24+
def captureEvent(c)
25+
c.read
26+
end

0 commit comments

Comments
 (0)