Skip to content

Tutorial fixes #2394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/tutorials/raycasting/example.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define N 500
#define N 150

float terrain(vec2 p)
{
Expand Down
47 changes: 34 additions & 13 deletions doc/tutorials/raycasting/example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
from pathlib import Path

import arcade
from arcade.experimental import Shadertoy
Expand All @@ -18,6 +19,7 @@
PLAYING_FIELD_WIDTH = 1600
PLAYING_FIELD_HEIGHT = 1600

CURRENT_DIR = Path(__file__).resolve().parent

class MyGame(arcade.Window):

Expand All @@ -31,7 +33,12 @@ def __init__(self, width, height, title):
self.load_shader()

# Sprites and sprite lists
self.player_sprite = None
self.player_sprite = arcade.Sprite(
":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING,
center_x=256,
center_y=512,
)
self.wall_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bomb_list = arcade.SpriteList()
Expand Down Expand Up @@ -61,18 +68,14 @@ def generate_sprites(self):
placed = True
self.bomb_list.append(bomb)

# Create the player
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING)
self.player_sprite.center_x = 256
self.player_sprite.center_y = 512
# Add player to spritelist
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 load_shader(self):
file = open("example.glsl")
file = open(CURRENT_DIR / "example.glsl")
shader_sourcecode = file.read()
size = self.width, self.height

Expand All @@ -88,19 +91,20 @@ def load_shader(self):
self.shadertoy.channel_1 = self.channel1.color_attachments[0]

def on_draw(self):
# Render in the walls for lighting
self.channel0.use()
# clear_color = 0, 0, 0, 0
# self.channel0.clear(color=clear_color)
self.channel0.clear()
self.wall_list.draw()

self.channel1.use()
# self.channel1.clear(color=clear_color)
self.channel1.clear(color=arcade.color.ARMY_GREEN)
self.channel1.clear(color=arcade.color.AMAZON)
self.bomb_list.draw()

self.use()
# self.camera_sprites.use()
self.shadertoy.render(mouse_position=(self.player_sprite.center_x, self.player_sprite.center_y))
self.clear()
# Do the shadow casting
self.shadertoy.render(mouse_position=self.player_sprite.position)
# Draw the player and walls on top
self.wall_list.draw()
self.player_list.draw()

Expand Down Expand Up @@ -131,6 +135,23 @@ def on_update(self, delta_time):
# example though.)
self.physics_engine.update()

def on_resize(self, width: int, height: int):
self.shadertoy.resize((width, height))
# Resize the channels
size = width, height
self.channel0 = self.shadertoy.ctx.framebuffer(
color_attachments=[self.shadertoy.ctx.texture(size, components=4)]
)
self.shadertoy.channel_0 = self.channel0.color_attachments[0]

self.channel1 = self.shadertoy.ctx.framebuffer(
color_attachments=[self.shadertoy.ctx.texture(size, components=4)]
)
self.shadertoy.channel_1 = self.channel1.color_attachments[0]


return super().on_resize(width, height)


if __name__ == "__main__":
MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
Expand Down
6 changes: 4 additions & 2 deletions doc/tutorials/raycasting/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ output.
How does this shader work?
For each point in our output, this ``mainImage`` function runs and
calculates our output color. For a window that is 800x600 pixels, this function runs
480,000 times for each frame. Modern GPUs can have anywhere between 500-5,000 "cores"
480,000 times for each frame. Modern GPUs can have anywhere between 500-5,000 "threads"
that can calculate these points in parallel for faster processing.

Our current coordinate we are calculating we've brought in as a parameter called ``fragCoord``.
Expand Down Expand Up @@ -333,7 +333,9 @@ With an N of 10:
:width: 40%

We can use an N of 500 to get a good quality shadow. We might need more if
your barriers are small, and the light range is large.
your barriers are small, and the light range is large. Note that the effect
gets increasingly expensive the higher N is. Some tweaks are likely needed
to find the lowest N that still looks good.

