gamengine3d 1.0.1__tar.gz → 1.1.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gamengine3d
3
- Version: 1.0.1
3
+ Version: 1.1.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
@@ -9,6 +9,7 @@ class Functions:
9
9
  self.draw_cylinder = lambda pos=vector3d.zero, length=2, radius=.5, color=Color.light_yellow, segments=32: None
10
10
  self.add_light = lambda light: None
11
11
  self.add_object = lambda obj: None
12
+ self.is_colliding_pos = lambda name, pos: None
12
13
  self.is_colliding = lambda name1, name2: None
13
14
  self.get_game_object = lambda name: None
14
15
  self.remove_object = lambda name: None
@@ -46,6 +46,7 @@ class Engine:
46
46
  self.context.functions.is_colliding = self.is_colliding
47
47
  self.context.functions.remove_light = self.remove_light
48
48
  self.context.functions.remove_object = self.remove_object
49
+ self.context.functions.is_colliding_pos = self.is_colliding_pos
49
50
 
50
51
  self.context.game_objects = self.gameobjects
51
52
  self.context.engine = self
@@ -80,8 +81,13 @@ class Engine:
80
81
  if self.resizable and event.type == pygame.VIDEORESIZE:
81
82
  self.width, self.height = event.w, event.h
82
83
  self.renderer.resize(self.width, self.height)
84
+
83
85
  self.camera.aspect_ratio = self.width / self.height
84
86
 
87
+ old_pos = self.camera.position.copy()
88
+ self.camera.move_to(vector3d.one)
89
+ self.camera.move_to(old_pos)
90
+
85
91
  if dynamic_view:
86
92
  if event.type == pygame.MOUSEBUTTONDOWN:
87
93
  if event.button in (1, 3): # Left or right
@@ -195,6 +201,13 @@ class Engine:
195
201
 
196
202
  return False
197
203
 
204
+ def is_colliding_pos(self, name, pos):
205
+ obj = self.get_object(name)
206
+ if not isinstance(obj, Cuboid):
207
+ raise NotImplementedError(f"Cylinder And Sphere Collision Not Implemented")
208
+
209
+ return obj.is_point_in(pos)
210
+
198
211
  def draw_cuboid(self, pos: vector3d=vector3d.zero, size: vector3d=vector3d(2), rotation: vector3d=vector3d.zero, color: Color = Color.light_blue):
199
212
  faces = [
200
213
  [0, 1, 3, 2],
@@ -232,70 +232,74 @@ class Renderer:
232
232
  pygame.init()
233
233
  self.screen = pygame.display.set_mode((width, height), flags)
234
234
  self.ctx = moderngl.create_context()
235
+ self.samples = 4
236
+ self.color_tex = self.ctx.texture((width, height), 3, samples=self.samples)
237
+ self.depth_rbo = self.ctx.depth_renderbuffer((width, height), samples=self.samples)
238
+ self.fbo = self.ctx.framebuffer(color_attachments=[self.color_tex], depth_attachment=self.depth_rbo)
235
239
  self.lights = []
236
240
  self.width = width
237
241
  self.height = height
238
-
239
242
  self.ambient = 0.2
240
-
241
243
  self.ctx.enable(moderngl.DEPTH_TEST)
242
-
243
244
  self.prog = self._load_shader("default.vert", "default.frag")
244
245
  self.prog["model"].write(np.eye(4, dtype="f4").tobytes())
245
-
246
246
  self.proj = Matrix44.perspective_projection(60.0, width / height, 0.1, 100.0)
247
247
  self.view = Matrix44.look_at(
248
248
  eye=(3.0, 2.0, 3.0),
249
249
  target=(0.0, 0.0, 0.0),
250
250
  up=(0.0, 1.0, 0.0)
251
251
  )
252
-
253
252
  self.mvp = self.proj * self.view
254
253
  self.prog["mvp"].write(self.mvp.astype("f4").tobytes())
255
254
  self.prog["ambient"].value = self.ambient
256
-
257
255
  def resize(self, width, height):
258
256
  self.width = width
259
257
  self.height = height
260
- pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF | (
261
- pygame.RESIZABLE if self.width and self.height else 0))
258
+ pygame.display.set_mode(
259
+ (width, height),
260
+ pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE
261
+ )
262
+
263
+ self.ctx = moderngl.create_context()
262
264
  self.ctx.viewport = (0, 0, width, height)
