| 1 |
|
| 2 |
|
| 3 |
import pyglet |
| 4 |
from pyglet.experimental import multitexture_sprite |
| 5 |
|
| 6 |
window = pyglet.window.Window() |
| 7 |
window.set_size(400, 400) |
| 8 |
|
| 9 |
pyglet.resource.path = ['../../examples/resources'] |
| 10 |
pyglet.resource.reindex() |
| 11 |
|
| 12 |
|
| 13 |
batch = pyglet.graphics.Batch() |
| 14 |
|
| 15 |
brick_grid = pyglet.image.ImageGrid(pyglet.resource.image("Brick1Gray.png", atlas=False), 1, 6) |
| 16 |
brick_gray = pyglet.image.Animation.from_image_sequence(brick_grid, 1.0 / 10.0) |
| 17 |
|
| 18 |
brick_grid = pyglet.image.ImageGrid(pyglet.resource.image("Brick1Blue.png"), 1, 6) |
| 19 |
brick_animation = pyglet.image.Animation.from_image_sequence(brick_grid, 1.0 / 10.0) |
| 20 |
crack_grid = pyglet.image.ImageGrid(pyglet.resource.image("Brick1Crack3.png"), 1, 3) |
| 21 |
crack_animation = pyglet.image.Animation.from_image_sequence(crack_grid, 1.0, False) |
| 22 |
shader_images = {'brick': brick_animation, 'crack': crack_animation} |
| 23 |
|
| 24 |
sprite = multitexture_sprite.MultiTextureSprite(shader_images, |
| 25 |
x=0, |
| 26 |
y=0, |
| 27 |
batch=batch) |
| 28 |
sprite2 = multitexture_sprite.MultiTextureSprite({'brick': brick_animation, 'crack': crack_animation}, |
| 29 |
x=100, |
| 30 |
y=100, |
| 31 |
batch=batch) |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
custom_vertex_source = """#version 150 core |
| 36 |
in vec3 translate; |
| 37 |
in vec4 colors; |
| 38 |
|
| 39 |
// The MultiTextureSprite class expects the texture coordinates for each texture |
| 40 |
// to follow the naming convention {name}_coords |
| 41 |
in vec3 kitten_coords; |
| 42 |
in vec3 logo_coords; |
| 43 |
|
| 44 |
in vec2 scale; |
| 45 |
in vec3 position; |
| 46 |
in float rotation; |
| 47 |
|
| 48 |
out vec4 vertex_colors; |
| 49 |
|
| 50 |
// The MultiTextureSprite class expects the texture coordinates for each texture |
| 51 |
// going to the fragment shader to follow the naming convention {name}_coords_frag |
| 52 |
out vec3 kitten_coords_frag; |
| 53 |
out vec3 logo_coords_frag; |
| 54 |
|
| 55 |
uniform WindowBlock |
| 56 |
{ |
| 57 |
mat4 projection; |
| 58 |
mat4 view; |
| 59 |
} window; |
| 60 |
|
| 61 |
mat4 m_scale = mat4(1.0); |
| 62 |
mat4 m_rotation = mat4(1.0); |
| 63 |
mat4 m_translate = mat4(1.0); |
| 64 |
|
| 65 |
void main() |
| 66 |
{ |
| 67 |
m_scale[0][0] = scale.x; |
| 68 |
m_scale[1][1] = scale.y; |
| 69 |
m_translate[3][0] = translate.x; |
| 70 |
m_translate[3][1] = translate.y; |
| 71 |
m_translate[3][2] = translate.z; |
| 72 |
m_rotation[0][0] = cos(-radians(rotation)); |
| 73 |
m_rotation[0][1] = sin(-radians(rotation)); |
| 74 |
m_rotation[1][0] = -sin(-radians(rotation)); |
| 75 |
m_rotation[1][1] = cos(-radians(rotation)); |
| 76 |
|
| 77 |
gl_Position = window.projection * window.view * m_translate * m_rotation * m_scale * vec4(position, 1.0); |
| 78 |
|
| 79 |
vertex_colors = colors; |
| 80 |
kitten_coords_frag = kitten_coords; |
| 81 |
logo_coords_frag = logo_coords; |
| 82 |
} |
| 83 |
""" |
| 84 |
|
| 85 |
custom_fragment_source = """#version 150 core |
| 86 |
in vec4 vertex_colors; |
| 87 |
|
| 88 |
// The MultiTextureSprite class expects the texture coordinates for each texture |
| 89 |
// to follow the naming convention {name}_coords_frag in the fragment shader |
| 90 |
in vec3 kitten_coords_frag; |
| 91 |
in vec3 logo_coords_frag; |
| 92 |
|
| 93 |
out vec4 final_colors; |
| 94 |
|
| 95 |
// The MultiTextureSprite class expects the sampler for each texture |
| 96 |
// to follow the naming convention {name} |
| 97 |
uniform sampler2D kitten; |
| 98 |
uniform sampler2D logo; |
| 99 |
|
| 100 |
void main() |
| 101 |
{ |
| 102 |
final_colors = texture(kitten, kitten_coords_frag.xy) * texture(logo, logo_coords_frag.xy) * vertex_colors; |
| 103 |
} |
| 104 |
""" |
| 105 |
|
| 106 |
logo = pyglet.resource.image("pyglet.png") |
| 107 |
kitten = pyglet.resource.image("kitten.jpg") |
| 108 |
multi_vert_shader = pyglet.graphics.shader.Shader(custom_vertex_source, 'vertex') |
| 109 |
multi_frag_shader = pyglet.graphics.shader.Shader(custom_fragment_source, 'fragment') |
| 110 |
multitex_shader_program = pyglet.graphics.shader.ShaderProgram(multi_vert_shader, multi_frag_shader) |
| 111 |
sprite3 = multitexture_sprite.MultiTextureSprite({'kitten': kitten, 'logo': logo}, |
| 112 |
x=150, |
| 113 |
y=150, |
| 114 |
batch=batch, |
| 115 |
program=multitex_shader_program) |
| 116 |
sprite3.scale = 0.2 |
| 117 |
|
| 118 |
f = 0 |
| 119 |
|
| 120 |
|
| 121 |
@window.event |
| 122 |
def on_draw(): |
| 123 |
window.clear() |
| 124 |
batch.draw() |
| 125 |
|
| 126 |
|
| 127 |
def crack(dt): |
| 128 |
"""Change frames for the second layer. |
| 129 |
""" |
| 130 |
global f |
| 131 |
f = (f + 1) % 3 |
| 132 |
sprite.set_frame_index('crack', f) |
| 133 |
pyglet.clock.schedule_once(crack, 1.0) |
| 134 |
|
| 135 |
|
| 136 |
def pause(dt): |
| 137 |
print(f"Current paused value of sprite2 {sprite2.paused}") |
| 138 |
sprite2.paused = not sprite2.paused |
| 139 |
pyglet.clock.schedule_once(pause, 3.5) |
| 140 |
|
| 141 |
|
| 142 |
def swap_animation(dt): |
| 143 |
sprite.set_layer('brick', brick_gray) |
| 144 |
|
| 145 |
|
| 146 |
pyglet.clock.schedule_once(crack, 1.0) |
| 147 |
pyglet.clock.schedule_once(pause, 3.5) |
| 148 |
pyglet.clock.schedule_once(swap_animation, 5) |
| 149 |
|
| 150 |
pyglet.app.run() |