Skip to content

Commit 1898745

Browse files
new effect :D
1 parent 8a51268 commit 1898745

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

src/3d perlin mesh.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import moderngl
2+
from moderngl_window import WindowConfig, run_window_config
3+
from moderngl_window.geometry import sphere
4+
from pyrr import Matrix44, Vector3
5+
import numpy as np
6+
import pmma
7+
8+
pmma.init()
9+
10+
noise = pmma.Perlin()
11+
color = pmma.ColorConverter()
12+
13+
class SphereGrid(WindowConfig):
14+
title = "3D Sphere Grid"
15+
window_size = (1280, 720)
16+
aspect_ratio = 16 / 9
17+
resource_dir = "."
18+
19+
# Grid size
20+
GRID_SIZE = 20
21+
SPHERE_SCALE = 0.3 # Adjust to scale the spheres
22+
GRID_SPACING = 3 # Space between spheres
23+
24+
def __init__(self, **kwargs):
25+
super().__init__(**kwargs)
26+
self.prog = self.ctx.program(
27+
vertex_shader="""
28+
#version 330
29+
uniform mat4 model;
30+
uniform mat4 view;
31+
uniform mat4 projection;
32+
in vec3 in_position;
33+
in vec3 in_normal;
34+
out vec3 normal;
35+
36+
void main() {
37+
gl_Position = projection * view * model * vec4(in_position, 1.0);
38+
normal = in_normal;
39+
}
40+
""",
41+
fragment_shader="""
42+
#version 330
43+
in vec3 normal;
44+
out vec4 fragColor;
45+
uniform vec3 top_shape_color;
46+
uniform vec3 bottom_shape_color;
47+
uniform float y_value;
48+
49+
float max_value = 10;
50+
51+
void main() {
52+
fragColor = vec4(mix(top_shape_color, bottom_shape_color, y_value/max_value), 1.0);
53+
}
54+
"""
55+
)
56+
57+
# Create a sphere geometry object
58+
self.sphere = sphere(radius=1.0, sectors=32, rings=32)
59+
self.camera_distance = 20.0
60+
self.camera_angle = 0.0
61+
62+
# Store grid positions
63+
self.grid_positions = [
64+
[
65+
Vector3([
66+
(i - self.GRID_SIZE / 2) * self.GRID_SPACING,
67+
0.0,
68+
(j - self.GRID_SIZE / 2) * self.GRID_SPACING
69+
])
70+
for j in range(self.GRID_SIZE)
71+
]
72+
for i in range(self.GRID_SIZE)
73+
]
74+
75+
def render(self, time, frametime):
76+
self.ctx.clear(0, 0, 0)
77+
self.ctx.enable(moderngl.DEPTH_TEST)
78+
79+
# Create a rotating camera
80+
self.camera_angle += frametime
81+
cam_x = self.camera_distance * np.sin(self.camera_angle)
82+
cam_z = self.camera_distance * np.cos(self.camera_angle)
83+
camera_position = Vector3([cam_x, self.camera_distance * 0.5, cam_z])
84+
look_at = Vector3([0.0, 0.0, 0.0])
85+
up = Vector3([0.0, 1.0, 0.0])
86+
87+
view = Matrix44.look_at(camera_position, look_at, up)
88+
projection = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 100.0)
89+
90+
self.prog["view"].write(view.astype("f4"))
91+
self.prog["projection"].write(projection.astype("f4"))
92+
93+
self.prog["top_shape_color"] = color.generate_color_from_perlin_noise(value=pmma.get_application_run_time()/5, format=pmma.Constants.SMALL_RGB) # Set the color of the spheres
94+
self.prog["bottom_shape_color"] = color.generate_color_from_perlin_noise(value=-pmma.get_application_run_time()/5, format=pmma.Constants.SMALL_RGB) # Set the color of the spheres
95+
96+
# Draw the grid of spheres
97+
for i in range(self.GRID_SIZE):
98+
for j in range(self.GRID_SIZE):
99+
x, y, z = self.grid_positions[i][j]
100+
# Example: make spheres bounce in a sine wave
101+
self.grid_positions[i][j] = Vector3([
102+
x,
103+
noise.generate_2D_perlin_noise(x=(x / 25)+pmma.get_application_run_time(), y=(z / 25)+pmma.get_application_run_time(), new_range=[0, 10]),
104+
z])
105+
106+
self.prog["y_value"] = self.grid_positions[i][j].y
107+
108+
position = self.grid_positions[i][j]
109+
model = Matrix44.from_translation(position) @ Matrix44.from_scale(
110+
[self.SPHERE_SCALE] * 3
111+
)
112+
self.prog["model"].write(model.astype("f4"))
113+
self.sphere.render(self.prog)
114+
115+
116+
if __name__ == "__main__":
117+
run_window_config(SphereGrid)

src/Explosion (Divided by 10).py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
pmma.init()
77

88
display = pmma.Display()
9-
display.create(1920, 1080, fullscreen=True)
9+
display.create()
1010

11-
events = pmma.Events(display)
11+
events = pmma.Events()
1212

1313
draw = pmma.Draw(display)
1414

0 commit comments

Comments
 (0)