From 72b3473cbe5260d5dbc4fae60c1cae409164590c Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Mon, 28 Nov 2022 09:03:22 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=20upgrade=20pyglet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/pyglet/math.py | 22 ++++++++++++++-------- libs/pyglet/shapes.py | 7 +++---- libs/pyglet/sprite.py | 11 ++++------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/libs/pyglet/math.py b/libs/pyglet/math.py index 8b746a0..206a80b 100644 --- a/libs/pyglet/math.py +++ b/libs/pyglet/math.py @@ -456,10 +456,11 @@ class Vec3: :returns: A unit vector with the same rotation. :rtype: Vec3 """ - d = self.__abs__() - if d: + try: + d = self.__abs__() return Vec3(self.x / d, self.y / d, self.z / d) - return self + except ZeroDivisionError: + return self def clamp(self, min_val: float, max_val: float) -> Vec3: """Restrict the value of the X, Y and Z components of the vector to be within the given values. @@ -868,11 +869,16 @@ class Mat4(tuple): 0.0, 0.0, 0.0, 1.0)) @classmethod - def look_at(cls, position: Vec3, target: Vec3, up: Vec3) -> Mat4: - direction = target - position - direction_mat4 = cls.look_at_direction(direction, up) - position_mat4 = cls.from_translation(-position) - return direction_mat4 @ position_mat4 + def look_at(cls: type[Mat4T], position: Vec3, target: Vec3, up: Vec3): + f = (target - position).normalize() + u = up.normalize() + s = f.cross(u) + u = s.cross(f) + + return cls([s.x, u.x, -f.x, 0.0, + s.y, u.y, -f.y, 0.0, + s.z, u.z, -f.z, 0.0, + -s.dot(position), -u.dot(position), f.dot(position), 1.0]) def row(self, index: int) -> tuple: """Get a specific row as a tuple.""" diff --git a/libs/pyglet/shapes.py b/libs/pyglet/shapes.py index 5d2a763..f656f55 100644 --- a/libs/pyglet/shapes.py +++ b/libs/pyglet/shapes.py @@ -185,14 +185,13 @@ class _ShapeGroup(Group): def __eq__(self, other): return (other.__class__ is self.__class__ and + self.program == other.program and self.parent == other.parent and - self.order == other.order and self.blend_src == other.blend_src and - self.blend_dest == other.blend_dest and - self.program == other.program) + self.blend_dest == other.blend_dest) def __hash__(self): - return hash((id(self.parent), self.blend_src, self.blend_dest, self.order, self.program)) + return hash((self.program, self.parent, self.blend_src, self.blend_dest)) class ShapeBase(ABC): diff --git a/libs/pyglet/sprite.py b/libs/pyglet/sprite.py index 970f905..57fa3f5 100644 --- a/libs/pyglet/sprite.py +++ b/libs/pyglet/sprite.py @@ -209,7 +209,7 @@ class SpriteGroup(graphics.Group): same parent group, texture and blend parameters. """ - def __init__(self, texture, blend_src, blend_dest, program, order=0, parent=None): + def __init__(self, texture, blend_src, blend_dest, program, parent=None): """Create a sprite group. The group is created internally when a :py:class:`~pyglet.sprite.Sprite` @@ -231,7 +231,7 @@ class SpriteGroup(graphics.Group): `parent` : `~pyglet.graphics.Group` Optional parent group. """ - super().__init__(order, parent) + super().__init__(parent=parent) self.texture = texture self.blend_src = blend_src self.blend_dest = blend_dest @@ -263,7 +263,7 @@ class SpriteGroup(graphics.Group): self.blend_dest == other.blend_dest) def __hash__(self): - return hash((self.parent, id(self.program), + return hash((self.program, self.parent, self.texture.id, self.texture.target, self.blend_src, self.blend_dest)) @@ -335,7 +335,7 @@ class Sprite(event.EventDispatcher): self._texture = img.get_texture() self._batch = batch or graphics.get_default_batch() - self._group = self.group_class(self._texture, blend_src, blend_dest, self.program, 0, group) + self._group = self.group_class(self._texture, blend_src, blend_dest, self.program, group) self._subpixel = subpixel self._create_vertex_list() @@ -434,7 +434,6 @@ class Sprite(event.EventDispatcher): self._group.blend_src, self._group.blend_dest, self._group.program, - 0, group) self._batch.migrate(self._vertex_list, GL_TRIANGLES, self._group, self._batch) @@ -472,7 +471,6 @@ class Sprite(event.EventDispatcher): self._group.blend_src, self._group.blend_dest, self._group.program, - 0, self._group.parent) self._vertex_list.delete() self._texture = texture @@ -871,7 +869,6 @@ class AdvancedSprite(pyglet.sprite.Sprite): self._group.blend_src, self._group.blend_dest, program, - 0, self._group) self._batch.migrate(self._vertex_list, GL_TRIANGLES, self._group, self._batch) self._program = program