vispy加载jpg格式地图纹理到球体上

按t键可以清除纹理或加载纹理,地球jpg图片可以从nasa官网下载

import numpy as np
from vispy import app, scene
from vispy.io import imread
from vispy.scene.visuals import Mesh
from vispy.scene import transforms
from vispy.visuals.filters import TextureFilter

# 加载地球纹理文件
texture_path = 'jpg/8081_earthmap10k.jpg'  # 请确保文件路径正确
texture = np.flipud(imread(texture_path))

# 创建一个新的 SceneCanvas
canvas = scene.SceneCanvas(keys='interactive', bgcolor='white', size=(800, 600))
view = canvas.central_widget.add_view()

# 使用 arcball 相机,以便可以通过鼠标交互来旋转视图
view.camera = 'arcball'
view.camera.depth_value = 10  # 设置相机的深度值

# 创建球体顶点、面和纹理坐标
def create_sphere(radius, rows, cols):
    phi = np.linspace(0, np.pi, rows)
    theta = np.linspace(0, 2 * np.pi, cols)
    phi, theta = np.meshgrid(phi, theta)

    x = radius * np.sin(phi) * np.cos(theta)
    y = radius * np.sin(phi) * np.sin(theta)
    z = radius * np.cos(phi)

    vertices = np.stack([x, y, z], axis=-1).reshape(-1, 3)
    texcoords = np.stack([theta / (2 * np.pi), phi / np.pi], axis=-1).reshape(-1, 2)

    faces = []
    for i in range(rows - 1):
        for j in range(cols - 1):
            p1 = i * cols + j
            p2 = p1 + 1
            p3 = p1 + cols
            p4 = p3 + 1
            faces.append([p1, p2, p3])
            faces.append([p2, p4, p3])

    faces = np.array(faces)

    return vertices, faces, texcoords

# 生成球体网格数据
vertices, faces, texcoords = create_sphere(radius=1, rows=50, cols=50)

# 创建 Mesh 对象,并将其添加到视图中
mesh = Mesh(vertices, faces, shading='smooth', color='white')
mesh.transform = transforms.MatrixTransform()
mesh.transform.rotate(90, (1, 0, 0))
mesh.transform.rotate(135, (0, 0, 1))
view.add(mesh)

# 创建纹理过滤器,并将其附加到 Mesh 对象上
texture_filter = TextureFilter(texture, texcoords)
mesh.attach(texture_filter)

# 设置键盘事件,用于启用或禁用纹理过滤器
@canvas.events.key_press.connect
def on_key_press(event):
    if event.key == "t":
        texture_filter.enabled = not texture_filter.enabled
        mesh.update()

# 显示画布
canvas.show()

if __name__ == "__main__":
    app.run()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值