Skip to content

Commit fce1197

Browse files
committed
Why doesn't reflection sketch work?
1 parent 93621f9 commit fce1197

File tree

6 files changed

+104
-9
lines changed

6 files changed

+104
-9
lines changed

demo/blend_mode.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env jruby -w
2+
13
# frozen_string_literal: true
24

35
require 'picrate'

demo/elegant_ball.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env jruby -w
12
require 'picrate'
23
# After a vanilla processing sketch by
34
# Ben Notorianni aka lazydog

demo/reflection.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#!/usr/bin/env jruby -w
12
require 'picrate'
23
# Taken from the Processing Examples.
3-
class Reflection < Processing::App
4+
class ReflectionSketch < Processing::App
45

56
def setup
67
sketch_title 'Reflection'
@@ -10,18 +11,18 @@ def setup
1011
end
1112

1213
def draw
13-
background 0
14-
translate width / 2, height / 2
14+
background 0
15+
translate width / 2, height / 2
1516
lights
16-
light_specular 1, 1, 1
17-
directional_light 0.8, 0.8, 0.8, 0, 0, -1
18-
specular mouse_x.to_f / width.to_f
19-
sphere 50
17+
light_specular 1, 1, 1
18+
directional_light 0.8, 0.8, 0.8, 0, 0, -1
19+
specular mouse_x.to_f / width.to_f
20+
sphere 50
2021
end
2122

2223
def settings
2324
size 200, 200, P3D
2425
end
2526
end
2627

27-
Reflection.new
28+
ReflectionSketch.new

demo/ribbon_doodle.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
#!/usr/bin/env jruby -w
12
# frozen_string_literal: true
3+
require 'picrate'
24
# Adapted to use Processing Vec2D and Vec3D classes by Martin Prout
35

46
# Use Face Struct triangle 'mesh'.
57
Face = Struct.new(:a, :b, :c) # triangle mesh face
6-
require 'picrate'
78

89
# Monkey patch the Vec3D class to support rotations
910
Vec3D.class_eval do # re-open the Vec3D class to add the rotate methods

shaders/data/metaballs.glsl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
/** ----------------------
7+
After a sketch on shadertoy https://www.shadertoy.com/user/atomek
8+
**/
9+
10+
uniform vec3 iResolution; // viewport resolution (in pixels)
11+
uniform float iTime; // shader playback time (in seconds)
12+
uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click
13+
14+
void main()
15+
{
16+
// Hold the mouse and drag to adjust
17+
18+
float ratio = iResolution.y / iResolution.x;
19+
float divider = float(iMouse.x / iResolution.x * 10.0) + 1.0;
20+
float intensity = float(iMouse.y / iResolution.y * 10.0) + 1.0;
21+
22+
float coordX = gl_FragCoord.x / iResolution.x;
23+
float coordY = gl_FragCoord.y / iResolution.x;
24+
25+
float ball1x = sin(iTime * 2.1) * 0.5 + 0.5;
26+
float ball1y = cos(iTime * 1.0) * 0.5 + 0.5;
27+
float ball1z = sin(iTime * 2.0) * 0.1 + 0.2;
28+
29+
float ball2x = sin(iTime * 1.0) * 0.5 + 0.5;
30+
float ball2y = cos(iTime * 1.8) * 0.5 + 0.5;
31+
float ball2z = cos(iTime * 2.0) * 0.1 + 0.2;
32+
33+
float ball3x = sin(iTime * 0.7) * 0.5 + 0.5;
34+
float ball3y = cos(iTime * 1.5) * 0.5 + 0.5;
35+
float ball3z = cos(iTime * 1.0) * 0.1 + 0.2;
36+
37+
vec3 ball1 = vec3(ball1x, ball1y * ratio, ball1z);
38+
vec3 ball2 = vec3(ball2x, ball2y * ratio, ball2z);
39+
vec3 ball3 = vec3(ball3x, ball3y * ratio, ball3z);
40+
41+
float sum = 0.0;
42+
sum += ball1.z / distance(ball1.xy, vec2(coordX, coordY));
43+
sum += ball2.z / distance(ball2.xy, vec2(coordX, coordY));
44+
sum += ball3.z / distance(ball3.xy, vec2(coordX, coordY));
45+
46+
sum = pow(sum / intensity, divider);
47+
48+
gl_FragColor = vec4(sum * 0.2, sum * 0.2, sum * 0.4, 1.0);
49+
}

shaders/metaballs.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'picrate'
2+
3+
class Metaballs < Processing::App
4+
attr_reader :last_mouse_position, :mouse_click_state, :mouse_dragged, :shader_t, :start
5+
6+
def settings
7+
size(640, 360, P2D)
8+
end
9+
10+
def setup
11+
sketch_title 'Shadertoy Metaballs'
12+
@previous_time = 0.0
13+
@mouse_dragged = false
14+
@mouse_click_state = 0.0
15+
# Load the shader file from the "data" folder
16+
@shader_t = load_shader(data_path('metaballs.glsl'))
17+
# Assume the dimension of the window will not change over time
18+
shader_t.set('iResolution', width.to_f, height.to_f, 0.0)
19+
@last_mouse_position = Vec2D.new(mouse_x, mouse_y)
20+
@start = java.lang.System.current_time_millis
21+
end
22+
23+
def draw
24+
# shader playback time (in seconds)
25+
current_time = (java.lang.System.current_time_millis - start) / 1000.0
26+
shader_t.set('iTime', current_time)
27+
if mouse_pressed?
28+
@last_mouse_position = Vec2D.new(mouse_x, mouse_y)
29+
@mouse_click_state = 1.0
30+
else
31+
@mouse_click_state = 0.0
32+
end
33+
shader_t.set('iMouse', last_mouse_position.x, last_mouse_position.y, mouse_click_state, mouse_click_state)
34+
# Apply the specified shader to any geometry drawn from this point
35+
shader(shader_t)
36+
# Draw the output of the shader onto a rectangle that covers the whole viewport.
37+
rect(0, 0, width, height)
38+
end
39+
end
40+
41+
Metaballs.new

0 commit comments

Comments
 (0)