gamengine3d 0.5.0__py3-none-any.whl
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/__init__.py +18 -0
- gamengine3d/assets/__init__.py +0 -0
- gamengine3d/assets/circular_crosshair.png +0 -0
- gamengine3d/assets/plus_sign_crosshair.png +0 -0
- gamengine3d/cli.py +83 -0
- gamengine3d/context.py +319 -0
- gamengine3d/engine.py +715 -0
- gamengine3d/examples/__init__.py +0 -0
- gamengine3d/examples/basics.py +66 -0
- gamengine3d/examples/parkour.py +285 -0
- gamengine3d/examples/player_movement.py +161 -0
- gamengine3d/examples/reactor.py +526 -0
- gamengine3d/examples/scripts/__init__.py +0 -0
- gamengine3d/examples/scripts/bouncer.py +18 -0
- gamengine3d/examples/scripts/breaker_coil.py +17 -0
- gamengine3d/examples/scripts/breaker_decoder.py +39 -0
- gamengine3d/examples/scripts/breaker_gate.py +23 -0
- gamengine3d/examples/scripts/breaker_shield.py +28 -0
- gamengine3d/examples/scripts/coin.py +12 -0
- gamengine3d/examples/scripts/collector.py +13 -0
- gamengine3d/examples/scripts/confetti.py +27 -0
- gamengine3d/examples/scripts/director.py +213 -0
- gamengine3d/examples/scripts/drone.py +21 -0
- gamengine3d/examples/scripts/ghost_pulse.py +24 -0
- gamengine3d/examples/scripts/horror_atmosphere.py +96 -0
- gamengine3d/examples/scripts/interact.py +45 -0
- gamengine3d/examples/scripts/orbiter.py +25 -0
- gamengine3d/examples/scripts/player_controller.py +165 -0
- gamengine3d/examples/scripts/pulse.py +22 -0
- gamengine3d/examples/scripts/reactor_core.py +29 -0
- gamengine3d/examples/scripts/spin.py +13 -0
- gamengine3d/examples/scripts/sway.py +19 -0
- gamengine3d/examples/scripts/void_guard.py +26 -0
- gamengine3d/examples/transparency.py +92 -0
- gamengine3d/game_objects.py +975 -0
- gamengine3d/helper.py +720 -0
- gamengine3d/obj_reader.py +29 -0
- gamengine3d/objects/0.obj +964 -0
- gamengine3d/objects/1.obj +48 -0
- gamengine3d/objects/2.obj +983 -0
- gamengine3d/objects/3.obj +1623 -0
- gamengine3d/objects/4.obj +163 -0
- gamengine3d/objects/5.obj +861 -0
- gamengine3d/objects/6.obj +1314 -0
- gamengine3d/objects/7.obj +70 -0
- gamengine3d/objects/8.obj +1932 -0
- gamengine3d/objects/9.obj +1458 -0
- gamengine3d/objects/A_capital.obj +124 -0
- gamengine3d/objects/B_capital.obj +1118 -0
- gamengine3d/objects/C_capital.obj +1082 -0
- gamengine3d/objects/D_capital.obj +548 -0
- gamengine3d/objects/E_capital.obj +128 -0
- gamengine3d/objects/F_capital.obj +108 -0
- gamengine3d/objects/G_capital.obj +1125 -0
- gamengine3d/objects/H_capital.obj +128 -0
- gamengine3d/objects/I_capital.obj +48 -0
- gamengine3d/objects/J_capital.obj +454 -0
- gamengine3d/objects/K_capital.obj +125 -0
- gamengine3d/objects/L_capital.obj +68 -0
- gamengine3d/objects/M_capital.obj +163 -0
- gamengine3d/objects/N_capital.obj +112 -0
- gamengine3d/objects/O_capital.obj +968 -0
- gamengine3d/objects/P_capital.obj +620 -0
- gamengine3d/objects/Q_capital.obj +1362 -0
- gamengine3d/objects/R_capital.obj +1036 -0
- gamengine3d/objects/S_capital.obj +1346 -0
- gamengine3d/objects/T_capital.obj +88 -0
- gamengine3d/objects/U_capital.obj +860 -0
- gamengine3d/objects/V_capital.obj +81 -0
- gamengine3d/objects/W_capital.obj +147 -0
- gamengine3d/objects/X_capital.obj +136 -0
- gamengine3d/objects/Y_capital.obj +103 -0
- gamengine3d/objects/Z_capital.obj +90 -0
- gamengine3d/objects/__init__.py +0 -0
- gamengine3d/objects/a.obj +1920 -0
- gamengine3d/objects/ampersand.obj +2186 -0
- gamengine3d/objects/asterisk.obj +2512 -0
- gamengine3d/objects/at.obj +2682 -0
- gamengine3d/objects/b.obj +1104 -0
- gamengine3d/objects/backslash.obj +48 -0
- gamengine3d/objects/brace_close.obj +1179 -0
- gamengine3d/objects/brace_open.obj +1179 -0
- gamengine3d/objects/bracket_close.obj +88 -0
- gamengine3d/objects/bracket_open.obj +88 -0
- gamengine3d/objects/c.obj +1082 -0
- gamengine3d/objects/caret.obj +81 -0
- gamengine3d/objects/colon.obj +912 -0
- gamengine3d/objects/comma.obj +675 -0
- gamengine3d/objects/d.obj +861 -0
- gamengine3d/objects/dash.obj +48 -0
- gamengine3d/objects/dollar.obj +1671 -0
- gamengine3d/objects/dot.obj +532 -0
- gamengine3d/objects/doublequote.obj +114 -0
- gamengine3d/objects/e.obj +1094 -0
- gamengine3d/objects/equals.obj +76 -0
- gamengine3d/objects/exclamation.obj +1336 -0
- gamengine3d/objects/f.obj +660 -0
- gamengine3d/objects/g.obj +2694 -0
- gamengine3d/objects/greater.obj +81 -0
- gamengine3d/objects/h.obj +628 -0
- gamengine3d/objects/hash.obj +332 -0
- gamengine3d/objects/i.obj +558 -0
- gamengine3d/objects/j.obj +824 -0
- gamengine3d/objects/k.obj +104 -0
- gamengine3d/objects/l.obj +48 -0
- gamengine3d/objects/less.obj +81 -0
- gamengine3d/objects/m.obj +1177 -0
- gamengine3d/objects/n.obj +628 -0
- gamengine3d/objects/o.obj +968 -0
- gamengine3d/objects/p.obj +861 -0
- gamengine3d/objects/paren_close.obj +554 -0
- gamengine3d/objects/paren_open.obj +554 -0
- gamengine3d/objects/percent.obj +1767 -0
- gamengine3d/objects/plus.obj +128 -0
- gamengine3d/objects/q.obj +861 -0
- gamengine3d/objects/question.obj +1306 -0
- gamengine3d/objects/r.obj +608 -0
- gamengine3d/objects/s.obj +1478 -0
- gamengine3d/objects/semicolon.obj +1185 -0
- gamengine3d/objects/singlequote.obj +70 -0
- gamengine3d/objects/slash.obj +48 -0
- gamengine3d/objects/t.obj +639 -0
- gamengine3d/objects/u.obj +618 -0
- gamengine3d/objects/underscore.obj +48 -0
- gamengine3d/objects/v.obj +81 -0
- gamengine3d/objects/w.obj +147 -0
- gamengine3d/objects/x.obj +136 -0
- gamengine3d/objects/y.obj +92 -0
- gamengine3d/objects/z.obj +90 -0
- gamengine3d/renderer.py +1390 -0
- gamengine3d/shaders/__init__.py +0 -0
- gamengine3d/shaders/default.frag +143 -0
- gamengine3d/shaders/default.vert +43 -0
- gamengine3d/shaders/depth.frag +2 -0
- gamengine3d/shaders/depth.vert +9 -0
- gamengine3d-0.5.0.dist-info/METADATA +177 -0
- gamengine3d-0.5.0.dist-info/RECORD +141 -0
- gamengine3d-0.5.0.dist-info/WHEEL +5 -0
- gamengine3d-0.5.0.dist-info/entry_points.txt +2 -0
- gamengine3d-0.5.0.dist-info/licenses/LICENSE.txt +21 -0
- gamengine3d-0.5.0.dist-info/top_level.txt +1 -0
gamengine3d/helper.py
ADDED
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import importlib.util
|
|
3
|
+
import math
|
|
4
|
+
import moderngl
|
|
5
|
+
import pygame
|
|
6
|
+
from pygame.locals import DOUBLEBUF, OPENGL
|
|
7
|
+
import importlib.resources as pkg_resources
|
|
8
|
+
import numpy as np
|
|
9
|
+
from pyrr import Matrix44
|
|
10
|
+
import os
|
|
11
|
+
import time
|
|
12
|
+
|
|
13
|
+
class classproperty(property):
|
|
14
|
+
def __get__(self, cls, owner):
|
|
15
|
+
return self.fget(owner)
|
|
16
|
+
|
|
17
|
+
class vector2d:
|
|
18
|
+
def __init__(self, x, y=None):
|
|
19
|
+
self.x = x
|
|
20
|
+
self.y = y
|
|
21
|
+
|
|
22
|
+
if y is None:
|
|
23
|
+
self.y = self.x
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def magnitude(self):
|
|
27
|
+
return math.sqrt(self.x ** 2 + self.y ** 2)
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def sqr_magnitude(self):
|
|
31
|
+
return self.x ** 2 + self.y ** 2
|
|
32
|
+
|
|
33
|
+
def normalize(self):
|
|
34
|
+
magnitude = self.magnitude
|
|
35
|
+
self.x = self.x / magnitude
|
|
36
|
+
self.y = self.y / magnitude
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def normalized(self):
|
|
40
|
+
magnitude = self.magnitude
|
|
41
|
+
return vector2d(self.x, self.y) / magnitude
|
|
42
|
+
|
|
43
|
+
def __add__(self, other):
|
|
44
|
+
if isinstance(other, vector2d):
|
|
45
|
+
return vector2d(self.x + other.x, self.y + other.y)
|
|
46
|
+
else:
|
|
47
|
+
return vector2d(self.x + other, self.y + other)
|
|
48
|
+
|
|
49
|
+
def __sub__(self, other):
|
|
50
|
+
if isinstance(other, vector2d):
|
|
51
|
+
return vector2d(self.x - other.x, self.y - other.y)
|
|
52
|
+
else:
|
|
53
|
+
return vector2d(self.x - other, self.y - other)
|
|
54
|
+
|
|
55
|
+
def __mul__(self, other):
|
|
56
|
+
if isinstance(other, vector2d):
|
|
57
|
+
return vector2d(self.x * other.x, self.y * other.y)
|
|
58
|
+
else:
|
|
59
|
+
return vector2d(self.x * other, self.y * other)
|
|
60
|
+
|
|
61
|
+
def __truediv__(self, other):
|
|
62
|
+
if isinstance(other, vector2d):
|
|
63
|
+
return vector2d(self.x / other.x, self.y / other.y)
|
|
64
|
+
else:
|
|
65
|
+
return vector2d(self.x / other, self.y / other)
|
|
66
|
+
|
|
67
|
+
def totuple(self):
|
|
68
|
+
return (self.x, self.y)
|
|
69
|
+
|
|
70
|
+
def __radd__(self, other):
|
|
71
|
+
return vector2d(self.x + other, self.y + other)
|
|
72
|
+
|
|
73
|
+
def __rsub__(self, other):
|
|
74
|
+
return vector2d(self.x - other, self.y - other)
|
|
75
|
+
|
|
76
|
+
def __rtruediv__(self, other):
|
|
77
|
+
return vector2d(self.x / other, self.y / other)
|
|
78
|
+
|
|
79
|
+
def __rmul__(self, other):
|
|
80
|
+
return vector2d(self.x * other, self.y * other)
|
|
81
|
+
|
|
82
|
+
@classmethod
|
|
83
|
+
def fromtuple(cls, tuple):
|
|
84
|
+
cls.x, cls.y = tuple
|
|
85
|
+
return vector2d(cls.x, cls.y)
|
|
86
|
+
|
|
87
|
+
def __repr__(self):
|
|
88
|
+
return f"vector2d({self.x}, {self.y})"
|
|
89
|
+
|
|
90
|
+
def copy(self):
|
|
91
|
+
return vector2d(self.x, self.y)
|
|
92
|
+
|
|
93
|
+
def __neg__(self):
|
|
94
|
+
return vector2d(-self.x, -self.y)
|
|
95
|
+
|
|
96
|
+
def __lt__(self, other):
|
|
97
|
+
return self.x < other.x and self.y < other.y
|
|
98
|
+
|
|
99
|
+
def __le__(self, other):
|
|
100
|
+
return self.x <= other.x and self.y <= other.y
|
|
101
|
+
|
|
102
|
+
def __eq__(self, other):
|
|
103
|
+
return self.x == other.x and self.y == other.y
|
|
104
|
+
|
|
105
|
+
def __ne__(self, other):
|
|
106
|
+
return self.x != other.x or self.y != other.y
|
|
107
|
+
|
|
108
|
+
def __gt__(self, other):
|
|
109
|
+
return self.x > other.x and self.y > other.y
|
|
110
|
+
|
|
111
|
+
def __ge__(self, other):
|
|
112
|
+
return self.x >= other.x and self.y >= other.y
|
|
113
|
+
|
|
114
|
+
@classproperty
|
|
115
|
+
def up(cls):
|
|
116
|
+
return cls(0, 1)
|
|
117
|
+
|
|
118
|
+
@classproperty
|
|
119
|
+
def down(cls):
|
|
120
|
+
return cls(0, -1)
|
|
121
|
+
|
|
122
|
+
@classproperty
|
|
123
|
+
def right(cls):
|
|
124
|
+
return cls(1, 0)
|
|
125
|
+
|
|
126
|
+
@classproperty
|
|
127
|
+
def left(cls):
|
|
128
|
+
return cls(-1, 0)
|
|
129
|
+
|
|
130
|
+
@classproperty
|
|
131
|
+
def one(cls):
|
|
132
|
+
return cls(1, 1)
|
|
133
|
+
|
|
134
|
+
@classproperty
|
|
135
|
+
def zero(cls):
|
|
136
|
+
return cls(0, 0)
|
|
137
|
+
|
|
138
|
+
class vector3d:
|
|
139
|
+
def __init__(self, x, y=None, z=None):
|
|
140
|
+
self.x = x
|
|
141
|
+
self.y = y
|
|
142
|
+
self.z = z
|
|
143
|
+
|
|
144
|
+
if y is None and z is None:
|
|
145
|
+
self.y = self.x
|
|
146
|
+
self.z = self.x
|
|
147
|
+
elif z is None:
|
|
148
|
+
self.z = 0.0
|
|
149
|
+
|
|
150
|
+
@property
|
|
151
|
+
def magnitude(self):
|
|
152
|
+
return math.sqrt(self.x**2 + self.y**2 + self.z**2)
|
|
153
|
+
|
|
154
|
+
@property
|
|
155
|
+
def sqr_magnitude(self):
|
|
156
|
+
return self.x**2 + self.y**2 + self.z**2
|
|
157
|
+
|
|
158
|
+
def normalize(self):
|
|
159
|
+
magnitude = self.magnitude
|
|
160
|
+
if magnitude != 0:
|
|
161
|
+
self.x /= magnitude
|
|
162
|
+
self.y /= magnitude
|
|
163
|
+
self.z /= magnitude
|
|
164
|
+
|
|
165
|
+
@property
|
|
166
|
+
def normalized(self):
|
|
167
|
+
magnitude = self.magnitude
|
|
168
|
+
if magnitude == 0:
|
|
169
|
+
return vector3d(0, 0, 0)
|
|
170
|
+
return vector3d(self.x / magnitude, self.y / magnitude, self.z / magnitude)
|
|
171
|
+
|
|
172
|
+
def dot(self, other):
|
|
173
|
+
return self.x * other.x + self.y * other.y + self.z * other.z
|
|
174
|
+
|
|
175
|
+
def cross(self, other):
|
|
176
|
+
return vector3d(
|
|
177
|
+
self.y * other.z - self.z * other.y,
|
|
178
|
+
self.z * other.x - self.x * other.z,
|
|
179
|
+
self.x * other.y - self.y * other.x,
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
def __add__(self, other):
|
|
183
|
+
if isinstance(other, vector3d):
|
|
184
|
+
return vector3d(self.x + other.x, self.y + other.y, self.z + other.z)
|
|
185
|
+
else:
|
|
186
|
+
return vector3d(self.x + other, self.y + other, self.z + other)
|
|
187
|
+
|
|
188
|
+
def __sub__(self, other):
|
|
189
|
+
if isinstance(other, vector3d):
|
|
190
|
+
return vector3d(self.x - other.x, self.y - other.y, self.z - other.z)
|
|
191
|
+
else:
|
|
192
|
+
return vector3d(self.x - other, self.y - other, self.z - other)
|
|
193
|
+
|
|
194
|
+
def __mul__(self, other):
|
|
195
|
+
if isinstance(other, vector3d):
|
|
196
|
+
return vector3d(self.x * other.x, self.y * other.y, self.z * other.z)
|
|
197
|
+
else:
|
|
198
|
+
return vector3d(self.x * other, self.y * other, self.z * other)
|
|
199
|
+
|
|
200
|
+
def __truediv__(self, other):
|
|
201
|
+
if isinstance(other, vector3d):
|
|
202
|
+
return vector3d(self.x / other.x, self.y / other.y, self.z / other.z)
|
|
203
|
+
else:
|
|
204
|
+
return vector3d(self.x / other, self.y / other, self.z / other)
|
|
205
|
+
|
|
206
|
+
def totuple(self):
|
|
207
|
+
return (self.x, self.y, self.z)
|
|
208
|
+
|
|
209
|
+
def __radd__(self, other):
|
|
210
|
+
return vector3d(self.x + other, self.y + other, self.z + other)
|
|
211
|
+
|
|
212
|
+
def __rsub__(self, other):
|
|
213
|
+
return vector3d(self.x - other, self.y - other, self.z - other)
|
|
214
|
+
|
|
215
|
+
def __rtruediv__(self, other):
|
|
216
|
+
return vector3d(self.x / other, self.y / other, self.z / other)
|
|
217
|
+
|
|
218
|
+
def __rmul__(self, other):
|
|
219
|
+
return vector3d(self.x * other, self.y * other, self.z * other)
|
|
220
|
+
|
|
221
|
+
@classmethod
|
|
222
|
+
def fromtuple(cls, tuple):
|
|
223
|
+
return cls(*tuple)
|
|
224
|
+
|
|
225
|
+
def __repr__(self):
|
|
226
|
+
return f"vector3d({self.x}, {self.y}, {self.z})"
|
|
227
|
+
|
|
228
|
+
def copy(self):
|
|
229
|
+
return vector3d(self.x, self.y, self.z)
|
|
230
|
+
|
|
231
|
+
def __neg__(self):
|
|
232
|
+
return vector3d(-self.x, -self.y, -self.z)
|
|
233
|
+
|
|
234
|
+
@classproperty
|
|
235
|
+
def up(cls):
|
|
236
|
+
return cls(0, 1, 0)
|
|
237
|
+
|
|
238
|
+
@classproperty
|
|
239
|
+
def down(cls):
|
|
240
|
+
return cls(0, -1, 0)
|
|
241
|
+
|
|
242
|
+
@classproperty
|
|
243
|
+
def right(cls):
|
|
244
|
+
return cls(1, 0, 0)
|
|
245
|
+
|
|
246
|
+
@classproperty
|
|
247
|
+
def left(cls):
|
|
248
|
+
return cls(-1, 0, 0)
|
|
249
|
+
|
|
250
|
+
@classproperty
|
|
251
|
+
def forward(cls):
|
|
252
|
+
return cls(0, 0, 1)
|
|
253
|
+
|
|
254
|
+
@classproperty
|
|
255
|
+
def back(cls):
|
|
256
|
+
return cls(0, 0, -1)
|
|
257
|
+
|
|
258
|
+
@classproperty
|
|
259
|
+
def one(cls):
|
|
260
|
+
return cls(1, 1, 1)
|
|
261
|
+
|
|
262
|
+
@classproperty
|
|
263
|
+
def zero(cls):
|
|
264
|
+
return cls(0, 0, 0)
|
|
265
|
+
|
|
266
|
+
class Color:
|
|
267
|
+
def __init__(self, r, g, b):
|
|
268
|
+
self.r = r
|
|
269
|
+
self.g = g
|
|
270
|
+
self.b = b
|
|
271
|
+
|
|
272
|
+
@staticmethod
|
|
273
|
+
def RGB(r, g, b): return Color(r, g, b)
|
|
274
|
+
|
|
275
|
+
@staticmethod
|
|
276
|
+
def hex(hex_string):
|
|
277
|
+
if not hex_string.startswith("#"): raise Exception("String must start with #")
|
|
278
|
+
if not len(hex_string) == 7: raise Exception("String must contain 7 characters")
|
|
279
|
+
r = int(hex_string[1:3], 16)
|
|
280
|
+
g = int(hex_string[3:5], 16)
|
|
281
|
+
b = int(hex_string[5:7], 16)
|
|
282
|
+
return Color(r, g, b)
|
|
283
|
+
|
|
284
|
+
def __eq__(self, other):
|
|
285
|
+
if self.r == other.r and self.g == other.g and self.b == other.b:
|
|
286
|
+
return True
|
|
287
|
+
return False
|
|
288
|
+
|
|
289
|
+
def to_hex(self):
|
|
290
|
+
if not all(0 <= x <= 255 for x in (self.r, self.g, self.b)):
|
|
291
|
+
raise ValueError("RGB values must be in the range 0-255")
|
|
292
|
+
return "#{:02X}{:02X}{:02X}".format(self.r, self.g, self.b)
|
|
293
|
+
|
|
294
|
+
def to_rgb(self):
|
|
295
|
+
return self.r, self.g, self.b
|
|
296
|
+
|
|
297
|
+
@classproperty
|
|
298
|
+
def black(cls):
|
|
299
|
+
return cls(0, 0, 0)
|
|
300
|
+
|
|
301
|
+
@classproperty
|
|
302
|
+
def white(cls):
|
|
303
|
+
return cls(255, 255, 255)
|
|
304
|
+
|
|
305
|
+
@classproperty
|
|
306
|
+
def red(cls): return cls(255, 0, 0)
|
|
307
|
+
|
|
308
|
+
@classproperty
|
|
309
|
+
def green(cls): return cls(0, 255, 0)
|
|
310
|
+
|
|
311
|
+
@classproperty
|
|
312
|
+
def blue(cls): return cls(0, 0, 255)
|
|
313
|
+
|
|
314
|
+
@classproperty
|
|
315
|
+
def yellow(cls): return cls(255, 255, 0)
|
|
316
|
+
|
|
317
|
+
@classproperty
|
|
318
|
+
def cyan(cls): return cls(0, 255, 255)
|
|
319
|
+
|
|
320
|
+
@classproperty
|
|
321
|
+
def magenta(cls): return cls(255, 0, 255)
|
|
322
|
+
|
|
323
|
+
@classproperty
|
|
324
|
+
def grey(cls): return cls(33, 33, 33)
|
|
325
|
+
|
|
326
|
+
@classproperty
|
|
327
|
+
def light_grey(cls): return cls(80, 80, 80)
|
|
328
|
+
|
|
329
|
+
@classproperty
|
|
330
|
+
def light_red(cls): return cls(253, 89, 111)
|
|
331
|
+
|
|
332
|
+
@classproperty
|
|
333
|
+
def light_green(cls): return cls(0, 190, 160)
|
|
334
|
+
|
|
335
|
+
@classproperty
|
|
336
|
+
def light_yellow(cls): return cls(254, 208, 95)
|
|
337
|
+
|
|
338
|
+
@classproperty
|
|
339
|
+
def orange(cls): return cls(254, 166, 93)
|
|
340
|
+
|
|
341
|
+
@classproperty
|
|
342
|
+
def light_blue(cls): return cls(109, 154, 218)
|
|
343
|
+
|
|
344
|
+
class Light:
|
|
345
|
+
def __init__(self, position: vector3d, color: Color, intensity: float = 1.0, show=False, name="Light"):
|
|
346
|
+
self.position = position
|
|
347
|
+
self.color = color
|
|
348
|
+
self.intensity = intensity
|
|
349
|
+
self.show = show
|
|
350
|
+
self.name = name
|
|
351
|
+
|
|
352
|
+
def serialize(self):
|
|
353
|
+
return {
|
|
354
|
+
"position": self.position.totuple(),
|
|
355
|
+
"color": self.color.to_rgb(),
|
|
356
|
+
"intensity": self.intensity,
|
|
357
|
+
"show": self.show,
|
|
358
|
+
"name": self.name,
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
@staticmethod
|
|
362
|
+
def deserialize(data):
|
|
363
|
+
position = data["position"]
|
|
364
|
+
color = data["color"]
|
|
365
|
+
intensity = data["intensity"]
|
|
366
|
+
show = data["show"]
|
|
367
|
+
name = data["name"]
|
|
368
|
+
|
|
369
|
+
return Light(position=vector3d.fromtuple(position), color=Color.RGB(*color), intensity=intensity, show=show, name=name)
|
|
370
|
+
|
|
371
|
+
class DirectionalLight:
|
|
372
|
+
def __init__(
|
|
373
|
+
self,
|
|
374
|
+
rotation: vector3d, # rotation defines direction
|
|
375
|
+
color: Color,
|
|
376
|
+
intensity: float = 1.0,
|
|
377
|
+
position: vector3d = None, # directional lights can have a position
|
|
378
|
+
show: bool = False,
|
|
379
|
+
name: str = "DirectionalLight"
|
|
380
|
+
):
|
|
381
|
+
if position is None:
|
|
382
|
+
position = vector3d.zero
|
|
383
|
+
self.rotation = rotation
|
|
384
|
+
self.position = position
|
|
385
|
+
self.color = color
|
|
386
|
+
self.intensity = intensity
|
|
387
|
+
self.show = show
|
|
388
|
+
self.name = name
|
|
389
|
+
|
|
390
|
+
# compute forward direction from rotation
|
|
391
|
+
self.direction = self._compute_direction()
|
|
392
|
+
|
|
393
|
+
def _compute_direction(self):
|
|
394
|
+
"""Turn rotation (Euler angles in degrees) into a normalized direction vector."""
|
|
395
|
+
|
|
396
|
+
# Convert to radians
|
|
397
|
+
rx = math.radians(self.rotation.x)
|
|
398
|
+
ry = math.radians(self.rotation.y)
|
|
399
|
+
rz = math.radians(self.rotation.z)
|
|
400
|
+
|
|
401
|
+
# Rotation matrices in ZYX order (standard for camera/light)
|
|
402
|
+
cz, sz = math.cos(rz), math.sin(rz)
|
|
403
|
+
cy, sy = math.cos(ry), math.sin(ry)
|
|
404
|
+
cx, sx = math.cos(rx), math.sin(rx)
|
|
405
|
+
|
|
406
|
+
# Forward vector through rotation matrix
|
|
407
|
+
# This matches right-handed forward = +Z convention
|
|
408
|
+
m00 = cy * cz
|
|
409
|
+
m01 = cz * sx * sy - cx * sz
|
|
410
|
+
m02 = sx * sz + cx * cz * sy
|
|
411
|
+
|
|
412
|
+
# Forward direction = (m02, m12, m22) depending on convention
|
|
413
|
+
# You use Z-forward, so:
|
|
414
|
+
forward = vector3d(m02, -(sx * cy), cy * cx)
|
|
415
|
+
|
|
416
|
+
return forward.normalized
|
|
417
|
+
|
|
418
|
+
# ---------------------- Serialization -------------------------
|
|
419
|
+
|
|
420
|
+
def serialize(self):
|
|
421
|
+
return {
|
|
422
|
+
"rotation": self.rotation.totuple(),
|
|
423
|
+
"position": self.position.totuple(),
|
|
424
|
+
"color": self.color.to_rgb(),
|
|
425
|
+
"intensity": self.intensity,
|
|
426
|
+
"show": self.show,
|
|
427
|
+
"name": self.name,
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
@staticmethod
|
|
431
|
+
def deserialize(data):
|
|
432
|
+
rotation = vector3d.fromtuple(data["rotation"])
|
|
433
|
+
position = vector3d.fromtuple(data["position"])
|
|
434
|
+
color = Color.RGB(*data["color"])
|
|
435
|
+
intensity = data["intensity"]
|
|
436
|
+
show = data["show"]
|
|
437
|
+
name = data["name"]
|
|
438
|
+
|
|
439
|
+
return DirectionalLight(
|
|
440
|
+
rotation=rotation,
|
|
441
|
+
position=position,
|
|
442
|
+
color=color,
|
|
443
|
+
intensity=intensity,
|
|
444
|
+
show=show,
|
|
445
|
+
name=name
|
|
446
|
+
)
|
|
447
|
+
|
|
448
|
+
class Script:
|
|
449
|
+
def __init__(self, obj, script_path, context):
|
|
450
|
+
self.obj = obj
|
|
451
|
+
self.context = context
|
|
452
|
+
self.script_path = script_path
|
|
453
|
+
self.module = None
|
|
454
|
+
self.cls = None
|
|
455
|
+
self.instance = None
|
|
456
|
+
self.load(script_path)
|
|
457
|
+
|
|
458
|
+
def load(self, path):
|
|
459
|
+
if not os.path.exists(path):
|
|
460
|
+
print(f"[Script] File not found: {path}")
|
|
461
|
+
return
|
|
462
|
+
module_name = os.path.splitext(os.path.basename(path))[0]
|
|
463
|
+
spec = importlib.util.spec_from_file_location(module_name, path)
|
|
464
|
+
module = importlib.util.module_from_spec(spec)
|
|
465
|
+
sys.modules[module_name] = module
|
|
466
|
+
try:
|
|
467
|
+
spec.loader.exec_module(module)
|
|
468
|
+
except Exception as e:
|
|
469
|
+
print(f"[Script] Error loading module {path}: {e}")
|
|
470
|
+
return
|
|
471
|
+
self.module = module
|
|
472
|
+
|
|
473
|
+
# Class is PascalCase version of file name
|
|
474
|
+
class_name = ''.join(word.capitalize() for word in module_name.split('_'))
|
|
475
|
+
if hasattr(module, class_name):
|
|
476
|
+
self.cls = getattr(module, class_name)
|
|
477
|
+
else:
|
|
478
|
+
print(f"[Script] No class '{class_name}' found in {path}")
|
|
479
|
+
|
|
480
|
+
def init_instance(self):
|
|
481
|
+
if self.cls is None or self.instance is not None:
|
|
482
|
+
return
|
|
483
|
+
try:
|
|
484
|
+
self.instance = self.cls(self.obj, self.context)
|
|
485
|
+
except Exception as e:
|
|
486
|
+
print(f"[Script] Failed to instantiate script for {self.obj.name if hasattr(self.obj, 'name') else 'Engine'}: {e}")
|
|
487
|
+
self.instance = None
|
|
488
|
+
|
|
489
|
+
if self.instance and hasattr(self.instance, "start"):
|
|
490
|
+
try:
|
|
491
|
+
self.instance.start()
|
|
492
|
+
except Exception:
|
|
493
|
+
pass
|
|
494
|
+
|
|
495
|
+
def update(self, dt):
|
|
496
|
+
if self.instance is None:
|
|
497
|
+
return
|
|
498
|
+
self.instance.update(dt)
|
|
499
|
+
|
|
500
|
+
def run(self, func_name):
|
|
501
|
+
if self.instance is None:
|
|
502
|
+
return
|
|
503
|
+
|
|
504
|
+
if hasattr(self.instance, func_name):
|
|
505
|
+
func = getattr(self.instance, func_name)
|
|
506
|
+
func()
|
|
507
|
+
|
|
508
|
+
class EngineError(Exception):
|
|
509
|
+
pass
|
|
510
|
+
|
|
511
|
+
class Message:
|
|
512
|
+
def __init__(self, message, sender):
|
|
513
|
+
self.message = message
|
|
514
|
+
self.sender = sender
|
|
515
|
+
self.time = time.time()
|
|
516
|
+
|
|
517
|
+
def __repr__(self):
|
|
518
|
+
return f"Message({self.message=}, {self.sender=}, {self.time=})"
|
|
519
|
+
|
|
520
|
+
class RigidBody:
|
|
521
|
+
def __init__(self, mass=1.0, bounce=0.3, gravity=True):
|
|
522
|
+
self.mass = mass
|
|
523
|
+
self.bounce = bounce
|
|
524
|
+
self.gravity = gravity
|
|
525
|
+
|
|
526
|
+
self.velocity = vector3d.zero
|
|
527
|
+
self.forces = vector3d.zero
|
|
528
|
+
|
|
529
|
+
def add_force(self, f):
|
|
530
|
+
self.forces += f
|
|
531
|
+
|
|
532
|
+
class ShadowVolume:
|
|
533
|
+
def __init__(self, minx, maxx, miny, maxy, minz, maxz):
|
|
534
|
+
self.minx = minx
|
|
535
|
+
self.maxx = maxx
|
|
536
|
+
self.miny = miny
|
|
537
|
+
self.maxy = maxy
|
|
538
|
+
self.minz = minz
|
|
539
|
+
self.maxz = maxz
|
|
540
|
+
|
|
541
|
+
def get_forward(rotation):
|
|
542
|
+
yaw = math.radians(rotation.z)
|
|
543
|
+
fx = -math.sin(yaw)
|
|
544
|
+
fy = 0
|
|
545
|
+
fz = math.cos(yaw)
|
|
546
|
+
return vector3d(fx, fy, fz)
|
|
547
|
+
|
|
548
|
+
class AreaLight:
|
|
549
|
+
def __init__(self, shape: str, params: dict, color: Color, intensity: float = 1.0, samples: int = 4, name: str = "AreaLight"):
|
|
550
|
+
self.shape = shape
|
|
551
|
+
self.params = params
|
|
552
|
+
self.color = color
|
|
553
|
+
self.intensity = intensity
|
|
554
|
+
self.samples = max(1, int(samples))
|
|
555
|
+
self.name = name
|
|
556
|
+
self.is_area = True
|
|
557
|
+
|
|
558
|
+
@staticmethod
|
|
559
|
+
def _lerp(a: vector3d, b: vector3d, t: float) -> vector3d:
|
|
560
|
+
return a * (1.0 - t) + b * t
|
|
561
|
+
|
|
562
|
+
def sample_points(self):
|
|
563
|
+
"""Return a list of vector3d sample positions on the surface."""
|
|
564
|
+
if self.shape == 'plane':
|
|
565
|
+
corners = self.params.get('corners')
|
|
566
|
+
if not corners or len(corners) < 4:
|
|
567
|
+
raise ValueError("AreaLight plane requires 'corners' list of 4 vector3d")
|
|
568
|
+
p1, p2, p3, p4 = corners
|
|
569
|
+
n = int(math.sqrt(self.samples))
|
|
570
|
+
if n * n < self.samples:
|
|
571
|
+
n += 1
|
|
572
|
+
points = []
|
|
573
|
+
count = 0
|
|
574
|
+
for i in range(n):
|
|
575
|
+
for j in range(n):
|
|
576
|
+
if count >= self.samples:
|
|
577
|
+
break
|
|
578
|
+
u = (i + 0.5) / n
|
|
579
|
+
v = (j + 0.5) / n
|
|
580
|
+
a = self._lerp(p1, p2, u)
|
|
581
|
+
b = self._lerp(p4, p3, u)
|
|
582
|
+
pos = self._lerp(a, b, v)
|
|
583
|
+
points.append(pos)
|
|
584
|
+
count += 1
|
|
585
|
+
if count >= self.samples:
|
|
586
|
+
break
|
|
587
|
+
return points
|
|
588
|
+
|
|
589
|
+
elif self.shape == 'sphere':
|
|
590
|
+
center = self.params.get('center')
|
|
591
|
+
radius = self.params.get('radius')
|
|
592
|
+
if center is None or radius is None:
|
|
593
|
+
raise ValueError("AreaLight sphere requires 'center' and 'radius'")
|
|
594
|
+
pts = []
|
|
595
|
+
n = self.samples
|
|
596
|
+
for i in range(n):
|
|
597
|
+
z = 1.0 - 2.0 * (i + 0.5) / n
|
|
598
|
+
r = math.sqrt(max(0.0, 1.0 - z * z))
|
|
599
|
+
phi = 2.0 * math.pi * ((i * 0.618033988749895) % 1.0)
|
|
600
|
+
x = r * math.cos(phi)
|
|
601
|
+
y = r * math.sin(phi)
|
|
602
|
+
pos = vector3d(center.x + radius * x, center.y + radius * z, center.z + radius * y)
|
|
603
|
+
pts.append(pos)
|
|
604
|
+
return pts
|
|
605
|
+
|
|
606
|
+
elif self.shape == 'cylinder':
|
|
607
|
+
center = self.params.get('center')
|
|
608
|
+
height = self.params.get('height')
|
|
609
|
+
radius = self.params.get('radius')
|
|
610
|
+
rotation = self.params.get('rotation', vector3d.zero)
|
|
611
|
+
if center is None or height is None or radius is None:
|
|
612
|
+
raise ValueError("AreaLight cylinder requires 'center', 'height', and 'radius'")
|
|
613
|
+
pts = []
|
|
614
|
+
n_axial = max(1, int(math.sqrt(self.samples)))
|
|
615
|
+
n_angular = max(1, int(math.ceil(self.samples / n_axial)))
|
|
616
|
+
count = 0
|
|
617
|
+
for i in range(n_axial):
|
|
618
|
+
y = -height / 2.0 + (i + 0.5) * (height / n_axial)
|
|
619
|
+
for j in range(n_angular):
|
|
620
|
+
if count >= self.samples:
|
|
621
|
+
break
|
|
622
|
+
theta = 2.0 * math.pi * (j + 0.5) / n_angular
|
|
623
|
+
x = radius * math.cos(theta)
|
|
624
|
+
z = radius * math.sin(theta)
|
|
625
|
+
local = vector3d(x, y, z)
|
|
626
|
+
# apply rotation (Euler degrees) if provided
|
|
627
|
+
rx, ry, rz = math.radians(rotation.x), math.radians(rotation.y), math.radians(rotation.z)
|
|
628
|
+
cx, sx = math.cos(rx), math.sin(rx)
|
|
629
|
+
cy, sy = math.cos(ry), math.sin(ry)
|
|
630
|
+
cz, sz = math.cos(rz), math.sin(rz)
|
|
631
|
+
Rx = [[1,0,0],[0,cx,-sx],[0,sx,cx]]
|
|
632
|
+
Ry = [[cy,0,sy],[0,1,0],[-sy,0,cy]]
|
|
633
|
+
Rz = [[cz,-sz,0],[sz,cz,0],[0,0,1]]
|
|
634
|
+
def mat_mult(A, B):
|
|
635
|
+
return [[sum(A[i][k]*B[k][j] for k in range(3)) for j in range(3)] for i in range(3)]
|
|
636
|
+
R = mat_mult(mat_mult(Rz, Ry), Rx)
|
|
637
|
+
lx = R[0][0]*local.x + R[0][1]*local.y + R[0][2]*local.z
|
|
638
|
+
ly = R[1][0]*local.x + R[1][1]*local.y + R[1][2]*local.z
|
|
639
|
+
lz = R[2][0]*local.x + R[2][1]*local.y + R[2][2]*local.z
|
|
640
|
+
pos = vector3d(center.x + lx, center.y + ly, center.z + lz)
|
|
641
|
+
pts.append(pos)
|
|
642
|
+
count += 1
|
|
643
|
+
if count >= self.samples:
|
|
644
|
+
break
|
|
645
|
+
return pts
|
|
646
|
+
|
|
647
|
+
else:
|
|
648
|
+
raise ValueError(f"Unsupported area light shape: {self.shape}")
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
def ray_intersect_aabb(ray_origin, ray_dir, aabb_min, aabb_max):
|
|
652
|
+
"""
|
|
653
|
+
Ray-AABB slab intersection.
|
|
654
|
+
ray_origin, ray_dir, aabb_min, aabb_max are vector3d.
|
|
655
|
+
Returns entry t (distance along ray_dir) or None if no hit.
|
|
656
|
+
"""
|
|
657
|
+
tmin = -float("inf")
|
|
658
|
+
tmax = float("inf")
|
|
659
|
+
|
|
660
|
+
# For each axis
|
|
661
|
+
for comp in ("x", "y", "z"):
|
|
662
|
+
ro = float(getattr(ray_origin, comp))
|
|
663
|
+
rd = float(getattr(ray_dir, comp))
|
|
664
|
+
amin = float(getattr(aabb_min, comp))
|
|
665
|
+
amax = float(getattr(aabb_max, comp))
|
|
666
|
+
|
|
667
|
+
if abs(rd) < 1e-9:
|
|
668
|
+
# Ray parallel to slab: if origin not within slab, no hit
|
|
669
|
+
if ro < amin or ro > amax:
|
|
670
|
+
return None
|
|
671
|
+
else:
|
|
672
|
+
# parallel and inside slab: no change to tmin/tmax
|
|
673
|
+
continue
|
|
674
|
+
|
|
675
|
+
# compute intersection t values for the two slab planes
|
|
676
|
+
t1 = (amin - ro) / rd
|
|
677
|
+
t2 = (amax - ro) / rd
|
|
678
|
+
t_near = min(t1, t2)
|
|
679
|
+
t_far = max(t1, t2)
|
|
680
|
+
|
|
681
|
+
tmin = max(tmin, t_near)
|
|
682
|
+
tmax = min(tmax, t_far)
|
|
683
|
+
|
|
684
|
+
if tmin > tmax:
|
|
685
|
+
return None
|
|
686
|
+
|
|
687
|
+
# tmax < 0 means AABB is behind the ray origin
|
|
688
|
+
if tmax < 0:
|
|
689
|
+
return None
|
|
690
|
+
|
|
691
|
+
# pick the first intersection in front of the origin
|
|
692
|
+
hit_t = tmin if tmin >= 0 else tmax
|
|
693
|
+
return hit_t
|
|
694
|
+
|
|
695
|
+
def ray_intersects_triangle(orig, dir, v0, v1, v2):
|
|
696
|
+
EPSILON = 1e-6
|
|
697
|
+
|
|
698
|
+
edge1 = v1 - v0
|
|
699
|
+
edge2 = v2 - v0
|
|
700
|
+
h = dir.cross(edge2)
|
|
701
|
+
a = edge1.dot(h)
|
|
702
|
+
|
|
703
|
+
if -EPSILON < a < EPSILON:
|
|
704
|
+
return False # Ray parallel to triangle
|
|
705
|
+
|
|
706
|
+
f = 1.0 / a
|
|
707
|
+
s = orig - v0
|
|
708
|
+
u = f * s.dot(h)
|
|
709
|
+
|
|
710
|
+
if u < 0.0 or u > 1.0:
|
|
711
|
+
return False
|
|
712
|
+
|
|
713
|
+
q = s.cross(edge1)
|
|
714
|
+
v = f * dir.dot(q)
|
|
715
|
+
|
|
716
|
+
if v < 0.0 or u + v > 1.0:
|
|
717
|
+
return False
|
|
718
|
+
|
|
719
|
+
t = f * edge2.dot(q)
|
|
720
|
+
return t > EPSILON
|