在Kivy中,有没有一种方法可以动态更改纹理的形状? [英] In Kivy, is there a way to dynamically change the shape of a texture?

查看:110
本文介绍了在Kivy中,有没有一种方法可以动态更改纹理的形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Kivy开发一种科学的可视化工具,在该工具中,我向用户显示一组2D数据.本质上,我使用my_texture = Texture.create(size=(my_data_x, my_data_y))之类的东西来创建与数据集相同大小的纹理.我使用my_texture.blit_buffer(Data_set)将它涂抹到画布上并显示出来,并且效果很好.问题是:我希望允许用户能够在程序运行时更改数据集的大小.由于数据大小在变化,因此我需要一个具有新大小的纹理.我尝试将my_texture变量重新分配给新创建的纹理,但是屏幕上实际显示的是更改之前的纹理.

I'm working on a scientific visualization tool using Kivy in which I display a set of 2D data to the user. Essentially, I create a texture with the same size as my data set using something like my_texture = Texture.create(size=(my_data_x, my_data_y)). I use my_texture.blit_buffer(Data_set) to blit it onto the canvas and display it and it all works great. The problem is: I want to allow the user to be able to change the size of the data set while the program is running. Since the data size is changing, I need to have a texture that also has the new size. I've tried reassigning the my_texture variable to a newly created texture, but what's actually being shown on the screen is the texture right before the change.

所以我的问题是:是否有一种方法可以在我的程序已经运行之后适当地创建纹理并使它们显示在画布上,还是必须始终在启动时生成纹理?另外,有什么方法可以重塑已经存在的纹理?

So my question is: is there a way to appropriately create textures after my program is already running and get them to display on the canvas, or do textures always have to be generated on startup? Alternatively, is there a way I can reshape an already existing texture?

推荐答案

重新加载纹理

如果OpenGL上下文丢失,则必须重新加载Texture.贴图 具有源的文件会自动重新加载,但会生成 纹理必须由用户重新加载 .

If the OpenGL context is lost, the Texture must be reloaded. Textures that have a source are automatically reloaded but generated textures must be reloaded by the user.

使用Texture.add_reload_observer()添加一个重新加载函数,该函数可以 会在需要时自动调用:

Use the Texture.add_reload_observer() to add a reloading function that will be automatically called when needed:

def __init__(self, **kwargs):
    super(...).__init__(**kwargs)
    self.texture = Texture.create(size=(512, 512), colorfmt='RGB',
        bufferfmt='ubyte')
    self.texture.add_reload_observer(self.populate_texture)

    # and load the data now.
    self.cbuffer = '\x00\xf0\xff' * 512 * 512
    self.populate_texture(self.texture)

def populate_texture(self, texture):
    texture.blit_buffer(self.cbuffer)

这样,您可以使用相同的方法进行初始化和 重新加载.

This way, you can use the same method for initialization and reloading.

注意

对于使用我们的核心文本渲染器进行的所有文本渲染,纹理为 生成,但我们已经绑定了一种方法来重做文本渲染并 将文本重新上传到纹理.您无需执行任何操作.

For all text rendering with our core text renderer, the texture is generated but we already bind a method to redo the text rendering and reupload the text to the texture. You don’t have to do anything.

这篇关于在Kivy中,有没有一种方法可以动态更改纹理的形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