Skip to content
Open
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
Expand Up @@ -10,7 +10,7 @@ float terrain(vec2 p)
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord/iResolution.xy;
vec2 l = iMouse.xy/iResolution.xy;
vec2 l = iMouse.xy / vec2(textureSize(iChannel1, 0));
vec2 d = p - l;
float b = 1.0;
for(float i = 0.0; i < N; i++)
Expand Down
5 changes: 3 additions & 2 deletions doc/tutorials/raycasting/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def on_draw(self):
self.use()
self.clear()
# Do the shadow casting
self.shadertoy.render(mouse_position=self.player_sprite.position)
self.shadertoy.render(
mouse_position=self.player_sprite.position, size=self.get_framebuffer_size()
)
# Draw the player and walls on top
self.wall_list.draw()
self.player_list.draw()
Expand Down Expand Up @@ -136,7 +138,6 @@ def on_update(self, delta_time):
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(
Expand Down
37 changes: 16 additions & 21 deletions doc/tutorials/raycasting/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ function. Note that the input data is labeled ``in`` and the output data is labe
The ``vec2`` data type is an array of two numbers. Likewise there are ``vec3`` and ``vec4``
data types. These can be used to store coordinates, and also colors.

Or first step is to normalize the x, y coordinate to a number between 0.0 and 1.0.
Our first step is to normalize the x, y coordinate to a number between 0.0 and 1.0.
This normalized two-number x/y vector we store in ``normalizedFragCoord``.

.. code-block:: glsl

vec2 p = fragCoord/iResolution.xy;

``fragCoord`` is measured in framebuffer pixels. We pass ``get_framebuffer_size()``
to ``Shadertoy.render`` so ``iResolution`` uses the same units. The offscreen scene
textures still use the logical window size that Arcade uses to draw sprites.

We need to grab the color at this point ``curPoint`` from the channel 0 FBO. We can do this
with the built-in ``texture`` function:

Expand Down Expand Up @@ -188,26 +192,16 @@ We first declare and use the variables in our shader program.
:language: glsl
:lines: 1-4

Next, we need to know how far away this point is from the light. We do that
by subtracting this point from the light position. We can perform mathematical
operations on vectors, so we just subtract. Then we use the build-in
``length`` function to get a floating point number of how long the length of this
vector is.

.. literalinclude:: step_03.glsl
:caption: GLSL Program for Step 3
:language: glsl
:lines: 8-9

Next, we need to get the coordinate of the pixel we are calculating, but
**normalized**. The coordinates will range from 0.0 to 1.0, with the left
bottom of the window at (0,0), and the top right at (1,1). Normalized coordinates
are used in shaders to make scaling up and down easy.
First, normalize the fragment coordinate. The coordinates range from 0.0 to 1.0,
with the bottom left of the window at (0, 0) and the top right at (1, 1).
Multiply that coordinate by the scene texture size to get the fragment position
in logical window pixels. Now we can subtract it from the light position and
use ``length`` to measure the distance in the same units as the light's size.

.. literalinclude:: step_03.glsl
:caption: GLSL Program for Step 3
:language: glsl
:lines: 11-12
:lines: 8-13

Then we need to calculate how much light is falling on this coordinate.
This number will also be normalized.
Expand Down Expand Up @@ -235,15 +229,15 @@ and 0.0 outside the light. So a simple subtraction from 1.0 will solve this issu
.. literalinclude:: step_03.glsl
:caption: GLSL Program for Step 3
:language: glsl
:lines: 14-18
:lines: 15-19

Next, we are going to use the built-in ``mix`` function and the ``lightAmount``
variable to alternate between whatever is in channel 1, and a black shadow color.

.. literalinclude:: step_03.glsl
:caption: GLSL Program for Step 3
:language: glsl
:lines: 20-25
:lines: 21-26

Finally we'll go back to the Python program and update our ``on_draw`` method to:

Expand Down Expand Up @@ -295,7 +289,7 @@ this pixel.
.. literalinclude:: step_04.glsl
:caption: GLSL Program for Step 4
:language: glsl
:lines: 23-30
:lines: 24-31
:emphasize-lines: 4-5

* :ref:`raycasting_step_04_gl` |larr| Full listing of where we are right now with the GLSL program
Expand Down Expand Up @@ -398,7 +392,8 @@ You'll need to enable resizing in the window's ``__init__``:
:lines: 1-2
:emphasize-lines: 2

Then we need to override the ``Window.resize`` method to also resize the shadertoy:
Override ``Window.on_resize`` to resize both scene textures to the new logical
window size. Each draw still passes the current framebuffer size to Shadertoy.

.. literalinclude:: step_07.py
:caption: Resizing the window
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorials/raycasting/step_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def on_draw(self):
# Clear to background color
self.clear()
# Run the shader and render to the window
self.shadertoy.render()
self.shadertoy.render(size=self.get_framebuffer_size())

def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed. """
Expand Down
11 changes: 6 additions & 5 deletions doc/tutorials/raycasting/step_03.glsl
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// x, y position of the light
uniform vec2 lightPosition;
// Size of light in pixels
// Size of light in logical window pixels
uniform float lightSize;

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Distance in pixels to the light
float distanceToLight = length(lightPosition - fragCoord);
// Normalize the fragment coordinate from framebuffer pixels to (0.0, 1.0).
vec2 normalizedFragCoord = fragCoord / iResolution.xy;

// Normalize the fragment coordinate from (0.0, 0.0) to (1.0, 1.0)
vec2 normalizedFragCoord = fragCoord/iResolution.xy;
// The scene texture and the light both use logical window coordinates.
vec2 windowSize = vec2(textureSize(iChannel1, 0));
float distanceToLight = length(lightPosition - normalizedFragCoord * windowSize);

// Start our mixing variable at 1.0
float lightAmount = 1.0;
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorials/raycasting/step_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def on_draw(self):
# Run the shader and render to the window
self.shadertoy.program['lightPosition'] = self.player_sprite.position
self.shadertoy.program['lightSize'] = 300
self.shadertoy.render()
self.shadertoy.render(size=self.get_framebuffer_size())
# Draw the player
self.player_list.draw()

Expand Down
11 changes: 6 additions & 5 deletions doc/tutorials/raycasting/step_04.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// x, y position of the light
uniform vec2 lightPosition;
// Size of light in pixels
// Size of light in logical window pixels
uniform float lightSize;

float terrain(vec2 samplePoint)
Expand All @@ -14,11 +14,12 @@ float terrain(vec2 samplePoint)

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Distance in pixels to the light
float distanceToLight = length(lightPosition - fragCoord);
// Normalize the fragment coordinate from framebuffer pixels to (0.0, 1.0).
vec2 normalizedFragCoord = fragCoord / iResolution.xy;

// Normalize the fragment coordinate from (0.0, 0.0) to (1.0, 1.0)
vec2 normalizedFragCoord = fragCoord/iResolution.xy;
// The scene texture and the light both use logical window coordinates.
vec2 windowSize = vec2(textureSize(iChannel1, 0));
float distanceToLight = length(lightPosition - normalizedFragCoord * windowSize);

// Start our mixing variable at 1.0
float lightAmount = 1.0;
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorials/raycasting/step_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def on_draw(self):
# Run the shader and render to the window
self.shadertoy.program['lightPosition'] = self.player_sprite.position
self.shadertoy.program['lightSize'] = 300
self.shadertoy.render()
self.shadertoy.render(size=self.get_framebuffer_size())
# Draw the player
self.player_list.draw()

Expand Down
13 changes: 7 additions & 6 deletions doc/tutorials/raycasting/step_05.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// x, y position of the light
uniform vec2 lightPosition;
// Size of light in pixels
// Size of light in logical window pixels
uniform float lightSize;

float terrain(vec2 samplePoint)
Expand All @@ -16,12 +16,13 @@ float terrain(vec2 samplePoint)

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Distance in pixels to the light
float distanceToLight = length(lightPosition - fragCoord);
// Normalize the fragment coordinate from framebuffer pixels to (0.0, 1.0).
vec2 normalizedFragCoord = fragCoord / iResolution.xy;

// Normalize the fragment coordinate from (0.0, 0.0) to (1.0, 1.0)
vec2 normalizedFragCoord = fragCoord/iResolution.xy;
vec2 normalizedLightCoord = lightPosition.xy/iResolution.xy;
// The scene texture and the light both use logical window coordinates.
vec2 windowSize = vec2(textureSize(iChannel1, 0));
float distanceToLight = length(lightPosition - normalizedFragCoord * windowSize);
vec2 normalizedLightCoord = lightPosition / windowSize;

// Start our mixing variable at 1.0
float lightAmount = 1.0;
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorials/raycasting/step_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def on_draw(self):
# Run the shader and render to the window
self.shadertoy.program['lightPosition'] = self.player_sprite.position
self.shadertoy.program['lightSize'] = 300
self.shadertoy.render()
self.shadertoy.render(size=self.get_framebuffer_size())
# Draw the player
self.player_list.draw()

Expand Down
13 changes: 7 additions & 6 deletions doc/tutorials/raycasting/step_06.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// x, y position of the light
uniform vec2 lightPosition;
// Size of light in pixels
// Size of light in logical window pixels
uniform float lightSize;

float terrain(vec2 samplePoint)
Expand All @@ -20,12 +20,13 @@ float terrain(vec2 samplePoint)

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Distance in pixels to the light
float distanceToLight = length(lightPosition - fragCoord);
// Normalize the fragment coordinate from framebuffer pixels to (0.0, 1.0).
vec2 normalizedFragCoord = fragCoord / iResolution.xy;

// Normalize the fragment coordinate from (0.0, 0.0) to (1.0, 1.0)
vec2 normalizedFragCoord = fragCoord/iResolution.xy;
vec2 normalizedLightCoord = lightPosition.xy/iResolution.xy;
// The scene texture and the light both use logical window coordinates.
vec2 windowSize = vec2(textureSize(iChannel1, 0));
float distanceToLight = length(lightPosition - normalizedFragCoord * windowSize);
vec2 normalizedLightCoord = lightPosition / windowSize;

// Start our mixing variable at 1.0
float lightAmount = 1.0;
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorials/raycasting/step_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def on_draw(self):
# Run the shader and render to the window
self.shadertoy.program['lightPosition'] = self.player_sprite.position
self.shadertoy.program['lightSize'] = 300
self.shadertoy.render()
self.shadertoy.render(size=self.get_framebuffer_size())

# Draw the walls
self.wall_list.draw()
Expand Down
10 changes: 7 additions & 3 deletions doc/tutorials/raycasting/step_07.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def on_draw(self):
# Run the shader and render to the window
self.shadertoy.program['lightPosition'] = self.player_sprite.position
self.shadertoy.program['lightSize'] = 300
self.shadertoy.render()
self.shadertoy.render(size=self.get_framebuffer_size())

# Draw the walls
self.wall_list.draw()
Expand Down Expand Up @@ -148,8 +148,12 @@ def on_update(self, delta_time):
self.physics_engine.update()

def on_resize(self, width: int, height: int):
super().on_resize(width, height)
self.shadertoy.resize((width, height))
if width <= 0 or height <= 0 or getattr(self, "channel0", None) is None:
return

for channel in (self.channel0, self.channel1):
channel.color_attachments[0].resize((width, height))
channel.resize()


if __name__ == "__main__":
Expand Down
10 changes: 7 additions & 3 deletions doc/tutorials/raycasting/step_08.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def on_draw(self):
self.shadertoy.program['lightSize'] = 300

# Run the shader and render to the window
self.shadertoy.render()
self.shadertoy.render(size=self.get_framebuffer_size())

# Draw the walls
self.wall_list.draw()
Expand Down Expand Up @@ -191,10 +191,14 @@ def scroll_to_player(self, speed=CAMERA_SPEED):
self.camera_sprites.position = arcade.math.lerp_2d(self.camera_sprites.position, position, CAMERA_SPEED)

def on_resize(self, width: int, height: int):
super().on_resize(width, height)
if width <= 0 or height <= 0 or getattr(self, "channel0", None) is None:
return

self.camera_sprites.match_window()
self.camera_gui.match_window()
self.shadertoy.resize((width, height))
for channel in (self.channel0, self.channel1):
channel.color_attachments[0].resize((width, height))
channel.resize()


if __name__ == "__main__":
Expand Down
53 changes: 53 additions & 0 deletions tests/integration/tutorials/test_raycasting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import runpy
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock

import pytest

import arcade

RAYCASTING_DIR = Path(arcade.__file__).parent.parent / "doc" / "tutorials" / "raycasting"


@pytest.mark.parametrize("scale", [1, 1.5, 2])
def test_raycasting_light_matches_player(ctx, monkeypatch, scale):
monkeypatch.chdir(RAYCASTING_DIR)
game_class = runpy.run_path("step_06.py")["MyGame"]
window_size = (160, 120)
framebuffer_size = tuple(int(dimension * scale) for dimension in window_size)
output = ctx.framebuffer(color_attachments=[ctx.texture(framebuffer_size, components=4)])
game = SimpleNamespace(
get_size=lambda: window_size,
get_framebuffer_size=lambda: framebuffer_size,
player_sprite=SimpleNamespace(position=(120, 80)),
use=output.use,
clear=output.clear,
wall_list=Mock(),
bomb_list=Mock(),
player_list=Mock(),
)
game_class.load_shader(game)
game_class.on_draw(game)

# Sample the framebuffer pixel at the light's logical center.
x, y = (int(coordinate * scale) for coordinate in game.player_sprite.position)
_, green, _ = output.read(viewport=(x, y, 1, 1))
assert green == pytest.approx(arcade.color.AMAZON[1], abs=2)


@pytest.mark.parametrize("step", ["step_07.py", "step_08.py"])
def test_raycasting_resizes_scene_buffers(ctx, monkeypatch, step):
monkeypatch.chdir(RAYCASTING_DIR)
game_class = runpy.run_path(step)["MyGame"]
game = SimpleNamespace(
get_size=lambda: (160, 120),
camera_sprites=Mock(),
camera_gui=Mock(),
)
game_class.load_shader(game)
game_class.on_resize(game, 240, 180)

for channel in (game.channel0, game.channel1):
assert channel.color_attachments[0].size == (240, 180)
assert channel.viewport == (0, 0, 240, 180)