.. image:: n500.png
:width: 40%
Expand Down
15 changes: 8 additions & 7 deletions doc/tutorials/raycasting/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ def __init__(self, width, height, title):
super().__init__(width, height, title, resizable=True)

# Sprites and sprite lists
self.player_sprite = None
self.player_sprite = arcade.Sprite(
":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING,
center_x=256,
center_y=512,
)
self.wall_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bomb_list = arcade.SpriteList()
Expand All @@ -33,7 +38,7 @@ def __init__(self, width, height, title):
self.background_color = arcade.color.ARMY_GREEN

def generate_sprites(self):
# -- Set up several columns of walls
# -- Set up several columns of walls (that will cast shadows)
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
Expand All @@ -54,11 +59,7 @@ def generate_sprites(self):
placed = True
self.bomb_list.append(bomb)

# Create the player
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING)
self.player_sprite.center_x = 256
self.player_sprite.center_y = 512
# Add player to spritelist
self.player_list.append(self.player_sprite)

# Physics engine, so we don't run into walls
Expand Down
13 changes: 7 additions & 6 deletions doc/tutorials/raycasting/step_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ def __init__(self, width, height, title):
self.load_shader()

# Sprites and sprite lists
self.player_sprite = None
self.player_sprite = arcade.Sprite(
":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING,
center_x=256,
center_y=512,
)
self.wall_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bomb_list = arcade.SpriteList()
Expand Down Expand Up @@ -82,11 +87,7 @@ def generate_sprites(self):
placed = True
self.bomb_list.append(bomb)

# Create the player
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING)
self.player_sprite.center_x = 256
self.player_sprite.center_y = 512
# Add player sprite to sprite list
self.player_list.append(self.player_sprite)

# Physics engine, so we don't run into walls
Expand Down
15 changes: 8 additions & 7 deletions doc/tutorials/raycasting/step_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ def __init__(self, width, height, title):
self.load_shader()

# Sprites and sprite lists
self.player_sprite = None
self.player_sprite = arcade.Sprite(
":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING,
center_x=256,
center_y=512,
)
self.wall_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bomb_list = arcade.SpriteList()
Expand Down Expand Up @@ -82,11 +87,7 @@ def generate_sprites(self):
placed = True
self.bomb_list.append(bomb)

# Create the player
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING)
self.player_sprite.center_x = 256
self.player_sprite.center_y = 512
# Add player sprite to sprite list
self.player_list.append(self.player_sprite)

# Physics engine, so we don't run into walls
Expand All @@ -100,7 +101,7 @@ def on_draw(self):
self.wall_list.draw()

self.channel1.use()
self.channel1.clear()
self.channel1.clear(color=arcade.color.AMAZON)
# Draw the bombs
self.bomb_list.draw()

Expand Down
9 changes: 7 additions & 2 deletions doc/tutorials/raycasting/step_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ def __init__(self, width, height, title):
self.load_shader()

# Sprites and sprite lists
self.player_sprite = None
self.player_sprite = arcade.Sprite(
":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING,
center_x=256,
center_y=512,
)
self.wall_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bomb_list = arcade.SpriteList()
Expand Down Expand Up @@ -100,7 +105,7 @@ def on_draw(self):
self.wall_list.draw()

self.channel1.use()
self.channel1.clear()
self.channel1.clear(color=arcade.color.AMAZON)
# Draw the bombs
self.bomb_list.draw()

Expand Down
15 changes: 8 additions & 7 deletions doc/tutorials/raycasting/step_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ def __init__(self, width, height, title):
self.load_shader()

# Sprites and sprite lists
self.player_sprite = None
self.player_sprite = arcade.Sprite(
":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING,
center_x=256,
center_y=512,
)
self.wall_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bomb_list = arcade.SpriteList()
Expand Down Expand Up @@ -83,11 +88,7 @@ def generate_sprites(self):
placed = True
self.bomb_list.append(bomb)

