Yur*_*nov 5 python opengl 3d pyglet python-3.x
Python:3.6.6
pyglet版本:1.3.2
abstract_model.py
import pyglet
def get_texture_group(file, order_group_index):
texture = pyglet.image.load(file).texture
order_group = pyglet.graphics.OrderedGroup(order_group_index)
return pyglet.graphics.TextureGroup(texture, order_group)
class AbstractModel(object):
def _create_as_vertex(self):
v_x = self.cell_data.get("x") * 32
v_y = self.cell_data.get("y") * -1 * 32
texture_group = self.map_type_iamge.get(self.cell_data.get("t"))
x_offset = self.x_offset * self.scale
x, y, z = v_x + x_offset, v_y, self.z
x_ = (texture_group.texture.width * self.scale + x_offset + v_x)
y_ = (texture_group.texture.height * self.scale + v_y)
tex_coords = ('t2f', (0, 0, 1, 0, 1, 1, 0, 1))
self.vertices = self.batch.add(
4, pyglet.gl.GL_QUADS,
texture_group,
('v3f', (x, y, z,
x_, y, z,
x_, y_, z,
x, y_, z)),
tex_coords)
def _animate(self, dt):
# lets assume that I have list of pyglet.graphics.TextureGroup
# and they should somehow be drawn one after other
print("I need change image. dt=", dt, self)
pyglet.clock.schedule_once(self._animate, 1)
Run Code Online (Sandbox Code Playgroud)
ground3d.py
import os
import pyglet
import settings
from models import abstract_model
GROUND_DIR = os.path.join(settings.STATIC_DIR, "ground")
order_group_index = 0
map_type_iamge = {
1: abstract_model.get_texture_group(os.path.join(GROUND_DIR, "w1.png"), order_group_index),
2: abstract_model.get_texture_group(os.path.join(GROUND_DIR, "t1.png"), order_group_index),
1001: abstract_model.get_texture_group(os.path.join(GROUND_DIR, "t1_direction.png"), order_group_index),
}
class Ground3D(abstract_model.AbstractModel):
def __init__(self, cell_data, batch):
self.batch = batch
self.cell_data = cell_data
self.map_type_iamge = map_type_iamge
self.scale = 1
self.x_offset = 0
self.z = 0
self.entity = None
self._create_as_vertex()
pyglet.clock.schedule_once(self._animate, 1)
Run Code Online (Sandbox Code Playgroud)
我有模型(只是平直的例子),它应放在3个维度上.并且这些模型应该是动画的,比如picture_1,在第二张图片_2之后......等等.
正如我从上一个问题中所理解的那样,使用pyglet.sprite.Sprite()3D批处理并不是一个好主意.
如何更改图片(使用TextureGroup或任何其他方法)self.vertices?
或者我使用哪些arroach /类来实现它.我找不到任何这样的例子(对于我的简单视觉)通常情况作为三维平面模型的动画.
有很多关于旋转/移动/调整大小的例子vertices,但是如何建立一个正确的问题(动画方面)来获得谷歌的答案 - 我不知道.
PS:如果你,读者,对这个主题有任何有用的链接(对于pyglet或仅用于OpenGL),我将非常感谢你在评论中分享这个链接.
纹理坐标。
您应该为所有不同的动画对象的所有帧拥有一个单一的纹理图集。
最好,所有内容都应始终具有相同的动画速度和相同的帧数。
假设有两个精灵,整个动画有 2 个帧,并且它们存储在 64x64 纹理图集中。(编辑:很抱歉有歧义,64x64 像素,只是因为它可能意味着我们有 64x64 瓷砖图集,在我提到这一点的其他地方都一样)
现在,您需要一个具有全局值的全局计时器,该全局值指示当前动画帧,而不是游戏帧。它应该与帧速率无关。
该值应该每隔一段时间以您想要的速度更新一次,如下所示:
current_frame = (current_frame + 1) % animation_length
由于本例中有 2 个帧,因此结果将如下所示:
# init
animation_length = 2
current_frame = 0
# updates:
current_frame = (0 + 1) % 2 # 1 % 2 -> 1
current_frame = (1 + 1) % 2 # 2 % 2 -> 0
...
Run Code Online (Sandbox Code Playgroud)
现在,仅当帧发生变化时,您才需要更新所有精灵的 UV。
UV 从左向右开始,从 0 到 1(据我所知,在这个例子中,它们是这样做的,嘘)。
由于我们每个都有 2 帧,因此我们可以像这样计算 UV 坐标中的“图块”:
tile_width = 1.0 / frames # 2 frames each, width will be 0.5
tile_height = 1.0 / sprites # 2 sprites each, height will be 0.5 too, perfect
Run Code Online (Sandbox Code Playgroud)
现在,在第一帧上,您像平常一样生成 UV,您只需获取垂直 ID 或其他内容,然后使用它tile_height * sprite_id来获取当前V坐标,并且您的U计算方式如下tile_width * current_frame。
这假设您已经有了精灵批处理,所以您要做的就是在更新时检查每个精灵,基本上只是用新帧重新计算新的 UV,这意味着所有精灵都将其帧更改为下一帧,耶!
如果您希望拥有彼此独立的系统,例如,某些系统的动画速度非常慢,而另一些系统的动画速度则更快,那么您将需要不同的精灵批次或正确计算需要更新顶点缓冲区数组中 UV 的位置。其他一切都完全相同,除了现在 current_frame 不是全局的而是包含的,最好是在管理动画计时器的某个列表或单独的对象中。
您不需要更改着色器中的任何内容,它们只需要框架的正确 UV 即可。
顺便说一下,这是非常基本的,您可以自己应用一些逻辑,这样您就可以在纹理中使用 32x32 像素的 16x16 网格,每行精灵有 4 个不同的动画,这些可以是精灵的状态(攻击、运行等) ),但如何做到这一点完全取决于你,最重要的是,让它发挥作用。祝你好运。
但如果你按照我说的方式去做,那么state将是另一个整数,UV为state,假设所有状态都有完全相同的宽度,它会是这样的:
state_width = 1 / states
tile_width = 1 / (states * frames_per_state)
U = state_width * current_state + tile_width * current_frame
Run Code Online (Sandbox Code Playgroud)
现在,出现了一个问题,玩家可以在最后一个攻击帧开始动画。
这是正常的,有动作的实体都应该有单独的计时器,我上面描述的,是针对大量只是背景的精灵,比如草。现在,当您将其划分时,您可以有一种正确的方法在分配新状态时将当前帧重置为 0。
如果您的实体是对象,您可以编写适当的方法,在每次使用这些精灵重建精灵批次时重新计算 UV,然后计时器本身可以包含在对象中。
我们需要画一些东西吗?检查动画状态,是否发生变化,没有?发送之前计算好的UV,否则,等一下,我们需要重新计算,然后将它们添加到VBO中,然后渲染你的东西,最后,它看起来就像你有动画一样,尽管实际上,它是只是一个简单但很棒的紫外线操作。
祝你好运。
| 归档时间: |
|
| 查看次数: |
524 次 |
| 最近记录: |