gamengine3d 1.2.0__tar.gz → 1.4.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.2.0 → gamengine3d-1.4.0}/PKG-INFO +1 -1
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d/context.py +1 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d/engine.py +1 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d/helper.py +123 -105
- gamengine3d-1.4.0/gamengine3d/shaders/default.frag +63 -0
- gamengine3d-1.4.0/gamengine3d/shaders/default.vert +24 -0
- gamengine3d-1.4.0/gamengine3d/shaders/depth.frag +2 -0
- gamengine3d-1.4.0/gamengine3d/shaders/depth.vert +9 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d.egg-info/PKG-INFO +1 -1
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d.egg-info/SOURCES.txt +3 -1
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/pyproject.toml +1 -1
- gamengine3d-1.2.0/gamengine3d/shaders/default.frag +0 -33
- gamengine3d-1.2.0/gamengine3d/shaders/default.vert +0 -20
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/LICENSE.txt +0 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/README.md +0 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d/__init__.py +0 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d/game_objects.py +0 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d/shaders/__init__.py +0 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d.egg-info/dependency_links.txt +0 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d.egg-info/requires.txt +0 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/gamengine3d.egg-info/top_level.txt +0 -0
- {gamengine3d-1.2.0 → gamengine3d-1.4.0}/setup.cfg +0 -0
|
@@ -133,6 +133,7 @@ class Context:
|
|
|
133
133
|
self.delays: list[Delay] = []
|
|
134
134
|
self.delay_vars: list[DelayVar] = []
|
|
135
135
|
self.scheduled_callers: list[ScheduledCaller] = []
|
|
136
|
+
self.runtime_vars = {}
|
|
136
137
|
self.exit = False
|
|
137
138
|
|
|
138
139
|
def on_key_press(self, key, callback, args=None, dt=False):
|
|
@@ -227,11 +227,13 @@ class Light:
|
|
|
227
227
|
self.show = show
|
|
228
228
|
self.name = name
|
|
229
229
|
|
|
230
|
+
|
|
230
231
|
class Renderer:
|
|
231
232
|
def __init__(self, width=800, height=600, flags=DOUBLEBUF | OPENGL):
|
|
232
233
|
pygame.init()
|
|
233
234
|
self.screen = pygame.display.set_mode((width, height), flags)
|
|
234
235
|
self.ctx = moderngl.create_context()
|
|
236
|
+
self.ctx.viewport = (0, 0, width, height)
|
|
235
237
|
self.samples = 4
|
|
236
238
|
self.color_tex = self.ctx.texture((width, height), 3, samples=self.samples)
|
|
237
239
|
self.depth_rbo = self.ctx.depth_renderbuffer((width, height), samples=self.samples)
|
|
@@ -241,6 +243,8 @@ class Renderer:
|
|
|
241
243
|
self.height = height
|
|
242
244
|
self.ambient = 0.2
|
|
243
245
|
self.ctx.enable(moderngl.DEPTH_TEST)
|
|
246
|
+
|
|
247
|
+
# === Main shader ===
|
|
244
248
|
self.prog = self._load_shader("default.vert", "default.frag")
|
|
245
249
|
self.prog["model"].write(np.eye(4, dtype="f4").tobytes())
|
|
246
250
|
self.proj = Matrix44.perspective_projection(60.0, width / height, 0.1, 100.0)
|
|
@@ -252,6 +256,21 @@ class Renderer:
|
|
|
252
256
|
self.mvp = self.proj * self.view
|
|
253
257
|
self.prog["mvp"].write(self.mvp.astype("f4").tobytes())
|
|
254
258
|
self.prog["ambient"].value = self.ambient
|
|
259
|
+
|
|
260
|
+
self.shadow_size = 4096
|
|
261
|
+
self.shadow_depth_tex = self.ctx.depth_texture((self.shadow_size, self.shadow_size))
|
|
262
|
+
try:
|
|
263
|
+
self.shadow_depth_tex.repeat_x = False
|
|
264
|
+
self.shadow_depth_tex.repeat_y = False
|
|
265
|
+
except Exception:
|
|
266
|
+
pass
|
|
267
|
+
self.shadow_fbo = self.ctx.framebuffer(depth_attachment=self.shadow_depth_tex)
|
|
268
|
+
self.depth_prog = self._load_shader("depth.vert", "depth.frag")
|
|
269
|
+
self.light_space_matrix = np.eye(4, dtype="f4")
|
|
270
|
+
|
|
271
|
+
# ------------------------------------------
|
|
272
|
+
# Window / Resize Handling
|
|
273
|
+
# ------------------------------------------
|
|
255
274
|
def resize(self, width, height):
|
|
256
275
|
self.width = width
|
|
257
276
|
self.height = height
|
|
@@ -262,26 +281,93 @@ class Renderer:
|
|
|
262
281
|
|
|
263
282
|
self.ctx = moderngl.create_context()
|
|
264
283
|
self.ctx.viewport = (0, 0, width, height)
|
|
284
|
+
|
|
285
|
+
# Recreate framebuffer and textures for new context
|
|
265
286
|
self.color_tex = self.ctx.texture((width, height), 3, samples=self.samples)
|
|
266
287
|
self.depth_rbo = self.ctx.depth_renderbuffer((width, height), samples=self.samples)
|
|
267
288
|
self.fbo = self.ctx.framebuffer(color_attachments=[self.color_tex], depth_attachment=self.depth_rbo)
|
|
268
289
|
|
|
290
|
+
# Recreate shaders in new context
|
|
269
291
|
self.prog = self._load_shader("default.vert", "default.frag")
|
|
270
292
|
self.prog["model"].write(np.eye(4, dtype="f4").tobytes())
|
|
271
|
-
|
|
272
293
|
aspect = width / height
|
|
273
294
|
self.proj = Matrix44.perspective_projection(60.0, aspect, 0.1, 100.0)
|
|
274
295
|
self.mvp = self.proj * self.view
|
|
275
296
|
self.prog["mvp"].write(self.mvp.astype("f4").tobytes())
|
|
276
297
|
self.prog["ambient"].value = self.ambient
|
|
277
298
|
|
|
299
|
+
# Recreate shadow map resources for this context
|
|
300
|
+
self.shadow_depth_tex = self.ctx.depth_texture((self.shadow_size, self.shadow_size))
|
|
301
|
+
try:
|
|
302
|
+
self.shadow_depth_tex.repeat_x = False
|
|
303
|
+
self.shadow_depth_tex.repeat_y = False
|
|
304
|
+
except Exception:
|
|
305
|
+
pass
|
|
306
|
+
self.shadow_fbo = self.ctx.framebuffer(depth_attachment=self.shadow_depth_tex)
|
|
307
|
+
self.depth_prog = self._load_shader("depth.vert", "depth.frag")
|
|
308
|
+
|
|
278
309
|
self._send_lights()
|
|
310
|
+
|
|
311
|
+
# ------------------------------------------
|
|
312
|
+
# Light & Matrix
|
|
313
|
+
# ------------------------------------------
|
|
279
314
|
def add_light(self, light: Light):
|
|
280
315
|
self.lights.append(light)
|
|
316
|
+
|
|
281
317
|
def _compute_normal(self, p1, p2, p3):
|
|
282
318
|
u = p2 - p1
|
|
283
319
|
v = p3 - p1
|
|
284
320
|
return u.cross(v).normalized
|
|
321
|
+
|
|
322
|
+
def compute_light_matrix(self, light: Light):
|
|
323
|
+
"""Compute light-space projection * view for shadow mapping."""
|
|
324
|
+
light_proj = Matrix44.orthogonal_projection(-10, 10, -10, 10, 1, 30)
|
|
325
|
+
light_view = Matrix44.look_at(
|
|
326
|
+
eye=light.position.totuple(),
|
|
327
|
+
target=(0, 0, 0),
|
|
328
|
+
up=(0, 1, 0)
|
|
329
|
+
)
|
|
330
|
+
return light_proj * light_view
|
|
331
|
+
|
|
332
|
+
# ------------------------------------------
|
|
333
|
+
# Shadow Pass
|
|
334
|
+
# ------------------------------------------
|
|
335
|
+
def render_shadow_pass(self, objects):
|
|
336
|
+
"""Render depth map from light's POV."""
|
|
337
|
+
if not self.lights:
|
|
338
|
+
return
|
|
339
|
+
light = self.lights[0]
|
|
340
|
+
self.light_space_matrix = self.compute_light_matrix(light)
|
|
341
|
+
|
|
342
|
+
self.shadow_fbo.clear(depth=1.0)
|
|
343
|
+
self.shadow_fbo.use()
|
|
344
|
+
self.ctx.enable(moderngl.DEPTH_TEST)
|
|
345
|
+
|
|
346
|
+
for obj in objects:
|
|
347
|
+
if not hasattr(obj, "get_corners"):
|
|
348
|
+
continue
|
|
349
|
+
corners = obj.get_corners()
|
|
350
|
+
vertices = []
|
|
351
|
+
for face in obj.faces:
|
|
352
|
+
p1, p2, p3, p4 = [corners[i] for i in face]
|
|
353
|
+
vertices.extend([
|
|
354
|
+
*p1.totuple(), *p2.totuple(), *p3.totuple(),
|
|
355
|
+
*p1.totuple(), *p3.totuple(), *p4.totuple()
|
|
356
|
+
])
|
|
357
|
+
vertices = np.array(vertices, dtype="f4")
|
|
358
|
+
vbo = self.ctx.buffer(vertices.tobytes())
|
|
359
|
+
vao = self.ctx.vertex_array(self.depth_prog, [(vbo, "3f", "in_vert")])
|
|
360
|
+
self.depth_prog["model"].write(np.eye(4, dtype="f4").tobytes())
|
|
361
|
+
self.depth_prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
362
|
+
vao.render()
|
|
363
|
+
vao.release()
|
|
364
|
+
vbo.release()
|
|
365
|
+
|
|
366
|
+
self.ctx.screen.use()
|
|
367
|
+
|
|
368
|
+
# ------------------------------------------
|
|
369
|
+
# Render Functions
|
|
370
|
+
# ------------------------------------------
|
|
285
371
|
def render_plane(self, p1, p2, p3, color):
|
|
286
372
|
normal = self._compute_normal(p1, p2, p3)
|
|
287
373
|
vertices = np.array([
|
|
@@ -291,13 +377,16 @@ class Renderer:
|
|
|
291
377
|
], dtype="f4")
|
|
292
378
|
vbo = self.ctx.buffer(vertices.tobytes())
|
|
293
379
|
vao = self.ctx.vertex_array(
|
|
294
|
-
self.prog,
|
|
295
|
-
[(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
380
|
+
self.prog, [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
296
381
|
)
|
|
297
382
|
self._send_lights()
|
|
383
|
+
self.prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
384
|
+
self.shadow_depth_tex.use(location=0)
|
|
385
|
+
self.prog["shadow_map"].value = 0
|
|
298
386
|
vao.render()
|
|
299
387
|
vao.release()
|
|
300
388
|
vbo.release()
|
|
389
|
+
|
|
301
390
|
def render_quad(self, p1, p2, p3, p4, color):
|
|
302
391
|
normal = self._compute_normal(p1, p2, p3)
|
|
303
392
|
vertices = np.array([
|
|
@@ -310,42 +399,36 @@ class Renderer:
|
|
|
310
399
|
], dtype="f4")
|
|
311
400
|
vbo = self.ctx.buffer(vertices.tobytes())
|
|
312
401
|
vao = self.ctx.vertex_array(
|
|
313
|
-
self.prog,
|
|
314
|
-
[(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
402
|
+
self.prog, [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
315
403
|
)
|
|
316
404
|
self._send_lights()
|
|
405
|
+
self.prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
406
|
+
self.shadow_depth_tex.use(location=0)
|
|
407
|
+
self.prog["shadow_map"].value = 0
|
|
317
408
|
vao.render()
|
|
318
409
|
vao.release()
|
|
319
410
|
vbo.release()
|
|
411
|
+
|
|
320
412
|
def render_sphere(self, center, radius, color, segments=32, rings=16):
|
|
321
413
|
vertices = []
|
|
322
|
-
normals = []
|
|
323
414
|
for i in range(rings):
|
|
324
415
|
theta1 = math.pi * i / rings
|
|
325
416
|
theta2 = math.pi * (i + 1) / rings
|
|
326
417
|
for j in range(segments):
|
|
327
418
|
phi1 = 2 * math.pi * j / segments
|
|
328
419
|
phi2 = 2 * math.pi * (j + 1) / segments
|
|
329
|
-
p1 = vector3d(
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
)
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
radius * math.cos(theta2),
|
|
342
|
-
radius * math.sin(theta2) * math.sin(phi2)
|
|
343
|
-
) + center
|
|
344
|
-
p4 = vector3d(
|
|
345
|
-
radius * math.sin(theta1) * math.cos(phi2),
|
|
346
|
-
radius * math.cos(theta1),
|
|
347
|
-
radius * math.sin(theta1) * math.sin(phi2)
|
|
348
|
-
) + center
|
|
420
|
+
p1 = vector3d(radius * math.sin(theta1) * math.cos(phi1),
|
|
421
|
+
radius * math.cos(theta1),
|
|
422
|
+
radius * math.sin(theta1) * math.sin(phi1)) + center
|
|
423
|
+
p2 = vector3d(radius * math.sin(theta2) * math.cos(phi1),
|
|
424
|
+
radius * math.cos(theta2),
|
|
425
|
+
radius * math.sin(theta2) * math.sin(phi1)) + center
|
|
426
|
+
p3 = vector3d(radius * math.sin(theta2) * math.cos(phi2),
|
|
427
|
+
radius * math.cos(theta2),
|
|
428
|
+
radius * math.sin(theta2) * math.sin(phi2)) + center
|
|
429
|
+
p4 = vector3d(radius * math.sin(theta1) * math.cos(phi2),
|
|
430
|
+
radius * math.cos(theta1),
|
|
431
|
+
radius * math.sin(theta1) * math.sin(phi2)) + center
|
|
349
432
|
for tri in [(p1, p2, p3), (p1, p3, p4)]:
|
|
350
433
|
n1 = (tri[0] - center).normalized
|
|
351
434
|
n2 = (tri[1] - center).normalized
|
|
@@ -358,88 +441,19 @@ class Renderer:
|
|
|
358
441
|
vertices = np.array(vertices, dtype="f4")
|
|
359
442
|
vbo = self.ctx.buffer(vertices.tobytes())
|
|
360
443
|
vao = self.ctx.vertex_array(
|
|
361
|
-
self.prog,
|
|
362
|
-
[(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
363
|
-
)
|
|
364
|
-
self._send_lights()
|
|
365
|
-
vao.render()
|
|
366
|
-
vao.release()
|
|
367
|
-
vbo.release()
|
|
368
|
-
def render_cylinder(self, center, height, radius, color, segments=32, rotation=vector3d.zero):
|
|
369
|
-
vertices = []
|
|
370
|
-
half_h = height / 2.0
|
|
371
|
-
rx, ry, rz = map(math.radians, (rotation.x, rotation.y, rotation.z))
|
|
372
|
-
rot_x = np.array([
|
|
373
|
-
[1, 0, 0],
|
|
374
|
-
[0, math.cos(rx), -math.sin(rx)],
|
|
375
|
-
[0, math.sin(rx), math.cos(rx)]
|
|
376
|
-
], dtype="f4")
|
|
377
|
-
rot_y = np.array([
|
|
378
|
-
[ math.cos(ry), 0, math.sin(ry)],
|
|
379
|
-
[0, 1, 0],
|
|
380
|
-
[-math.sin(ry), 0, math.cos(ry)]
|
|
381
|
-
], dtype="f4")
|
|
382
|
-
rot_z = np.array([
|
|
383
|
-
[math.cos(rz), -math.sin(rz), 0],
|
|
384
|
-
[math.sin(rz), math.cos(rz), 0],
|
|
385
|
-
[0, 0, 1]
|
|
386
|
-
], dtype="f4")
|
|
387
|
-
# Combined rotation matrix (Z * Y * X)
|
|
388
|
-
rotation_matrix = rot_z @ rot_y @ rot_x
|
|
389
|
-
def apply_rotation(v: vector3d):
|
|
390
|
-
vec = np.array([[v.x], [v.y], [v.z]], dtype="f4")
|
|
391
|
-
rotated = rotation_matrix @ vec
|
|
392
|
-
return vector3d(rotated[0][0], rotated[1][0], rotated[2][0])
|
|
393
|
-
for i in range(segments):
|
|
394
|
-
theta1 = 2 * math.pi * i / segments
|
|
395
|
-
theta2 = 2 * math.pi * (i + 1) / segments
|
|
396
|
-
x1, z1 = radius * math.cos(theta1), radius * math.sin(theta1)
|
|
397
|
-
x2, z2 = radius * math.cos(theta2), radius * math.sin(theta2)
|
|
398
|
-
# Cylinder face vertices
|
|
399
|
-
p1 = vector3d(x1, -half_h, z1)
|
|
400
|
-
p2 = vector3d(x2, -half_h, z2)
|
|
401
|
-
p3 = vector3d(x2, half_h, z2)
|
|
402
|
-
p4 = vector3d(x1, half_h, z1)
|
|
403
|
-
# Apply rotation
|
|
404
|
-
p1 = apply_rotation(p1) + center
|
|
405
|
-
p2 = apply_rotation(p2) + center
|
|
406
|
-
p3 = apply_rotation(p3) + center
|
|
407
|
-
p4 = apply_rotation(p4) + center
|
|
408
|
-
n1 = apply_rotation(vector3d(x1, 0, z1).normalized)
|
|
409
|
-
n2 = apply_rotation(vector3d(x2, 0, z2).normalized)
|
|
410
|
-
# Side faces
|
|
411
|
-
vertices.extend([
|
|
412
|
-
*p1.totuple(), *color.to_rgb(), *n1.totuple(),
|
|
413
|
-
*p2.totuple(), *color.to_rgb(), *n2.totuple(),
|
|
414
|
-
*p3.totuple(), *color.to_rgb(), *n2.totuple(),
|
|
415
|
-
*p1.totuple(), *color.to_rgb(), *n1.totuple(),
|
|
416
|
-
*p3.totuple(), *color.to_rgb(), *n2.totuple(),
|
|
417
|
-
*p4.totuple(), *color.to_rgb(), *n1.totuple(),
|
|
418
|
-
])
|
|
419
|
-
# Base
|
|
420
|
-
base_normal = apply_rotation(vector3d(0, -1, 0))
|
|
421
|
-
vertices.extend([
|
|
422
|
-
*apply_rotation(vector3d(0, -half_h, 0) + center).totuple(), *color.to_rgb(), *base_normal.totuple(),
|
|
423
|
-
*p2.totuple(), *color.to_rgb(), *base_normal.totuple(),
|
|
424
|
-
*p1.totuple(), *color.to_rgb(), *base_normal.totuple(),
|
|
425
|
-
])
|
|
426
|
-
# Top
|
|
427
|
-
top_normal = apply_rotation(vector3d(0, 1, 0))
|
|
428
|
-
vertices.extend([
|
|
429
|
-
*apply_rotation(vector3d(0, half_h, 0) + center).totuple(), *color.to_rgb(), *top_normal.totuple(),
|
|
430
|
-
*p4.totuple(), *color.to_rgb(), *top_normal.totuple(),
|
|
431
|
-
*p3.totuple(), *color.to_rgb(), *top_normal.totuple(),
|
|
432
|
-
])
|
|
433
|
-
vertices = np.array(vertices, dtype="f4")
|
|
434
|
-
vbo = self.ctx.buffer(vertices.tobytes())
|
|
435
|
-
vao = self.ctx.vertex_array(
|
|
436
|
-
self.prog,
|
|
437
|
-
[(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
444
|
+
self.prog, [(vbo, "3f 3f 3f", "in_vert", "in_color", "in_normal")]
|
|
438
445
|
)
|
|
439
446
|
self._send_lights()
|
|
447
|
+
self.prog["light_space_matrix"].write(self.light_space_matrix.astype("f4").tobytes())
|
|
448
|
+
self.shadow_depth_tex.use(location=0)
|
|
449
|
+
self.prog["shadow_map"].value = 0
|
|
440
450
|
vao.render()
|
|
441
451
|
vao.release()
|
|
442
452
|
vbo.release()
|
|
453
|
+
|
|
454
|
+
# ------------------------------------------
|
|
455
|
+
# Utility
|
|
456
|
+
# ------------------------------------------
|
|
443
457
|
def _send_lights(self):
|
|
444
458
|
max_lights = 8
|
|
445
459
|
num_lights = min(len(self.lights), max_lights)
|
|
@@ -450,18 +464,22 @@ class Renderer:
|
|
|
450
464
|
self.prog[f"lights[{i}].position"].value = light.position.totuple()
|
|
451
465
|
self.prog[f"lights[{i}].color"].value = tuple(c / 255 for c in light.color.to_rgb())
|
|
452
466
|
self.prog[f"lights[{i}].intensity"].value = light.intensity
|
|
467
|
+
|
|
453
468
|
def clear(self, color=Color(26, 26, 31)):
|
|
454
469
|
color_f = np.array(color.to_rgb(), dtype="f4") / 255.0
|
|
455
470
|
self.fbo.clear(*color_f, depth=1.0)
|
|
456
471
|
self.fbo.use()
|
|
472
|
+
|
|
457
473
|
def swap(self):
|
|
458
474
|
self.ctx.copy_framebuffer(dst=self.ctx.screen, src=self.fbo)
|
|
459
475
|
pygame.display.flip()
|
|
476
|
+
|
|
460
477
|
def quit(self):
|
|
461
478
|
pygame.quit()
|
|
479
|
+
|
|
462
480
|
def _load_shader(self, vert_name, frag_name):
|
|
463
|
-
vert_src = pkg_resources.read_text(f"{__package__}.shaders",
|
|
464
|
-
frag_src = pkg_resources.read_text(f"{__package__}.shaders",
|
|
481
|
+
vert_src = pkg_resources.read_text(f"{__package__}.shaders", vert_name)
|
|
482
|
+
frag_src = pkg_resources.read_text(f"{__package__}.shaders", frag_name)
|
|
465
483
|
return self.ctx.program(vertex_shader=vert_src, fragment_shader=frag_src)
|
|
466
484
|
|
|
467
485
|
class Camera:
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
uniform sampler2DShadow shadow_map; // NOTE: sampler2DShadow
|
|
15
|
+
|
|
16
|
+
in vec3 frag_pos;
|
|
17
|
+
in vec3 frag_normal;
|
|
18
|
+
in vec3 frag_color;
|
|
19
|
+
in vec4 frag_pos_light_space;
|
|
20
|
+
|
|
21
|
+
out vec4 f_color;
|
|
22
|
+
|
|
23
|
+
void main() {
|
|
24
|
+
vec3 normal = normalize(frag_normal);
|
|
25
|
+
vec3 color = frag_color / 255.0;
|
|
26
|
+
vec3 result = ambient * color;
|
|
27
|
+
|
|
28
|
+
for (int i = 0; i < num_lights; i++) {
|
|
29
|
+
vec3 light_dir = normalize(lights[i].position - frag_pos);
|
|
30
|
+
float diff = max(dot(normal, light_dir), 0.0);
|
|
31
|
+
|
|
32
|
+
// Project to [0,1] for shadow sampling
|
|
33
|
+
vec3 proj_coords = frag_pos_light_space.xyz / frag_pos_light_space.w;
|
|
34
|
+
proj_coords = proj_coords * 0.5 + 0.5;
|
|
35
|
+
|
|
36
|
+
// Hardware PCF shadow sampling
|
|
37
|
+
float shadow = 0.0;
|
|
38
|
+
if (proj_coords.x >= 0.0 && proj_coords.x <= 1.0 &&
|
|
39
|
+
proj_coords.y >= 0.0 && proj_coords.y <= 1.0) {
|
|
40
|
+
|
|
41
|
+
float bias = max(0.02 * (1.0 - dot(normal, light_dir)), 0.005);
|
|
42
|
+
|
|
43
|
+
// Simple 3x3 PCF
|
|
44
|
+
ivec2 texSize = textureSize(shadow_map, 0);
|
|
45
|
+
float texelSizeX = 1.0 / float(texSize.x);
|
|
46
|
+
float texelSizeY = 1.0 / float(texSize.y);
|
|
47
|
+
|
|
48
|
+
for (int x = -1; x <= 1; ++x) {
|
|
49
|
+
for (int y = -1; y <= 1; ++y) {
|
|
50
|
+
vec2 offset = vec2(float(x) * texelSizeX, float(y) * texelSizeY);
|
|
51
|
+
shadow += texture(shadow_map, vec3(proj_coords.xy + offset, proj_coords.z - bias));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
shadow /= 9.0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
// No inversion needed
|
|
59
|
+
result += shadow * diff * lights[i].intensity * lights[i].color * color;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
f_color = vec4(result, 1.0);
|
|
63
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
out vec4 frag_pos_light_space;
|
|
12
|
+
|
|
13
|
+
uniform mat4 mvp;
|
|
14
|
+
uniform mat4 model;
|
|
15
|
+
uniform mat4 light_space_matrix;
|
|
16
|
+
|
|
17
|
+
void main() {
|
|
18
|
+
vec4 world_pos = model * vec4(in_vert, 1.0);
|
|
19
|
+
frag_pos = world_pos.xyz;
|
|
20
|
+
frag_normal = mat3(transpose(inverse(model))) * in_normal;
|
|
21
|
+
frag_color = in_color;
|
|
22
|
+
frag_pos_light_space = light_space_matrix * world_pos;
|
|
23
|
+
gl_Position = mvp * vec4(in_vert, 1.0);
|
|
24
|
+
}
|
|
@@ -13,4 +13,6 @@ gamengine3d.egg-info/requires.txt
|
|
|
13
13
|
gamengine3d.egg-info/top_level.txt
|
|
14
14
|
gamengine3d/shaders/__init__.py
|
|
15
15
|
gamengine3d/shaders/default.frag
|
|
16
|
-
gamengine3d/shaders/default.vert
|
|
16
|
+
gamengine3d/shaders/default.vert
|
|
17
|
+
gamengine3d/shaders/depth.frag
|
|
18
|
+
gamengine3d/shaders/depth.vert
|
|
@@ -1,33 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
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
|
-
}
|
|
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
|