265
+ self.color_tex = self.ctx.texture((width, height), 3, samples=self.samples)
266
+ self.depth_rbo = self.ctx.depth_renderbuffer((width, height), samples=self.samples)
267
+ self.fbo = self.ctx.framebuffer(color_attachments=[self.color_tex], depth_attachment=self.depth_rbo)
263
268
 
264
- self.proj = Matrix44.perspective_projection(60.0, width / height, 0.1, 100.0)
269
+ self.prog = self._load_shader("default.vert", "default.frag")
270
+ self.prog["model"].write(np.eye(4, dtype="f4").tobytes())
271
+
272
+ aspect = width / height
273
+ self.proj = Matrix44.perspective_projection(60.0, aspect, 0.1, 100.0)
265
274
  self.mvp = self.proj * self.view
266
275
  self.prog["mvp"].write(self.mvp.astype("f4").tobytes())
276
+ self.prog["ambient"].value = self.ambient
267
277
 
278
+ self._send_lights()
268
279
  def add_light(self, light: Light):
269
280
  self.lights.append(light)
270
-
271
281
  def _compute_normal(self, p1, p2, p3):
272
282
  u = p2 - p1
273
283
  v = p3 - p1
274
284
  return u.cross(v).normalized
275
-
276
285
  def render_plane(self, p1, p2, p3, color):
277
286
  normal = self._compute_normal(p1, p2, p3)
278
-
279
287
  vertices = np.array([
280
288
  *p1.totuple(), *color.to_rgb(), *normal.totuple(),
281
289
  *p2.totuple(), *color.to_rgb(), *normal.totuple(),
282
290
  *p3.totuple(), *color.to_rgb(), *normal.totuple()
283
291
  ], dtype="f4")
284
-
285
292
  vbo = self.ctx.buffer(vertices.tobytes())
