Skip to content

Commit f68bfed

Browse files
authored
Merge pull request #1 from ruby-processing/processing3
Processing3
2 parents c6aabbd + 4ff6195 commit f68bfed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+634
-105
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# propane-examples
2-
Example Sketches for propane see also [Example-Sketches][examples] for ruby-processing (many of with only need to be class wrapped to run with propane).
2+
Example Sketches for propane-2.0+ see also [Example-Sketches][examples] for ruby-processing (many of with only need to be class wrapped to run with propane).
33

4-
1. Sketches using the [data_path wrapper][path] (yields absolute path to `data` folder). NB without wrapper these sketches would need to be run using jruby-complete (`propane --run sketch.rb` rather than `jruby sketch.rb`
4+
1. Sketches using the [data_path wrapper][path] (yields absolute path to `data` folder).
55

6-
2. Other [sketches][regular], can be run with `propane --run sketch.rb` or `jruby sketch.rb`
6+
2. Other [sketches][regular]
77

88
[path]:https://github.com/ruby-processing/propane-examples/tree/master/data_path
99
[regular]:https://github.com/ruby-processing/propane-examples/tree/master/data_path

data_path/bw_shader.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ class BwShader < Propane::App
77

88
attr_reader :label, :can, :angle, :bw_shader
99

10-
def setup
10+
def settings
1111
size(640, 360, P3D)
12+
end
13+
14+
def setup
15+
sketch_title 'Black and White Shader'
1216
@label = load_image(data_path('lachoy.jpg'))
1317
@can = create_can(100, 200, 32, label)
1418
@bw_shader = load_shader(data_path('bwfrag.glsl'))
@@ -44,4 +48,4 @@ def create_can(r, h, detail, tex)
4448
end
4549
end
4650

47-
BwShader.new title: 'Black & White Shader'
51+
BwShader.new

data_path/complex_3D.rb

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/usr/bin/env jruby -v -W2
2+
#
3+
# Geometry
4+
# by Marius Watz.
5+
#
6+
7+
require 'propane'
8+
9+
class Complex3D < Propane::App
10+
load_library :pdf
11+
include_package 'processing.pdf'
12+
attr_reader :num, :pt, :style, :dosave
13+
14+
def setup
15+
sketch_title 'Complex 3 D'
16+
background(255)
17+
@dosave = false
18+
@num = 150
19+
@pt = []
20+
@style = []
21+
# Set up arc shapes
22+
index = 0
23+
(0...num).each do |i|
24+
pt << rand(TAU) # Random X axis rotation
25+
pt << rand(TAU) # Random Y axis rotation
26+
pt << rand(60..80) # Short to quarter-circle arcs
27+
pt[pt.length - 1] = rand(8..27) * 10 if rand(100) > 90
28+
pt << rand(2..50) * 5 # Radius. Space them out nicely
29+
pt << rand(4..32) # Width of band
30+
pt[pt.length - 1] = rand(40..60) if (rand(100) > 90) # Width of band
31+
pt << rand(0.005..0.0334)# Speed of rotation
32+
# get colors
33+
prob = rand(100)
34+
case prob
35+
when (0..30)
36+
style[i * 2] = color_blended(rand, 255,0,100, 255,0,0, 210)
37+
when (30..70)
38+
style[i * 2] = color_blended(rand, 0,153,255, 170,225,255, 210)
39+
when (70..90)
40+
style[i * 2] = color_blended(rand, 200,255,0, 150,255,0, 210)
41+
else
42+
style[i * 2] = color(255,255,255, 220)
43+
end
44+
case prob
45+
when (0..50)
46+
style[i * 2] = color_blended(rand, 200,255,0, 50,120,0, 210)
47+
when (50..90)
48+
style[i * 2] = color_blended(rand, 255,100,0, 255,255,0, 210)
49+
else
50+
style[i * 2] = color(255, 255, 255, 220)
51+
style[i * 2 + 1] = rand(100) % 3
52+
end
53+
end
54+
end
55+
56+
def draw
57+
if dosave
58+
# set up PGraphicsPDF for use with beginRaw
59+
pdf = begin_raw(PDF, data_path('pdf_complex_out.pdf'))
60+
# set default Illustrator stroke styles and paint background rect.
61+
pdf.stroke_join(MITER)
62+
pdf.stroke_cap(SQUARE)
63+
pdf.fill(0)
64+
pdf.no_stroke
65+
pdf.rect(0, 0, width, height)
66+
end
67+
background(0)
68+
index = 0
69+
translate(width / 2, height / 2, 0)
70+
rotate_x(PI / 6)
71+
rotate_y(PI / 6)
72+
(0 ... num).each do |i|
73+
push_matrix
74+
rotate_x(pt[index])
75+
rotate_y(pt[index + 1])
76+
index += 2
77+
case (style[i * 2 + 1])
78+
when 0
79+
stroke(style[i * 2])
80+
no_fill
81+
stroke_weight(1)
82+
arc_line(0, 0, pt[index], pt[index + 1], pt[index + 2])
83+
index += 3
84+
when 1
85+
fill(style[i * 2])
86+
no_stroke
87+
arc_line_bars(0, 0, pt[index], pt[index + 1],pt[index + 2])
88+
index += 3
89+
else
90+
fill(style[i * 2])
91+
no_stroke
92+
arc(0, 0, pt[index], pt[index + 1], pt[index + 2])
93+
index += 3
94+
end
95+
# increase rotation
96+
pt[index - 5] += pt[index] / 10
97+
pt[index - 4] += pt[index] / 20
98+
index += 1
99+
pop_matrix
100+
end
101+
if dosave
102+
end_raw
103+
@dosave=false
104+
end
105+
end
106+
107+
# Get blend of two colors
108+
def color_blended(fract, r, g, b, r2, g2, b2, a)
109+
r2 = (r2 - r)
110+
g2 = (g2 - g)
111+
b2 = (b2 - b)
112+
color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a)
113+
end
114+
115+
# Draw arc line
116+
def arc_line(x, y, deg, rad, w)
117+
a = (deg < 360)? deg : 0
118+
numlines = w / 2
119+
(0...numlines).each do
120+
begin_shape
121+
(0...a).each do |i|
122+
vertex(DegLut.cos(i) * rad + x, DegLut.sin(i) * rad + y)
123+
end
124+
end_shape
125+
rad += 2
126+
end
127+
end
128+
129+
# Draw arc line with bars
130+
def arc_line_bars(x, y, deg, rad, w)
131+
a=(deg < 360)? deg / 16 : 0
132+
begin_shape(QUADS)
133+
(0...a).step(4) do |i|
134+
vertex(DegLut.cos(i) * rad + x,DegLut.sin(i) * (rad) + y)
135+
vertex(DegLut.cos(i) * (rad + w) + x,DegLut.sin(i) * (rad + w) + y)
136+
vertex(DegLut.cos((i + 2)) * (rad + w) + x, DegLut.sin((i + 2)) * (rad + w) + y)
137+
vertex(DegLut.cos((i + 2)) * rad + x, DegLut.sin((i + 2)) * rad + y)
138+
end
139+
end_shape
140+
end
141+
142+
# Draw solid arc
143+
def arc(x, y, deg, rad, w)
144+
a = (deg < 360)? deg : 0
145+
begin_shape(QUAD_STRIP)
146+
(0...a).each do |i|
147+
vertex(DegLut.cos(i) * rad + x, DegLut.sin(i) * rad + y)
148+
vertex(DegLut.cos(i) * (rad + w) + x, DegLut.sin(i) * (rad + w) + y)
149+
end
150+
end_shape
151+
end
152+
153+
def mouse_pressed
154+
@dosave = true
155+
end
156+
157+
def settings
158+
size(1024, 768, P3D)
159+
end
160+
end
161+
162+
Complex3D.new

