diff --git a/game/chladni_plate_simulator/game.py b/game/chladni_plate_simulator/game.py index 374ce14..6932884 100644 --- a/game/chladni_plate_simulator/game.py +++ b/game/chladni_plate_simulator/game.py @@ -56,35 +56,28 @@ class Game(arcade.gui.UIView): with self.shader_program: self.shader_program["source_count"] = len(self.sources) self.shader_program["k"] = self.settings["chladni_plate_simulator"]["k"] - self.shader_program.dispatch(self.plate_image.width, self.plate_image.height, 1) + self.shader_program.dispatch(int(self.plate_image.width / 32), int(self.plate_image.height / 32), 1) def setup(self): self.shader_program, self.plate_image, self.sources_ssbo = create_shader(int(self.window.width * 0.8), self.window.height) self.image_sprite = pyglet.sprite.Sprite(img=self.plate_image) - - scale_x = (self.window.width * 0.8) / self.image_sprite.width - scale_y = self.window.height / self.image_sprite.height - - self.image_sprite.scale_x = scale_x - self.image_sprite.scale_y = scale_y - - def add_source(self): - tx = self.window.width * 0.4 - ty = self.window.height / 2 - self.sources = np.vstack([self.sources, [tx, ty]]).astype(np.float32) + def add_source(self): + self.sources = np.vstack([self.sources, [self.window.width * 0.4, self.window.height / 2]]).astype(np.float32) self.needs_redraw = True - def on_mouse_drag(self, x, y, dx, dy, _buttons, _modifiers): + def on_mouse_press(self, x, y, button, modifiers): if not self.dragged_source: for i, source in enumerate(self.sources.reshape(-1, 2)): if arcade.math.Vec2(x, y).distance(arcade.math.Vec2(*source)) <= 10: self.dragged_source = i break - self.sources[self.dragged_source] = [x, y] + def on_mouse_drag(self, x, y, dx, dy, _buttons, _modifiers): + if self.dragged_source is not None: + self.sources[self.dragged_source] = [x, y] def on_mouse_release(self, x, y, button, modifiers): self.dragged_source = None diff --git a/game/chladni_plate_simulator/shader.py b/game/chladni_plate_simulator/shader.py index e10a2ee..a22d997 100644 --- a/game/chladni_plate_simulator/shader.py +++ b/game/chladni_plate_simulator/shader.py @@ -11,7 +11,7 @@ layout (std430, binding = 3) buffer Sources { uniform int source_count; uniform float k; -layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in; +layout (local_size_x = 32, local_size_y = 32, local_size_z = 1) in; layout (location = 0, rgba32f) uniform image2D img_output; void main() { diff --git a/game/lissajous_simulator/game.py b/game/lissajous_simulator/game.py new file mode 100644 index 0000000..530bd8d --- /dev/null +++ b/game/lissajous_simulator/game.py @@ -0,0 +1,107 @@ +import pyglet, arcade, arcade.gui, os, json + +from game.lissajous_simulator.shader import create_shader + +class Game(arcade.gui.UIView): + def __init__(self, pypresence_client): + super().__init__() + + self.pypresence_client = pypresence_client + self.pypresence_client.update(state="Playing a simulator", details="Lissajous Simulator") + + self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) + + self.settings_box = self.anchor.add(arcade.gui.UIBoxLayout(align="center", size_hint=(0.2, 1)).with_background(color=arcade.color.GRAY), anchor_x="right", anchor_y="bottom") + self.settings_label = self.settings_box.add(arcade.gui.UILabel(text="Settings", font_size=24)) + + if os.path.exists("data.json"): + with open("data.json", "r") as file: + self.settings = json.load(file) + else: + self.settings = {} + + if not "lissajous_simulator" in self.settings: + self.settings["lissajous_simulator"] = { + "amplitude_x": 0.8, + "amplitude_y": 0.8, + "frequency_x": 3.0, + "frequency_y": 2.0, + "thickness": 0.005, + "samples": 50, + "phase_shift": 6.283 + } + + self.time = 0 + + def on_show_view(self): + super().on_show_view() + + self.settings_box.add(arcade.gui.UISpace(height=self.window.height / 75)) + + self.add_setting("X Amplitude: {value}", 0, 1.5, 0.1, "amplitude_x") + self.add_setting("Y Amplitude: {value}", 0, 1.5, 0.1, "amplitude_y") + + self.add_setting("X Frequency: {value}", 1, 10, 1, "frequency_x") + self.add_setting("Y Frequency: {value}", 1, 10, 1, "frequency_y") + + self.add_setting("Phase Shift: {value}", 0, 6.283, 0.001, "phase_shift") + + self.add_setting("Thickness: {value}", 0.001, 0.05, 0.001, "thickness") + self.add_setting("Samples: {value}", 1, 1000, 25, "samples") + + self.setup() + + def add_setting(self, text, min_value, max_value, step, settings_key): + label = self.settings_box.add(arcade.gui.UILabel(text.format(value=self.settings["lissajous_simulator"][settings_key]))) + slider = self.settings_box.add(arcade.gui.UISlider(value=self.settings["lissajous_simulator"][settings_key], min_value=min_value, max_value=max_value, step=step)) + slider._render_steps = lambda surface: None + + slider.on_change = lambda event, label=label: self.change_value(label, text, settings_key, event.new_value) + + def change_value(self, label, text, settings_key, value): + label.text = text.format(value=value) + + self.settings["lissajous_simulator"][settings_key] = value + + def setup(self): + self.shader_program, self.lissajous_image = create_shader(int(self.window.width * 0.8), self.window.height) + + self.image_sprite = pyglet.sprite.Sprite(img=self.lissajous_image) + + def on_update(self, delta_time): + self.time += delta_time + + current_settings = self.settings["lissajous_simulator"] + + with self.shader_program: + self.shader_program["x_amp"] = current_settings["amplitude_x"] + self.shader_program["y_amp"] = current_settings["amplitude_y"] + + self.shader_program["x_freq"] = current_settings["frequency_x"] + self.shader_program["y_freq"] = current_settings["frequency_y"] + + self.shader_program["thickness"] = current_settings["thickness"] + self.shader_program["samples"] = int(current_settings["samples"]) + self.shader_program["phase_shift"] = current_settings["phase_shift"] + + self.shader_program["resolution"] = (int(self.window.width * 0.8), self.window.height) + + self.shader_program["time"] = self.time + + self.shader_program.dispatch(int(self.lissajous_image.width / 32), int(self.lissajous_image.height / 32), 1) + + def on_key_press(self, symbol, modifiers): + if symbol == arcade.key.ESCAPE: + self.shader_program.delete() + + with open("data.json", "w") as file: + file.write(json.dumps(self.settings, indent=4)) + + from menus.main import Main + + self.window.show_view(Main(self.pypresence_client)) + + def on_draw(self): + super().on_draw() + + self.image_sprite.draw() \ No newline at end of file diff --git a/game/lissajous_simulator/shader.py b/game/lissajous_simulator/shader.py new file mode 100644 index 0000000..4100a3f --- /dev/null +++ b/game/lissajous_simulator/shader.py @@ -0,0 +1,63 @@ +import pyglet, pyglet.graphics + +from pyglet.gl import GL_NEAREST + +shader_source = """#version 430 core + +uniform float x_amp; +uniform float y_amp; +uniform float x_freq; +uniform float y_freq; +uniform float phase_shift; + +uniform float thickness; +uniform int samples; +uniform float time; +uniform vec2 resolution; + +layout (local_size_x = 32, local_size_y = 32, local_size_z = 1) in; +layout (location = 0, rgba32f) uniform image2D img_output; + +vec2 lissajous(float t) { + return vec2( + x_amp * sin(x_freq * t + phase_shift + time), + y_amp * sin(y_freq * t) + ); +} + +float curve_distribution(vec2 uv) { + float d = 1e5; + + for (int i = 0; i < samples; i++) { + float t = float(i) / float(samples) * 6.28318; + vec2 p = lissajous(t); + d = min(d, length(uv - p)); + } + + return d; +} + +void main() { + ivec2 texel_coord = ivec2(gl_GlobalInvocationID.xy); + + vec2 uv = (vec2(texel_coord) / resolution) * 2.0 - 1.0; + uv.x *= float(resolution.x) / float(resolution.y); + + float d = curve_distribution(uv); + + float line = smoothstep(thickness, thickness * 0.3, d); + + vec4 color = vec4(vec3(line), 1.0); + imageStore(img_output, texel_coord, color); +} +""" + +def create_shader(width, height): + shader_program = pyglet.graphics.shader.ComputeShaderProgram(shader_source) + + lissajous_image = pyglet.image.Texture.create(width, height, internalformat=pyglet.gl.GL_RGBA32F, min_filter=GL_NEAREST, mag_filter=GL_NEAREST) + + uniform_location = shader_program["img_output"] + lissajous_image.bind_image_texture(unit=uniform_location) + + return shader_program, lissajous_image \ No newline at end of file diff --git a/game/voronoi_diagram_simulator/game.py b/game/voronoi_diagram_simulator/game.py new file mode 100644 index 0000000..a020718 --- /dev/null +++ b/game/voronoi_diagram_simulator/game.py @@ -0,0 +1,126 @@ +import pyglet, arcade, arcade.gui, os, json, numpy as np + +from game.voronoi_diagram_simulator.shader import create_shader + +from utils.constants import button_style +from utils.preload import button_texture, button_hovered_texture + +class Game(arcade.gui.UIView): + def __init__(self, pypresence_client): + super().__init__() + + self.pypresence_client = pypresence_client + self.pypresence_client.update(state="Playing a simulator", details="Voronoi Diagram Simulator") + + self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) + + self.settings_box = self.anchor.add(arcade.gui.UIBoxLayout(align="center", size_hint=(0.2, 1)).with_background(color=arcade.color.GRAY), anchor_x="right", anchor_y="bottom") + self.settings_label = self.settings_box.add(arcade.gui.UILabel(text="Settings", font_size=24)) + + if os.path.exists("data.json"): + with open("data.json", "r") as file: + self.settings = json.load(file) + + else: + self.settings = {} + + if not "voronoi_diagram_simulator" in self.settings: + self.settings["voronoi_diagram_simulator"] = { + "edge_thickness": 0.01, + "edge_smoothness": 0.005 + } + + self.points = np.empty((0, 2), dtype=np.float32) + + self.dragged_point = None + self.needs_redraw = True + + def setup(self): + self.shader_program, self.voronoi_image, self.points_sbbo = create_shader(int(self.window.width * 0.8), self.window.height) + + self.image_sprite = pyglet.sprite.Sprite(img=self.voronoi_image) + + def on_show_view(self): + super().on_show_view() + + self.settings_box.add(arcade.gui.UISpace(height=self.window.height / 75)) + + self.add_setting("Edge Thickness: {value}", 0, 0.1, 0.01, "edge_thickness") + self.add_setting("Edge Smoothness: {value}", 0, 0.1, 0.01, "edge_smoothness") + + self.add_point_button = self.settings_box.add(arcade.gui.UITextureButton(text="Add point", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width * 0.2)) + self.add_point_button.on_click = lambda event: self.add_point() + + self.setup() + + def add_point(self): + self.points = np.vstack([self.points, [self.window.width * 0.4, self.window.height / 2]]).astype(np.float32) + + self.needs_redraw = True + + def add_setting(self, text, min_value, max_value, step, settings_key): + label = self.settings_box.add(arcade.gui.UILabel(text.format(value=self.settings["voronoi_diagram_simulator"][settings_key]))) + slider = self.settings_box.add(arcade.gui.UISlider(value=self.settings["voronoi_diagram_simulator"][settings_key], min_value=min_value, max_value=max_value, step=step)) + slider._render_steps = lambda surface: None + + slider.on_change = lambda event, label=label: self.change_value(label, text, settings_key, event.new_value) + + def change_value(self, label, text, settings_key, value): + label.text = text.format(value=value) + + self.settings["voronoi_diagram_simulator"][settings_key] = value + + self.needs_redraw = True + + def on_update(self, delta_time): + if self.needs_redraw: + self.needs_redraw = False + + self.points_sbbo.set_data(self.points.tobytes()) + + with self.shader_program: + self.shader_program["point_count"] = len(self.points) + self.shader_program["resolution"] = (int(self.window.width * 0.8), self.window.height) + self.shader_program["edge_smoothness"] = self.settings["voronoi_diagram_simulator"]["edge_smoothness"] + self.shader_program["edge_thickness"] = self.settings["voronoi_diagram_simulator"]["edge_thickness"] + + self.shader_program.dispatch(int(int(self.window.width * 0.8) / 32), int(self.window.height / 32)) + + def on_mouse_press(self, x, y, button, modifiers): + if not self.dragged_point: + for i, point in enumerate(self.points.reshape(-1, 2)): + if arcade.math.Vec2(x, y).distance(arcade.math.Vec2(*point)) <= 10: + self.dragged_point = i + break + + def on_mouse_drag(self, x, y, dx, dy, _buttons, _modifiers): + if self.dragged_point is not None: + self.points[self.dragged_point] = [x, y] + + def on_mouse_release(self, x, y, button, modifiers): + self.dragged_point = None + self.needs_redraw = True + + def on_key_press(self, symbol, modifiers): + if symbol == arcade.key.ESCAPE: + self.shader_program.delete() + + with open("data.json", "w") as file: + file.write(json.dumps(self.settings, indent=4)) + + from menus.main import Main + self.window.show_view(Main(self.pypresence_client)) + + elif symbol == arcade.key.C: + del self.points + self.points = np.empty((0, 2), dtype=np.float32) + + self.needs_redraw = True + + def on_draw(self): + super().on_draw() + + self.image_sprite.draw() + + for point in self.points.reshape(-1, 2): + arcade.draw_circle_filled(point[0], point[1], 10, arcade.color.GRAY) \ No newline at end of file diff --git a/game/voronoi_diagram_simulator/shader.py b/game/voronoi_diagram_simulator/shader.py new file mode 100644 index 0000000..1ce0467 --- /dev/null +++ b/game/voronoi_diagram_simulator/shader.py @@ -0,0 +1,67 @@ +import pyglet, pyglet.graphics + +from pyglet.gl import GL_NEAREST, glBindBufferBase, GL_SHADER_STORAGE_BUFFER + +shader_source = """#version 430 core + +layout (std430, binding = 3) buffer Points { + vec2 points[]; +}; + +uniform int point_count; +uniform vec2 resolution; +uniform float edge_thickness; +uniform float edge_smoothness; + +layout (local_size_x = 32, local_size_y = 32, local_size_z = 1) in; +layout (location = 0, rgba32f) uniform image2D img_output; + +vec3 hash3(float p) { + vec3 p3 = vec3(p * 127.1, p * 311.7, p * 74.7); + return fract(sin(p3) * 43758.5453123); +} + +void main() { + ivec2 texel_coord = ivec2(gl_GlobalInvocationID.xy); + vec2 uv = vec2(texel_coord) / resolution; + + float min_dist = 1e5; + float second_min = 1e5; + int closest_id = 0; + + for (int i = 0; i < point_count; i++) { + vec2 point = points[i] / resolution; + float dist = length(uv - point); + + if (dist < min_dist) { + second_min = min_dist; + min_dist = dist; + closest_id = i; + } + else if (dist < second_min) { + second_min = dist; + } + } + + float edge_dist = second_min - min_dist; + float edge = 1.0 - smoothstep(edge_thickness - edge_smoothness, edge_thickness + edge_smoothness, edge_dist); + + vec3 cell_color = hash3(float(closest_id)); + vec3 color = mix(cell_color, vec3(0.0), edge); + + imageStore(img_output, texel_coord, vec4(color, 1.0)); +} +""" + +def create_shader(width, height): + shader_program = pyglet.graphics.shader.ComputeShaderProgram(shader_source) + + voronoi_diagram_image = pyglet.image.Texture.create(width, height, internalformat=pyglet.gl.GL_RGBA32F, min_filter=GL_NEAREST, mag_filter=GL_NEAREST) + + uniform_location = shader_program["img_output"] + voronoi_diagram_image.bind_image_texture(unit=uniform_location) + + points_ssbo = pyglet.graphics.BufferObject(128 * 8) + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, points_ssbo.id) + + return shader_program, voronoi_diagram_image, points_ssbo \ No newline at end of file diff --git a/menus/main.py b/menus/main.py index d9c6054..6c06402 100644 --- a/menus/main.py +++ b/menus/main.py @@ -52,22 +52,28 @@ class Main(arcade.gui.UIView): self.title_label = self.box.add(arcade.gui.UILabel(text="Simulator Games", font_name="Roboto", font_size=48)) - self.boid_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Boid Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.boid_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Boid Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.boid_simulator_button.on_click = lambda event: self.boid_simulator() - self.water_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Water Splash Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.water_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Water Splash Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.water_simulator_button.on_click = lambda event: self.water_simulator() - self.physics_playground_button = self.box.add(arcade.gui.UITextureButton(text="Physics Playground", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.physics_playground_button = self.box.add(arcade.gui.UITextureButton(text="Physics Playground", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.physics_playground_button.on_click = lambda event: self.physics_playground() - self.fourier_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Fourier Drawing Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.fourier_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Fourier Drawing Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.fourier_simulator_button.on_click = lambda event: self.fourier_simulator() - self.chadni_plate_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Chadni Plate Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.chadni_plate_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Chadni Plate Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.chadni_plate_simulator_button.on_click = lambda event: self.chladni_plate_simulator() - self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 10, style=big_button_style)) + self.lissajous_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Lissajous Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) + self.lissajous_simulator_button.on_click = lambda event: self.lissajous_simulator() + + self.voronoi_diagram_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Voronoi Diagram Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) + self.voronoi_diagram_simulator_button.on_click = lambda event: self.voronoi_diagram_simulator() + + self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 12, style=big_button_style)) self.settings_button.on_click = lambda event: self.settings() def physics_playground(self): @@ -90,6 +96,14 @@ class Main(arcade.gui.UIView): from game.chladni_plate_simulator.game import Game self.window.show_view(Game(self.pypresence_client)) + def lissajous_simulator(self): + from game.lissajous_simulator.game import Game + self.window.show_view(Game(self.pypresence_client)) + + def voronoi_diagram_simulator(self): + from game.voronoi_diagram_simulator.game import Game + self.window.show_view(Game(self.pypresence_client)) + def settings(self): from menus.settings import Settings self.window.show_view(Settings(self.pypresence_client))