# Create the player
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING)
self.player_sprite.center_x = 256
self.player_sprite.center_y = 512
# Add the player to the player list
self.player_list.append(self.player_sprite)

# Physics engine, so we don't run into walls
Expand All @@ -101,7 +102,7 @@ def on_draw(self):
self.wall_list.draw()

self.channel1.use()
self.channel1.clear()
self.channel1.clear(color=arcade.color.AMAZON)
# Draw the bombs
self.bomb_list.draw()

Expand Down
15 changes: 8 additions & 7 deletions doc/tutorials/raycasting/step_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ def __init__(self, width, height, title):
self.load_shader()

# Sprites and sprite lists
self.player_sprite = None
self.player_sprite = arcade.Sprite(
":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING,
center_x=256,
center_y=512,
)
self.wall_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bomb_list = arcade.SpriteList()
Expand Down Expand Up @@ -82,11 +87,7 @@ def generate_sprites(self):
placed = True
self.bomb_list.append(bomb)

# Create the player
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING)
self.player_sprite.center_x = 256
self.player_sprite.center_y = 512
# Add the player to the player list
self.player_list.append(self.player_sprite)

# Physics engine, so we don't run into walls
Expand All @@ -100,7 +101,7 @@ def on_draw(self):
self.wall_list.draw()

self.channel1.use()
self.channel1.clear()
self.channel1.clear(color=arcade.color.AMAZON)
# Draw the bombs
self.bomb_list.draw()

Expand Down
15 changes: 8 additions & 7 deletions doc/tutorials/raycasting/step_07.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ def __init__(self, width, height, title):
self.load_shader()

# Sprites and sprite lists
self.player_sprite = None
self.player_sprite = arcade.Sprite(
":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING,
center_x=256,
center_y=512,
)
self.wall_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bomb_list = arcade.SpriteList()
Expand Down Expand Up @@ -82,11 +87,7 @@ def generate_sprites(self):
placed = True
self.bomb_list.append(bomb)

# Create the player
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING)
self.player_sprite.center_x = 256
self.player_sprite.center_y = 512
# Add the player to the player list
self.player_list.append(self.player_sprite)

# Physics engine, so we don't run into walls
Expand All @@ -100,7 +101,7 @@ def on_draw(self):
self.wall_list.draw()

self.channel1.use()
self.channel1.clear()
self.channel1.clear(color=arcade.color.AMAZON)
# Draw the bombs
self.bomb_list.draw()

Expand Down
15 changes: 8 additions & 7 deletions doc/tutorials/raycasting/step_08.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ def __init__(self, width, height, title):
self.load_shader()

# Sprites and sprite lists
self.player_sprite = None
self.player_sprite = arcade.Sprite(
":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING,
center_x=256,
center_y=512,
)
self.wall_list = arcade.SpriteList()
self.player_list = arcade.SpriteList()
self.bomb_list = arcade.SpriteList()
Expand Down Expand Up @@ -91,11 +96,7 @@ def generate_sprites(self):
placed = True
self.bomb_list.append(bomb)

# Create the player
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
scale=SPRITE_SCALING)
self.player_sprite.center_x = 256
self.player_sprite.center_y = 512
# Add the player to the player list
self.player_list.append(self.player_sprite)

# Physics engine, so we don't run into walls
Expand All @@ -115,7 +116,7 @@ def on_draw(self):
self.wall_list.draw()

self.channel1.use()
self.channel1.clear()
self.channel1.clear(color=arcade.color.AMAZON)
# Draw the bombs
self.bomb_list.draw()

Expand Down
1 change: 1 addition & 0 deletions doc/tutorials/shader_toy_particles/shadertoy_demo_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self):

def on_draw(self):
self.clear()
self.ctx.enable_only(self.ctx.BLEND)
# Set uniform data to send to the GLSL shader
self.shadertoy.program['pos'] = self.mouse["x"], self.mouse["y"]

Expand Down
Loading
Loading