data_path/data/arrow.svg

+13
Loading

data_path/edge_detection.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ class EdgeDetection < Propane::App
1010
KERNEL = [[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]].freeze
1111
attr_reader :img
1212

13-
def setup
13+
def settings
1414
size(640, 360)
15+
end
16+
17+
def setup
18+
sketch_title 'Edge Detection'
1519
@img = load_image(data_path('moon.jpg')) # Load the original image
1620
no_loop
1721
end
@@ -46,4 +50,4 @@ def draw
4650
end
4751
end
4852

49-
EdgeDetection.new title: 'Edge Detection'
53+
EdgeDetection.new

data_path/glsl_heightmap_noise.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ class HeightMap < Propane::App
2929
attr_reader :images # array to hold 2 input images
3030
attr_reader :color_map # variable to keep track of the current colorMap
3131

32-
def setup
32+
def settings
3333
size(1280, 720, P3D) # use the P3D OpenGL renderer
34+
end
35+
36+
def setup
37+
sketch_title 'Height Map'
3438
@blur_factor = 3
3539
@resize_factor = 0.25
3640
displace_strength = 0.25 # the displace strength of the GLSL shader displacement effect
@@ -122,4 +126,4 @@ def key_released
122126
end
123127
end
124128

125-
HeightMap.new title: 'Height Map'
129+
HeightMap.new

data_path/kinetic_type.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
# dispay them 'words'
1414
class KineticType < Propane::App
1515

