gamengine3d 1.6.0__tar.gz → 1.7.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/PKG-INFO +1 -1
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/engine.py +36 -1
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/examples/scripts/cylinder.py +8 -6
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/examples/the_everything_demo.py +7 -5
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/game_objects.py +90 -9
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/helper.py +19 -15
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d.egg-info/PKG-INFO +1 -1
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/pyproject.toml +1 -1
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/LICENSE.txt +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/README.md +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/__init__.py +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/cli.py +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/context.py +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/examples/__init__.py +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/examples/basics.py +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/examples/player_movement.py +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/examples/scripts/player_movement.py +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/examples/scripts/settings.py +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/shaders/__init__.py +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/shaders/default.frag +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/shaders/default.vert +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/shaders/depth.frag +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d/shaders/depth.vert +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d.egg-info/SOURCES.txt +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d.egg-info/dependency_links.txt +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d.egg-info/entry_points.txt +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d.egg-info/requires.txt +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/gamengine3d.egg-info/top_level.txt +0 -0
- {gamengine3d-1.6.0 → gamengine3d-1.7.0}/setup.cfg +0 -0
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import json
|
|
1
2
|
from itertools import product
|
|
2
3
|
from pyrr import Matrix44
|
|
3
4
|
from .context import Context
|
|
4
5
|
import pygame
|
|
5
|
-
from .game_objects import Cuboid, GameObject, Sphere
|
|
6
|
+
from .game_objects import Cuboid, GameObject, Sphere, Cylinder
|
|
6
7
|
from .helper import Renderer, vector3d, Color, Camera, Light, EngineError, Script
|
|
7
8
|
import math
|
|
8
9
|
|
|
@@ -261,3 +262,37 @@ class Engine:
|
|
|
261
262
|
def init_scripts(self):
|
|
262
263
|
for script in self.scripts:
|
|
263
264
|
script.init_instance()
|
|
265
|
+
|
|
266
|
+
def save_scene(self, filename):
|
|
267
|
+
all_gameobjects = []
|
|
268
|
+
for obj in self.gameobjects:
|
|
269
|
+
all_gameobjects.append(obj.serialize())
|
|
270
|
+
|
|
271
|
+
all_lights = []
|
|
272
|
+
for light in self.renderer.lights:
|
|
273
|
+
all_lights.append(light.serialize())
|
|
274
|
+
|
|
275
|
+
with open(filename, "w") as f:
|
|
276
|
+
json.dump({"game-objects": all_gameobjects, "lights": all_lights}, f, indent=4)
|
|
277
|
+
|
|
278
|
+
def load_scene(self, filename):
|
|
279
|
+
with open(filename, "r") as f:
|
|
280
|
+
data = json.load(f)
|
|
281
|
+
|
|
282
|
+
lights = data["lights"]
|
|
283
|
+
for light in lights:
|
|
284
|
+
self.renderer.add_light(Light.deserialize(light))
|
|
285
|
+
|
|
286
|
+
gameobjects = data["game-objects"]
|
|
287
|
+
for gameobject in gameobjects:
|
|
288
|
+
if gameobject["type"] == "cuboid":
|
|
289
|
+
obj = Cuboid.deserialize(gameobject, self.context)
|
|
290
|
+
self.add_object(obj)
|
|
291
|
+
|
|
292
|
+
elif gameobject["type"] == "sphere":
|
|
293
|
+
obj = Sphere.deserialize(gameobject, self.context)
|
|
294
|
+
self.add_object(obj)
|
|
295
|
+
|
|
296
|
+
elif gameobject["type"] == "cylinder":
|
|
297
|
+
obj = Cylinder.deserialize(gameobject, self.context)
|
|
298
|
+
self.add_object(obj)
|
|
@@ -7,15 +7,17 @@ class Cylinder:
|
|
|
7
7
|
self.color_idx = 0
|
|
8
8
|
self.obj.color = self.context.runtime_vars.cylinder_colors[0]
|
|
9
9
|
self.context.add_scheduled_caller(n_times=-1, delay=1, callback=self.switch_color)
|
|
10
|
-
self.context.on_key_held("w", self.draw_cylinder)
|
|
11
|
-
self.obj.visible = False
|
|
12
10
|
|
|
13
11
|
def update(self, dt):
|
|
14
|
-
|
|
12
|
+
if "w" in self.context.keys_held:
|
|
13
|
+
self.context.functions.draw_cylinder(vector3d(4, 3, 5), 2, 1, Color.light_green, 32)
|
|
14
|
+
|
|
15
|
+
if "e" in self.context.keys_pressed:
|
|
16
|
+
self.context.functions.draw_sphere(vector3d(1, 5, 6), radius=1, color=Color.blue)
|
|
17
|
+
|
|
18
|
+
if "r" in self.context.keys_released:
|
|
19
|
+
self.context.functions.draw_cube(pos=vector3d(5, 1, 4), size=vector3d(.2, 1, .5), color=Color.orange)
|
|
15
20
|
|
|
16
21
|
def switch_color(self):
|
|
17
22
|
self.color_idx += 1
|
|
18
23
|
self.obj.color = self.context.runtime_vars.cylinder_colors[self.color_idx % self.context.runtime_vars.num_cylinder_colors]
|
|
19
|
-
|
|
20
|
-
def draw_cylinder(self):
|
|
21
|
-
self.context.renderer.render_cylinder(vector3d.zero, 2, .1, Color.blue, 32)
|
|
@@ -33,10 +33,12 @@ cylinder = Cylinder(vector3d.zero, 2, .5, Color.light_green, 64, vector3d(40, 60
|
|
|
33
33
|
cylinder.attach(scripts_dir/"cylinder.py", engine.context)
|
|
34
34
|
engine.add_object(cylinder)
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
cuboid = Cuboid(vector3d(3, 2, 5), color=Color.orange, size=vector3d(2, 1, 3))
|
|
37
|
+
engine.add_object(cuboid)
|
|
38
|
+
|
|
39
|
+
sphere = Sphere(vector3d(0, 5, 2), color=Color.light_blue, radius=1, rings=32, segments=64)
|
|
40
|
+
engine.add_object(sphere)
|
|
41
|
+
|
|
42
|
+
engine.save_scene("scene.json")
|
|
41
43
|
|
|
42
44
|
engine.run()
|
|
@@ -9,6 +9,7 @@ class GameObject:
|
|
|
9
9
|
def __init__(self, name):
|
|
10
10
|
self.name = name
|
|
11
11
|
self.scripts = []
|
|
12
|
+
self.script_paths = []
|
|
12
13
|
|
|
13
14
|
def update(self, dt):
|
|
14
15
|
for script in self.scripts:
|
|
@@ -18,10 +19,18 @@ class GameObject:
|
|
|
18
19
|
pass
|
|
19
20
|
|
|
20
21
|
def attach(self, file_path, context):
|
|
22
|
+
if isinstance(file_path, str):
|
|
23
|
+
self.script_paths.append(file_path)
|
|
24
|
+
else:
|
|
25
|
+
self.script_paths.append(str(file_path))
|
|
21
26
|
script = Script(self, file_path, context)
|
|
22
27
|
script.init_instance()
|
|
23
28
|
self.scripts.append(script)
|
|
24
29
|
|
|
30
|
+
def attach_scripts(self, scripts, context):
|
|
31
|
+
for path in scripts:
|
|
32
|
+
self.attach(path, context)
|
|
33
|
+
|
|
25
34
|
def init_scripts(self):
|
|
26
35
|
for script in self.scripts:
|
|
27
36
|
script.init_instance()
|
|
@@ -50,7 +59,6 @@ class Cuboid(GameObject):
|
|
|
50
59
|
]
|
|
51
60
|
|
|
52
61
|
def get_rotation_matrix(self):
|
|
53
|
-
"""Return 3x3 rotation matrix (without translation)."""
|
|
54
62
|
return Matrix44.from_eulers((
|
|
55
63
|
math.radians(self.rotation.x),
|
|
56
64
|
math.radians(self.rotation.y),
|
|
@@ -58,7 +66,6 @@ class Cuboid(GameObject):
|
|
|
58
66
|
)).transpose()[:3, :3] # extract 3x3 rotation
|
|
59
67
|
|
|
60
68
|
def get_corners(self):
|
|
61
|
-
"""Return the 8 corners of the cuboid with rotation applied."""
|
|
62
69
|
corners = []
|
|
63
70
|
half_size = self.size / 2
|
|
64
71
|
rot = self.get_rotation_matrix()
|
|
@@ -72,22 +79,15 @@ class Cuboid(GameObject):
|
|
|
72
79
|
return corners
|
|
73
80
|
|
|
74
81
|
def is_point_in(self, point: vector3d) -> bool:
|
|
75
|
-
"""
|
|
76
|
-
Checks if a point is inside the cuboid, accounting for full 3D rotation.
|
|
77
|
-
"""
|
|
78
|
-
# Convert to numpy for math
|
|
79
82
|
p = np.array([point.x, point.y, point.z])
|
|
80
83
|
center = np.array([self.pos.x, self.pos.y, self.pos.z])
|
|
81
84
|
half = np.array([self.size.x / 2, self.size.y / 2, self.size.z / 2])
|
|
82
85
|
|
|
83
|
-
# Get inverse rotation matrix (undo rotation)
|
|
84
86
|
rot = self.get_rotation_matrix()
|
|
85
87
|
inv_rot = rot.T # inverse of rotation matrix is its transpose
|
|
86
88
|
|
|
87
|
-
# Transform point into cuboid's local coordinate system
|
|
88
89
|
local_point = inv_rot @ (p - center)
|
|
89
90
|
|
|
90
|
-
# Inside check in local space
|
|
91
91
|
return np.all(np.abs(local_point) <= half + 1e-6)
|
|
92
92
|
|
|
93
93
|
def draw(self, renderer: Renderer):
|
|
@@ -103,6 +103,31 @@ class Cuboid(GameObject):
|
|
|
103
103
|
color=self.color
|
|
104
104
|
)
|
|
105
105
|
|
|
106
|
+
def serialize(self):
|
|
107
|
+
return {
|
|
108
|
+
"type": "cuboid",
|
|
109
|
+
"pos": self.pos.totuple(),
|
|
110
|
+
"size": self.size.totuple(),
|
|
111
|
+
"color": self.color.to_rgb(),
|
|
112
|
+
"name": self.name,
|
|
113
|
+
"rotation": self.rotation.totuple(),
|
|
114
|
+
"scripts": self.script_paths,
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@staticmethod
|
|
118
|
+
def deserialize(data, context):
|
|
119
|
+
pos = data["pos"]
|
|
120
|
+
size = data["size"]
|
|
121
|
+
color = data["color"]
|
|
122
|
+
name = data["name"]
|
|
123
|
+
rotation = data["rotation"]
|
|
124
|
+
scripts = data["scripts"]
|
|
125
|
+
|
|
126
|
+
obj = Cuboid(pos=vector3d.fromtuple(pos), color=Color.RGB(*color), size=vector3d.fromtuple(size), name=name, rotation=vector3d.fromtuple(rotation))
|
|
127
|
+
obj.attach_scripts(scripts, context)
|
|
128
|
+
|
|
129
|
+
return obj
|
|
130
|
+
|
|
106
131
|
class Sphere(GameObject):
|
|
107
132
|
def __init__(self, pos: vector3d = vector3d.zero,
|
|
108
133
|
radius: float = 5,
|
|
@@ -131,6 +156,33 @@ class Sphere(GameObject):
|
|
|
131
156
|
return
|
|
132
157
|
renderer.render_sphere(self.pos, self.radius, self.color, self.segments, self.rings)
|
|
133
158
|
|
|
159
|
+
def serialize(self):
|
|
160
|
+
return {
|
|
161
|
+
"type": "sphere",
|
|
162
|
+
"pos": self.pos.totuple(),
|
|
163
|
+
"color": self.color.to_rgb(),
|
|
164
|
+
"radius": self.radius,
|
|
165
|
+
"segments": self.segments,
|
|
166
|
+
"rings": self.rings,
|
|
167
|
+
"name": self.name,
|
|
168
|
+
"scripts": self.script_paths
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
@staticmethod
|
|
172
|
+
def deserialize(data, context):
|
|
173
|
+
pos = data["pos"]
|
|
174
|
+
color = data["color"]
|
|
175
|
+
radius = data["radius"]
|
|
176
|
+
segments = data["segments"]
|
|
177
|
+
rings = data["rings"]
|
|
178
|
+
name = data["name"]
|
|
179
|
+
scripts = data["scripts"]
|
|
180
|
+
|
|
181
|
+
obj = Sphere(pos=vector3d.fromtuple(pos), color=Color.RGB(*color), radius=radius, segments=segments, rings=rings, name=name)
|
|
182
|
+
obj.attach_scripts(scripts, context)
|
|
183
|
+
|
|
184
|
+
return obj
|
|
185
|
+
|
|
134
186
|
class Cylinder(GameObject):
|
|
135
187
|
def __init__(self, pos: vector3d = vector3d.zero,
|
|
136
188
|
length: float = 2,
|
|
@@ -183,3 +235,32 @@ class Cylinder(GameObject):
|
|
|
183
235
|
return
|
|
184
236
|
renderer.render_cylinder(center=self.pos, height=self.length, radius=self.radius,
|
|
185
237
|
color=self.color, segments=self.segments, rotation=self.rotation)
|
|
238
|
+
|
|
239
|
+
def serialize(self):
|
|
240
|
+
return {
|
|
241
|
+
"type": "cylinder",
|
|
242
|
+
"pos": self.pos.totuple(),
|
|
243
|
+
"length": self.length,
|
|
244
|
+
"radius": self.radius,
|
|
245
|
+
"color": self.color.to_rgb(),
|
|
246
|
+
"segments": self.segments,
|
|
247
|
+
"rotation": self.rotation.totuple(),
|
|
248
|
+
"name": self.name,
|
|
249
|
+
"scripts": self.script_paths
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
@staticmethod
|
|
253
|
+
def deserialize(data, context):
|
|
254
|
+
pos = data["pos"]
|
|
255
|
+
length = data["length"]
|
|
256
|
+
radius = data["radius"]
|
|
257
|
+
color = data["color"]
|
|
258
|
+
segments = data["segments"]
|
|
259
|
+
rotation = data["rotation"]
|
|
260
|
+
name = data["name"]
|
|
261
|
+
scripts = data["scripts"]
|
|
262
|
+
|
|
263
|
+
obj = Cylinder(pos=vector3d.fromtuple(pos), length=length, radius=radius, color=Color.RGB(*color), segments=segments, rotation=vector3d.fromtuple(rotation), name=name)
|
|
264
|
+
obj.attach_scripts(scripts, context)
|
|
265
|
+
|
|
266
|
+
return obj
|
|
@@ -227,6 +227,25 @@ class Light:
|
|
|
227
227
|
self.show = show
|
|
228
228
|
self.name = name
|
|
229
229
|
|
|
230
|
+
def serialize(self):
|
|
231
|
+
return {
|
|
232
|
+
"position": self.position.totuple(),
|
|
233
|
+
"color": self.color.to_rgb(),
|
|
234
|
+
"intensity": self.intensity,
|
|
235
|
+
"show": self.show,
|
|
236
|
+
"name": self.name,
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
@staticmethod
|
|
240
|
+
def deserialize(data):
|
|
241
|
+
position = data["position"]
|
|
242
|
+
color = data["color"]
|
|
243
|
+
intensity = data["intensity"]
|
|
244
|
+
show = data["show"]
|
|
245
|
+
name = data["name"]
|
|
246
|
+
|
|
247
|
+
return Light(position=vector3d.fromtuple(position), color=Color.RGB(*color), intensity=intensity, show=show, name=name)
|
|
248
|
+
|
|
230
249
|
class Renderer:
|
|
231
250
|
def __init__(self, width=800, height=600, flags=DOUBLEBUF | OPENGL):
|
|
232
251
|
pygame.init()
|
|
@@ -267,9 +286,6 @@ class Renderer:
|
|
|
267
286
|
self.depth_prog = self._load_shader("depth.vert", "depth.frag")
|
|
268
287
|
self.light_space_matrix = np.eye(4, dtype="f4")
|
|
269
288
|
|
|
270
|
-
# ------------------------------------------
|
|
271
|
-
# Window / Resize Handling
|
|
272
|
-
# ------------------------------------------
|
|
273
289
|
def resize(self, width, height):
|
|
274
290
|
self.width = width
|
|
275
291
|
self.height = height
|
|
@@ -307,9 +323,6 @@ class Renderer:
|
|
|
307
323
|
|
|
308
324
|
self._send_lights()
|
|
309
325
|
|
|
310
|
-
# ------------------------------------------
|
|
311
|
-
# Light & Matrix
|
|
312
|
-
# ------------------------------------------
|
|
313
326
|
def add_light(self, light: Light):
|
|
314
327
|
self.lights.append(light)
|
|
315
328
|
|
|
@@ -328,9 +341,6 @@ class Renderer:
|
|
|
328
341
|
)
|
|
329
342
|
return light_proj * light_view
|
|
330
343
|
|
|
331
|
-
# ------------------------------------------
|
|
332
|
-
# Shadow Pass
|
|
333
|
-
# ------------------------------------------
|
|
334
344
|
def render_shadow_pass(self, objects):
|
|
335
345
|
"""Render depth map from light's POV."""
|
|
336
346
|
if not self.lights:
|
|
@@ -383,11 +393,9 @@ class Renderer:
|
|
|
383
393
|
vao.release()
|
|
384
394
|
vbo.release()
|
|
385
395
|
|
|
386
|
-
|
|
387
396
|
def render_cylinder(self, center, height, radius, color, segments=32, rotation=vector3d(0, 0, 0)):
|
|
388
397
|
vertices = []
|
|
389
398
|
|
|
390
|
-
# Helper functions for matrix multiplication
|
|
391
399
|
def apply_matrix(v: vector3d, m: Matrix44) -> vector3d:
|
|
392
400
|
vec4 = np.array([v.x, v.y, v.z, 1.0], dtype="f4")
|
|
393
401
|
transformed = m @ vec4
|
|
@@ -478,7 +486,6 @@ class Renderer:
|
|
|
478
486
|
vao.release()
|
|
479
487
|
vbo.release()
|
|
480
488
|
|
|
481
|
-
|
|
482
489
|
def render_quad(self, p1, p2, p3, p4, color):
|
|
483
490
|
normal = self._compute_normal(p1, p2, p3)
|
|
484
491
|
vertices = np.array([
|
|
@@ -543,9 +550,6 @@ class Renderer:
|
|
|
543
550
|
vao.release()
|
|
544
551
|
vbo.release()
|
|
545
552
|
|
|
546
|
-
# ------------------------------------------
|
|
547
|
-
# Utility
|
|
548
|
-
# ------------------------------------------
|
|
549
553
|
def _send_lights(self):
|
|
550
554
|
max_lights = 8
|
|
551
555
|
num_lights = min(len(self.lights), max_lights)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|