Debian Codegraph @

experimental/instancing/sprite_instance_object.py

raw symbols SCIP
1 import pyglet
2 import random
3
4 from pyglet.gl import GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_TRIANGLES
5 from pyglet.graphics.instance import InstanceSourceMixin
6
7 window = pyglet.window.Window(1920, 1080, vsync=False)
8
9 # Set example resource path.
10 pyglet.resource.path = ['../resources']
11 pyglet.resource.reindex()
12
13 # Load example image from resource path.
14 # Avoid using pyglet.image.load as it creates multiple textures.
15 # Resource will load images into a texture atlas, allowing significantly faster performance.
16 image = pyglet.resource.image("pyglet.png")
17
18 # Anchor point on an image is bottom left corner by default.
19 # Set to center point with anchor properties.
20 image.anchor_x = image.width // 2
21 image.anchor_y = image.height // 2
22
23 # Batching allows rendering groups of objects all at once instead of drawing one by one.
24 batch = pyglet.graphics.Batch()
25
26 scales = [1.0, 0.75, 0.5, 0.25]
27
28
29 class SpriteInstanceSource(pyglet.sprite.Sprite, InstanceSourceMixin):
30 """Subclass example."""
31
32 def __init__(self, img, attributes, x=0, y=0, z=0, blend_src=GL_SRC_ALPHA, blend_dest=GL_ONE_MINUS_SRC_ALPHA, batch=None,
33 group=None, subpixel=False):
34 self.instance_attributes = attributes
35 InstanceSourceMixin.__init__(self, attributes)
36 pyglet.sprite.Sprite.__init__(self, img, x, y, z, blend_src, blend_dest, batch, group, subpixel)
37
38
39 def _create_vertex_list(self):
40 self._vertex_list = self.program.vertex_list_instanced_indexed(
41 4, GL_TRIANGLES, [0, 1, 2, 0, 2, 3], self.instance_attributes,
42 self._batch, self._group,
43 position=('f', self._get_vertices()),
44 colors=('Bn', (*self._rgb, int(self._opacity)) * (1 if 'colors' in self.instance_attributes else 4)),
45 translate=('f', (self._x, self._y, self._z) * (1 if 'translate' in self.instance_attributes else 4)),
46 scale=('f', (self._scale*self._scale_x, self._scale*self._scale_y) * (1 if 'scale' in self.instance_attributes else 4)),
47 rotation=('f', (self._rotation,) * (1 if 'rotation' in self.instance_attributes else 4)),
48 tex_coords=('f', self._texture.tex_coords))
49
50
51 sprites = []
52 # Create 1000 sprites at various scales.
53 sprite = SpriteInstanceSource(image, ('translate', 'colors'),
54 x=random.randint(0, window.width),
55 y=random.randint(0, window.height),
56 batch=batch)
57
58 instance = sprite.create_instance(translate=(100, 50, 0), colors=(255, 255, 255, 255))
59
60 for i in range(10):
61 sprite_instance = sprite.create_instance(
62 translate=(random.randint(0, window.width), random.randint(0, window.height), 0),
63 colors=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255),
64 )
65
66 #Randomize scale.
67 #sprite_instance.scale = random.choice(scales)
68
69 #Random color multiplier.
70 #sprite_instance.colors = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
71
72 # Add sprites to keep in memory, like a list. Otherwise they will get GC'd when out of scope.
73 sprites.append(sprite_instance)
74
75 #fps = pyglet.window.FPSDisplay(window)
76
77 @window.event
78 def on_key_press(symbol, modifiers):
79 if symbol == pyglet.window.key.SPACE:
80 instance.translate = (random.randint(0, window.width), random.randint(0, window.height), 0)
81 # instance.translate[:] = (random.randint(0, window.width), random.randint(0, window.height), 0)
82 elif symbol == pyglet.window.key.DELETE:
83 del_instance = instance_sprite.pop()
84 sprites.remove(del_instance)
85 elif symbol == pyglet.window.key.INSERT:
86 sprite_instance = instance_sprite.create(
87 translate=(random.randint(0, window.width), random.randint(0, window.height), 0),
88 colors=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255),
89 )
90 sprites.append(sprite_instance)
91
92 @window.event
93 def on_draw():
94 window.clear()
95 batch.draw()
96 #fps.draw()
97
98
99 pyglet.app.run(0)
100