Repository navigation
Ideas for drawing many triangles #805
Replies: 3 comments 5 replies
|
I squeezed quite a bit more performance with more NumPy 😄 Version 3import itertools
import py5
import numpy as np
colors = []
positions = []
TS = 10 # triangle size
N = 5000 # number of positions
RS = 2 # amplitude of random movemnt
def setup():
py5.size(400, 400, py5.P3D)
py5.no_stroke()
create_positions(N)
py5.profile_draw()
def create_positions(n):
global positions, shp, colors
positions = np.array([
(py5.random_int(py5.width),py5.random_int(py5.height))
for _ in range(N)
])
all_vertices = list(itertools.chain.from_iterable(
((rx, ry), (rx + TS, ry), (rx, ry + TS))
for rx, ry in positions
))
shp = py5.create_shape()
shp.begin_shape(py5.TRIANGLES)
shp.vertices(all_vertices)
shp.end_shape()
colors = [py5.color(py5.random_int(255),
py5.random_int(255),
py5.random_int(255))
for triangle in positions for _ in range(3)]
shp.set_fills(colors)
def draw():
global positions
py5.background(0)
# recreate shape
rnd_offsets = np.random.randint(-RS, RS, size=(N, 2))
positions += rnd_offsets
positions %= 400, 400
all_vertices = np.empty((N * 3, 2), dtype=positions.dtype)
all_vertices[0::3] = positions
all_vertices[1::3] = positions + [TS, 0]
all_vertices[2::3] = positions + [0, TS]
shp = py5.create_shape()
shp.begin_shape(py5.TRIANGLES)
shp.vertices(all_vertices)
shp.end_shape()
shp.set_fills(colors)
# # update shape
# rnd_offsets = np.random.randint(-RS, RS, size=(N, 2))
# positions += rnd_offsets
# positions %= 400, 400
# for i, (nx, ny) in enumerate(positions):
# shp.set_vertex(i * 3, nx, ny)
# shp.set_vertex(i * 3 + 1, nx + TS, ny)
# shp.set_vertex(i * 3 + 2, nx, ny + TS)
# draw shape
py5.shape(shp)
# show frame rate
py5.window_title(f'{py5.get_frame_rate():.1f}')
if py5.frame_count == 1001:
py5.print_line_profiler_stats()
py5.run_sketch() |
|
Great use of py5's profile tool! Performance tuning is a great way to learn.
Each time you access those properties you pull the number from Java which has some extra overhead. It would be faster to write code like For this code: positions = np.array([
(py5.random_int(py5.width),py5.random_int(py5.height))
for _ in range(N)
])you don't need an iterator. You can do something like similarly, all_vertices = list(itertools.chain.from_iterable(
((rx, ry), (rx + TS, ry), (rx, ry + TS))
for rx, ry in positions
))this could be redone without the iterator. I think you'd need |

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Last week we were doing some "flocking" explorations at the community center where I work. Inspired by Shiffman's Nature of Code implementation of the Reynolds' Boids simulation model.
Collecting all the boid's "bodies", with boid.update() that returns the boid's position and two other vertices to make a triangle, I was drawing them like this:
This in turn made me think about strategies for drawing and "updating" many colored triangles, so I made these two minimal examples to test perfomance... using
py5.P2Dfor the vertex based coloring, and thepy5.profiler_draw()feature to check performance of some ideas.The main idea is to create a Py5Shape and color the triangles using
shp.set_fills(color_sequence).I knew calling random for individual values was slow, so I got a all the values from NumPy's random before the loop. 😉
But I was surprised to learn that using
py5.widthandpy5.heightinside my triangle loop (thousands of times for each frame) was very slow! 😮The main difference I wanted to test was to update vertices of the triangles, in lieu of recreating the shape, but the result was kind of disapointing... you can judge by yourself! More tests and ideas are welcome!
Version 1
Draw profiler result for version 1
Version 2
Draw profiler result for version 2
I'm not sure my interpretation is correct, but it looks like drawing the same updated shape is much faster than drawing a freshly created shape each frame, but on the other hand
shp.set_vertex()can be rather slow and it offsets the performance making version 2 slower (even if version 1 has to use time recreating the shape and recoloring it).All reactions