|
1 | | -#Terreno |
2 | | -# |
3 | | -# Transcrito a Jruby_Art por Carlos Rocha |
4 | | -# Tomado de The Coding Train https://www.youtube.com/watch?v=IKB1hWWedMk |
5 | | -class Terreno < Processing::App |
6 | | - def settings |
7 | | - size 800, 800, P3D |
8 | | - end |
9 | | - |
10 | | - def setup |
11 | | - sketch_title 'Terreno' |
12 | | - @w = 1400 |
13 | | - @h = 1100 |
14 | | - @scl = 30 |
15 | | - @col = @w/@scl |
16 | | - @filas = @h/@scl |
17 | | - @terreno = {} |
18 | | - @mover = 0 |
19 | | - end |
20 | | - |
21 | | - def draw |
22 | | - background 0 |
23 | | - @mover -= 0.1 |
24 | | - yoff = @mover |
25 | | - for y in 0..@filas |
26 | | - xoff = 0 |
27 | | - for x in 0..@col |
28 | | - @terreno["#{x}.#{y}"]= p5map noise(xoff,yoff), 0, 1, -65, 65 |
29 | | - xoff += 0.2 |
30 | | - end |
31 | | - yoff += 0.2 |
32 | | - end |
33 | | - stroke 255 |
34 | | - no_fill |
35 | | - stroke 235, 69,129 |
36 | | - translate width/2, height/2 |
37 | | - rotate_x PI/3 |
38 | | - translate -@w/2, -@h/2 |
39 | | - for y in 0..@filas-1 |
40 | | - begin_shape(TRIANGLE_STRIP) |
41 | | - for x in 0..@col |
42 | | - vertex(x*@scl, y*@scl, @terreno["#{x}.#{y}"]) |
43 | | - vertex(x*@scl,(y+1)*@scl, @terreno["#{x}.#{y+1}"]) |
44 | | - end |
45 | | - end_shape(CLOSE) |
46 | | - end |
47 | | - end |
48 | | -end |
| 1 | +# After a sketch by Carlos Rocha, press mouse for smooth terrain |
| 2 | +WIDTH = 1_400 |
| 3 | +HEIGHT = 1_100 |
| 4 | +SCL = 30 |
| 5 | +attr_reader :terrain, :rows, :columns, :mover, :hash_key |
| 6 | + |
| 7 | +def settings |
| 8 | + size 800, 800, P3D |
| 9 | +end |
| 10 | + |
| 11 | +def setup |
| 12 | + sketch_title 'Terreno' |
| 13 | + @columns = WIDTH / SCL |
| 14 | + @rows = HEIGHT / SCL |
| 15 | + @terrain = {} |
| 16 | + @hash_key = ->(x, y) { y * WIDTH + x } |
| 17 | + @mover = 0 |
| 18 | +end |
| 19 | + |
| 20 | +def draw |
| 21 | + background 0 |
| 22 | + @mover -= 0.1 |
| 23 | + yoff = mover |
| 24 | + (0..rows).each do |y| |
| 25 | + xoff = 0 |
| 26 | + (0..columns).each do |x| |
| 27 | + terrain[hash_key.call(x, y)] = Vec3D.new( |
| 28 | + x * SCL, y * SCL, |
| 29 | + map1d(SmoothNoise.tnoise(xoff, yoff), -1.0..1.0, -65..65) |
| 30 | + ) |
| 31 | + xoff += 0.2 |
| 32 | + end |
| 33 | + yoff += 0.2 |
| 34 | + end |
| 35 | + no_fill |
| 36 | + stroke 235, 69, 129 |
| 37 | + translate width / 2, height / 2 |
| 38 | + rotate_x PI / 3 |
| 39 | + translate(-WIDTH / 2, -HEIGHT / 2) |
| 40 | + (0...rows).each do |y| |
| 41 | + begin_shape(TRIANGLE_STRIP) |
| 42 | + (0..columns).each do |x| |
| 43 | + terrain[hash_key.call(x, y)].to_vertex(renderer) |
| 44 | + terrain[hash_key.call(x, y.succ)].to_vertex(renderer) |
| 45 | + end |
| 46 | + end_shape |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +def mouse_pressed |
| 51 | + @smth = !smth |
| 52 | +end |
| 53 | + |
| 54 | +private |
| 55 | + |
| 56 | +def renderer |
| 57 | + @renderer ||= GfxRender.new(g) |
| 58 | +end |
0 commit comments