Skip to content

Commit 06eb25e

Browse files
author
monkstone
committed
new sketches
1 parent ba4a714 commit 06eb25e

4 files changed

Lines changed: 232 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Original shader by RavenWorks
2+
// Fake Floyd-Steinberg dithering
3+
// https://www.shadertoy.com/view/4sjGRD
4+
5+
// Adapted for Ruby-Processing by Martin Prout <@monkstoneT>
6+
7+
#ifdef GL_ES
8+
precision mediump float;
9+
precision mediump int;
10+
#endif
11+
12+
uniform sampler2D texture; // iChannel0 in Shadertoy
13+
uniform vec2 sketchSize; // iResolution in Shadertoy
14+
15+
const int lookupSize = 64;
16+
const float errorCarry = 0.3;
17+
18+
float getGrayscale(vec2 coords){
19+
vec2 uv = coords / sketchSize.xy;
20+
// processing is already using inverted y coordinates
21+
// uv.y = 1.0-uv.y;
22+
vec3 sourcePixel = texture2D(texture, uv).rgb;
23+
return length(sourcePixel*vec3(0.2126,0.7152,0.0722));
24+
}
25+
26+
// in regular glsl was
27+
// void mainImage( out vec4 fragColor, in vec2 fragCoord )
28+
29+
void main() {
30+
31+
int topGapY = int(sketchSize.y - gl_FragCoord.y);
32+
33+
int cornerGapX = int((gl_FragCoord.x < 10.0) ? gl_FragCoord.x : sketchSize.x - gl_FragCoord.x);
34+
int cornerGapY = int((gl_FragCoord.y < 10.0) ? sketchSize.y : gl_FragCoord.y - gl_FragCoord.y);
35+
int cornerThreshhold = ((cornerGapX == 0) || (topGapY == 0)) ? 5 : 4;
36+
37+
if (cornerGapX+cornerGapY < cornerThreshhold) {
38+
39+
gl_FragColor = vec4(0,0,0,1);
40+
41+
} else if (topGapY < 20) {
42+
43+
if (topGapY == 19) {
44+
45+
gl_FragColor = vec4(0,0,0,1);
46+
47+
} else {
48+
49+
gl_FragColor = vec4(1,1,1,1);
50+
51+
}
52+
53+
} else {
54+
55+
float xError = 0.0;
56+
for(int xLook=0; xLook<lookupSize; xLook++){
57+
float grayscale = getGrayscale(gl_FragCoord.xy + vec2(-lookupSize+xLook,0));
58+
grayscale += xError;
59+
float bit = grayscale >= 0.5 ? 1.0 : 0.0;
60+
xError = (grayscale - bit)*errorCarry;
61+
}
62+
63+
float yError = 0.0;
64+
for(int yLook=0; yLook<lookupSize; yLook++){
65+
float grayscale = getGrayscale(gl_FragCoord.xy + vec2(0,-lookupSize+yLook));
66+
grayscale += yError;
67+
float bit = grayscale >= 0.5 ? 1.0 : 0.0;
68+
yError = (grayscale - bit)*errorCarry;
69+
}
70+
71+
float finalGrayscale = getGrayscale(gl_FragCoord.xy);
72+
finalGrayscale += xError*0.5 + yError*0.5;
73+
float finalBit = finalGrayscale >= 0.5 ? 1.0 : 0.0;
74+
75+
gl_FragColor = vec4(finalBit,finalBit,finalBit,1);
76+
77+
}
78+
79+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# Mirror 2
3+
# by Daniel Shiffman.
4+
#
5+
# Each pixel from the video source is drawn as a rectangle with size based
6+
# on brightness.
7+
8+
load_library :video
9+
include_package 'processing.video'
10+
11+
CELL_SIZE = 15
12+
13+
attr_reader :cols, :rows, :video
14+
15+
def setup
16+
size(640, 480)
17+
# Set up columns and rows
18+
@cols = width / CELL_SIZE
19+
@rows = height / CELL_SIZE
20+
color_mode(RGB, 255, 255, 255, 100)
21+
rect_mode(CENTER)
22+
# This the default video input, see the GettingStartedCapture
23+
# example if it creates an error
24+
@video = Capture.new(self, width, height)
25+
# Start capturing the images from the camera
26+
video.start
27+
background(0)
28+
end
29+
30+
def draw
31+
return unless video.available
32+
video.read
33+
video.load_pixels
34+
background(0, 0, 255)
35+
# Begin loop for columns
36+
cols.times do |i|
37+
# Begin loop for rows
38+
rows.times do |j|
39+
# Where are we, pixel-wise?
40+
x = i * CELL_SIZE
41+
y = j * CELL_SIZE
42+
# Reversing x to mirror the image
43+
loc = (video.width - x - 1) + y * video.width
44+
# Each rect is colored white with a size determined by brightness
45+
c = video.pixels[loc]
46+
sz = (brightness(c) / 255.0) * CELL_SIZE
47+
fill(255)
48+
no_stroke
49+
rect(x + CELL_SIZE / 2, y + CELL_SIZE / 2, sz, sz)
50+
end
51+
end
52+
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifdef GL_ES
2+
precision highp float;
3+
#endif
4+
5+
// This line is optional from Processing 2.1 and up
6+
#define PROCESSING_TEXTURE_SHADER
7+
8+
uniform sampler2D texture; // iChannel0 in Shadertoy
9+
uniform vec2 sketchSize; // iResolution in Shadertoy
10+
11+
12+
const int lookupSize = 64;
13+
const float errorCarry = 0.3;
14+
15+
float getGrayscale(vec2 coords){
16+
vec2 uv = coords / sketchSize.xy;
17+
uv.y = 1.0-uv.y;
18+
vec3 sourcePixel = texture2D(texture, uv).rgb;
19+
return length(sourcePixel*vec3(0.2126,0.7152,0.0722));
20+
}
21+
22+
void main( void ) {
23+
24+
int topGapY = int(sketchSize.y - gl_FragCoord.y);
25+
26+
int cornerGapX = int((gl_FragCoord.x < 10.0) ? gl_FragCoord.x : sketchSize.x - gl_FragCoord.x);
27+
int cornerGapY = int((gl_FragCoord.y < 10.0) ? gl_FragCoord.y : sketchSize.y - gl_FragCoord.y);
28+
int cornerThreshhold = ((cornerGapX == 0) || (topGapY == 0)) ? 5 : 4;
29+
30+
if (cornerGapX+cornerGapY < cornerThreshhold) {
31+
32+
gl_FragColor = vec4(0,0,0,1);
33+
34+
} else if (topGapY < 20) {
35+
36+
if (topGapY == 19) {
37+
38+
gl_FragColor = vec4(0,0,0,1);
39+
40+
} else {
41+
42+
gl_FragColor = vec4(1,1,1,1);
43+
44+
}
45+
46+
} else {
47+
48+
float xError = 0.0;
49+
for(int xLook=0; xLook<lookupSize; xLook++){
50+
float grayscale = getGrayscale(gl_FragCoord.xy + vec2(-lookupSize+xLook,0));
51+
grayscale += xError;
52+
float bit = grayscale >= 0.5 ? 1.0 : 0.0;
53+
xError = (grayscale - bit)*errorCarry;
54+
}
55+
56+
float yError = 0.0;
57+
for(int yLook=0; yLook<lookupSize; yLook++){
58+
float grayscale = getGrayscale(gl_FragCoord.xy + vec2(0,-lookupSize+yLook));
59+
grayscale += yError;
60+
float bit = grayscale >= 0.5 ? 1.0 : 0.0;
61+
yError = (grayscale - bit)*errorCarry;
62+
}
63+
64+
float finalGrayscale = getGrayscale(gl_FragCoord.xy);
65+
finalGrayscale += xError*0.5 + yError*0.5;
66+
float finalBit = finalGrayscale >= 0.5 ? 1.0 : 0.0;
67+
68+
gl_FragColor = vec4(finalBit,finalBit,finalBit,1);
69+
70+
}
71+
72+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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('steinberg.glsl')
11+
my_shader.set('sketchSize', width.to_f, height.to_f)
12+
start_capture(width, height)
13+
end
14+
15+
def start_capture(w, h)
16+
@cam = Capture.new(self, w, h)
17+
cam.start
18+
end
19+
20+
def draw
21+
image(cam, 0, 0)
22+
return if mouse_pressed?
23+
filter(my_shader)
24+
end
25+
26+
# using snake case to match java reflect method
27+
def captureEvent(c)
28+
c.read
29+
end

0 commit comments

Comments
 (0)