basilisk-engine 0.1.29__py3-none-any.whl → 0.1.30__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.
Potentially problematic release.
This version of basilisk-engine might be problematic. Click here for more details.
- basilisk/__init__.py +15 -15
- basilisk/audio/sound.py +27 -27
- basilisk/bsk_assets/cube.obj +48 -48
- basilisk/collisions/broad/broad_aabb.py +102 -102
- basilisk/collisions/broad/broad_bvh.py +137 -137
- basilisk/collisions/collider.py +95 -95
- basilisk/collisions/collider_handler.py +224 -224
- basilisk/collisions/narrow/contact_manifold.py +95 -95
- basilisk/collisions/narrow/dataclasses.py +34 -34
- basilisk/collisions/narrow/deprecated.py +46 -46
- basilisk/collisions/narrow/epa.py +91 -91
- basilisk/collisions/narrow/gjk.py +66 -66
- basilisk/collisions/narrow/graham_scan.py +24 -24
- basilisk/collisions/narrow/helper.py +29 -29
- basilisk/collisions/narrow/line_intersections.py +106 -106
- basilisk/collisions/narrow/sutherland_hodgman.py +75 -75
- basilisk/config.py +3 -3
- basilisk/draw/draw.py +100 -100
- basilisk/draw/draw_handler.py +175 -175
- basilisk/draw/font_renderer.py +28 -28
- basilisk/engine.py +166 -170
- basilisk/generic/abstract_bvh.py +15 -15
- basilisk/generic/abstract_custom.py +133 -133
- basilisk/generic/collisions.py +70 -70
- basilisk/generic/input_validation.py +74 -74
- basilisk/generic/math.py +6 -6
- basilisk/generic/matrices.py +35 -35
- basilisk/generic/meshes.py +72 -72
- basilisk/generic/quat.py +142 -142
- basilisk/generic/quat_methods.py +7 -7
- basilisk/generic/raycast_result.py +26 -26
- basilisk/generic/vec3.py +143 -143
- basilisk/input_output/IO_handler.py +91 -91
- basilisk/input_output/clock.py +49 -49
- basilisk/input_output/keys.py +43 -43
- basilisk/input_output/mouse.py +89 -89
- basilisk/input_output/path.py +14 -14
- basilisk/mesh/cube.py +33 -33
- basilisk/mesh/mesh.py +233 -233
- basilisk/mesh/mesh_from_data.py +150 -150
- basilisk/mesh/model.py +271 -271
- basilisk/mesh/narrow_aabb.py +89 -89
- basilisk/mesh/narrow_bvh.py +91 -91
- basilisk/mesh/narrow_primative.py +23 -23
- basilisk/nodes/helper.py +28 -28
- basilisk/nodes/node.py +689 -689
- basilisk/nodes/node_handler.py +97 -97
- basilisk/particles/particle_handler.py +64 -64
- basilisk/particles/particle_renderer.py +92 -92
- basilisk/physics/impulse.py +112 -112
- basilisk/physics/physics_body.py +43 -43
- basilisk/physics/physics_engine.py +35 -35
- basilisk/render/batch.py +103 -103
- basilisk/render/camera.py +260 -260
- basilisk/render/chunk.py +106 -106
- basilisk/render/chunk_handler.py +165 -165
- basilisk/render/frame.py +95 -95
- basilisk/render/framebuffer.py +191 -182
- basilisk/render/image.py +120 -120
- basilisk/render/image_handler.py +120 -120
- basilisk/render/light.py +96 -96
- basilisk/render/light_handler.py +58 -58
- basilisk/render/material.py +221 -221
- basilisk/render/material_handler.py +133 -133
- basilisk/render/post_process.py +139 -139
- basilisk/render/shader.py +134 -134
- basilisk/render/shader_handler.py +83 -83
- basilisk/render/sky.py +120 -120
- basilisk/scene.py +290 -280
- basilisk/shaders/batch.frag +276 -276
- basilisk/shaders/batch.vert +115 -115
- basilisk/shaders/crt.frag +31 -31
- basilisk/shaders/draw.frag +22 -22
- basilisk/shaders/draw.vert +25 -25
- basilisk/shaders/filter.frag +22 -22
- basilisk/shaders/frame.frag +12 -12
- basilisk/shaders/frame.vert +13 -13
- basilisk/shaders/geometry.frag +8 -8
- basilisk/shaders/geometry.vert +41 -41
- basilisk/shaders/normal.frag +59 -59
- basilisk/shaders/normal.vert +96 -96
- basilisk/shaders/particle.frag +71 -71
- basilisk/shaders/particle.vert +84 -84
- basilisk/shaders/sky.frag +9 -9
- basilisk/shaders/sky.vert +13 -13
- {basilisk_engine-0.1.29.dist-info → basilisk_engine-0.1.30.dist-info}/METADATA +38 -45
- basilisk_engine-0.1.30.dist-info/RECORD +106 -0
- {basilisk_engine-0.1.29.dist-info → basilisk_engine-0.1.30.dist-info}/WHEEL +1 -1
- basilisk_engine-0.1.29.dist-info/RECORD +0 -106
- {basilisk_engine-0.1.29.dist-info → basilisk_engine-0.1.30.dist-info}/top_level.txt +0 -0
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
import glm
|
|
2
|
-
from .helper import is_ccw_turn
|
|
3
|
-
|
|
4
|
-
# intersectionn algorithms for lines
|
|
5
|
-
def line_line_intersect(points1:list[glm.vec2], points2:list[glm.vec2]) -> list[glm.vec2]:
|
|
6
|
-
"""gets the intersection of 2 2d lines. if the lines are parallel, returns the second line"""
|
|
7
|
-
# orders points from smallest x to greatest x
|
|
8
|
-
points1 = sorted(points1, key=lambda p: (p.x, p.y))
|
|
9
|
-
points2 = sorted(points2, key=lambda p: (p.x, p.y))
|
|
10
|
-
vec1, vec2 = points1[1] - points1[0], points2[1] - points2[0]
|
|
11
|
-
|
|
12
|
-
# if vectors have the same slope return the smallest line
|
|
13
|
-
if have_same_slope(vec1, vec2): return sorted(points1 + points2, key=lambda p: (p.x, p.y))[1:3]
|
|
14
|
-
|
|
15
|
-
# line - line intersection
|
|
16
|
-
det = vec1.x * vec2.y - vec1.y * vec2.x
|
|
17
|
-
if det == 0: return []
|
|
18
|
-
t = (points2[0].x - points1[0].x) * vec2.y - (points2[0].y - points1[0].y) * vec2.x
|
|
19
|
-
t /= det
|
|
20
|
-
return [points1[0] + t * vec1]
|
|
21
|
-
|
|
22
|
-
def have_same_slope(vec1:glm.vec2, vec2:glm.vec2, epsilon:float=1e-5) -> bool:
|
|
23
|
-
"""determines if two vectors moving in the positive x direction have the same slope"""
|
|
24
|
-
return abs(vec1.y * vec2.x - vec2.y * vec1.x) < epsilon
|
|
25
|
-
|
|
26
|
-
def line_poly_intersect(line:list[glm.vec2], polygon:list[glm.vec2]) -> list[glm.vec2]: #TODO Reseach into faster algorithm < O(2n)
|
|
27
|
-
"""computes which parts of the line clip with the polygon"""
|
|
28
|
-
# calculate the center of the polygon
|
|
29
|
-
assert len(polygon) > 2, 'polygon is does not contain engough points'
|
|
30
|
-
center = glm.vec2(0,0)
|
|
31
|
-
for point in polygon: center += point
|
|
32
|
-
center /= len(polygon)
|
|
33
|
-
orig_line = line[:]
|
|
34
|
-
# determine which points are in or out of the polygon
|
|
35
|
-
exterior_points = []
|
|
36
|
-
for i in range(len(polygon)): # nearest even number below n
|
|
37
|
-
for line_point in line[:]:
|
|
38
|
-
if not is_ccw_turn(polygon[i], polygon[(i + 1) % len(polygon)], line_point): # if point is on the outside
|
|
39
|
-
exterior_points.append((polygon[i], polygon[(i + 1) % len(polygon)], line_point)) # polypoint1, polypoint2, linepoint
|
|
40
|
-
line.remove(line_point) # removes line point if it is confirmed to be outside
|
|
41
|
-
|
|
42
|
-
# determine what to with line based on number of points found outside
|
|
43
|
-
if len(exterior_points) == 0:
|
|
44
|
-
return line
|
|
45
|
-
if len(exterior_points) == 1:
|
|
46
|
-
return line_line_intersect(line + [exterior_points[0][2]], exterior_points[0][0:2]) + [line[0]] # [intersecting point, exterior point]
|
|
47
|
-
if len(exterior_points) == 2: # line must intersect with two edges
|
|
48
|
-
points = []
|
|
49
|
-
for i in range(len(polygon)):
|
|
50
|
-
intersection = line_line_intersect(orig_line, [polygon[i], polygon[(i + 1) % len(polygon)]])
|
|
51
|
-
if len(intersection) > 0: points += intersection
|
|
52
|
-
if len(points) > 1: break # exit if two intersections have been found
|
|
53
|
-
else: return [] # fallback if 0 or one intersections found
|
|
54
|
-
return points
|
|
55
|
-
|
|
56
|
-
def closest_two_lines(p1: glm.vec3, q1: glm.vec3, p2: glm.vec3, q2: glm.vec3, epsilon: float=1e-7) -> tuple[glm.vec3, glm.vec3]:
|
|
57
|
-
"""
|
|
58
|
-
Determines the closest point on each line segment to the other line segment.
|
|
59
|
-
"""
|
|
60
|
-
# create direction vector
|
|
61
|
-
d1 = q1 - p1
|
|
62
|
-
d2 = q2 - p2
|
|
63
|
-
r = p1 - p2
|
|
64
|
-
|
|
65
|
-
# get lengths of line segments
|
|
66
|
-
a = glm.dot(d1, d1)
|
|
67
|
-
e = glm.dot(d2, d2)
|
|
68
|
-
f = glm.dot(d2, r)
|
|
69
|
-
|
|
70
|
-
# check if either or both line segment degenerate into points
|
|
71
|
-
if a <= epsilon and e <= epsilon:
|
|
72
|
-
# both segments degenerate
|
|
73
|
-
return p1, p2
|
|
74
|
-
|
|
75
|
-
if a <= epsilon:
|
|
76
|
-
s = 0
|
|
77
|
-
t = glm.clamp(f / e, 0, 1)
|
|
78
|
-
else:
|
|
79
|
-
c = glm.dot(d1, r)
|
|
80
|
-
if e <= epsilon:
|
|
81
|
-
# the second line degenerates to a point
|
|
82
|
-
t = 0
|
|
83
|
-
s = glm.clamp(-c / a, 0, 1)
|
|
84
|
-
else:
|
|
85
|
-
# if neither of them degenerate to a point
|
|
86
|
-
b = glm.dot(d1, d2)
|
|
87
|
-
denom = a * e - b ** 2 # this will always be non-negative
|
|
88
|
-
|
|
89
|
-
# if segments are not parallel, compute closest point from l1 to l2
|
|
90
|
-
s = glm.clamp((b * f - c * e) / denom, 0, 1) if denom else 0
|
|
91
|
-
|
|
92
|
-
# compute closest point from l2 on s1(s)
|
|
93
|
-
t = (b * s + f) / e
|
|
94
|
-
|
|
95
|
-
# if t is not in [0, 1], clamp and recompute s
|
|
96
|
-
if t < 0:
|
|
97
|
-
t = 0
|
|
98
|
-
s = glm.clamp(-c / a, 0, 1)
|
|
99
|
-
elif t > 1:
|
|
100
|
-
t = 1
|
|
101
|
-
s = glm.clamp((b - c) / a, 0, 1)
|
|
102
|
-
|
|
103
|
-
c1 = p1 + d1 * s
|
|
104
|
-
c2 = p2 + d2 * t
|
|
105
|
-
return c1, c2
|
|
106
|
-
|
|
1
|
+
import glm
|
|
2
|
+
from .helper import is_ccw_turn
|
|
3
|
+
|
|
4
|
+
# intersectionn algorithms for lines
|
|
5
|
+
def line_line_intersect(points1:list[glm.vec2], points2:list[glm.vec2]) -> list[glm.vec2]:
|
|
6
|
+
"""gets the intersection of 2 2d lines. if the lines are parallel, returns the second line"""
|
|
7
|
+
# orders points from smallest x to greatest x
|
|
8
|
+
points1 = sorted(points1, key=lambda p: (p.x, p.y))
|
|
9
|
+
points2 = sorted(points2, key=lambda p: (p.x, p.y))
|
|
10
|
+
vec1, vec2 = points1[1] - points1[0], points2[1] - points2[0]
|
|
11
|
+
|
|
12
|
+
# if vectors have the same slope return the smallest line
|
|
13
|
+
if have_same_slope(vec1, vec2): return sorted(points1 + points2, key=lambda p: (p.x, p.y))[1:3]
|
|
14
|
+
|
|
15
|
+
# line - line intersection
|
|
16
|
+
det = vec1.x * vec2.y - vec1.y * vec2.x
|
|
17
|
+
if det == 0: return []
|
|
18
|
+
t = (points2[0].x - points1[0].x) * vec2.y - (points2[0].y - points1[0].y) * vec2.x
|
|
19
|
+
t /= det
|
|
20
|
+
return [points1[0] + t * vec1]
|
|
21
|
+
|
|
22
|
+
def have_same_slope(vec1:glm.vec2, vec2:glm.vec2, epsilon:float=1e-5) -> bool:
|
|
23
|
+
"""determines if two vectors moving in the positive x direction have the same slope"""
|
|
24
|
+
return abs(vec1.y * vec2.x - vec2.y * vec1.x) < epsilon
|
|
25
|
+
|
|
26
|
+
def line_poly_intersect(line:list[glm.vec2], polygon:list[glm.vec2]) -> list[glm.vec2]: #TODO Reseach into faster algorithm < O(2n)
|
|
27
|
+
"""computes which parts of the line clip with the polygon"""
|
|
28
|
+
# calculate the center of the polygon
|
|
29
|
+
assert len(polygon) > 2, 'polygon is does not contain engough points'
|
|
30
|
+
center = glm.vec2(0,0)
|
|
31
|
+
for point in polygon: center += point
|
|
32
|
+
center /= len(polygon)
|
|
33
|
+
orig_line = line[:]
|
|
34
|
+
# determine which points are in or out of the polygon
|
|
35
|
+
exterior_points = []
|
|
36
|
+
for i in range(len(polygon)): # nearest even number below n
|
|
37
|
+
for line_point in line[:]:
|
|
38
|
+
if not is_ccw_turn(polygon[i], polygon[(i + 1) % len(polygon)], line_point): # if point is on the outside
|
|
39
|
+
exterior_points.append((polygon[i], polygon[(i + 1) % len(polygon)], line_point)) # polypoint1, polypoint2, linepoint
|
|
40
|
+
line.remove(line_point) # removes line point if it is confirmed to be outside
|
|
41
|
+
|
|
42
|
+
# determine what to with line based on number of points found outside
|
|
43
|
+
if len(exterior_points) == 0:
|
|
44
|
+
return line
|
|
45
|
+
if len(exterior_points) == 1:
|
|
46
|
+
return line_line_intersect(line + [exterior_points[0][2]], exterior_points[0][0:2]) + [line[0]] # [intersecting point, exterior point]
|
|
47
|
+
if len(exterior_points) == 2: # line must intersect with two edges
|
|
48
|
+
points = []
|
|
49
|
+
for i in range(len(polygon)):
|
|
50
|
+
intersection = line_line_intersect(orig_line, [polygon[i], polygon[(i + 1) % len(polygon)]])
|
|
51
|
+
if len(intersection) > 0: points += intersection
|
|
52
|
+
if len(points) > 1: break # exit if two intersections have been found
|
|
53
|
+
else: return [] # fallback if 0 or one intersections found
|
|
54
|
+
return points
|
|
55
|
+
|
|
56
|
+
def closest_two_lines(p1: glm.vec3, q1: glm.vec3, p2: glm.vec3, q2: glm.vec3, epsilon: float=1e-7) -> tuple[glm.vec3, glm.vec3]:
|
|
57
|
+
"""
|
|
58
|
+
Determines the closest point on each line segment to the other line segment.
|
|
59
|
+
"""
|
|
60
|
+
# create direction vector
|
|
61
|
+
d1 = q1 - p1
|
|
62
|
+
d2 = q2 - p2
|
|
63
|
+
r = p1 - p2
|
|
64
|
+
|
|
65
|
+
# get lengths of line segments
|
|
66
|
+
a = glm.dot(d1, d1)
|
|
67
|
+
e = glm.dot(d2, d2)
|
|
68
|
+
f = glm.dot(d2, r)
|
|
69
|
+
|
|
70
|
+
# check if either or both line segment degenerate into points
|
|
71
|
+
if a <= epsilon and e <= epsilon:
|
|
72
|
+
# both segments degenerate
|
|
73
|
+
return p1, p2
|
|
74
|
+
|
|
75
|
+
if a <= epsilon:
|
|
76
|
+
s = 0
|
|
77
|
+
t = glm.clamp(f / e, 0, 1)
|
|
78
|
+
else:
|
|
79
|
+
c = glm.dot(d1, r)
|
|
80
|
+
if e <= epsilon:
|
|
81
|
+
# the second line degenerates to a point
|
|
82
|
+
t = 0
|
|
83
|
+
s = glm.clamp(-c / a, 0, 1)
|
|
84
|
+
else:
|
|
85
|
+
# if neither of them degenerate to a point
|
|
86
|
+
b = glm.dot(d1, d2)
|
|
87
|
+
denom = a * e - b ** 2 # this will always be non-negative
|
|
88
|
+
|
|
89
|
+
# if segments are not parallel, compute closest point from l1 to l2
|
|
90
|
+
s = glm.clamp((b * f - c * e) / denom, 0, 1) if denom else 0
|
|
91
|
+
|
|
92
|
+
# compute closest point from l2 on s1(s)
|
|
93
|
+
t = (b * s + f) / e
|
|
94
|
+
|
|
95
|
+
# if t is not in [0, 1], clamp and recompute s
|
|
96
|
+
if t < 0:
|
|
97
|
+
t = 0
|
|
98
|
+
s = glm.clamp(-c / a, 0, 1)
|
|
99
|
+
elif t > 1:
|
|
100
|
+
t = 1
|
|
101
|
+
s = glm.clamp((b - c) / a, 0, 1)
|
|
102
|
+
|
|
103
|
+
c1 = p1 + d1 * s
|
|
104
|
+
c2 = p2 + d2 * t
|
|
105
|
+
return c1, c2
|
|
106
|
+
|
|
107
107
|
|
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
import glm
|
|
2
|
-
from .helper import is_ccw_turn
|
|
3
|
-
from .line_intersections import line_line_intersect
|
|
4
|
-
|
|
5
|
-
def sutherland_hodgman(subject:list[glm.vec2], clip:list[glm.vec2]) -> list[glm.vec2]:
|
|
6
|
-
"""determines the clipped polygon vertices from ccw oriented polygons"""
|
|
7
|
-
output_poly = subject
|
|
8
|
-
|
|
9
|
-
for i in range(len(clip)):
|
|
10
|
-
input_poly = output_poly
|
|
11
|
-
output_poly = []
|
|
12
|
-
|
|
13
|
-
edge_start, edge_end = clip[i], clip[(i + 1) % len(clip)]
|
|
14
|
-
for j in range(len(input_poly)):
|
|
15
|
-
prev_point, curr_point = input_poly[j - 1], input_poly[j]
|
|
16
|
-
|
|
17
|
-
if is_ccw_turn(curr_point, edge_start, edge_end):
|
|
18
|
-
if not is_ccw_turn(prev_point, edge_start, edge_end):
|
|
19
|
-
output_poly += line_line_intersect([edge_end, edge_start], [prev_point, curr_point])
|
|
20
|
-
output_poly.append(curr_point)
|
|
21
|
-
elif is_ccw_turn(prev_point, edge_start, edge_end):
|
|
22
|
-
output_poly += line_line_intersect([edge_end, edge_start], [prev_point, curr_point])
|
|
23
|
-
|
|
24
|
-
return output_poly
|
|
25
|
-
|
|
26
|
-
# def get_intersect(one: glm.vec2, two: glm.vec2, thr: glm.vec2, fou: glm.vec2) -> glm.vec2:
|
|
27
|
-
# """
|
|
28
|
-
# Gets the intersection point between two lines
|
|
29
|
-
# """
|
|
30
|
-
# deno = (one.x - two.x) * (thr.y - fou.y) - (one.y - two.y) * (thr.x - fou.x)
|
|
31
|
-
# if deno == 0: # TODO determine if this happens
|
|
32
|
-
# print('sutherland-hodgman line intersection had zero denominator')
|
|
33
|
-
# return None
|
|
34
|
-
# x_num = (one.x * two.y - one.y * two.x) * (thr.x - fou.x) - (one.x - two.x) * (thr.x * fou.y - thr.y * fou.x)
|
|
35
|
-
# y_num = (one.x * two.y - one.y * two.x) * (thr.y - fou.y) - (one.y - two.y) * (thr.x * fou.y - thr.y * fou.x)
|
|
36
|
-
# return glm.vec2(x_num / deno, y_num / deno)
|
|
37
|
-
|
|
38
|
-
# def clip(poly: list[glm.vec2], one: glm.vec2, two: glm.vec2) -> list[glm.vec2]:
|
|
39
|
-
# """
|
|
40
|
-
# Clip all edges of polygon with one of the clipping edges
|
|
41
|
-
# """
|
|
42
|
-
# num_points = len(poly)
|
|
43
|
-
# new_points = []
|
|
44
|
-
|
|
45
|
-
# for i in range(num_points):
|
|
46
|
-
# k = (i + 1) % num_points
|
|
47
|
-
# veci = poly[i]
|
|
48
|
-
# veck = poly[k]
|
|
49
|
-
|
|
50
|
-
# posi = (two.x - one.x) * (veci.y - one.y) - (two.y - one.y) * (veci.x - one.x)
|
|
51
|
-
# posk = (two.x - one.x) * (veck.y - one.y) - (two.y - one.y) * (veck.x - one.x)
|
|
52
|
-
|
|
53
|
-
# if posi < 0 and posk < 0: new_points.append(veck)
|
|
54
|
-
# elif posi >= 0 and posk < 0:
|
|
55
|
-
|
|
56
|
-
# new_points.append(get_intersect(one, two, veci, veck))
|
|
57
|
-
# new_points.append(veck)
|
|
58
|
-
|
|
59
|
-
# elif posi < 0 and posk >= 0:
|
|
60
|
-
|
|
61
|
-
# new_points.append(get_intersect(one, two, veci, veck))
|
|
62
|
-
|
|
63
|
-
# return new_points
|
|
64
|
-
|
|
65
|
-
# def sutherland_hodgman(subj_poly:list[glm.vec2], clip_poly:list[glm.vec2]) -> list[glm.vec2]:
|
|
66
|
-
# """
|
|
67
|
-
# Determines the clipped polygon vertices from ccw oriented polygons.
|
|
68
|
-
# """
|
|
69
|
-
# num_clip = len(clip_poly)
|
|
70
|
-
|
|
71
|
-
# for i in range(num_clip):
|
|
72
|
-
# k = (i + 1) % num_clip
|
|
73
|
-
|
|
74
|
-
# subj_poly = clip(subj_poly, clip_poly[i], clip_poly[k])
|
|
75
|
-
|
|
1
|
+
import glm
|
|
2
|
+
from .helper import is_ccw_turn
|
|
3
|
+
from .line_intersections import line_line_intersect
|
|
4
|
+
|
|
5
|
+
def sutherland_hodgman(subject:list[glm.vec2], clip:list[glm.vec2]) -> list[glm.vec2]:
|
|
6
|
+
"""determines the clipped polygon vertices from ccw oriented polygons"""
|
|
7
|
+
output_poly = subject
|
|
8
|
+
|
|
9
|
+
for i in range(len(clip)):
|
|
10
|
+
input_poly = output_poly
|
|
11
|
+
output_poly = []
|
|
12
|
+
|
|
13
|
+
edge_start, edge_end = clip[i], clip[(i + 1) % len(clip)]
|
|
14
|
+
for j in range(len(input_poly)):
|
|
15
|
+
prev_point, curr_point = input_poly[j - 1], input_poly[j]
|
|
16
|
+
|
|
17
|
+
if is_ccw_turn(curr_point, edge_start, edge_end):
|
|
18
|
+
if not is_ccw_turn(prev_point, edge_start, edge_end):
|
|
19
|
+
output_poly += line_line_intersect([edge_end, edge_start], [prev_point, curr_point])
|
|
20
|
+
output_poly.append(curr_point)
|
|
21
|
+
elif is_ccw_turn(prev_point, edge_start, edge_end):
|
|
22
|
+
output_poly += line_line_intersect([edge_end, edge_start], [prev_point, curr_point])
|
|
23
|
+
|
|
24
|
+
return output_poly
|
|
25
|
+
|
|
26
|
+
# def get_intersect(one: glm.vec2, two: glm.vec2, thr: glm.vec2, fou: glm.vec2) -> glm.vec2:
|
|
27
|
+
# """
|
|
28
|
+
# Gets the intersection point between two lines
|
|
29
|
+
# """
|
|
30
|
+
# deno = (one.x - two.x) * (thr.y - fou.y) - (one.y - two.y) * (thr.x - fou.x)
|
|
31
|
+
# if deno == 0: # TODO determine if this happens
|
|
32
|
+
# print('sutherland-hodgman line intersection had zero denominator')
|
|
33
|
+
# return None
|
|
34
|
+
# x_num = (one.x * two.y - one.y * two.x) * (thr.x - fou.x) - (one.x - two.x) * (thr.x * fou.y - thr.y * fou.x)
|
|
35
|
+
# y_num = (one.x * two.y - one.y * two.x) * (thr.y - fou.y) - (one.y - two.y) * (thr.x * fou.y - thr.y * fou.x)
|
|
36
|
+
# return glm.vec2(x_num / deno, y_num / deno)
|
|
37
|
+
|
|
38
|
+
# def clip(poly: list[glm.vec2], one: glm.vec2, two: glm.vec2) -> list[glm.vec2]:
|
|
39
|
+
# """
|
|
40
|
+
# Clip all edges of polygon with one of the clipping edges
|
|
41
|
+
# """
|
|
42
|
+
# num_points = len(poly)
|
|
43
|
+
# new_points = []
|
|
44
|
+
|
|
45
|
+
# for i in range(num_points):
|
|
46
|
+
# k = (i + 1) % num_points
|
|
47
|
+
# veci = poly[i]
|
|
48
|
+
# veck = poly[k]
|
|
49
|
+
|
|
50
|
+
# posi = (two.x - one.x) * (veci.y - one.y) - (two.y - one.y) * (veci.x - one.x)
|
|
51
|
+
# posk = (two.x - one.x) * (veck.y - one.y) - (two.y - one.y) * (veck.x - one.x)
|
|
52
|
+
|
|
53
|
+
# if posi < 0 and posk < 0: new_points.append(veck)
|
|
54
|
+
# elif posi >= 0 and posk < 0:
|
|
55
|
+
|
|
56
|
+
# new_points.append(get_intersect(one, two, veci, veck))
|
|
57
|
+
# new_points.append(veck)
|
|
58
|
+
|
|
59
|
+
# elif posi < 0 and posk >= 0:
|
|
60
|
+
|
|
61
|
+
# new_points.append(get_intersect(one, two, veci, veck))
|
|
62
|
+
|
|
63
|
+
# return new_points
|
|
64
|
+
|
|
65
|
+
# def sutherland_hodgman(subj_poly:list[glm.vec2], clip_poly:list[glm.vec2]) -> list[glm.vec2]:
|
|
66
|
+
# """
|
|
67
|
+
# Determines the clipped polygon vertices from ccw oriented polygons.
|
|
68
|
+
# """
|
|
69
|
+
# num_clip = len(clip_poly)
|
|
70
|
+
|
|
71
|
+
# for i in range(num_clip):
|
|
72
|
+
# k = (i + 1) % num_clip
|
|
73
|
+
|
|
74
|
+
# subj_poly = clip(subj_poly, clip_poly[i], clip_poly[k])
|
|
75
|
+
|
|
76
76
|
# return subj_poly
|
basilisk/config.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class Config():
|
|
2
|
-
def __init__(self) -> None:
|
|
3
|
-
self.chunk_size = 40
|
|
1
|
+
class Config():
|
|
2
|
+
def __init__(self) -> None:
|
|
3
|
+
self.chunk_size = 40
|
|
4
4
|
self.render_distance = 5
|
basilisk/draw/draw.py
CHANGED
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
from ..engine import Engine
|
|
2
|
-
from ..render.image import Image
|
|
3
|
-
|
|
4
|
-
def rect(engine: Engine, color: tuple, rect: tuple) -> None:
|
|
5
|
-
"""
|
|
6
|
-
Draws a rectagle to the screen
|
|
7
|
-
Args:
|
|
8
|
-
engine: bsk.Engine
|
|
9
|
-
The destination engine for the rectangle
|
|
10
|
-
color: tuple(r, g, b) | tuple(r, g, b, a)
|
|
11
|
-
The color value of the rectangle, with int components in range [0, 255]
|
|
12
|
-
rect: tuple(x, y, w, h)
|
|
13
|
-
The screen position and size of the rectangle given in pixels
|
|
14
|
-
"""
|
|
15
|
-
|
|
16
|
-
# Get the draw handler from the engine
|
|
17
|
-
draw_handler = engine.draw_handler
|
|
18
|
-
if not draw_handler: return
|
|
19
|
-
|
|
20
|
-
# Draw the rect
|
|
21
|
-
draw_handler.draw_rect(color, rect)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def circle(engine: Engine, color: tuple, center: tuple, radius: int, resolution: int=20, outer_color: tuple=None) -> None:
|
|
25
|
-
"""
|
|
26
|
-
Draws a rect between centered on x, y with width and height
|
|
27
|
-
Args:
|
|
28
|
-
color: tuple(r, g, b) | tuple(r, g, b, a)
|
|
29
|
-
The color value of the circle, with int components in range [0, 255]
|
|
30
|
-
center: tuple (x: float, y: float)
|
|
31
|
-
Center of the circle, given in pixels
|
|
32
|
-
radius: float
|
|
33
|
-
Radius of the circle, given in pixels
|
|
34
|
-
resolution: float
|
|
35
|
-
The number of triangles used to approximate the circle
|
|
36
|
-
"""
|
|
37
|
-
|
|
38
|
-
# Get the draw handler from the engine
|
|
39
|
-
draw_handler = engine.draw_handler
|
|
40
|
-
if not draw_handler: return
|
|
41
|
-
|
|
42
|
-
# Draw the circle
|
|
43
|
-
draw_handler.draw_circle(color, center, radius, resolution, outer_color)
|
|
44
|
-
|
|
45
|
-
def line(engine: Engine, color: tuple, p1: tuple, p2: tuple, thickness: int=1) -> None:
|
|
46
|
-
"""
|
|
47
|
-
Draws a line between two points
|
|
48
|
-
Args:
|
|
49
|
-
color: tuple=(r, g, b) | tuple=(r, g, b, a)
|
|
50
|
-
Color of the line
|
|
51
|
-
p1: tuple=((x1, y1), (x2, y2))
|
|
52
|
-
Starting point of the line. Given in pixels
|
|
53
|
-
p1: tuple=((x1, y1), (x2, y2))
|
|
54
|
-
Starting point of the line. Given in pixels
|
|
55
|
-
thickness: int
|
|
56
|
-
Size of the line on either side. pixels
|
|
57
|
-
"""
|
|
58
|
-
|
|
59
|
-
# Get the draw handler from the engine
|
|
60
|
-
draw_handler = engine.draw_handler
|
|
61
|
-
if not draw_handler: return
|
|
62
|
-
|
|
63
|
-
# Draw the line
|
|
64
|
-
draw_handler.draw_line(color, p1, p2, thickness)
|
|
65
|
-
|
|
66
|
-
def blit(engine: Engine, image: Image, rect: tuple, alpha: float=1.0):
|
|
67
|
-
"""
|
|
68
|
-
Blits a basilisk image to the engine screen.
|
|
69
|
-
Args:
|
|
70
|
-
image: bsk.Image
|
|
71
|
-
The image to display on the screen
|
|
72
|
-
rect: tuple(x, y, w, h)
|
|
73
|
-
The screen position and size of the image given in pixels
|
|
74
|
-
"""
|
|
75
|
-
|
|
76
|
-
# Get the draw handler from the engine
|
|
77
|
-
draw_handler = engine.draw_handler
|
|
78
|
-
if not draw_handler: return
|
|
79
|
-
|
|
80
|
-
engine.material_handler.image_handler.add(image)
|
|
81
|
-
|
|
82
|
-
# Blit the image
|
|
83
|
-
draw_handler.blit(image, rect, alpha)
|
|
84
|
-
|
|
85
|
-
def text(engine: Engine, text: str, position: tuple, scale: float=1.0):
|
|
86
|
-
"""
|
|
87
|
-
Renders text do the screen
|
|
88
|
-
USE SPARINGLY, INEFFICIENT IMPLAMENTATION
|
|
89
|
-
"""
|
|
90
|
-
|
|
91
|
-
font_renderer = engine.draw_handler.font_renderer
|
|
92
|
-
|
|
93
|
-
# Render the text if it has not been cached
|
|
94
|
-
if text not in font_renderer.text_renders:
|
|
95
|
-
surf = font_renderer.render(text).convert_alpha()
|
|
96
|
-
text_image = Image(surf)
|
|
97
|
-
font_renderer.text_renders[text] = (text_image, surf.get_rect())
|
|
98
|
-
|
|
99
|
-
# Blit the text image
|
|
100
|
-
img, rect = font_renderer.text_renders[text]
|
|
1
|
+
from ..engine import Engine
|
|
2
|
+
from ..render.image import Image
|
|
3
|
+
|
|
4
|
+
def rect(engine: Engine, color: tuple, rect: tuple) -> None:
|
|
5
|
+
"""
|
|
6
|
+
Draws a rectagle to the screen
|
|
7
|
+
Args:
|
|
8
|
+
engine: bsk.Engine
|
|
9
|
+
The destination engine for the rectangle
|
|
10
|
+
color: tuple(r, g, b) | tuple(r, g, b, a)
|
|
11
|
+
The color value of the rectangle, with int components in range [0, 255]
|
|
12
|
+
rect: tuple(x, y, w, h)
|
|
13
|
+
The screen position and size of the rectangle given in pixels
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
# Get the draw handler from the engine
|
|
17
|
+
draw_handler = engine.draw_handler
|
|
18
|
+
if not draw_handler: return
|
|
19
|
+
|
|
20
|
+
# Draw the rect
|
|
21
|
+
draw_handler.draw_rect(color, rect)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def circle(engine: Engine, color: tuple, center: tuple, radius: int, resolution: int=20, outer_color: tuple=None) -> None:
|
|
25
|
+
"""
|
|
26
|
+
Draws a rect between centered on x, y with width and height
|
|
27
|
+
Args:
|
|
28
|
+
color: tuple(r, g, b) | tuple(r, g, b, a)
|
|
29
|
+
The color value of the circle, with int components in range [0, 255]
|
|
30
|
+
center: tuple (x: float, y: float)
|
|
31
|
+
Center of the circle, given in pixels
|
|
32
|
+
radius: float
|
|
33
|
+
Radius of the circle, given in pixels
|
|
34
|
+
resolution: float
|
|
35
|
+
The number of triangles used to approximate the circle
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
# Get the draw handler from the engine
|
|
39
|
+
draw_handler = engine.draw_handler
|
|
40
|
+
if not draw_handler: return
|
|
41
|
+
|
|
42
|
+
# Draw the circle
|
|
43
|
+
draw_handler.draw_circle(color, center, radius, resolution, outer_color)
|
|
44
|
+
|
|
45
|
+
def line(engine: Engine, color: tuple, p1: tuple, p2: tuple, thickness: int=1) -> None:
|
|
46
|
+
"""
|
|
47
|
+
Draws a line between two points
|
|
48
|
+
Args:
|
|
49
|
+
color: tuple=(r, g, b) | tuple=(r, g, b, a)
|
|
50
|
+
Color of the line
|
|
51
|
+
p1: tuple=((x1, y1), (x2, y2))
|
|
52
|
+
Starting point of the line. Given in pixels
|
|
53
|
+
p1: tuple=((x1, y1), (x2, y2))
|
|
54
|
+
Starting point of the line. Given in pixels
|
|
55
|
+
thickness: int
|
|
56
|
+
Size of the line on either side. pixels
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
# Get the draw handler from the engine
|
|
60
|
+
draw_handler = engine.draw_handler
|
|
61
|
+
if not draw_handler: return
|
|
62
|
+
|
|
63
|
+
# Draw the line
|
|
64
|
+
draw_handler.draw_line(color, p1, p2, thickness)
|
|
65
|
+
|
|
66
|
+
def blit(engine: Engine, image: Image, rect: tuple, alpha: float=1.0):
|
|
67
|
+
"""
|
|
68
|
+
Blits a basilisk image to the engine screen.
|
|
69
|
+
Args:
|
|
70
|
+
image: bsk.Image
|
|
71
|
+
The image to display on the screen
|
|
72
|
+
rect: tuple(x, y, w, h)
|
|
73
|
+
The screen position and size of the image given in pixels
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
# Get the draw handler from the engine
|
|
77
|
+
draw_handler = engine.draw_handler
|
|
78
|
+
if not draw_handler: return
|
|
79
|
+
|
|
80
|
+
engine.material_handler.image_handler.add(image)
|
|
81
|
+
|
|
82
|
+
# Blit the image
|
|
83
|
+
draw_handler.blit(image, rect, alpha)
|
|
84
|
+
|
|
85
|
+
def text(engine: Engine, text: str, position: tuple, scale: float=1.0):
|
|
86
|
+
"""
|
|
87
|
+
Renders text do the screen
|
|
88
|
+
USE SPARINGLY, INEFFICIENT IMPLAMENTATION
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
font_renderer = engine.draw_handler.font_renderer
|
|
92
|
+
|
|
93
|
+
# Render the text if it has not been cached
|
|
94
|
+
if text not in font_renderer.text_renders:
|
|
95
|
+
surf = font_renderer.render(text).convert_alpha()
|
|
96
|
+
text_image = Image(surf)
|
|
97
|
+
font_renderer.text_renders[text] = (text_image, surf.get_rect())
|
|
98
|
+
|
|
99
|
+
# Blit the text image
|
|
100
|
+
img, rect = font_renderer.text_renders[text]
|
|
101
101
|
blit(engine, img, (position[0] - rect[2] * scale / 2, position[1] - rect[3] * scale / 2, rect[2] * scale, rect[3] * scale))
|