From c39bd5ce47c77a01d46fe6ac8487e9ea90cfccf8 Mon Sep 17 00:00:00 2001 From: Kuldeep Singh Sisodiya <51110406079@piemr.edu.in> Date: Mon, 30 Nov 2026 13:49:24 +0530 Subject: [PATCH] Align ray-casting tutorial lighting on HiDPI displays --- doc/tutorials/raycasting/example.glsl | 2 +- doc/tutorials/raycasting/example.py | 5 +- doc/tutorials/raycasting/index.rst | 37 ++++++------- doc/tutorials/raycasting/step_01.py | 2 +- doc/tutorials/raycasting/step_03.glsl | 11 ++-- doc/tutorials/raycasting/step_03.py | 2 +- doc/tutorials/raycasting/step_04.glsl | 11 ++-- doc/tutorials/raycasting/step_04.py | 2 +- doc/tutorials/raycasting/step_05.glsl | 13 ++--- doc/tutorials/raycasting/step_05.py | 2 +- doc/tutorials/raycasting/step_06.glsl | 13 ++--- doc/tutorials/raycasting/step_06.py | 2 +- doc/tutorials/raycasting/step_07.py | 10 ++-- doc/tutorials/raycasting/step_08.py | 10 ++-- .../integration/tutorials/test_raycasting.py | 53 +++++++++++++++++++ 15 files changed, 118 insertions(+), 57 deletions(-) create mode 100644 tests/integration/tutorials/test_raycasting.py diff --git a/doc/tutorials/raycasting/example.glsl b/doc/tutorials/raycasting/example.glsl index 1757853dd8..2784b8bad0 100644 --- a/doc/tutorials/raycasting/example.glsl +++ b/doc/tutorials/raycasting/example.glsl @@ -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++) diff --git a/doc/tutorials/raycasting/example.py b/doc/tutorials/raycasting/example.py index 57c6a7c353..4210d71eaa 100644 --- a/doc/tutorials/raycasting/example.py +++ b/doc/tutorials/raycasting/example.py @@ -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() @@ -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( diff --git a/doc/tutorials/raycasting/index.rst b/doc/tutorials/raycasting/index.rst index 49e56959eb..ef00a6f94f 100644 --- a/doc/tutorials/raycasting/index.rst +++ b/doc/tutorials/raycasting/index.rst @@ -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: @@ -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. @@ -235,7 +229,7 @@ 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. @@ -243,7 +237,7 @@ 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: @@ -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 @@ -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 diff --git a/doc/tutorials/raycasting/step_01.py b/doc/tutorials/raycasting/step_01.py index 3be5c6f8cb..c94c3f0b78 100644 --- a/doc/tutorials/raycasting/step_01.py +++ b/doc/tutorials/raycasting/step_01.py @@ -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. """ diff --git a/doc/tutorials/raycasting/step_03.glsl b/doc/tutorials/raycasting/step_03.glsl index a7f81ec6c3..b78dfceac1 100644 --- a/doc/tutorials/raycasting/step_03.glsl +++ b/doc/tutorials/raycasting/step_03.glsl @@ -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; diff --git a/doc/tutorials/raycasting/step_03.py b/doc/tutorials/raycasting/step_03.py index f0658d76ca..2e9017261a 100644 --- a/doc/tutorials/raycasting/step_03.py +++ b/doc/tutorials/raycasting/step_03.py @@ -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() diff --git a/doc/tutorials/raycasting/step_04.glsl b/doc/tutorials/raycasting/step_04.glsl index 97a79620b3..31cbe6374b 100644 --- a/doc/tutorials/raycasting/step_04.glsl +++ b/doc/tutorials/raycasting/step_04.glsl @@ -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) @@ -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; diff --git a/doc/tutorials/raycasting/step_04.py b/doc/tutorials/raycasting/step_04.py index 9a82398bc1..6d559ac3fd 100644 --- a/doc/tutorials/raycasting/step_04.py +++ b/doc/tutorials/raycasting/step_04.py @@ -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() diff --git a/doc/tutorials/raycasting/step_05.glsl b/doc/tutorials/raycasting/step_05.glsl index 02bc51e248..68c27c358d 100644 --- a/doc/tutorials/raycasting/step_05.glsl +++ b/doc/tutorials/raycasting/step_05.glsl @@ -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) @@ -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; diff --git a/doc/tutorials/raycasting/step_05.py b/doc/tutorials/raycasting/step_05.py index 8b154fa257..afd019bc5d 100644 --- a/doc/tutorials/raycasting/step_05.py +++ b/doc/tutorials/raycasting/step_05.py @@ -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() diff --git a/doc/tutorials/raycasting/step_06.glsl b/doc/tutorials/raycasting/step_06.glsl index 6a76cc89ae..5b4e736321 100644 --- a/doc/tutorials/raycasting/step_06.glsl +++ b/doc/tutorials/raycasting/step_06.glsl @@ -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) @@ -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; diff --git a/doc/tutorials/raycasting/step_06.py b/doc/tutorials/raycasting/step_06.py index 1271e9baf3..4a718e1802 100644 --- a/doc/tutorials/raycasting/step_06.py +++ b/doc/tutorials/raycasting/step_06.py @@ -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() diff --git a/doc/tutorials/raycasting/step_07.py b/doc/tutorials/raycasting/step_07.py index 6f5a4fa373..9f105dd502 100644 --- a/doc/tutorials/raycasting/step_07.py +++ b/doc/tutorials/raycasting/step_07.py @@ -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() @@ -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__": diff --git a/doc/tutorials/raycasting/step_08.py b/doc/tutorials/raycasting/step_08.py index b613a8cb17..f5bfee9725 100644 --- a/doc/tutorials/raycasting/step_08.py +++ b/doc/tutorials/raycasting/step_08.py @@ -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() @@ -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__": diff --git a/tests/integration/tutorials/test_raycasting.py b/tests/integration/tutorials/test_raycasting.py new file mode 100644 index 0000000000..6524dbb976 --- /dev/null +++ b/tests/integration/tutorials/test_raycasting.py @@ -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)