gamengine3d 1.8.0__tar.gz → 1.10.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.8.0 → gamengine3d-1.10.0}/PKG-INFO +3 -1
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/README.md +2 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/__init__.py +1 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/context.py +69 -1
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/engine.py +74 -1
- gamengine3d-1.10.0/gamengine3d/examples/from_obj.py +14 -0
- gamengine3d-1.10.0/gamengine3d/examples/load_scene.py +11 -0
- gamengine3d-1.10.0/gamengine3d/examples/save_scene.py +47 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/examples/the_everything_demo.py +0 -2
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/game_objects.py +76 -11
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/helper.py +26 -0
- gamengine3d-1.10.0/gamengine3d/obj_reader.py +30 -0
- gamengine3d-1.10.0/gamengine3d/shaders/__init__.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d.egg-info/PKG-INFO +3 -1
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d.egg-info/SOURCES.txt +5 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/pyproject.toml +1 -1
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/LICENSE.txt +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/cli.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/examples/__init__.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/examples/basics.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/examples/messages_demo.py +0 -0
- {gamengine3d-1.8.0/gamengine3d/shaders → gamengine3d-1.10.0/gamengine3d/examples/objects}/__init__.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/examples/player_movement.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/examples/scripts/cylinder.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/examples/scripts/message_receiver.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/examples/scripts/messanger_script.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/examples/scripts/player_movement.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/examples/scripts/settings.py +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/shaders/default.frag +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/shaders/default.vert +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/shaders/depth.frag +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d/shaders/depth.vert +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d.egg-info/dependency_links.txt +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d.egg-info/entry_points.txt +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d.egg-info/requires.txt +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/gamengine3d.egg-info/top_level.txt +0 -0
- {gamengine3d-1.8.0 → gamengine3d-1.10.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: gamengine3d
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.10.0
|
|
4
4
|
Summary: A Unity-like 3D engine in Python using pygame
|
|
5
5
|
Author-email: Samarth Javagal <samarthjavagal@gmail.com>
|
|
6
6
|
Classifier: Programming Language :: Python :: 3
|
|
@@ -78,3 +78,5 @@ This minimal PlayerController shows how to attach a script to an object and resp
|
|
|
78
78
|
You can run any built-in demo with ``gamengine3d demo <demo_name>`` and see all available options using ``gamengine3d demo -h``.
|
|
79
79
|
|
|
80
80
|
The 2d version of ```gamengine3d``` is out under ``gamengine2d``
|
|
81
|
+
|
|
82
|
+
Check out the full documentation for this package here: [Package Docs](https://sites.google.com/view/samarthsprojects/python-packages/gamengine3d)
|
|
@@ -60,3 +60,5 @@ This minimal PlayerController shows how to attach a script to an object and resp
|
|
|
60
60
|
You can run any built-in demo with ``gamengine3d demo <demo_name>`` and see all available options using ``gamengine3d demo -h``.
|
|
61
61
|
|
|
62
62
|
The 2d version of ```gamengine3d``` is out under ``gamengine2d``
|
|
63
|
+
|
|
64
|
+
Check out the full documentation for this package here: [Package Docs](https://sites.google.com/view/samarthsprojects/python-packages/gamengine3d)
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
from .game_objects import GameObject
|
|
2
|
-
from .helper import Color, vector3d, Light, Camera, Renderer
|
|
2
|
+
from .helper import Color, vector3d, Light, Camera, Renderer, EngineError
|
|
3
3
|
from pygame.time import Clock
|
|
4
4
|
import time
|
|
5
5
|
import importlib
|
|
6
|
+
import warnings
|
|
6
7
|
|
|
7
8
|
class Functions:
|
|
8
9
|
def __init__(self):
|
|
@@ -126,6 +127,67 @@ class RuntimeVars:
|
|
|
126
127
|
if not attr_name.startswith("__"):
|
|
127
128
|
setattr(self, attr_name, getattr(module, attr_name))
|
|
128
129
|
|
|
130
|
+
def serialize(self):
|
|
131
|
+
attributes = {}
|
|
132
|
+
for attr_name in dir(self):
|
|
133
|
+
if not attr_name.startswith("__") and attr_name not in ("serialize", "from_settings", "_serialize", "_deserialize", "deserialize"):
|
|
134
|
+
attr = getattr(self, attr_name)
|
|
135
|
+
serialized_attr = self._serialize(attr)
|
|
136
|
+
if serialized_attr[0] not in ("type", "unknown"):
|
|
137
|
+
attributes[attr_name] = serialized_attr
|
|
138
|
+
return attributes
|
|
139
|
+
|
|
140
|
+
def _serialize(self, attr):
|
|
141
|
+
if isinstance(attr, (list, tuple, str, type(None), int, float, bool, dict)):
|
|
142
|
+
new_attr = attr
|
|
143
|
+
if isinstance(attr, (list, tuple)):
|
|
144
|
+
new_attr = [self._serialize(subattr) for subattr in attr]
|
|
145
|
+
elif isinstance(attr, dict):
|
|
146
|
+
new_attr = {k: self._serialize(v) for k, v in attr.items()}
|
|
147
|
+
return [type(attr).__name__, new_attr]
|
|
148
|
+
|
|
149
|
+
elif isinstance(attr, vector3d):
|
|
150
|
+
return [vector3d.__name__, attr.totuple()]
|
|
151
|
+
|
|
152
|
+
elif isinstance(attr, Color):
|
|
153
|
+
return [Color.__name__, attr.to_rgb()]
|
|
154
|
+
|
|
155
|
+
elif isinstance(attr, type):
|
|
156
|
+
warnings.warn(f"Unable to serialize type object: {attr.__name__}", stacklevel=2)
|
|
157
|
+
return ["type", attr.__name__]
|
|
158
|
+
|
|
159
|
+
warnings.warn(f"Cannot serialize attribute of type {type(attr).__name__}", stacklevel=2)
|
|
160
|
+
return ["unknown", str(attr)]
|
|
161
|
+
|
|
162
|
+
def _deserialize(self, attr_metadata):
|
|
163
|
+
type_name, value = attr_metadata
|
|
164
|
+
|
|
165
|
+
if type_name == "list":
|
|
166
|
+
return [self._deserialize(v) for v in value]
|
|
167
|
+
elif type_name == "tuple":
|
|
168
|
+
return tuple(self._deserialize(v) for v in value)
|
|
169
|
+
elif type_name == "dict":
|
|
170
|
+
return {k: self._deserialize(v) for k, v in value.items()}
|
|
171
|
+
elif type_name == vector3d.__name__:
|
|
172
|
+
return vector3d(*value)
|
|
173
|
+
elif type_name == Color.__name__:
|
|
174
|
+
return Color.RGB(*value)
|
|
175
|
+
elif type_name in ("int", "float", "bool", "str", "NoneType"):
|
|
176
|
+
return value
|
|
177
|
+
elif type_name == "type":
|
|
178
|
+
warnings.warn(f"Cannot deserialize type object: {value}", stacklevel=2)
|
|
179
|
+
return None
|
|
180
|
+
elif type_name == "unknown":
|
|
181
|
+
warnings.warn(f"Cannot deserialize unknown object: {value}", stacklevel=2)
|
|
182
|
+
return None
|
|
183
|
+
else:
|
|
184
|
+
warnings.warn(f"Unrecognized type during deserialization: {type_name}", stacklevel=2)
|
|
185
|
+
return None
|
|
186
|
+
|
|
187
|
+
def deserialize(self, data):
|
|
188
|
+
for attr_name, attr_metadata in data.items():
|
|
189
|
+
setattr(self, attr_name, self._deserialize(attr_metadata))
|
|
190
|
+
|
|
129
191
|
class Context:
|
|
130
192
|
def __init__(self):
|
|
131
193
|
self.functions: Functions = Functions()
|
|
@@ -223,3 +285,9 @@ class Context:
|
|
|
223
285
|
def send_message(self, target_name, message):
|
|
224
286
|
target = self.functions.get_game_object(target_name)
|
|
225
287
|
target.message(message=message, sender=self.current_caller.name)
|
|
288
|
+
|
|
289
|
+
def serialize(self):
|
|
290
|
+
return self.runtime_vars.serialize()
|
|
291
|
+
|
|
292
|
+
def deserialize(self, data):
|
|
293
|
+
self.runtime_vars.deserialize(data)
|
|
@@ -59,6 +59,7 @@ class Engine:
|
|
|
59
59
|
self.context.clock = self.clock
|
|
60
60
|
|
|
61
61
|
self.context.lights = self.renderer.lights
|
|
62
|
+
self.context.ambient_light = ambient_light
|
|
62
63
|
|
|
63
64
|
def run(self, fps=60, dynamic_view=True):
|
|
64
65
|
running = True
|
|
@@ -272,6 +273,74 @@ class Engine:
|
|
|
272
273
|
for script in self.scripts:
|
|
273
274
|
script.init_instance()
|
|
274
275
|
|
|
276
|
+
def physics_update(self, dt):
|
|
277
|
+
for obj in self.gameobjects:
|
|
278
|
+
if isinstance(obj, Cuboid):
|
|
279
|
+
cube = obj
|
|
280
|
+
rb = cube.rigid_body
|
|
281
|
+
if rb is not None:
|
|
282
|
+
if rb.gravity:
|
|
283
|
+
rb.velocity += vector3d.down * 9.8 * dt
|
|
284
|
+
|
|
285
|
+
# integrate forces
|
|
286
|
+
rb.velocity += (rb.forces / rb.mass) * dt
|
|
287
|
+
rb.forces = vector3d.zero
|
|
288
|
+
|
|
289
|
+
# integrate movement
|
|
290
|
+
cube.pos += rb.velocity * dt
|
|
291
|
+
|
|
292
|
+
def resolve_collision(self, name1, name2):
|
|
293
|
+
obj1 = self.get_object(name1)
|
|
294
|
+
obj2 = self.get_object(name2)
|
|
295
|
+
|
|
296
|
+
if not (isinstance(obj1, Cuboid) and isinstance(obj2, Cuboid)):
|
|
297
|
+
raise NotImplementedError("Cylinder And Sphere Collision Not Implemented")
|
|
298
|
+
|
|
299
|
+
# If they do not overlap, exit
|
|
300
|
+
if not self.is_colliding(name1, name2):
|
|
301
|
+
return
|
|
302
|
+
|
|
303
|
+
rb = obj1.rigid_body # the moving object
|
|
304
|
+
# obj2 may or may not have rigid body — we treat it as static for now
|
|
305
|
+
|
|
306
|
+
# Determine correction direction by checking corners
|
|
307
|
+
corners = obj1.get_corners()
|
|
308
|
+
|
|
309
|
+
min_penetration = float("inf")
|
|
310
|
+
best_axis = None
|
|
311
|
+
|
|
312
|
+
for corner in corners:
|
|
313
|
+
if obj2.is_point_in(corner):
|
|
314
|
+
# penetration vector = from obj2 center to corner
|
|
315
|
+
penetration_vec = corner - obj2.pos
|
|
316
|
+
# absolute magnitude along each axis
|
|
317
|
+
px = abs(penetration_vec.x)
|
|
318
|
+
py = abs(penetration_vec.y)
|
|
319
|
+
pz = abs(penetration_vec.z)
|
|
320
|
+
|
|
321
|
+
# pick smallest axis penetration
|
|
322
|
+
axis_val = min(px, py, pz)
|
|
323
|
+
if axis_val < min_penetration:
|
|
324
|
+
min_penetration = axis_val
|
|
325
|
+
if axis_val == px:
|
|
326
|
+
best_axis = vector3d.right if penetration_vec.x > 0 else vector3d.left
|
|
327
|
+
elif axis_val == py:
|
|
328
|
+
best_axis = vector3d.up if penetration_vec.y > 0 else vector3d.down
|
|
329
|
+
else:
|
|
330
|
+
best_axis = vector3d.forward if penetration_vec.z > 0 else vector3d.back
|
|
331
|
+
|
|
332
|
+
if best_axis is None:
|
|
333
|
+
return
|
|
334
|
+
|
|
335
|
+
correction = best_axis * min_penetration
|
|
336
|
+
obj1.pos += correction
|
|
337
|
+
|
|
338
|
+
normal = best_axis.normalized()
|
|
339
|
+
vel_along_normal = rb.velocity.dot(normal)
|
|
340
|
+
|
|
341
|
+
if vel_along_normal < 0:
|
|
342
|
+
rb.velocity -= (1 + rb.bounce) * vel_along_normal * normal
|
|
343
|
+
|
|
275
344
|
def save_scene(self, filename):
|
|
276
345
|
all_gameobjects = []
|
|
277
346
|
for obj in self.gameobjects:
|
|
@@ -281,13 +350,17 @@ class Engine:
|
|
|
281
350
|
for light in self.renderer.lights:
|
|
282
351
|
all_lights.append(light.serialize())
|
|
283
352
|
|
|
353
|
+
context_attrs = self.context.serialize()
|
|
354
|
+
|
|
284
355
|
with open(filename, "w") as f:
|
|
285
|
-
json.dump({"game-objects": all_gameobjects, "lights": all_lights, "engine": self.serialize()}, f, indent=4)
|
|
356
|
+
json.dump({"game-objects": all_gameobjects, "lights": all_lights, "engine": self.serialize(), "context": context_attrs}, f, indent=4)
|
|
286
357
|
|
|
287
358
|
def load_scene(self, filename):
|
|
288
359
|
with open(filename, "r") as f:
|
|
289
360
|
data = json.load(f)
|
|
290
361
|
|
|
362
|
+
self.context.deserialize(data["context"])
|
|
363
|
+
|
|
291
364
|
lights = data["lights"]
|
|
292
365
|
for light in lights:
|
|
293
366
|
self.renderer.add_light(Light.deserialize(light))
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from gamengine3d import Engine, Color, vector3d, Light, ObjModel
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
EXAMPLE_DIR = Path(__file__).parent
|
|
5
|
+
|
|
6
|
+
engine = Engine(1200, 800, background_color=Color.light_grey, ambient_light=.5, name="Loading an OBJ File")
|
|
7
|
+
|
|
8
|
+
obj = ObjModel(name="Icosahedron", pos=vector3d(4, 2, 1), color=Color.blue, file_path=str(EXAMPLE_DIR / "objects" / "icosahedron.obj"), scale=2.0)
|
|
9
|
+
|
|
10
|
+
engine.add_object(obj)
|
|
11
|
+
|
|
12
|
+
engine.add_light(Light(vector3d(20, 20, 20), Color.white, 0.8))
|
|
13
|
+
|
|
14
|
+
engine.run()
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This demo will load the saved scene from a JSON file. If you haven't run the save_scene demo yet, please do so first to generate the scene file.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from gamengine3d import Engine
|
|
6
|
+
|
|
7
|
+
engine = Engine(1200, 800)
|
|
8
|
+
|
|
9
|
+
engine.load_scene("the_everything_demo.json")
|
|
10
|
+
|
|
11
|
+
engine.run()
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The recreates the everything demo, and then saves the scene into a JSON file called "the_everything_demo.json".
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from gamengine3d import *
|
|
6
|
+
import random
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
engine = Engine(1000, 800, name="The Everything DEMO", background_color=Color.grey)
|
|
10
|
+
engine.context.runtime_vars.from_settings("gamengine3d.examples.scripts.settings") # set the runtime vars from a module
|
|
11
|
+
|
|
12
|
+
positions = [
|
|
13
|
+
vector3d(0, 4, 0.5),
|
|
14
|
+
vector3d(-3, 2, 2),
|
|
15
|
+
vector3d(3, 3, -2),
|
|
16
|
+
vector3d(-2, 5, -3),
|
|
17
|
+
vector3d(4, 6, 4),
|
|
18
|
+
vector3d(1, 6, 7),
|
|
19
|
+
vector3d(-4, 3, 5),
|
|
20
|
+
vector3d(5, 5, 9),
|
|
21
|
+
vector3d(0, 7, -4),
|
|
22
|
+
vector3d(-5, 4, 3),
|
|
23
|
+
vector3d(2, 8, 2),
|
|
24
|
+
vector3d(3, 5, -5),
|
|
25
|
+
vector3d(-3, 6, 6),
|
|
26
|
+
vector3d(6, 3, 3)
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
for pos in positions:
|
|
30
|
+
intensity = random.uniform(0.4, .8)
|
|
31
|
+
engine.add_light(Light(position=pos, color=Color.white, intensity=intensity))
|
|
32
|
+
|
|
33
|
+
EXAMPLE_DIR = Path(__file__).parent
|
|
34
|
+
scripts_dir = EXAMPLE_DIR / "scripts"
|
|
35
|
+
|
|
36
|
+
cylinder = Cylinder(vector3d.zero, 2, .5, Color.light_green, 64, vector3d(40, 60, 100), "Cylinder")
|
|
37
|
+
cylinder.attach(scripts_dir/"cylinder.py", engine.context)
|
|
38
|
+
engine.add_object(cylinder)
|
|
39
|
+
|
|
40
|
+
cuboid = Cuboid(vector3d(3, 2, 5), color=Color.orange, size=vector3d(2, 1, 3))
|
|
41
|
+
engine.add_object(cuboid)
|
|
42
|
+
|
|
43
|
+
sphere = Sphere(vector3d(0, 5, 2), color=Color.light_blue, radius=1, rings=32, segments=64)
|
|
44
|
+
engine.add_object(sphere)
|
|
45
|
+
|
|
46
|
+
engine.save_scene("the_everything_demo.json") # Save the current scene to a JSON file
|
|
47
|
+
|
|
@@ -3,6 +3,7 @@ from itertools import product
|
|
|
3
3
|
from pyrr import Matrix44
|
|
4
4
|
import math
|
|
5
5
|
|
|
6
|
+
from .obj_reader import OBJReader
|
|
6
7
|
from .helper import Color, vector3d, Renderer, Script, Message
|
|
7
8
|
|
|
8
9
|
class GameObject:
|
|
@@ -48,14 +49,16 @@ class Cuboid(GameObject):
|
|
|
48
49
|
size: vector3d = vector3d(5),
|
|
49
50
|
color: Color = Color.green,
|
|
50
51
|
name: str = "Cuboid",
|
|
51
|
-
rotation: vector3d = vector3d.zero
|
|
52
|
+
rotation: vector3d = vector3d.zero,
|
|
53
|
+
visible: bool = True):
|
|
52
54
|
super().__init__(name)
|
|
53
55
|
self.name = name
|
|
54
56
|
self.pos = pos
|
|
55
57
|
self.size = size
|
|
56
58
|
self.color = color
|
|
57
59
|
self.rotation = rotation
|
|
58
|
-
self.visible =
|
|
60
|
+
self.visible = visible
|
|
61
|
+
self.rigid_body = None
|
|
59
62
|
|
|
60
63
|
self.faces = [
|
|
61
64
|
[0, 1, 3, 2],
|
|
@@ -120,6 +123,7 @@ class Cuboid(GameObject):
|
|
|
120
123
|
"name": self.name,
|
|
121
124
|
"rotation": self.rotation.totuple(),
|
|
122
125
|
"scripts": self.script_paths,
|
|
126
|
+
"visible": self.visible
|
|
123
127
|
}
|
|
124
128
|
|
|
125
129
|
@staticmethod
|
|
@@ -130,8 +134,9 @@ class Cuboid(GameObject):
|
|
|
130
134
|
name = data["name"]
|
|
131
135
|
rotation = data["rotation"]
|
|
132
136
|
scripts = data["scripts"]
|
|
137
|
+
visible = data["visible"]
|
|
133
138
|
|
|
134
|
-
obj = Cuboid(pos=vector3d.fromtuple(pos), color=Color.RGB(*color), size=vector3d.fromtuple(size), name=name, rotation=vector3d.fromtuple(rotation))
|
|
139
|
+
obj = Cuboid(pos=vector3d.fromtuple(pos), color=Color.RGB(*color), size=vector3d.fromtuple(size), name=name, rotation=vector3d.fromtuple(rotation), visible=visible)
|
|
135
140
|
obj.attach_scripts(scripts, context)
|
|
136
141
|
|
|
137
142
|
return obj
|
|
@@ -142,7 +147,7 @@ class Sphere(GameObject):
|
|
|
142
147
|
color: Color = Color.green,
|
|
143
148
|
segments: int = 32,
|
|
144
149
|
rings: int = 16,
|
|
145
|
-
name: str = "Sphere"):
|
|
150
|
+
name: str = "Sphere", visible=True):
|
|
146
151
|
super().__init__(name)
|
|
147
152
|
self.name = name
|
|
148
153
|
self.pos = pos
|
|
@@ -150,7 +155,7 @@ class Sphere(GameObject):
|
|
|
150
155
|
self.color = color
|
|
151
156
|
self.segments = segments
|
|
152
157
|
self.rings = rings
|
|
153
|
-
self.visible =
|
|
158
|
+
self.visible = visible
|
|
154
159
|
|
|
155
160
|
def is_point_in(self, point: vector3d) -> bool:
|
|
156
161
|
dx = point.x - self.pos.x
|
|
@@ -173,7 +178,8 @@ class Sphere(GameObject):
|
|
|
173
178
|
"segments": self.segments,
|
|
174
179
|
"rings": self.rings,
|
|
175
180
|
"name": self.name,
|
|
176
|
-
"scripts": self.script_paths
|
|
181
|
+
"scripts": self.script_paths,
|
|
182
|
+
"visible": self.visible
|
|
177
183
|
}
|
|
178
184
|
|
|
179
185
|
@staticmethod
|
|
@@ -185,8 +191,9 @@ class Sphere(GameObject):
|
|
|
185
191
|
rings = data["rings"]
|
|
186
192
|
name = data["name"]
|
|
187
193
|
scripts = data["scripts"]
|
|
194
|
+
visible = data["visible"]
|
|
188
195
|
|
|
189
|
-
obj = Sphere(pos=vector3d.fromtuple(pos), color=Color.RGB(*color), radius=radius, segments=segments, rings=rings, name=name)
|
|
196
|
+
obj = Sphere(pos=vector3d.fromtuple(pos), color=Color.RGB(*color), radius=radius, segments=segments, rings=rings, name=name, visible=visible)
|
|
190
197
|
obj.attach_scripts(scripts, context)
|
|
191
198
|
|
|
192
199
|
return obj
|
|
@@ -198,7 +205,7 @@ class Cylinder(GameObject):
|
|
|
198
205
|
color: Color = Color.light_yellow,
|
|
199
206
|
segments: int = 32,
|
|
200
207
|
rotation: vector3d = vector3d.zero,
|
|
201
|
-
name: str = "Cylinder"):
|
|
208
|
+
name: str = "Cylinder", visible: bool = True):
|
|
202
209
|
super().__init__(name)
|
|
203
210
|
self.name = name
|
|
204
211
|
self.pos = pos
|
|
@@ -207,7 +214,7 @@ class Cylinder(GameObject):
|
|
|
207
214
|
self.color = color
|
|
208
215
|
self.segments = segments
|
|
209
216
|
self.rotation = rotation # Euler angles in degrees
|
|
210
|
-
self.visible =
|
|
217
|
+
self.visible = visible
|
|
211
218
|
|
|
212
219
|
def get_rotation_matrix(self):
|
|
213
220
|
rx, ry, rz = math.radians(self.rotation.x), math.radians(self.rotation.y), math.radians(self.rotation.z)
|
|
@@ -254,7 +261,8 @@ class Cylinder(GameObject):
|
|
|
254
261
|
"segments": self.segments,
|
|
255
262
|
"rotation": self.rotation.totuple(),
|
|
256
263
|
"name": self.name,
|
|
257
|
-
"scripts": self.script_paths
|
|
264
|
+
"scripts": self.script_paths,
|
|
265
|
+
"visible": self.visible,
|
|
258
266
|
}
|
|
259
267
|
|
|
260
268
|
@staticmethod
|
|
@@ -267,8 +275,65 @@ class Cylinder(GameObject):
|
|
|
267
275
|
rotation = data["rotation"]
|
|
268
276
|
name = data["name"]
|
|
269
277
|
scripts = data["scripts"]
|
|
278
|
+
visible = data["visible"]
|
|
279
|
+
|
|
280
|
+
obj = Cylinder(pos=vector3d.fromtuple(pos), length=length, radius=radius, color=Color.RGB(*color), segments=segments, rotation=vector3d.fromtuple(rotation), name=name, visible=visible)
|
|
281
|
+
obj.attach_scripts(scripts, context)
|
|
282
|
+
|
|
283
|
+
return obj
|
|
284
|
+
|
|
285
|
+
class ObjModel(GameObject):
|
|
286
|
+
def __init__(self, pos: vector3d = vector3d.zero,
|
|
287
|
+
color: Color = Color.white,
|
|
288
|
+
name: str = "ObjModel",
|
|
289
|
+
scale: float = 1.0,
|
|
290
|
+
file_path: str = "", visible: bool = True):
|
|
291
|
+
super().__init__(name)
|
|
292
|
+
self.name = name
|
|
293
|
+
self.pos = pos
|
|
294
|
+
self.color = color
|
|
295
|
+
self.file_path = file_path
|
|
296
|
+
self.visible = visible
|
|
297
|
+
self.scale = scale
|
|
298
|
+
|
|
299
|
+
self.vertices = []
|
|
300
|
+
self.faces = []
|
|
301
|
+
|
|
302
|
+
self.load_from_file()
|
|
303
|
+
|
|
304
|
+
def load_from_file(self):
|
|
305
|
+
reader = OBJReader(self.file_path)
|
|
306
|
+
self.vertices, self.faces = reader.read()
|
|
307
|
+
|
|
308
|
+
def draw(self, renderer: Renderer):
|
|
309
|
+
if not self.visible:
|
|
310
|
+
return
|
|
311
|
+
|
|
312
|
+
for face in self.faces:
|
|
313
|
+
renderer.render_plane(p1=self.vertices[face[0]]*self.scale+self.pos, p2=self.vertices[face[1]]*self.scale+self.pos, p3=self.vertices[face[2]]*self.scale+self.pos,
|
|
314
|
+
color=self.color)
|
|
315
|
+
|
|
316
|
+
def serialize(self):
|
|
317
|
+
return {
|
|
318
|
+
"type": "obj-model",
|
|
319
|
+
"pos": self.pos.totuple(),
|
|
320
|
+
"color": self.color.to_rgb(),
|
|
321
|
+
"name": self.name,
|
|
322
|
+
"scripts": self.script_paths,
|
|
323
|
+
"scale": self.scale,
|
|
324
|
+
"visible": self.visible,
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
@staticmethod
|
|
328
|
+
def deserialize(data, context):
|
|
329
|
+
pos = data["pos"]
|
|
330
|
+
color = data["color"]
|
|
331
|
+
name = data["name"]
|
|
332
|
+
scripts = data["scripts"]
|
|
333
|
+
scale = data["scale"]
|
|
334
|
+
visible = data["visible"]
|
|
270
335
|
|
|
271
|
-
obj =
|
|
336
|
+
obj = ObjModel(pos=pos, name=name, color=color, scale=scale, visible=visible)
|
|
272
337
|
obj.attach_scripts(scripts, context)
|
|
273
338
|
|
|
274
339
|
return obj
|
|
@@ -667,6 +667,20 @@ class Camera:
|
|
|
667
667
|
renderer.mvp = renderer.proj * renderer.view
|
|
668
668
|
renderer.prog["mvp"].write(renderer.mvp.astype("f4").tobytes())
|
|
669
669
|
|
|
670
|
+
def look_at(self, point: vector3d):
|
|
671
|
+
"""
|
|
672
|
+
Make the camera look at a specific point in space.
|
|
673
|
+
- In orbiting mode, sets the target and updates spherical coordinates.
|
|
674
|
+
- In free mode, sets the look direction vector.
|
|
675
|
+
"""
|
|
676
|
+
if self.orbiting:
|
|
677
|
+
self.target = point
|
|
678
|
+
self._update_spherical(self.position)
|
|
679
|
+
else:
|
|
680
|
+
self.look_dir = (point - self.position).normalized # direction vector in free mode
|
|
681
|
+
|
|
682
|
+
self._update_position()
|
|
683
|
+
|
|
670
684
|
class Script:
|
|
671
685
|
def __init__(self, obj, script_path, context):
|
|
672
686
|
self.obj = obj
|
|
@@ -738,3 +752,15 @@ class Message:
|
|
|
738
752
|
|
|
739
753
|
def __repr__(self):
|
|
740
754
|
return f"Message({self.message=}, {self.sender=}, {self.time=})"
|
|
755
|
+
|
|
756
|
+
class RigidBody:
|
|
757
|
+
def __init__(self, mass=1.0, bounce=0.3, gravity=True):
|
|
758
|
+
self.mass = mass
|
|
759
|
+
self.bounce = bounce
|
|
760
|
+
self.gravity = gravity
|
|
761
|
+
|
|
762
|
+
self.velocity = vector3d.zero
|
|
763
|
+
self.forces = vector3d.zero
|
|
764
|
+
|
|
765
|
+
def add_force(self, f):
|
|
766
|
+
self.forces += f
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
|
|
2
|
+
from .helper import vector3d
|
|
3
|
+
|
|
4
|
+
class OBJReader:
|
|
5
|
+
def __init__(self, filename):
|
|
6
|
+
self.filename = filename
|
|
7
|
+
self.vertices = []
|
|
8
|
+
self.faces = []
|
|
9
|
+
|
|
10
|
+
def read(self):
|
|
11
|
+
with open(self.filename, 'r') as f:
|
|
12
|
+
for line in f:
|
|
13
|
+
line = line.strip()
|
|
14
|
+
if line.startswith('v '):
|
|
15
|
+
parts = line.split()[1:]
|
|
16
|
+
self.vertices.append(vector3d(*[float(p) for p in parts]))
|
|
17
|
+
elif line.startswith('f '):
|
|
18
|
+
parts = line.split()[1:]
|
|
19
|
+
# Convert face into vertex indices
|
|
20
|
+
face_indices = [int(p.split('/')[0]) - 1 for p in parts]
|
|
21
|
+
|
|
22
|
+
# Triangulate if more than 3 vertices
|
|
23
|
+
if len(face_indices) == 3:
|
|
24
|
+
self.faces.append(face_indices)
|
|
25
|
+
elif len(face_indices) > 3:
|
|
26
|
+
# Fan triangulation: v0,v1,v2 ; v0,v2,v3 ; v0,v3,v4 ...
|
|
27
|
+
v0 = face_indices[0]
|
|
28
|
+
for i in range(1, len(face_indices) - 1):
|
|
29
|
+
self.faces.append([v0, face_indices[i], face_indices[i+1]])
|
|
30
|
+
return self.vertices, self.faces
|
|
File without changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: gamengine3d
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.10.0
|
|
4
4
|
Summary: A Unity-like 3D engine in Python using pygame
|
|
5
5
|
Author-email: Samarth Javagal <samarthjavagal@gmail.com>
|
|
6
6
|
Classifier: Programming Language :: Python :: 3
|
|
@@ -78,3 +78,5 @@ This minimal PlayerController shows how to attach a script to an object and resp
|
|
|
78
78
|
You can run any built-in demo with ``gamengine3d demo <demo_name>`` and see all available options using ``gamengine3d demo -h``.
|
|
79
79
|
|
|
80
80
|
The 2d version of ```gamengine3d``` is out under ``gamengine2d``
|
|
81
|
+
|
|
82
|
+
Check out the full documentation for this package here: [Package Docs](https://sites.google.com/view/samarthsprojects/python-packages/gamengine3d)
|
|
@@ -7,6 +7,7 @@ gamengine3d/context.py
|
|
|
7
7
|
gamengine3d/engine.py
|
|
8
8
|
gamengine3d/game_objects.py
|
|
9
9
|
gamengine3d/helper.py
|
|
10
|
+
gamengine3d/obj_reader.py
|
|
10
11
|
gamengine3d.egg-info/PKG-INFO
|
|
11
12
|
gamengine3d.egg-info/SOURCES.txt
|
|
12
13
|
gamengine3d.egg-info/dependency_links.txt
|
|
@@ -15,9 +16,13 @@ gamengine3d.egg-info/requires.txt
|
|
|
15
16
|
gamengine3d.egg-info/top_level.txt
|
|
16
17
|
gamengine3d/examples/__init__.py
|
|
17
18
|
gamengine3d/examples/basics.py
|
|
19
|
+
gamengine3d/examples/from_obj.py
|
|
20
|
+
gamengine3d/examples/load_scene.py
|
|
18
21
|
gamengine3d/examples/messages_demo.py
|
|
19
22
|
gamengine3d/examples/player_movement.py
|
|
23
|
+
gamengine3d/examples/save_scene.py
|
|
20
24
|
gamengine3d/examples/the_everything_demo.py
|
|
25
|
+
gamengine3d/examples/objects/__init__.py
|
|
21
26
|
gamengine3d/examples/scripts/cylinder.py
|
|
22
27
|
gamengine3d/examples/scripts/message_receiver.py
|
|
23
28
|
gamengine3d/examples/scripts/messanger_script.py
|
|
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
|