step_01.py Diff
start.py to step_01.py diff
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/raycasting/start.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/raycasting/step_01.py
@@ -1,5 +1,7 @@
import random
+
import arcade
+from arcade.experimental import Shadertoy
# Do the math to figure out our screen dimensions
SCREEN_WIDTH = 800
@@ -20,7 +22,13 @@
class MyGame(arcade.Window):
def __init__(self, width, height, title):
- super().__init__(width, height, title, resizable=True)
+ super().__init__(width, height, title)
+
+ # The shader toy and 'channels' we'll be using
+ self.shadertoy = None
+ self.channel0 = None
+ self.channel1 = None
+ self.load_shader()
# Sprites and sprite lists
self.player_sprite = arcade.Sprite(
@@ -37,8 +45,28 @@
self.generate_sprites()
self.background_color = arcade.color.ARMY_GREEN
+ def load_shader(self):
+ # Size of the window
+ window_size = self.get_size()
+
+ # Create the shader toy, passing in a path for the shader source
+ self.shadertoy = Shadertoy.create_from_file(window_size, "step_01.glsl")
+
+ # Create the channels 0 and 1 frame buffers.
+ # Make the buffer the size of the window, with 4 channels (RGBA)
+ self.channel0 = self.shadertoy.ctx.framebuffer(
+ color_attachments=[self.shadertoy.ctx.texture(window_size, components=4)]
+ )
+ self.channel1 = self.shadertoy.ctx.framebuffer(
+ color_attachments=[self.shadertoy.ctx.texture(window_size, components=4)]
+ )
+
+ # Assign the frame buffers to the channels
+ self.shadertoy.channel_0 = self.channel0.color_attachments[0]
+ self.shadertoy.channel_1 = self.channel1.color_attachments[0]
+
def generate_sprites(self):
- # -- Set up several columns of walls (that will cast shadows)
+ # -- Set up several columns of walls
for x in range(0, PLAYING_FIELD_WIDTH, 128):
for y in range(0, PLAYING_FIELD_HEIGHT, int(128 * SPRITE_SCALING)):
# Randomly skip a box so the player can find a way through
@@ -59,18 +87,25 @@
placed = True
self.bomb_list.append(bomb)
- # Add player to spritelist
+ # Add player sprite to sprite list
self.player_list.append(self.player_sprite)
# Physics engine, so we don't run into walls
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
def on_draw(self):
+ # Select the channel 0 frame buffer to draw on
+ self.channel0.use()
+ self.channel0.clear()
+ # Draw the walls
+ self.wall_list.draw()
+
+ # Select this window to draw on
+ self.use()
+ # Clear to background color
self.clear()
-
- self.wall_list.draw()
- self.bomb_list.draw()
- self.player_list.draw()
+ # Run the shader and render to the window
+ self.shadertoy.render()
def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed. """