From 9467fcf31d75937e1e7a3d88b7863073f63c4db9 Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Fri, 31 Mar 2023 08:03:36 -0400 Subject: [PATCH 01/13] add glsl files for torch --- assets/__init__.py | 2 +- assets/shaders/torch_main.glsl | 31 +++++++++++++++++++++++++++++++ assets/shaders/torch_shadow.glsl | 18 ++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 assets/shaders/torch_main.glsl create mode 100644 assets/shaders/torch_shadow.glsl diff --git a/assets/__init__.py b/assets/__init__.py index 2f6f234..30215d5 100644 --- a/assets/__init__.py +++ b/assets/__init__.py @@ -1,3 +1,3 @@ from .manager import sprites, tilemaps, sounds, fonts, items -__all__ = ["sprites", "tilemaps", "sounds", "fonts", "items"] +__all__ = ["sprites", "tilemaps", "sounds", "fonts", "items", "shaders"] diff --git a/assets/shaders/torch_main.glsl b/assets/shaders/torch_main.glsl new file mode 100644 index 0000000..a815289 --- /dev/null +++ b/assets/shaders/torch_main.glsl @@ -0,0 +1,31 @@ +#version 330 core + +uniform vec2 resolution; +uniform vec2 player_pos; +uniform sampler2D tex; +uniform sampler2D shadow_tex; +uniform float ambient_intensity; +uniform float light_intensity; + +in vec2 frag_pos; + +out vec4 frag_color; + +void main() { + vec4 tex_color = texture(tex, frag_pos); + + // calculate distance between current pixel and player position + float dist = length(player_pos - frag_pos); + + // calculate light intensity based on distance and ambient intensity + float light = ambient_intensity + (light_intensity - ambient_intensity) / (1.0 + pow(dist, 2.0)); + + // get shadow intensity from shadow texture + float shadow = texture(shadow_tex, frag_pos).r; + + // combine light and shadow to get final intensity + float intensity = light * shadow; + + // apply intensity to texture color + frag_color = tex_color * vec4(intensity, intensity, intensity, 1.0); +} diff --git a/assets/shaders/torch_shadow.glsl b/assets/shaders/torch_shadow.glsl new file mode 100644 index 0000000..a1a1266 --- /dev/null +++ b/assets/shaders/torch_shadow.glsl @@ -0,0 +1,18 @@ +#version 330 core + +uniform vec2 resolution; +uniform vec2 player_pos; +uniform sampler2D tex; +uniform float light_radius; +uniform float falloff; + +in vec2 frag_pos; + +out vec4 frag_color; + +void main() { + float dist = length(player_pos - frag_pos); + float alpha = 1.0 - smoothstep(light_radius, light_radius * falloff, dist); + + frag_color = vec4(0.0, 0.0, 0.0, alpha); +} From cc8cd4e0faf2d63738fdaaebc64ba2fbdf8044e3 Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Fri, 31 Mar 2023 08:17:44 -0400 Subject: [PATCH 02/13] implement the torch.py --- .pylintrc | 2 +- game/torch.py | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 game/torch.py diff --git a/.pylintrc b/.pylintrc index 686902a..3d34cf3 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,3 +1,3 @@ [MESSAGES CONTROL] # temporary, until documentation is complete -disable=C0114, C0115, C0116, C0103, W0511, R0902, E1121, W0613 +disable=C0114, C0115, C0116, C0103, W0511, R0902, E1121, W0613, E1101, E1120 diff --git a/game/torch.py b/game/torch.py new file mode 100644 index 0000000..ce70fb1 --- /dev/null +++ b/game/torch.py @@ -0,0 +1,126 @@ +from arcade.experimental import Shadertoy + + +class TorchShaderToy(Shadertoy): + """ShaderToy program to create a torch with shadow effect.""" + + def __init__(self): + super().__init__() + + # Define the uniforms + self.set_uniform("time", 0.0) + self.set_uniform("resolution", (0.0, 0.0)) + self.set_uniform("torch_pos", (0.5, 0.5)) + self.set_uniform("torch_radius", 0.2) + self.set_uniform("light_intensity", 1.0) + + # Set the fragment shader code + self.set_fragment_shader( + """ + #define MAX_STEPS 50 + #define MAX_DIST 200.0 + #define EPSILON 0.001 + + uniform vec2 resolution; + uniform float time; + uniform vec2 torch_pos; + uniform float torch_radius; + uniform float light_intensity; + + float scene(vec3 p) + { + float d = length(p.xy - torch_pos) - torch_radius; + return d; + } + + float trace(vec3 origin, vec3 direction) + { + float total_dist = 0.0; + for(int i = 0; i < MAX_STEPS; i++) + { + vec3 p = origin + total_dist * direction; + float d = scene(p); + total_dist += d; + if(d < EPSILON || total_dist > MAX_DIST) + break; + } + return total_dist; + } + + vec3 calcNormal(vec3 p) + { + vec2 offset = vec2(0.001, 0.0); + float d1 = scene(p - offset.xyy); + float d2 = scene(p + offset.xyy); + float d3 = scene(p - offset.yxy); + float d4 = scene(p + offset.yxy); + float d5 = scene(p - offset.yyx); + float d6 = scene(p + offset.yyx); + return normalize(vec3(d1 - d2, d3 - d4, d5 - d6)); + } + + vec3 calcLight(vec3 p, vec3 n, vec3 dir) + { + vec3 l = normalize(vec3(0.0, 0.0, 1.0)); + float intensity = max(dot(n, l), 0.0); + vec3 color = vec3(1.0, 0.5, 0.2) * intensity * light_intensity; + + // Calculate the shadow factor + vec3 shadow_dir = normalize(l + dir); + float dist_to_light = trace(p + n * EPSILON, shadow_dir); + float shadow = dist_to_light > length(l) ? 1.0 : 0.5; + + return color * shadow; + } + + void mainImage(out vec4 fragColor, in vec2 fragCoord) + { + vec2 uv = (fragCoord.xy / resolution.xy) + * 2.0 - 1.0; + uv.x *= resolution.x / resolution.y; + + // Camera position and direction + vec3 ro = vec3(0.0, 0.0, -3.0); + vec3 rd = normalize(vec3(uv, 1.0)); + + // Trace the ray and get the distance to the closest object + float dist = trace(ro, rd); + + // Calculate the intersection point + vec3 p = ro + rd * dist; + + // Calculate the surface normal at the intersection point + vec3 n = calcNormal(p); + + // Calculate the light color and shadow factor + vec3 light_color = calcLight(p, n, rd); + + // Combine the light color and background color + vec3 bg_color = vec3(0.1, 0.1, 0.2); + vec3 color = mix(bg_color, light_color, light_intensity); + + // Output the final color + fragColor = vec4(color, 1.0); + } + """ + ) + + +def on_update(self, delta_time): + # Update the time uniform + self.set_uniform("time", self.time) + + +def set_torch_pos(self, x, y): + # Set the torch position uniform + self.set_uniform("torch_pos", (x, y)) + + +def set_torch_radius(self, radius): + # Set the torch radius uniform + self.set_uniform("torch_radius", radius) + + +def set_light_intensity(self, intensity): + # Set the light intensity uniform + self.set_uniform("light_intensity", intensity) From 2de7c768eb12c5df72abb379e668ddb2e1dba6fd Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Fri, 31 Mar 2023 08:19:23 -0400 Subject: [PATCH 03/13] load torch in game_view.py --- .pylintrc | 2 +- game/views/game_view.py | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.pylintrc b/.pylintrc index 3d34cf3..d06e0cd 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,3 +1,3 @@ [MESSAGES CONTROL] # temporary, until documentation is complete -disable=C0114, C0115, C0116, C0103, W0511, R0902, E1121, W0613, E1101, E1120 +disable=C0114, C0115, C0116, C0103, W0511, R0902, E1121, W0613, E1101, E1120, C0301, E1123, I1101, E1129 diff --git a/game/views/game_view.py b/game/views/game_view.py index 1e92297..cf80b28 100644 --- a/game/views/game_view.py +++ b/game/views/game_view.py @@ -8,6 +8,8 @@ from game.sounds import change_music from game.views import change_views +from game.torch import TorchShaderToy + arcade.enable_timings() @@ -43,6 +45,13 @@ def __init__(self, level): # select the level self.select_level(level) + # create instance of TorchShaderToy and assign to torch attribute + self.torch = TorchShaderToy( + size=(self.player.width, self.player.height), + main_source=assets.shaders.torch_main, + shadow_source=assets.shaders.torch_shadow, + ) + def select_level(self, level: int = 1): """Select the level and set up the game view""" level_map = arcade.TileMap( @@ -160,18 +169,32 @@ def on_draw(self): self.clear() self.scene_camera.use() - if self.floor is not None: - self.floor.draw() - self.walls.draw() - if self.objects is not None: - self.objects.draw() + + # Render the torch shader program on the player sprite + with self.torch: + # Set the resolution of the torch to match the player sprite size + self.torch.set_resolution(self.player.width, self.player.height) + + # Set the position of the torch to the player's position + self.torch.set_torch_pos( + self.player.center_x / self.window.width, + self.player.center_y / self.window.height, + ) + + # Set the light intensity of the torch + self.torch.set_light_intensity(1.0) + + # Render the torch on the player sprite + self.torch.render(self.player.center_x, self.player.center_y) + + # Draw other entities and walls self.entities_list.draw() # Add GUI self.gui_camera.use() arcade.Text( f"Health: 100, Time: " - f"{':'.join(map(lambda x: f'{int(x):02d}',divmod(time.time()-self.start_time, 60)))}", + f"{':'.join(map(lambda x: f'{int(x):02d}', divmod(time.time() - self.start_time, 60)))}", self.window.width - 200, self.window.height - 25, ).draw() From 9cfb48204ac3ff2c3e31d22b589b42a02fe22112 Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Fri, 31 Mar 2023 09:15:52 -0400 Subject: [PATCH 04/13] fix location identification of glsl files --- assets/__init__.py | 2 +- assets/manager.py | 5 +++-- game/views/game_view.py | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/assets/__init__.py b/assets/__init__.py index 30215d5..7f57472 100644 --- a/assets/__init__.py +++ b/assets/__init__.py @@ -1,3 +1,3 @@ -from .manager import sprites, tilemaps, sounds, fonts, items +from .manager import sprites, tilemaps, sounds, fonts, items, shaders __all__ = ["sprites", "tilemaps", "sounds", "fonts", "items", "shaders"] diff --git a/assets/manager.py b/assets/manager.py index 409c501..ea1e62a 100644 --- a/assets/manager.py +++ b/assets/manager.py @@ -21,6 +21,7 @@ def get_resolved_path(self, file): tilemaps = AssetManager("tilemaps") fonts = AssetManager("fonts") items = AssetManager("items") +shaders = AssetManager("shaders") class AudioManager: @@ -35,9 +36,9 @@ def __init__(self, sound_folder): self.heart = arcade.Sound(self.path.resolve("cinematic_heartbeat.wav")) self.horror = arcade.Sound(self.path.resolve("horror_ambience.wav")) self.whoosh = arcade.Sound(self.path.resolve("woosh_hit.wav")) - self.glacier = arcade.Sound(self.path.resolve("glacier.mp3")) + # self.glacier = arcade.Sound(self.path.resolve("glacier.mp3")) self.japan = arcade.Sound(self.path.resolve("japan.mp3")) - self.insomnia = arcade.Sound(self.path.resolve("insomnia.mp3")) + # self.insomnia = arcade.Sound(self.path.resolve("insomnia.mp3")) sounds = AudioManager("sounds") diff --git a/game/views/game_view.py b/game/views/game_view.py index cf80b28..762b42c 100644 --- a/game/views/game_view.py +++ b/game/views/game_view.py @@ -48,8 +48,8 @@ def __init__(self, level): # create instance of TorchShaderToy and assign to torch attribute self.torch = TorchShaderToy( size=(self.player.width, self.player.height), - main_source=assets.shaders.torch_main, - shadow_source=assets.shaders.torch_shadow, + main_source=assets.shaders.resolve("torch_main.glsl"), + shadow_source=assets.shaders.resolve("torch_shadow.glsl"), ) def select_level(self, level: int = 1): From 61c41befab4590ddf883e7874e2c484489d85835 Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Fri, 31 Mar 2023 09:34:51 -0400 Subject: [PATCH 05/13] pass in size and main to torch.py --- game/__main__.py | 4 ++-- game/torch.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/game/__main__.py b/game/__main__.py index 53a0bba..b023fa5 100644 --- a/game/__main__.py +++ b/game/__main__.py @@ -24,7 +24,7 @@ def __init__(self, *args, **kwargs): self.level = None def setup(self): - self.bgm = arcade.play_sound(assets.sounds.glacier, looping=True) + # self.bgm = arcade.play_sound(assets.sounds.glacier, looping=True) self.views = { "StartView": { # This is the first view, the entrypoint @@ -51,7 +51,7 @@ def setup(self): "MenuView": { # This shows the menus "color": arcade.color.BLACK, - "bgm": assets.sounds.glacier, + # "bgm": assets.sounds.glacier, "text": [ arcade.Text( "Escape!!", diff --git a/game/torch.py b/game/torch.py index ce70fb1..3943cf1 100644 --- a/game/torch.py +++ b/game/torch.py @@ -4,8 +4,8 @@ class TorchShaderToy(Shadertoy): """ShaderToy program to create a torch with shadow effect.""" - def __init__(self): - super().__init__() + def __init__(self, size, main_source, shadow_source): + super().__init__(size, main_source) # Define the uniforms self.set_uniform("time", 0.0) From eab96c3bb7126615292c1fbbfec877d7e8b0db73 Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Sat, 1 Apr 2023 09:01:15 -0400 Subject: [PATCH 06/13] fix game loading --- assets/shaders/torch_main.glsl | 71 +++++++------ assets/shaders/torch_shadow.glsl | 18 ---- game/torch.py | 164 +++++++++---------------------- game/views/game_view.py | 24 +---- 4 files changed, 90 insertions(+), 187 deletions(-) delete mode 100644 assets/shaders/torch_shadow.glsl diff --git a/assets/shaders/torch_main.glsl b/assets/shaders/torch_main.glsl index a815289..62583fe 100644 --- a/assets/shaders/torch_main.glsl +++ b/assets/shaders/torch_main.glsl @@ -1,31 +1,44 @@ -#version 330 core - -uniform vec2 resolution; -uniform vec2 player_pos; -uniform sampler2D tex; -uniform sampler2D shadow_tex; -uniform float ambient_intensity; -uniform float light_intensity; - -in vec2 frag_pos; - -out vec4 frag_color; - -void main() { - vec4 tex_color = texture(tex, frag_pos); - - // calculate distance between current pixel and player position - float dist = length(player_pos - frag_pos); - - // calculate light intensity based on distance and ambient intensity - float light = ambient_intensity + (light_intensity - ambient_intensity) / (1.0 + pow(dist, 2.0)); - - // get shadow intensity from shadow texture - float shadow = texture(shadow_tex, frag_pos).r; - - // combine light and shadow to get final intensity - float intensity = light * shadow; +#define N 100 +#define FOV 60 + +uniform vec2 lightPosition; +uniform float lightSize; +uniform float angle; + +float terrain(vec2 samplePoint) +{ + float samplePointAlpha = texture(iChannel0, samplePoint).a; + float sampleStepped = step(0.1, samplePointAlpha); + float returnValue = 1.0 - sampleStepped; + return returnValue; +} - // apply intensity to texture color - frag_color = tex_color * vec4(intensity, intensity, intensity, 1.0); +void mainImage( out vec4 fragColor, in vec2 fragCoord ) +{ + float distanceToLight = length(lightPosition - fragCoord); + vec2 normalizedFragCoord = fragCoord/iResolution.xy; + vec2 normalizedLightCoord = lightPosition.xy/iResolution.xy; + float lightAmount = 1.0; + vec2 dir = normalizedFragCoord - normalizedLightCoord; + float thisAngle = atan(dir.y, dir.x); + + // TODO: Fix the angle comparison, angle is is changing somehow + if(thisAngle < radians(angle)-radians(FOV/2) || thisAngle > radians(angle)+radians(FOV/2)) + { + // amount of light where it is shadow (0.0 - 1.0) + lightAmount = 0.0; + } + else + { + for(float i = 0.0; i < N; i++) + { + float t = i / N; + vec2 samplePoint = mix(normalizedFragCoord, normalizedLightCoord, t); + float shadowAmount = terrain(samplePoint); + lightAmount *= shadowAmount; + } + } + lightAmount *= 1.0 - smoothstep(0.0, lightSize, distanceToLight); + vec4 blackColor = vec4(0.0, 0.0, 0.0, 1.0); + fragColor = mix(blackColor, texture(iChannel1, normalizedFragCoord), lightAmount); } diff --git a/assets/shaders/torch_shadow.glsl b/assets/shaders/torch_shadow.glsl deleted file mode 100644 index a1a1266..0000000 --- a/assets/shaders/torch_shadow.glsl +++ /dev/null @@ -1,18 +0,0 @@ -#version 330 core - -uniform vec2 resolution; -uniform vec2 player_pos; -uniform sampler2D tex; -uniform float light_radius; -uniform float falloff; - -in vec2 frag_pos; - -out vec4 frag_color; - -void main() { - float dist = length(player_pos - frag_pos); - float alpha = 1.0 - smoothstep(light_radius, light_radius * falloff, dist); - - frag_color = vec4(0.0, 0.0, 0.0, alpha); -} diff --git a/game/torch.py b/game/torch.py index 3943cf1..fdf481b 100644 --- a/game/torch.py +++ b/game/torch.py @@ -1,126 +1,50 @@ from arcade.experimental import Shadertoy +from pyglet.math import Vec2 +from game.config import SCREEN_WIDTH, SCREEN_HEIGHT -class TorchShaderToy(Shadertoy): - """ShaderToy program to create a torch with shadow effect.""" - - def __init__(self, size, main_source, shadow_source): - super().__init__(size, main_source) - - # Define the uniforms - self.set_uniform("time", 0.0) - self.set_uniform("resolution", (0.0, 0.0)) - self.set_uniform("torch_pos", (0.5, 0.5)) - self.set_uniform("torch_radius", 0.2) - self.set_uniform("light_intensity", 1.0) - - # Set the fragment shader code - self.set_fragment_shader( - """ - #define MAX_STEPS 50 - #define MAX_DIST 200.0 - #define EPSILON 0.001 - - uniform vec2 resolution; - uniform float time; - uniform vec2 torch_pos; - uniform float torch_radius; - uniform float light_intensity; - - float scene(vec3 p) - { - float d = length(p.xy - torch_pos) - torch_radius; - return d; - } - - float trace(vec3 origin, vec3 direction) - { - float total_dist = 0.0; - for(int i = 0; i < MAX_STEPS; i++) - { - vec3 p = origin + total_dist * direction; - float d = scene(p); - total_dist += d; - if(d < EPSILON || total_dist > MAX_DIST) - break; - } - return total_dist; - } - - vec3 calcNormal(vec3 p) - { - vec2 offset = vec2(0.001, 0.0); - float d1 = scene(p - offset.xyy); - float d2 = scene(p + offset.xyy); - float d3 = scene(p - offset.yxy); - float d4 = scene(p + offset.yxy); - float d5 = scene(p - offset.yyx); - float d6 = scene(p + offset.yyx); - return normalize(vec3(d1 - d2, d3 - d4, d5 - d6)); - } - - vec3 calcLight(vec3 p, vec3 n, vec3 dir) - { - vec3 l = normalize(vec3(0.0, 0.0, 1.0)); - float intensity = max(dot(n, l), 0.0); - vec3 color = vec3(1.0, 0.5, 0.2) * intensity * light_intensity; - - // Calculate the shadow factor - vec3 shadow_dir = normalize(l + dir); - float dist_to_light = trace(p + n * EPSILON, shadow_dir); - float shadow = dist_to_light > length(l) ? 1.0 : 0.5; - - return color * shadow; - } - - void mainImage(out vec4 fragColor, in vec2 fragCoord) - { - vec2 uv = (fragCoord.xy / resolution.xy) - * 2.0 - 1.0; - uv.x *= resolution.x / resolution.y; - - // Camera position and direction - vec3 ro = vec3(0.0, 0.0, -3.0); - vec3 rd = normalize(vec3(uv, 1.0)); - - // Trace the ray and get the distance to the closest object - float dist = trace(ro, rd); - // Calculate the intersection point - vec3 p = ro + rd * dist; - - // Calculate the surface normal at the intersection point - vec3 n = calcNormal(p); - - // Calculate the light color and shadow factor - vec3 light_color = calcLight(p, n, rd); - - // Combine the light color and background color - vec3 bg_color = vec3(0.1, 0.1, 0.2); - vec3 color = mix(bg_color, light_color, light_intensity); +class Torch(Shadertoy): + """ShaderToy program to create a torch with shadow effect.""" - // Output the final color - fragColor = vec4(color, 1.0); - } - """ + def __init__(self, size, main_source): + super().__init__() + # self.shadertoy = None + # self.channel0 = None + # self.channel1 = None + self.load_shader(size, main_source) + print("Torch init") + + def load_shader(self, size, main_source): + shader_file_path = main_source + window_size = size + self.shadertoy = Shadertoy.create_from_file(window_size, shader_file_path) + self.channel0 = self.shadertoy.ctx.framebuffer( + color_attachments=[self.shadertoy.ctx.texture(window_size, components=4)] ) - - -def on_update(self, delta_time): - # Update the time uniform - self.set_uniform("time", self.time) - - -def set_torch_pos(self, x, y): - # Set the torch position uniform - self.set_uniform("torch_pos", (x, y)) - - -def set_torch_radius(self, radius): - # Set the torch radius uniform - self.set_uniform("torch_radius", radius) - - -def set_light_intensity(self, intensity): - # Set the light intensity uniform - self.set_uniform("light_intensity", intensity) + self.channel1 = self.shadertoy.ctx.framebuffer( + color_attachments=[self.shadertoy.ctx.texture(window_size, components=4)] + ) + self.shadertoy.channel_0 = self.channel0.color_attachments[0] + self.shadertoy.channel_1 = self.channel1.color_attachments[0] + print("Torch load_shader") + + def draw(self): + print("Torch draw") + # self.channel0.use() + # self.channel0.clear() + # + # self.channel1.use() + # self.channel1.clear() + # # Draw everything that can be hidden in shadows bur does not cast shadow + # # self.walls.draw() + # + # # self.use() + # # self.clear() + self.shadertoy.program["lightPosition"] = Vec2( + SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + ) + self.shadertoy.program["lightSize"] = 300 + # self.shadertoy.program["angle"] = self.mc.angle + self.shadertoy.render() + print("Torch draw end") diff --git a/game/views/game_view.py b/game/views/game_view.py index 762b42c..e727004 100644 --- a/game/views/game_view.py +++ b/game/views/game_view.py @@ -8,7 +8,7 @@ from game.sounds import change_music from game.views import change_views -from game.torch import TorchShaderToy +from game.torch import Torch arcade.enable_timings() @@ -46,10 +46,9 @@ def __init__(self, level): self.select_level(level) # create instance of TorchShaderToy and assign to torch attribute - self.torch = TorchShaderToy( + self.torch = Torch( size=(self.player.width, self.player.height), - main_source=assets.shaders.resolve("torch_main.glsl"), - shadow_source=assets.shaders.resolve("torch_shadow.glsl"), + main_source=assets.sprites.resolve("shadow.glsl"), ) def select_level(self, level: int = 1): @@ -170,22 +169,7 @@ def on_draw(self): self.scene_camera.use() - # Render the torch shader program on the player sprite - with self.torch: - # Set the resolution of the torch to match the player sprite size - self.torch.set_resolution(self.player.width, self.player.height) - - # Set the position of the torch to the player's position - self.torch.set_torch_pos( - self.player.center_x / self.window.width, - self.player.center_y / self.window.height, - ) - - # Set the light intensity of the torch - self.torch.set_light_intensity(1.0) - - # Render the torch on the player sprite - self.torch.render(self.player.center_x, self.player.center_y) + self.torch.draw() # Draw other entities and walls self.entities_list.draw() From c616b72d5ccf3e0de7faeac397bcf9ceeae2e598 Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Sat, 1 Apr 2023 09:12:20 -0400 Subject: [PATCH 07/13] pass angle into torch --- .pylintrc | 2 +- assets/shaders/torch_main.glsl | 44 ---------------------------------- game/torch.py | 15 +++++++----- game/views/game_view.py | 2 +- 4 files changed, 11 insertions(+), 52 deletions(-) delete mode 100644 assets/shaders/torch_main.glsl diff --git a/.pylintrc b/.pylintrc index d06e0cd..44f6619 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,3 +1,3 @@ [MESSAGES CONTROL] # temporary, until documentation is complete -disable=C0114, C0115, C0116, C0103, W0511, R0902, E1121, W0613, E1101, E1120, C0301, E1123, I1101, E1129 +disable=C0114, C0115, C0116, C0103, W0511, R0902, E1121, W0613, E1101, E1120, C0301, E1123, I1101, E1129, W0231 diff --git a/assets/shaders/torch_main.glsl b/assets/shaders/torch_main.glsl deleted file mode 100644 index 62583fe..0000000 --- a/assets/shaders/torch_main.glsl +++ /dev/null @@ -1,44 +0,0 @@ -#define N 100 -#define FOV 60 - -uniform vec2 lightPosition; -uniform float lightSize; -uniform float angle; - -float terrain(vec2 samplePoint) -{ - float samplePointAlpha = texture(iChannel0, samplePoint).a; - float sampleStepped = step(0.1, samplePointAlpha); - float returnValue = 1.0 - sampleStepped; - return returnValue; -} - -void mainImage( out vec4 fragColor, in vec2 fragCoord ) -{ - float distanceToLight = length(lightPosition - fragCoord); - vec2 normalizedFragCoord = fragCoord/iResolution.xy; - vec2 normalizedLightCoord = lightPosition.xy/iResolution.xy; - float lightAmount = 1.0; - vec2 dir = normalizedFragCoord - normalizedLightCoord; - float thisAngle = atan(dir.y, dir.x); - - // TODO: Fix the angle comparison, angle is is changing somehow - if(thisAngle < radians(angle)-radians(FOV/2) || thisAngle > radians(angle)+radians(FOV/2)) - { - // amount of light where it is shadow (0.0 - 1.0) - lightAmount = 0.0; - } - else - { - for(float i = 0.0; i < N; i++) - { - float t = i / N; - vec2 samplePoint = mix(normalizedFragCoord, normalizedLightCoord, t); - float shadowAmount = terrain(samplePoint); - lightAmount *= shadowAmount; - } - } - lightAmount *= 1.0 - smoothstep(0.0, lightSize, distanceToLight); - vec4 blackColor = vec4(0.0, 0.0, 0.0, 1.0); - fragColor = mix(blackColor, texture(iChannel1, normalizedFragCoord), lightAmount); -} diff --git a/game/torch.py b/game/torch.py index fdf481b..08109bb 100644 --- a/game/torch.py +++ b/game/torch.py @@ -8,8 +8,11 @@ class Torch(Shadertoy): """ShaderToy program to create a torch with shadow effect.""" def __init__(self, size, main_source): - super().__init__() - # self.shadertoy = None + # super().__init__(size, main_source) + print("Torch init") + print(size) + print(main_source) + self.shadertoy = None # self.channel0 = None # self.channel1 = None self.load_shader(size, main_source) @@ -29,8 +32,9 @@ def load_shader(self, size, main_source): self.shadertoy.channel_1 = self.channel1.color_attachments[0] print("Torch load_shader") - def draw(self): - print("Torch draw") + def draw(self, angle): + print(angle) + # self.channel0.use() # self.channel0.clear() # @@ -45,6 +49,5 @@ def draw(self): SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ) self.shadertoy.program["lightSize"] = 300 - # self.shadertoy.program["angle"] = self.mc.angle + self.shadertoy.program["angle"] = angle self.shadertoy.render() - print("Torch draw end") diff --git a/game/views/game_view.py b/game/views/game_view.py index e727004..5bede92 100644 --- a/game/views/game_view.py +++ b/game/views/game_view.py @@ -169,7 +169,7 @@ def on_draw(self): self.scene_camera.use() - self.torch.draw() + self.torch.draw(self.player.angle) # Draw other entities and walls self.entities_list.draw() From 80495c7d697fc19dd347b9b40801a9033302580d Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Sat, 1 Apr 2023 09:14:00 -0400 Subject: [PATCH 08/13] re-add deleted code in game_view --- game/views/game_view.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/game/views/game_view.py b/game/views/game_view.py index 5bede92..d2365d2 100644 --- a/game/views/game_view.py +++ b/game/views/game_view.py @@ -169,6 +169,12 @@ def on_draw(self): self.scene_camera.use() + if self.floor is not None: + self.floor.draw() + self.walls.draw() + if self.objects is not None: + self.objects.draw() + self.torch.draw(self.player.angle) # Draw other entities and walls From 8cc7497c4b06aed455ced3ea94abfd3bc35f0bce Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Sat, 1 Apr 2023 09:18:47 -0400 Subject: [PATCH 09/13] remove shaders from assets --- assets/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/__init__.py b/assets/__init__.py index 7f57472..2f6f234 100644 --- a/assets/__init__.py +++ b/assets/__init__.py @@ -1,3 +1,3 @@ -from .manager import sprites, tilemaps, sounds, fonts, items, shaders +from .manager import sprites, tilemaps, sounds, fonts, items -__all__ = ["sprites", "tilemaps", "sounds", "fonts", "items", "shaders"] +__all__ = ["sprites", "tilemaps", "sounds", "fonts", "items"] From 12a7d79d4960fa3612a5ba4fdf07e7f9818dcef4 Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Sat, 1 Apr 2023 09:19:11 -0400 Subject: [PATCH 10/13] remove shaders from assets --- assets/manager.py | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/manager.py b/assets/manager.py index ea1e62a..81d29ac 100644 --- a/assets/manager.py +++ b/assets/manager.py @@ -21,7 +21,6 @@ def get_resolved_path(self, file): tilemaps = AssetManager("tilemaps") fonts = AssetManager("fonts") items = AssetManager("items") -shaders = AssetManager("shaders") class AudioManager: From 60421c8bf5febc56a6966e1118fc18ad260c7563 Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Sat, 1 Apr 2023 09:20:41 -0400 Subject: [PATCH 11/13] undo commenting out sounds They were not working on mac, as they need to me streamed. --- assets/manager.py | 4 ++-- game/__main__.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/assets/manager.py b/assets/manager.py index 81d29ac..409c501 100644 --- a/assets/manager.py +++ b/assets/manager.py @@ -35,9 +35,9 @@ def __init__(self, sound_folder): self.heart = arcade.Sound(self.path.resolve("cinematic_heartbeat.wav")) self.horror = arcade.Sound(self.path.resolve("horror_ambience.wav")) self.whoosh = arcade.Sound(self.path.resolve("woosh_hit.wav")) - # self.glacier = arcade.Sound(self.path.resolve("glacier.mp3")) + self.glacier = arcade.Sound(self.path.resolve("glacier.mp3")) self.japan = arcade.Sound(self.path.resolve("japan.mp3")) - # self.insomnia = arcade.Sound(self.path.resolve("insomnia.mp3")) + self.insomnia = arcade.Sound(self.path.resolve("insomnia.mp3")) sounds = AudioManager("sounds") diff --git a/game/__main__.py b/game/__main__.py index 41993e4..0e73f56 100644 --- a/game/__main__.py +++ b/game/__main__.py @@ -61,7 +61,7 @@ def setup(self): "MenuView": { # This shows the menus "color": arcade.color.BLACK, - # "bgm": assets.sounds.glacier, + "bgm": assets.sounds.glacier, "text": [ arcade.Text( "Escape!!", From ff4d92b02125d740df23e1b6c77b1f6bfcc5e190 Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Sat, 1 Apr 2023 09:21:08 -0400 Subject: [PATCH 12/13] fix pylint error --- .pylintrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pylintrc b/.pylintrc index 2384360..5d824b8 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,3 +1,3 @@ [MESSAGES CONTROL] # temporary, until documentation is complete -disable=C0114, C0115, C0116, C0103, W0511, R0902, E1121, W0613, R0801, E1101, C0301 +disable=C0114, C0115, C0116, C0103, W0511, R0902, E1121, W0613, R0801, E1101, C0301, W0231 From 7cc79b4d25bd6d456f85b30334aeae045d56162c Mon Sep 17 00:00:00 2001 From: Vagish Vela Date: Sat, 1 Apr 2023 10:01:58 -0400 Subject: [PATCH 13/13] disable channels for torch --- game/torch.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/game/torch.py b/game/torch.py index 08109bb..61b06b4 100644 --- a/game/torch.py +++ b/game/torch.py @@ -22,14 +22,14 @@ def load_shader(self, size, main_source): shader_file_path = main_source window_size = size self.shadertoy = Shadertoy.create_from_file(window_size, shader_file_path) - 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)] - ) - self.shadertoy.channel_0 = self.channel0.color_attachments[0] - self.shadertoy.channel_1 = self.channel1.color_attachments[0] + # 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)] + # ) + # self.shadertoy.channel_0 = self.channel0.color_attachments[0] + # self.shadertoy.channel_1 = self.channel1.color_attachments[0] print("Torch load_shader") def draw(self, angle):