286
293
  vao = self.ctx.vertex_array(
287
294
  self.prog,
288
295
  [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
289
296
  )
290
-
291
297
  self._send_lights()
292
298
  vao.render()
293
299
  vao.release()
294
300
  vbo.release()
295
-
296
301
  def render_quad(self, p1, p2, p3, p4, color):
297
302
  normal = self._compute_normal(p1, p2, p3)
298
-
299
303
  vertices = np.array([
300
304
  *p1.totuple(), *color.to_rgb(), *normal.totuple(),
301
305
  *p2.totuple(), *color.to_rgb(), *normal.totuple(),
@@ -304,145 +308,114 @@ class Renderer:
304
308
  *p3.totuple(), *color.to_rgb(), *normal.totuple(),
305
309
  *p4.totuple(), *color.to_rgb(), *normal.totuple()
306
310
  ], dtype="f4")
307
-
308
311
  vbo = self.ctx.buffer(vertices.tobytes())
309
312
  vao = self.ctx.vertex_array(
310
313
  self.prog,
311
314
  [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
312
315
  )
313
-
314
316
  self._send_lights()
315
317
  vao.render()
316
318
  vao.release()
317
319
  vbo.release()
318
-
319
320
  def render_sphere(self, center, radius, color, segments=32, rings=16):
320
321
  vertices = []
321
322
  normals = []
322
-
323
323
  for i in range(rings):
324
324
  theta1 = math.pi * i / rings
325
325
  theta2 = math.pi * (i + 1) / rings
326
-
327
326
  for j in range(segments):
328
327
  phi1 = 2 * math.pi * j / segments
329
328
  phi2 = 2 * math.pi * (j + 1) / segments
330
-
331
329
  p1 = vector3d(
332
330
  radius * math.sin(theta1) * math.cos(phi1),
333
331
  radius * math.cos(theta1),
334
332
  radius * math.sin(theta1) * math.sin(phi1)
335
333
  ) + center
336
-
337
334
  p2 = vector3d(
338
335
  radius * math.sin(theta2) * math.cos(phi1),
339
336
  radius * math.cos(theta2),
340
337
  radius * math.sin(theta2) * math.sin(phi1)
341
338
  ) + center
342
-
343
339
  p3 = vector3d(
344
340
  radius * math.sin(theta2) * math.cos(phi2),
345
341
  radius * math.cos(theta2),
346
342
  radius * math.sin(theta2) * math.sin(phi2)
347
343
  ) + center
348
-
349
344
  p4 = vector3d(
350
345
  radius * math.sin(theta1) * math.cos(phi2),
351
346
  radius * math.cos(theta1),
352
347
  radius * math.sin(theta1) * math.sin(phi2)
353
348
  ) + center
354
-
355
349
  for tri in [(p1, p2, p3), (p1, p3, p4)]:
356
350
  n1 = (tri[0] - center).normalized
357
351
  n2 = (tri[1] - center).normalized
358
352
  n3 = (tri[2] - center).normalized
359
-
360
353
  vertices.extend([
361
354
  *tri[0].totuple(), *color.to_rgb(), *n1.totuple(),
362
355
  *tri[1].totuple(), *color.to_rgb(), *n2.totuple(),
363
356
  *tri[2].totuple(), *color.to_rgb(), *n3.totuple(),
364
357
  ])
365
-
366
358
  vertices = np.array(vertices, dtype="f4")
367
-
368
359
  vbo = self.ctx.buffer(vertices.tobytes())
369
360
  vao = self.ctx.vertex_array(
370
361
  self.prog,
371
362
  [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
372
363
  )
373
-
374
364
  self._send_lights()
375
365
  vao.render()
376
366
  vao.release()
377
367
  vbo.release()
378
-
379
-
380
368
  def render_cylinder(self, center, height, radius, color, segments=32, rotation=vector3d.zero):
381
369
  vertices = []
382
-
383
370
  half_h = height / 2.0
384
-
385
371
  rx, ry, rz = map(math.radians, (rotation.x, rotation.y, rotation.z))
386
-
387
372
  rot_x = np.array([
388
373
  [1, 0, 0],
389
374
  [0, math.cos(rx), -math.sin(rx)],
390
- [0, math.sin(rx), math.cos(rx)]
375
+ [0, math.sin(rx), math.cos(rx)]
391
376
  ], dtype="f4")
392
-
393
377
  rot_y = np.array([
394
378
  [ math.cos(ry), 0, math.sin(ry)],
395
379
  [0, 1, 0],
396
380
  [-math.sin(ry), 0, math.cos(ry)]
397
381
  ], dtype="f4")
398
-
399
382
  rot_z = np.array([
400
383
  [math.cos(rz), -math.sin(rz), 0],
401
- [math.sin(rz), math.cos(rz), 0],
384
+ [math.sin(rz), math.cos(rz), 0],
402
385
  [0, 0, 1]
403
386
  ], dtype="f4")
404
-
405
387
  # Combined rotation matrix (Z * Y * X)
406
388
  rotation_matrix = rot_z @ rot_y @ rot_x
407
-
408
389
  def apply_rotation(v: vector3d):
409
390
  vec = np.array([[v.x], [v.y], [v.z]], dtype="f4")
410
391
  rotated = rotation_matrix @ vec
411
392
  return vector3d(rotated[0][0], rotated[1][0], rotated[2][0])
412
-
413
393
  for i in range(segments):
414
394
  theta1 = 2 * math.pi * i / segments
415
395
  theta2 = 2 * math.pi * (i + 1) / segments
416
-
417
396
  x1, z1 = radius * math.cos(theta1), radius * math.sin(theta1)
418
397
  x2, z2 = radius * math.cos(theta2), radius * math.sin(theta2)
419
-
420
398
  # Cylinder face vertices
421
399
  p1 = vector3d(x1, -half_h, z1)
422
400
  p2 = vector3d(x2, -half_h, z2)
423
401
  p3 = vector3d(x2, half_h, z2)
424
402
  p4 = vector3d(x1, half_h, z1)
425
-
426
403
  # Apply rotation
427
404
  p1 = apply_rotation(p1) + center
428
405
  p2 = apply_rotation(p2) + center
429
406
  p3 = apply_rotation(p3) + center
430
407
  p4 = apply_rotation(p4) + center
431
-
432
408
  n1 = apply_rotation(vector3d(x1, 0, z1).normalized)
433
409
  n2 = apply_rotation(vector3d(x2, 0, z2).normalized)
434
-
435
410
  # Side faces
436
411
  vertices.extend([
437
412
  *p1.totuple(), *color.to_rgb(), *n1.totuple(),
438
413
  *p2.totuple(), *color.to_rgb(), *n2.totuple(),
439
414
  *p3.totuple(), *color.to_rgb(), *n2.totuple(),
440
-
441
415
  *p1.totuple(), *color.to_rgb(), *n1.totuple(),
442
416
  *p3.totuple(), *color.to_rgb(), *n2.totuple(),
443
417
  *p4.totuple(), *color.to_rgb(), *n1.totuple(),
444
418
  ])
445
-
446
419
  # Base
447
420
  base_normal = apply_rotation(vector3d(0, -1, 0))
448
421
  vertices.extend([
@@ -450,7 +423,6 @@ class Renderer:
450
423
  *p2.totuple(), *color.to_rgb(), *base_normal.totuple(),
451
424
  *p1.totuple(), *color.to_rgb(), *base_normal.totuple(),
452
425
  ])
453
-
454
426
  # Top
455
427
  top_normal = apply_rotation(vector3d(0, 1, 0))
456
428
  vertices.extend([
@@ -458,19 +430,16 @@ class Renderer:
458
430
  *p4.totuple(), *color.to_rgb(), *top_normal.totuple(),
459
431
  *p3.totuple(), *color.to_rgb(), *top_normal.totuple(),
460
432
  ])
461
-
462
433
  vertices = np.array(vertices, dtype="f4")
463
434
  vbo = self.ctx.buffer(vertices.tobytes())
464
435
  vao = self.ctx.vertex_array(
465
436
  self.prog,
466
437
  [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
467
438
  )
468
-
469
439
  self._send_lights()
470
440
  vao.render()
471
441
  vao.release()
472
442
  vbo.release()
473
-
474
443
  def _send_lights(self):
475
444
  max_lights = 8
476
445
  num_lights = min(len(self.lights), max_lights)
@@ -481,17 +450,15 @@ class Renderer:
481
450
  self.prog[f"lights[{i}].position"].value = light.position.totuple()
482
451
  self.prog[f"lights[{i}].color"].value = tuple(c / 255 for c in light.color.to_rgb())
483
452
  self.prog[f"lights[{i}].intensity"].value = light.intensity
484
-
485
453
  def clear(self, color=Color(26, 26, 31)):
486
454
  color_f = np.array(color.to_rgb(), dtype="f4") / 255.0
487
- self.ctx.clear(*color_f, depth=1.0)
488
-
455
+ self.fbo.clear(*color_f, depth=1.0)
456
+ self.fbo.use()
489
457
  def swap(self):
458
+ self.ctx.copy_framebuffer(dst=self.ctx.screen, src=self.fbo)
490
459
  pygame.display.flip()
491
-
492
460
  def quit(self):
493
461
  pygame.quit()
494
-
495
462
  def _load_shader(self, vert_name, frag_name):
496
463
  vert_src = pkg_resources.read_text(f"{__package__}.shaders", "default.vert")
497
464
  frag_src = pkg_resources.read_text(f"{__package__}.shaders", "default.frag")
@@ -538,7 +505,6 @@ class Camera:
538
505
  self.elevation = math.asin(dir_vec.y / self.radius) # pitch
539
506
 
540
507
  def _update_position(self):
541
- """Update Cartesian position and view/projection matrices."""
542
508
  if self.orbiting:
543
509
  x = self.radius * math.cos(self.elevation) * math.cos(self.azimuth)
544
510
  y = self.radius * math.sin(self.elevation)
@@ -0,0 +1,33 @@
1
+
2
+ #version 330
3
+
4
+ struct Light {
5
+ vec3 position;
6
+ vec3 color;
7
+ float intensity;
8
+ };
9
+
10
+ #define MAX_LIGHTS 8
11
+ uniform int num_lights;
12
+ uniform Light lights[MAX_LIGHTS];
13
+ uniform float ambient;
14
+
15
+ in vec3 frag_pos;
16
+ in vec3 frag_normal;
17
+ in vec3 frag_color;
18
+
19
+ out vec4 f_color;
20
+
21
+ void main() {
22
+ vec3 normal = normalize(frag_normal);
23
+ vec3 color = frag_color / 255.0; // <-- convert vertex color to 0-1
24
+ vec3 result = ambient * color;
25
+
26
+ for (int i = 0; i < num_lights; i++) {
27
+ vec3 light_dir = normalize(lights[i].position - frag_pos);
28
+ float diff = max(dot(normal, light_dir), 0.0);
29
+ result += diff * lights[i].intensity * lights[i].color * color;
30
+ }
31
+
32
+ f_color = vec4(result, 1.0);
33
+ }
@@ -0,0 +1,20 @@
1
+
2
+ #version 330
3
+
4
+ in vec3 in_vert;
5
+ in vec3 in_color;
6
+ in vec3 in_normal;
7
+
8
+ out vec3 frag_pos;
9
+ out vec3 frag_normal;
10
+ out vec3 frag_color;
11
+
12
+ uniform mat4 mvp;
13
+ uniform mat4 model; // optional if you want model transforms
14
+
15
+ void main() {
16
+ frag_pos = vec3(model * vec4(in_vert, 1.0)); // world space position
17
+ frag_normal = mat3(transpose(inverse(model))) * in_normal; // transform normal
18
+ frag_color = in_color;
19
+ gl_Position = mvp * vec4(in_vert, 1.0);
20
+ }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gamengine3d
3
- Version: 1.0.1
3
+ Version: 1.1.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
@@ -11,4 +11,6 @@ gamengine3d.egg-info/SOURCES.txt
11
11
  gamengine3d.egg-info/dependency_links.txt
12
12
  gamengine3d.egg-info/requires.txt
13
13
  gamengine3d.egg-info/top_level.txt
14
- gamengine3d/shaders/__init__.py
14
+ gamengine3d/shaders/__init__.py
15
+ gamengine3d/shaders/default.frag
16
+ gamengine3d/shaders/default.vert
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
5
5
 
6
6
  [project]
7
7
  name = "gamengine3d"
8
- version = "1.0.1"
8
+ version = "1.1.0"
9
9
  description = "A Unity-like 3D engine in Python using pygame"
10
10
  authors = [
11
11
  { name = "Samarth Javagal", email = "samarthjavagal@gmail.com" }
@@ -32,4 +32,4 @@ where = ["."]
32
32
  include = ["gamengine3d*"]
33
33
 
34
34
  [tool.setuptools.package-data]
35
- "gamengine3d" = ["shaders/*.glsl"]
35
+ "gamengine3d" = ["shaders/*.vert", "shaders/*.frag"]
File without changes
File without changes
File without changes