16-
def setup
16+
def settings
1717
size(200, 200, P3D)
18+
end
19+
20+
def setup
21+
sketch_title 'Kinetic Type'
1822
frame_rate 30
1923
# Load the font from the sketch's data directory.
2024
text_font loadFont(data_path('Univers45.vlw')), 1.0
@@ -76,4 +80,4 @@ def initialize(c, x, y)
7680
end
7781
end
7882

79-
KineticType.new title: 'Kinetic Type'
83+
KineticType.new

data_path/landscape.rb

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ class Landscape < Propane::App
1111
# Processing port by Raphaël de Courville.
1212
#
1313
attr_reader :landscape
14-
java_alias :background_int, :background, [Java::int]
14+
java_alias :background_int, :background, [Java::int]
15+
TITLE_FORMAT = 'frame: %d - fps: %0.2f'.freeze
1516

16-
def setup
17+
def settings
1718
size(640, 360, P2D)
19+
end
20+
21+
def setup
22+
sketch_title 'Landscape'
1823
no_stroke
1924
# The code of this shader shows how to integrate shaders from shadertoy
20-
# into Processing with minimal changes.
25+
# into Processing/JRubyArt/propane with minimal changes.
2126
@landscape = load_shader(data_path('landscape.glsl'))
2227
landscape.set('resolution', width.to_f, height.to_f)
2328
end
@@ -27,8 +32,8 @@ def draw
2732
landscape.set('time', (millis/1000.0).to_f)
2833
shader(landscape)
2934
rect(0, 0, width, height)
30-
frame.set_title("frame: #{frame_count} - fps: #{format('%0.2f', frame_rate)}")
35+
sketch_title(format(TITLE_FORMAT, frame_count, frame_rate))
3136
end
3237
end
3338

34-
Landscape.new title: 'Landscape'
39+
Landscape.new

data_path/linear_image.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
class LinearImage < Propane::App
1313
attr_reader :signal, :img, :direction
1414

15-
def setup
15+
def settings
1616
size(640, 360)
17+
end
18+
19+
def setup
20+
sketch_title 'Linear Image'
1721
stroke(255)
1822
@img = load_image(data_path('sea.jpg'))
1923
@direction = 1
@@ -48,4 +52,4 @@ def draw
4852
end
4953
end
5054

51-
LinearImage.new title: 'Linear Image'
55+
LinearImage.new

data_path/magnetic_field.rb

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# Iconic ruby-processing example for Propane
5+
class MagneticField < Propane::App
6+
7+
load_library :hype
8+
include_package 'hype'
9+
# Use Hype namespace
10+
module Hype
11+
java_import 'hype.extended.layout.HGridLayout'
12+
java_import 'hype.extended.behavior.HMagneticField'
13+
java_import 'hype.extended.behavior.HSwarm'
14+
end
15+
16+
attr_reader :pool, :pool_swarm, :field, :swarm
17+
NUM_MAGNETS = 10
18+
19+
def settings
20+
size(640, 640)
21+
end
22+
23+
def setup
24+
sketch_title 'Magnetic Field'
25+
H.init(self)
26+
H.background(color('#000000'))
27+
@field = Hype::HMagneticField.new
28+
NUM_MAGNETS.times do
29+
if rand > 0.5
30+
# x, y, north polarity / strength = 3 / repel
31+
field.add_pole(rand(0..width), rand(0..height), 3)
32+
else
33+
# x, y, south polarity / strength = -3 / attract
34+
field.add_pole(rand(0..width), rand(0..height), -3)
35+
end
36+
end
37+
38+
@pool = HDrawablePool.new(2_500)
39+
pool.auto_add_to_stage
40+
.add(HShape.new(data_path('arrow.svg')).enable_style(false).anchor_at(H::CENTER))
41+
.layout(Hype::HGridLayout.new.start_x(-60).start_y(-60).spacing(16, 16).cols(50))
42+
.on_create do |obj|
43+
obj.no_stroke.anchor(-20, -20)
44+
field.add_target(obj)
45+
end
46+
.requestAll
47+
48+
@swarm = Hype::HSwarm.new.add_goal(width / 2, height / 2).speed(7).turn_ease(0.03).twitch(20)
49+
@pool_swarm = HDrawablePool.new(NUM_MAGNETS)
50+
pool_swarm.auto_add_to_stage
51+
.add(HRect.new(5))
52+
.on_create do |obj|
53+
obj.no_stroke.no_fill.loc(rand(0..width), rand(0..width)).visibility(false)
54+
swarm.add_target(obj)
55+
end
56+
.requestAll
57+
end
58+
59+
def draw
60+
pool_swarm.each_with_index do |d, i|
61+
p = field.pole(i)
62+
p.x = d.x
63+
p.y = d.y
64+
end
65+
H.draw_stage
66+
end
67+
end
68+
69+
MagneticField.new

0 commit comments

Comments
 (0)